Algebra equation parser for java

You could make use of Java 1.6’s scripting capabilities: import javax.script.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName(“JavaScript”); Map<String, Object> vars = new HashMap<String, Object>(); vars.put(“x”, 2); vars.put(“y”, 1); vars.put(“z”, 3); System.out.println(“result = “+engine.eval(“x + y + z”, new SimpleBindings(vars))); } } which … Read more

How to plot a circle in Matlab?

Don’t laugh, but the easiest would be to use the rectangle function, indeed 😉 %// radius r = 2; %// center c = [3 3]; pos = [c-r 2*r 2*r]; rectangle(‘Position’,pos,’Curvature’,[1 1]) axis equal but set the curvature of the rectangle to 1! The position vector defines the rectangle, the first two values x and … Read more

Safely evaluate simple string equation

One way would be to use numexpr. It’s mostly a module for optimizing (and multithreading) numpy operations but it can also handle mathematical python expressions: >>> import numexpr >>> numexpr.evaluate(‘2 + 4.1 * 3′) array(14.299999999999999) You can call .item on the result to get a python-like type: >>> numexpr.evaluate(’17 / 3′).item() 5.666666666666667 It’s a 3rd … Read more

Equation parsing in Python

Python’s own internal compiler can parse this, if you use Python notation. If your change the notation slightly, you’ll be happier. import compiler eq= “sin(x)*x**2” ast= compiler.parse( eq ) You get an abstract syntax tree that you can work with.