Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > org > lsmp > djepExamples > RpExample

 `1 /* @author rich2  * Created on 26-Feb-20043  *4  * This code is covered by a Creative Commons5  * Attribution, Non Commercial, Share Alike license6  * License7  */8 9 package org.lsmp.djepExamples;10 import org.nfunk.jep.*;11 import org.lsmp.djep.rpe.*;12 /**13  * Examples using vectors and matricies14  */15 public class RpExample {16     static JEP j;17     18     public static void main(String args[]) {19         j = new JEP();20         j.addStandardConstants();21         j.addStandardFunctions();22         j.addComplex();23         j.setAllowUndeclared(true);24         j.setImplicitMul(true);25         j.setAllowAssignment(true);26 27         // parse and evaluate each equation in turn28 29         doStuff("1*2*3+4*5*6+7*8*9");30         doAll(new String []{"x1=1","x2=2","x3=3","x4=4","x5=5","x6=6","x7=7","x8=8","x9=9",31             "x1*x2*x3+x4*x5*x6+x7*x8*x9"});32         doAll(new String []{"x=0.7","cos(x)^2+sin(x)^2"});33         // vectors and matricies can be used with assignment34 // doStuff("x=[1,2,3]"); // Value: [1.0,2.0,3.0]35 // doStuff("x+x"); // Value: [2.0,4.0,6.0]36 // doStuff("x . x"); // Value: 14.037 // doStuff("x^x"); // Value: [0.0,0.0,0.0]38 // doStuff("y=[[1,2],[3,4]]"); // Value: [[1.0,2.0],[3.0,4.0]]39 // doStuff("y * y"); // Value: [[7.0,10.0],[15.0,22.0]]40 // accessing the elements on an array or vector41 // doStuff("ele(x,2)"); // Value: 2.042 // doStuff("ele(y,[1,2])"); // Value: 2.043 // using differentation44 // doStuff("x=2"); // 2.045 // doStuff("y=[x^3,x^2,x]"); // [8.0,4.0,2.0]46 // doStuff("z=diff(y,x)"); // [12.0,4.0,1.0]47 // doStuff("diff([x^3,x^2,x],x)");48 // System.out.println("dim(z) "+((MatrixVariableI) j.getVar("z")).getDimensions());49 }50 51     public static void doStuff(String str) {52         try {53             Node node = j.parse(str);54 55             RpEval rpe = new RpEval(j);56             RpCommandList list = rpe.compile(node);57             double res = rpe.evaluate(list);58 59             // conversion to String60 System.out.println(str+"\nres " + res);61             62             // List of commands63 System.out.println("Commands");64             System.out.println(list.toString());65         }66         catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); } 67         catch(Exception e) { System.out.println("evaluation error "+e.getMessage()); e.printStackTrace(); } 68     }69 70     public static void doAll(String str[]) {71         try {72             RpEval rpe = new RpEval(j);73 74             for(int i=0;i