SymJava is a Java library for symbolic-numeric computation.
There are two unique features which make SymJava different:
Operator Overloading is implemented by using Java-OO (https://github.com/amelentev/java-oo)
Java bytecode is generated at runtime for symbolic expressions which make the numerical evaluation really fast.
Install java-oo Eclipse plugin for Java Operator Overloading support (https://github.com/amelentev/java-oo): Click in menu: Help -> Install New Software. Enter in "Work with" field: http://amelentev.github.io/eclipse.jdt-oo-site/
If you are using Eclipse-Kepler you need to install SR2 4.3.2 here https://www.eclipse.org/downloads/packages/release/kepler/sr2)
If you are using Eclipse 4.4+, you need Scalar IDE plugin. see https://github.com/amelentev/java-oo
Both Java 7 and 8 are supported.
If you were using Futureye_JIT for academic research, you are encouraged to cite the following papers:
packagesymjava.examples;
importstaticsymjava.symbolic.Symbol.*;
importsymjava.bytecode.BytecodeFunc;
importsymjava.symbolic.*;
/** * This example uses Java Operator Overloading for symbolic computation. * See https://github.com/amelentev/java-oo for Java Operator Overloading. * */publicclassExample1 {
publicstaticvoidmain(String[] args) {
Exprexpr = x + y * z;
System.out.println(expr); //x + y*zExprexpr2 = expr.subs(x, y*y);
System.out.println(expr2); //y^2 + y*zSystem.out.println(expr2.diff(y)); //2*y + zFuncf = newFunc("f1", expr2.diff(y));
System.out.println(f); //2*y + zBytecodeFuncfunc = f.toBytecodeFunc();
System.out.println(func.apply(1,2)); //4.0
}
}packagesymjava.examples;
importsymjava.relational.Eq;
importsymjava.symbolic.Symbol;
importstaticsymjava.symbolic.Symbol.*;
publicclassExample2 {
/** * Example from Wikipedia * (http://en.wikipedia.org/wiki/Gauss-Newton_algorithm) * * Use Gauss-Newton algorithm to fit a given model y=a*x/(b-x) * */publicstaticvoidexample1() {
//Model y=a*x/(b-x), Unknown parameters: a, bSymbol[] freeVars = {x};
Symbol[] params = {a, b};
Eqeq = newEq(y, a*x/(b+x), freeVars, params); //Data for (x,y)double[][] data = {
{0.038,0.050},
{0.194,0.127},
{0.425,0.094},
{0.626,0.2122},
{1.253,0.2729},
{2.500,0.2665},
{3.740,0.3317}
};
double[] initialGuess = {0.9, 0.2};
//Here we go ...GaussNewton.solve(eq, initialGuess, data, 100, 1e-4);
}
/** * Example from Apache Commons Math * (http://commons.apache.org/proper/commons-math/userguide/optimization.html) * * "We are looking to find the best parameters [a, b, c] for the quadratic function * * f(x) = a x2 + b x + c. * * The data set below was generated using [a = 8, b = 10, c = 16]. A random number * between zero and one was added to each y value calculated. " * */publicstaticvoidexample2() {
Symbol[] freeVars = {x};
Symbol[] params = {a, b, c};
Eqeq = newEq(y, a*x*x + b*x + c, freeVars, params);
double[][] data = {
{1 , 34.234064369},
{2 , 68.2681162306108},
{3 , 118.615899084602},
{4 , 184.138197238557},
{5 , 266.599877916276},
{6 , 364.147735251579},
{7 , 478.019226091914},
{8 , 608.140949270688},
{9 , 754.598868667148},
{10, 916.128818085883}, };
double[] initialGuess = {1, 1, 1};
GaussNewton.solve(eq, initialGuess, data, 100, 1e-4);
}
publicstaticvoidmain(String[] args) {
example1();
example2();
}
}Output in Latex:
Iterativly sovle ... a=0.33266 b=0.26017 a=0.34281 b=0.42608 a=0.35778 b=0.52951 a=0.36141 b=0.55366 a=0.36180 b=0.55607 a=0.36183 b=0.55625 Iterativly sovle ... a=7.99883 b=10.00184 c=16.32401 packagesymjava.examples;
importJama.Matrix;
importsymjava.matrix.*;
importsymjava.relational.Eq;
importsymjava.symbolic.Expr;
/** * A general Gauss Newton solver using SymJava for simbolic computations * instead of writing your own Jacobian matrix and Residuals */publicclassGaussNewton {
publicstaticvoidsolve(Eqeq, double[] init, double[][] data, intmaxIter, doubleeps) {
intn = data.length;
//Construct Jacobian Matrix and ResidualsSymVectorres = newSymVector(n);
SymMatrixJ = newSymMatrix(n, eq.getParams().length);
Expr[] params = eq.getParams();
for(inti=0; i<n; i++) {
EqsubEq = eq.subsUnknowns(data[i]);
res[i] = subEq.lhs - subEq.rhs; //res[i] =y[i] - a*x[i]/(b + x[i]); for(intj=0; j<eq.getParams().length; j++) {
Exprdf = res[i].diff(params[j]);
J[i][j] = df;
}
}
System.out.println("Jacobian Matrix = ");
System.out.println(J);
System.out.println("Residuals = ");
System.out.println(res);
//Convert symbolic staff to Bytecode staff to speedup evaluationNumVectorNres = newNumVector(res, eq.getParams());
NumMatrixNJ = newNumMatrix(J, eq.getParams());
System.out.println("Iterativly sovle ... ");
for(inti=0; i<maxIter; i++) {
//Use JAMA to solve the systemMatrixA = newMatrix(NJ.eval(init));
Matrixb = newMatrix(Nres.eval(init), Nres.dim());
Matrixx = A.solve(b); //Lease Square solutionif(x.norm2() < eps) break;
//Update initial guessfor(intj=0; j<init.length; j++) {
init[j] = init[j] - x.get(j, 0);
System.out.print(String.format("%s=%.5f",eq.getParams()[j], init[j])+" ");
}
System.out.println();
} }
}packagesymjava.examples;
importstaticsymjava.symbolic.Symbol.*;
importsymjava.relational.Eq;
importsymjava.symbolic.*;
publicclassExample3 {
/** * Square root of a number * (http://en.wikipedia.org/wiki/Newton's_method) */publicstaticvoidexample1() {
Expr[] freeVars = {x};
doublenum = 612;
Eq[] eq = newEq[] {
newEq(x*x-num, C0, freeVars, null)
};
double[] guess = newdouble[]{ 10 };
Newton.solve(eq, guess, 100, 1e-3);
}
/** * Example from Wikipedia * (http://en.wikipedia.org/wiki/Gauss-Newton_algorithm) * * Use Lagrange Multipliers and Newton method to fit a given model y=a*x/(b-x) * */publicstaticvoidexample2() {
//Model y=a*x/(b-x), Unknown parameters: a, bSymbol[] freeVars = {x};
Symbol[] params = {a, b};
Eqeq = newEq(y - a*x/(b+x), C0, freeVars, params); //Data for (x,y)double[][] data = {
{0.038,0.050},
{0.194,0.127},
{0.425,0.094},
{0.626,0.2122},
{1.253,0.2729},
{2.500,0.2665},
{3.740,0.3317}
};
double[] initialGuess = {0.9, 0.2};
LagrangeMultiplierslm = newLagrangeMultipliers(eq, initialGuess, data);
//Just for purpose of displaying summation expressionEqL = lm.getEqForDisplay(); System.out.println("L("+SymPrinting.join(L.getUnknowns(),",")+")=\n "+L.lhs);
System.out.println("where data array is (X_i, Y_i), i=0..."+(data.length-1));
NewtonOptimization.solve(L, lm.getInitialGuess(), 100, 1e-4, true);
EqL2 = lm.getEq();
System.out.println("L("+SymPrinting.join(L.getUnknowns(),",")+")=\n "+L2.lhs);
NewtonOptimization.solve(L2, lm.getInitialGuess(), 100, 1e-4, false);
}
publicstaticvoidmain(String[] args) {
example1();
example2();
}
}Jacobian Matrix = \left[ {\begin{array}{c}
2*x\\
\end{array} } \right]
Iterativly sovle ... x=10.00000 x=35.60000 x=26.39551 x=24.79064 x=24.73869 Output in Latex:
where data array is (X_i, Y_i), i=0...6
Iterativly sovle ... y_0=0.00000 y_1=0.00000 y_2=0.00000 y_3=0.00000 y_4=0.00000 y_5=0.00000 y_6=0.00000 \lambda_0=0.00000 \lambda_1=0.00000 \lambda_2=0.00000 \lambda_3=0.00000 \lambda_4=0.00000 \lambda_5=0.00000 \lambda_6=0.00000 a=0.90000 b=0.20000 y_0=0.01678 y_1=0.09612 y_2=0.16729 y_3=0.20243 y_4=0.25473 y_5=0.28945 y_6=0.30273 \lambda_0=0.06643 \lambda_1=0.06176 \lambda_2=-0.14658 \lambda_3=0.01955 \lambda_4=0.03634 \lambda_5=-0.04590 \lambda_6=0.05794 a=0.33266 b=0.26017 y_0=0.01624 y_1=0.08735 y_2=0.15765 y_3=0.19518 y_4=0.25469 y_5=0.29667 y_6=0.31327 \lambda_0=0.06752 \lambda_1=0.07930 \lambda_2=-0.12729 \lambda_3=0.03404 \lambda_4=0.03642 \lambda_5=-0.06034 \lambda_6=0.03687 a=0.35178 b=0.46125 y_0=0.02256 y_1=0.09240 y_2=0.15593 y_3=0.19116 y_4=0.25076 y_5=0.29644 y_6=0.31550 \lambda_0=0.05487 \lambda_1=0.06919 \lambda_2=-0.12387 \lambda_3=0.04207 \lambda_4=0.04428 \lambda_5=-0.05989 \lambda_6=0.03240 a=0.36223 b=0.55462 y_0=0.02314 y_1=0.09356 y_2=0.15671 y_3=0.19159 y_4=0.25059 y_5=0.29598 y_6=0.31499 \lambda_0=0.05373 \lambda_1=0.06689 \lambda_2=-0.12542 \lambda_3=0.04123 \lambda_4=0.04463 \lambda_5=-0.05896 \lambda_6=0.03342 a=0.36185 b=0.55631 packagesymjava.examples; importstaticsymjava.symbolic.Symbol.*; importsymjava.matrix.*; importsymjava.symbolic.*; /** * Example for PDE Constrained Parameters Optimization * */publicclassExample4 { publicstaticvoidmain(String[] args) { Funcu = newFunc("u", x,y,z); Funcu0 = newFunc("u0", x,y,z); Funcq = newFunc("q", x,y,z); Funcq0 = newFunc("q0", x,y,z); Funcf = newFunc("f", x,y,z); Funclamd = newFunc("\\lambda ", x,y,z); Exprreg_term = (q-q0)*(q-q0)*0.5*0.1; FuncL = newFunc("L",(u-u0)*(u-u0)/2 + reg_term + q*Dot.apply(Grad.apply(u), Grad.apply(lamd)) - f*lamd); System.out.println("Lagrange L(u, \\lambda, q) = \n"+L); Funcphi = newFunc("\\phi ", x,y,z); Funcpsi = newFunc("\\psi ", x,y,z); Funcchi = newFunc("\\chi ", x,y,z); Expr[] xs = newExpr[]{u, lamd, q }; Expr[] dxs = newExpr[]{phi, psi, chi }; SymVectorLx = Grad.apply(L, xs, dxs); System.out.println("\nGradient Lx = (Lu, Llamd, Lq) ="); System.out.println(Lx); Funcdu = newFunc("\\delta{u}", x,y,z); Funcdl = newFunc("\\delta{\\lambda}", x,y,z); Funcdq = newFunc("\\delta{q}", x,y,z); Expr[] dxs2 = newExpr[] { du, dl, dq }; SymMatrixLxx = newSymMatrix(); for(ExprLxi : Lx) { Lxx.add(Grad.apply(Lxi, xs, dxs2)); } System.out.println("\nHessian Matrix ="); System.out.println(Lxx); } } 









