rechne.exe • Integration


JPlotter

JPlotter is a Java-based program with a graphical user interface which uses the rechne library.
The program allows for plotting of mathematical functions and provides many options, such as:
  • plotting complex functions with real and imaginary part (or amplitude and phase)
  • derivating function
  • integrating function
  • performing Discrete Fourier analysis
  • calculating areas between functions
  • finding zeros / extrema of functions
  • many more...
Example plot of a complex function

JPlotter also has a number of integrated tools:
  • solving linear equation systems
  • calculating (complex) zeros of a polynomial
  • analyzing a vector (Discrete Fourier Transformation, different vector norms)
  • analyzing a matrix (Eigen values, inverse matrix, matrix norms)
  • Rechne Console: this is basically a Java fontend for rechne.exe


JPlotter Website: https://jplotter.sourceforge.io
Rechne Console




Shell commands

The rechne.exe binary can be called from command-line and hence can be integrated into scripts (shell scripts on Linux or batch scripts on Windows). If rechne.exe is called with arguments it tries to process them and terminates afterwards where if rechne.exe is called without arguments, it starts in the interactive shell mode.
All inputs known from the shell mode can be passed as command-line arguments.

Some examples:
» Call with command-line arguments (brackets may need to be escaped):
> ./rechne.exe -v 12.8*sin\(1.5*Pi\)
» Pipe echos:
> echo "sqrt(144)+19*ln(5)" | ./rechne.exe -v
» Calculate the SHA-512 sum of rechne.exe as a decimal number:
> sha512sum rechne.exe | cut -c -128 | ./rechne.exe bn -q -ilow -odec
» Shell script to plot data pairs of the function si(x)*cos(x)*sig(x) using gnuplot:
#!/bin/bash
for i in {-100..100} ; do
  ./rechne.exe -q si\($i/10\)*cos\($i/10\)*sig\($i/10\) >> datapair
  echo -n " " >> datapair
  ./rechne.exe -q \($i\)/10 >> datapair
  echo >> datapair
done
gnuplot -persist << EOF
  set title 'si(x)*cos(x)*sig(x)'
  set grid
  plot "datapair" using 2:1
EOF
rm -f datapair

gnuplot frame




librechne

All compiled rechne.exe functions (except for the main function) are contained in the library file librechne.a. This allows for an integration of rechne.exe functions in C/C++ projects. The download archiv comes with a header file librechne.h that contains structures and prototypes for rechne.exe function calls.
The Makefile automatically creates this library file (in the lib folder) on compilation (see Installation).

Example of a C program that includes librechne: #include <stdio.h>
#include <string.h>
#include "librechne.h"

int main() {
  /* calculation result (complex number) */
  CN res;

  char input_buffer[1024];

  /* return value */
  int ret;

  /*
   * Some examples for solving mathematical terms.
   *
   * PLEASE NOTE: by default the library will print to stdout.
   * If you dont wish output on stdout then redirect to /dev/null
   * (on Windows: NUL) -> rechne("2*(3.5+1) > /dev/null", ...
   */
  ret = rechne("2*(3.5+1)", &res);
  printf("return_code=%d result=%f\n", ret, res.real); /* result: 9 */
  ret = rechne("cos(2*Pi)*2.5)^2", &res);
  printf("return_code=%d result=%f\n", ret, res.real); /* result: 6.25 */

  /*
   * Example with complex result.
   * e.g. result of sqrt(-4) is actually 2i
   */
  ret = rechne("sqrt(-4)+3", &res);
  printf("return_code=%d result(real)=%f result(img)=%f\n", ret, res.real, res.img); /* 3+2i */

  /*
   * Example with geo coordinate:
   *     distance/azimuth between point 51.5, 12.8 and 60.8, 11.0
   */
  ret = rechne("geo -d 51.5,12.8 60.8,11.0", &res);
  printf("return_code=%d distance=%f azimuth=%f\n", ret, res.real, res.img);

  /*
   * Now custom user inputs read from stdin:
   */
  printf("type term (e.g. '12.7*42') or 'exit')\n");
  while (fgets(input_buffer, sizeof(input_buffer), stdin)) {
    /* remove line break at the end */
    input_buffer[strlen(input_buffer) - 1] = 0;
    if (strcmp(input_buffer, "exit") == 0)
      break;

    /* call rechne.exe function */
    ret = rechne(input_buffer, &res);

    /* evaluate return value */
    if (ret == 0)
      printf("result is: %f+%f*i\n", res.real, res.img);
    else
      printf("error %u occured\n", __errno);
  }

  return 0;
}
myprog.c

Copy librechne.h (comes with the librechne download archive or is in the lib folder after "make") to the source directory (next to myprog.c) and compile with: gcc -Wall myprog.c -L./lib -lrechne -lm -o myprog
Replace "./lib" with wherever librechne.a is located.

Download full example




JNI

With JNI (Java Native Interface) rechne.exe functions (that is C code) can be called from Java code.
That allows for the calculation of a result (as a double value) for any given calculation string in Java. The advantage of calling C code is that processing is very fast. The dll file rechne.dll (→ Windows 64 Bit) already has the JNI functions enabled and can be used for Java projects on Windows. Equivalently the shared object file librechne.so can be used on Linux (64 Bit).

The Makefile automatically creates the dll file on Windows or the shared object file on Linux but not with JNI enabled. This is because compilation requires the JNI header files from the Java Development Kit and the path is different on every system (or there might be no JDK installed at all).

To enable the compilation of the JNI module of rechne.exe (that are the files rechne_jni.c and rechne_jni.h) the Makefile has to be edited. Therefore read the comment and enable the following both lines in the Makefile:
# JNI is for calling native rechne.exe functions from Java code.
# See the JNI example on https://rechne-exe.sourceforge.io.
# rechne_jni.h contains the JNI header (generated from javah).
# To enable JNI compilation uncomment the following two lines.
# Replace <PATH_TO_JDK> with the path of the JDK installation.
#
#CFLGS += -DCOMPILE_JNI
#CINCL += -I<PATH_TO_JDK>/include -I<PATH_TO_JDK>/include/win32
Once again you only have to recompile rechne.exe with JNI enabled if you cannot use the precompiled libaries from the download folder for some reason.
After editing the Makefile the program has to be re-compiled ('make clean' + 'make')

Now the following example shows how to call the native rechne.exe function from Java:
/*
 * This class provides the main class for the JNI example 1.
 * Use the Rechne.rechne() method to solve mathematical terms.
 * In this example we only receive the result without handling
 * of the output stream.
 *
 * PLEASE NOTE:
 * - rechne.dll (or on Linux librechne.so) must be located in this folder!
 * - if not otherwise configured the rechne library will write to
 * stdout or stderr!
 *
 * This class can be edited or moved. Only the Rechne class
 * must stay where it is in order to work with JNI.
 */

package de.jsteltze.rechne;

import de.jsteltze.rechne.Rechne.RechneResult;

public class JniExample1 {

  public static void main(String[] args) {
    System.out.println("rechne library version: " + Rechne.version());

    /* No additional parameters -> rechne library will print output to stdout */
    eval("5*Pi^2", false);

    /* The parameter '-q' reduces library outputs on stdout */
    eval("-q sin(Pi/2)*3.5", false);

    /* The parameter '-v' will print all intermediate calculation steps */
    eval("-v (1+2*3-4*5/2)^2", false);

    /* Redirect output to /dev/null (on Windows: NUL) to suppress any outputs */
    eval("(1+2*3-4*5/2)^2", true);
  }

  public static void eval(String command, boolean quiet) {
    String redirect = "";
    if (quiet)
      redirect = System.getProperty("os.name").startsWith("Windows") ? " > NUL" : " > /dev/null";

    /* Let the 'rechne' library evaluate the command */
    RechneResult result = Rechne.rechne(command + redirect);

    if (result.return_code == 0)
      /* Return code 0 means success */
      System.out.println(command + " -> Result=" + result.real);
    else
      System.out.println("ERROR! rechne call returned " + result.return_code);

    System.out.println("----------------------");
  }
}
Rechne.java

It is important that you DO NOT rename the Java class or relocate it (that is change the package name). Also you MUST NOT change the signature of the inner class RechneResult or relocate it and you MUST NOT change the signature of the native rechne function.
Otherwise it is likely that you will get a UnsatisfiedLinkError.
Have a look at rechne_jni.h (which was generated by javah) if you want a deeper understanding of how the interaction between Java and C code works.

Download JNI example 1
Download JNI example 2
My other projects: