Skip to content

Commit

Permalink
fix Interpreter eval calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mesut146 committed Apr 13, 2021
1 parent aedfb13 commit 6bf6b22
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 163 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,27 +16,27 @@ 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
```

### 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

Expand Down
30 changes: 5 additions & 25 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<maven.compiler.target>1.7</maven.compiler.target>
<mainClass>com.mesut.math.Main</mainClass>
</properties>

<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

<build>
Expand All @@ -28,26 +28,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/mesut/math/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
113 changes: 0 additions & 113 deletions src/main/java/com/mesut/math/Main.java

This file was deleted.

12 changes: 2 additions & 10 deletions src/main/java/com/mesut/math/core/FuncCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ public List<func> 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
Expand Down
17 changes: 7 additions & 10 deletions src/test/java/InterpreterTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 6bf6b22

Please # to comment.