From 30cd8ac70858e0859ac00248edd1d9c2ba4ceb4f Mon Sep 17 00:00:00 2001 From: villberg Date: Wed, 15 Apr 2015 11:44:30 +0000 Subject: [PATCH] (fixes #5769) Add java.lang.Math functions as built-in functions git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31175 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../semantum/sysdyn/solver/Environment.java | 517 +++++++++++++++--- 1 file changed, 435 insertions(+), 82 deletions(-) diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java index 247a93d1..207d8f8a 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java @@ -91,13 +91,75 @@ final public class Environment implements IEnvironment, ISystem { this.step = step; this.time = start; - model.functions.put("size", new Fn1(2) { + model.functions.put("sin", new Fn1(2) { @Override public Object evaluate(IEnvironment environment, int argc) { - Array array = (Array)environment.getValue(0); - Double col = (Double)environment.getValue(1); - return Double.valueOf(array.size(col.intValue())); + Double x = (Double)environment.getValue(0); + return Math.sin(x); + } + + }); + model.functions.put("cos", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.cos(x); + } + + }); + model.functions.put("tan", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.tan(x); + } + + }); + model.functions.put("asin", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.asin(x); + } + + }); + model.functions.put("acos", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.acos(x); + } + + }); + model.functions.put("atan", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.atan(x); + } + + }); + model.functions.put("toRadians", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.toRadians(x); + } + + }); + model.functions.put("toDegrees", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.toDegrees(x); } }); @@ -118,6 +180,375 @@ final public class Environment implements IEnvironment, ISystem { return Math.log(x); } + }); + model.functions.put("log10", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.log10(x); + } + + }); + model.functions.put("sqrt", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.sqrt(x); + } + + }); + model.functions.put("cbrt", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.cbrt(x); + } + + }); + model.functions.put("IEEEremainder", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + Double y = (Double)environment.getValue(1); + return Math.IEEEremainder(x, y); + } + + }); + model.functions.put("ceil", new Fn1(1) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Object value = environment.getValue(0); + if(value instanceof Double) { + return Math.ceil((Double)value); + } else if(value instanceof Array) { + return ((Array)value).copy(new Modifier() { + + @Override + public Object modify(Object o) { + if(o instanceof Double) { + return Math.ceil((Double)o); + } else { + throw new IllegalStateException(); + } + } + + }); + } else { + throw new IllegalStateException(); + } + } + + }); + model.functions.put("floor", new Fn1(1) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Object value = environment.getValue(0); + if(value instanceof Double) { + return Math.floor((Double)value); + } else if(value instanceof Array) { + return ((Array)value).copy(new Modifier() { + + @Override + public Object modify(Object o) { + if(o instanceof Double) { + return Math.floor((Double)o); + } else { + throw new IllegalStateException(); + } + } + + }); + } else { + throw new IllegalStateException(); + } + } + + }); + model.functions.put("rint", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.rint(x); + } + + }); + model.functions.put("atan2", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + Double y = (Double)environment.getValue(1); + return Math.atan2(x, y); + } + + }); + model.functions.put("pow", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + Double y = (Double)environment.getValue(1); + return Math.pow(x, y); + } + + }); + model.functions.put("round", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + long result = Math.round(x); + return (int)result; + } + + }); + model.functions.put("floorDiv", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Integer x = (Integer)environment.getValue(0); + Integer y = (Integer)environment.getValue(1); + return Math.floorDiv(x, y); + } + + }); + model.functions.put("floorMod", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Integer x = (Integer)environment.getValue(0); + Integer y = (Integer)environment.getValue(1); + return Math.floorMod(x, y); + } + + }); + model.functions.put("abs", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Object o = environment.getValue(0); + if(o instanceof Double) { + return Math.abs((Double)o); + } else if (o instanceof Integer) { + return Math.abs((Integer)o); + } else { + throw new IllegalStateException("Unsupported argument to abs: " + o); + } + } + + }); + + model.functions.put("min", new Fn1(5) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Object first = environment.getValue(0); + if(first instanceof Double) { + Double result = (Double)first; + for(int i=1;i result) result = d; + } + return result; + } else if (first instanceof Integer) { + Integer result = (Integer )first; + for(int i=1;i result) result = d; + } + return result; + } else { + throw new IllegalStateException("Unsupported argument to max: " + first); + } + } + + }); + + model.functions.put("ulp", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.ulp(x); + } + + }); + + model.functions.put("signum", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.signum(x); + } + + }); + + model.functions.put("sinh", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.sinh(x); + } + + }); + + model.functions.put("cosh", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.cosh(x); + } + + }); + + model.functions.put("tanh", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.tanh(x); + } + + }); + + model.functions.put("hypot", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + Double y = (Double)environment.getValue(1); + return Math.hypot(x, y); + } + + }); + + model.functions.put("expm1", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.expm1(x); + } + + }); + + model.functions.put("log1p", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.log1p(x); + } + + }); + + model.functions.put("copySign", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + Double y = (Double)environment.getValue(1); + return Math.copySign(x, y); + } + + }); + + model.functions.put("getExponent", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.getExponent(x); + } + + }); + + model.functions.put("nextAfter", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + Double y = (Double)environment.getValue(1); + return Math.nextAfter(x, y); + } + + }); + + model.functions.put("nextUp", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.nextUp(x); + } + + }); + + model.functions.put("nextDown", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + return Math.nextDown(x); + } + + }); + + model.functions.put("scalb", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Double x = (Double)environment.getValue(0); + Integer y = (Integer)environment.getValue(1); + return Math.scalb(x, y); + } + + }); + + model.functions.put("size", new Fn1(2) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Array array = (Array)environment.getValue(0); + Double col = (Double)environment.getValue(1); + return Double.valueOf(array.size(col.intValue())); + } + }); model.functions.put("zidz", new Fn1(2) { @@ -248,32 +679,6 @@ final public class Environment implements IEnvironment, ISystem { } - }); - model.functions.put("min", new Fn1(5) { - - @Override - public Object evaluate(IEnvironment environment, int argc) { - Double result = (Double)environment.getValue(0); - for(int i=1;i result) result = d; - } - return result; - } - }); model.functions.put("integer", new Fn1(1) { @@ -302,58 +707,6 @@ final public class Environment implements IEnvironment, ISystem { } } - }); - model.functions.put("ceil", new Fn1(1) { - - @Override - public Object evaluate(IEnvironment environment, int argc) { - Object value = environment.getValue(0); - if(value instanceof Double) { - return Math.ceil((Double)value); - } else if(value instanceof Array) { - return ((Array)value).copy(new Modifier() { - - @Override - public Object modify(Object o) { - if(o instanceof Double) { - return Math.ceil((Double)o); - } else { - throw new IllegalStateException(); - } - } - - }); - } else { - throw new IllegalStateException(); - } - } - - }); - model.functions.put("floor", new Fn1(1) { - - @Override - public Object evaluate(IEnvironment environment, int argc) { - Object value = environment.getValue(0); - if(value instanceof Double) { - return Math.floor((Double)value); - } else if(value instanceof Array) { - return ((Array)value).copy(new Modifier() { - - @Override - public Object modify(Object o) { - if(o instanceof Double) { - return Math.floor((Double)o); - } else { - throw new IllegalStateException(); - } - } - - }); - } else { - throw new IllegalStateException(); - } - } - }); model.functions.put("sum", new Fn1(1) { -- 2.47.1