From 6bf6b2277557eff3f670bc68bdb1a6a426eef781 Mon Sep 17 00:00:00 2001 From: Mesut Date: Tue, 13 Apr 2021 13:27:39 +0300 Subject: [PATCH] fix Interpreter eval calls --- README.md | 10 +- pom.xml | 30 +---- src/main/java/com/mesut/math/Interpreter.java | 11 ++ src/main/java/com/mesut/math/Main.java | 113 ------------------ .../java/com/mesut/math/core/FuncCall.java | 12 +- src/test/java/InterpreterTest.java | 17 ++- 6 files changed, 30 insertions(+), 163 deletions(-) delete mode 100755 src/main/java/com/mesut/math/Main.java diff --git a/README.md b/README.md index 2260802..53177ce 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ a simple math library written in java ### Features: - supports infinite multivariables - parsing from postfix -- taking derivatives in multivariables +- symbolic derivatives - numeric derivatives - numeric integration - outputs latex code @@ -16,14 +16,14 @@ a simple math library written in java ### Derivative ```java -func f=func.parse("e^(x^2)"); +func f = func.parse("e^(x^2)"); System.out.println(f.derivative()); Output: (e^(x^2))*x*2 ``` ### Integral ```java -func f=func.parse("e^-x*x^5");//gamma +func f = func.parse("e^-x*x^5");//gamma System.out.println(f.integrate("x",0,cons.INF)); Output: 120 ``` @@ -31,12 +31,12 @@ Output: 120 ### Taylor series ```java //e^x -func f=func.parse("e^x"); +func f = func.parse("e^x"); System.out.println(taylor(0,5)); Output: 1+x+(x^2)*0.5+(x^3)*0.16666666666666666+(x^4)*0.041666666666666664+(x^5)*0.008333333333333333 //Lambert-W -func f=func.parse("x*e^x"); +func f = func.parse("x*e^x"); System.out.println(f.inverse().taylor(0,5)); Output: x-x^2+(x^3)*1.5-(x^4)*2.6666666666666665+(x^5)*5.208333333333333 diff --git a/pom.xml b/pom.xml index aad5819..2dbbf7a 100644 --- a/pom.xml +++ b/pom.xml @@ -14,12 +14,12 @@ 1.7 com.mesut.math.Main - + - - The Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + @@ -28,26 +28,6 @@ org.apache.maven.plugins maven-jar-plugin 2.4 - - - - ${mainClass} - - - - - - maven-deploy-plugin - 2.8.1 - - - default-deploy - deploy - - deploy - - - diff --git a/src/main/java/com/mesut/math/Interpreter.java b/src/main/java/com/mesut/math/Interpreter.java index 3d300ca..e22fe15 100644 --- a/src/main/java/com/mesut/math/Interpreter.java +++ b/src/main/java/com/mesut/math/Interpreter.java @@ -43,6 +43,17 @@ func normalizeCalls(func f) { return new Visitor() { @Override public func visit(FuncCall call) { + if (call.getName().equals("derivative")) { + if (call.getArgs().isEmpty()) { + return call.getScope().derivative(); + } + else { + return call.getScope().derivative((variable) call.getArgs().get(0)); + } + } + else if (call.getName().equals("simplify")) { + return call.getScope().simplify(); + } variable v = variable.from(call.getName()); func val = checkVal(v); if (call.getArgs().isEmpty()) { diff --git a/src/main/java/com/mesut/math/Main.java b/src/main/java/com/mesut/math/Main.java deleted file mode 100755 index 47483c8..0000000 --- a/src/main/java/com/mesut/math/Main.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.mesut.math; - -import com.mesut.math.core.*; -import com.mesut.math.funcs.zeta; - -import java.util.Random; - -public class Main { - - public static void main(String[] args) throws Exception { - for (int i = 0; i < 10; i++) { - System.out.println(random(20)); - } - } - - - static void matrix() { - matrix m1 = new matrix("(1,2,3),(4,9,7)"); - matrix m2 = new matrix("(4,7,2),(9,8,0)"); - - System.out.println(m1.add(m2)); - } - - static void set() { - set s = new set(1, 2, 3); - set s2 = new set(5, 8); - set s3 = new set(func.parse("2*n+1")); - System.out.println(s3.print(0, 22)); - } - - static void simp() { - func f = func.parse("cos(x)"); - - System.out.println(f.derivative(21)); - } - - static void zeta() { - /*prime pn=new prime("n"); - - sigma s=new sigma("ln(p)/(p^2-1)","n",1,100); - System.out.println(s); - s=(sigma)s.substitude(new var("p"),pn); - System.out.println(s); - System.out.println(s.eval());*/ - //System.out.println(func.parse("2*0.5")); - zeta z = new zeta(func.parse("0.5+i*14.13472514173")); - System.out.println(z.sum.getImaginary().eval()); - - //System.out.println(func.parse("atan(1+i)").getComplex()); - } - - - static void floor() { - func f = func.parse("floor((n+3)/3)-floor((n+2)/3)"); - System.out.println(f); - for (int n = 0; n < 20; n++) { - System.out.printf("n=%d %s%n", n, f.eval("n", n)); - } - } - - static func random(int max) { - Random random = new Random(); - int type = random.nextInt(5); - if (max <= 1) { - type = 1; - } - //max--; - func func = null; - if (type == 0) {//var - func = getVar(); - } - else if (type == 1) {//cons - func = getCons(); - } - else if (type == 2) {//add - func = random(max / 2).add(random(max / 2)); - } - else if (type == 3) {//mul - func = random(max / 2).mul(random(max / 2)); - } - else if (type == 4) {//pow - func = random(max / 2).pow(random(max / 2)); - } - return func; - } - - static func getVar() { - int c = new Random().nextInt('z' - 'a'); - return new variable(String.valueOf((char) ('a' + c))); - } - - static func getCons() { - int len = new Random().nextInt(4) + 1; - return new cons(new Random().nextInt((int) Math.pow(10, len))); - } - - static void integral() { - func f; - //gamma gamma=new gamma(var.x); - //System.out.println(gamma.eval(7)); - - //li li=new li(func.parse("x")); - //System.out.println(li.eval("x=5")); - - f = func.parse("ln(1-x)/x"); - System.out.println(f.integrate(0, 1, variable.x)); - - //f=func.parse("ln(sin(x))"); - //System.out.println(f.integrate(0,Math.PI,var.x)); - } - - -} diff --git a/src/main/java/com/mesut/math/core/FuncCall.java b/src/main/java/com/mesut/math/core/FuncCall.java index 27046ef..ee11d7f 100644 --- a/src/main/java/com/mesut/math/core/FuncCall.java +++ b/src/main/java/com/mesut/math/core/FuncCall.java @@ -24,16 +24,8 @@ public List getArgs() { return args; } - public func call(func f) { - if (name.equals("derivative")) { - if (args.isEmpty()) { - return scope.derivative(); - } - else { - return scope.derivative((variable) args.get(0)); - } - } - return this; + public func getScope() { + return scope; } @Override diff --git a/src/test/java/InterpreterTest.java b/src/test/java/InterpreterTest.java index 98d524c..f26bf41 100644 --- a/src/test/java/InterpreterTest.java +++ b/src/test/java/InterpreterTest.java @@ -1,24 +1,21 @@ import com.mesut.math.Interpreter; -import com.mesut.math.parser.MathParser; import com.mesut.math.parser.ParseException; import org.junit.Test; -import java.io.StringReader; - public class InterpreterTest { - @Test - public void parse() throws ParseException { - String line = "x = sin(y)"; - MathParser parser = new MathParser(new StringReader(line)); - System.out.println(parser.equation()); + @Test + public void calls() throws ParseException { + Interpreter interpreter = new Interpreter(); + interpreter.execute("a = x^2"); + interpreter.execute("1 + a(2)"); } @Test - public void interpret() throws ParseException { + public void deriv() throws ParseException { Interpreter interpreter = new Interpreter(); interpreter.execute("a = x^2"); - interpreter.execute("1 + a(2)"); + interpreter.execute("a.derivative() + 3"); } @Test