]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
(fixes #5769) Add java.lang.Math functions as built-in functions
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 15 Apr 2015 11:44:30 +0000 (11:44 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 15 Apr 2015 11:44:30 +0000 (11:44 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31175 ac1ea38d-2e2b-0410-8846-a27921b304fc

fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java

index 247a93d1ec1de650078a55ef25c9ad58992f9f12..207d8f8a959cba7673f4d5bd8054ca61157e118c 100644 (file)
@@ -91,13 +91,75 @@ final public class Environment implements IEnvironment, ISystem {
                this.step = step;\r
                this.time = start;\r
                \r
-               model.functions.put("size", new Fn1(2) {\r
+               model.functions.put("sin", new Fn1(2) {\r
 \r
                        @Override\r
                        public Object evaluate(IEnvironment environment, int argc) {\r
-                               Array array = (Array)environment.getValue(0);\r
-                               Double col = (Double)environment.getValue(1);\r
-                               return Double.valueOf(array.size(col.intValue()));\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.sin(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("cos", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.cos(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("tan", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.tan(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("asin", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.asin(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("acos", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.acos(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("atan", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.atan(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("toRadians", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.toRadians(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("toDegrees", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.toDegrees(x);\r
                        }\r
                        \r
                });\r
@@ -118,6 +180,375 @@ final public class Environment implements IEnvironment, ISystem {
                                return Math.log(x);\r
                        }\r
                        \r
+               });\r
+               model.functions.put("log10", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.log10(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("sqrt", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.sqrt(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("cbrt", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.cbrt(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("IEEEremainder", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               Double y = (Double)environment.getValue(1);\r
+                               return Math.IEEEremainder(x, y);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("ceil", new Fn1(1) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Object value = environment.getValue(0);\r
+                               if(value instanceof Double) {\r
+                                       return Math.ceil((Double)value);\r
+                               } else if(value instanceof Array) {\r
+                                       return ((Array)value).copy(new Modifier() {\r
+\r
+                                               @Override\r
+                                               public Object modify(Object o) {\r
+                                                       if(o instanceof Double) {\r
+                                                               return Math.ceil((Double)o);\r
+                                                       } else {\r
+                                                               throw new IllegalStateException();\r
+                                                       }\r
+                                               }\r
+                                               \r
+                                       });\r
+                               } else {\r
+                                       throw new IllegalStateException();\r
+                               }\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("floor", new Fn1(1) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Object value = environment.getValue(0);\r
+                               if(value instanceof Double) {\r
+                                       return Math.floor((Double)value);\r
+                               } else if(value instanceof Array) {\r
+                                       return ((Array)value).copy(new Modifier() {\r
+\r
+                                               @Override\r
+                                               public Object modify(Object o) {\r
+                                                       if(o instanceof Double) {\r
+                                                               return Math.floor((Double)o);\r
+                                                       } else {\r
+                                                               throw new IllegalStateException();\r
+                                                       }\r
+                                               }\r
+                                               \r
+                                       });\r
+                               } else {\r
+                                       throw new IllegalStateException();\r
+                               }\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("rint", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.rint(x);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("atan2", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               Double y = (Double)environment.getValue(1);\r
+                               return Math.atan2(x, y);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("pow", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               Double y = (Double)environment.getValue(1);\r
+                               return Math.pow(x, y);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("round", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               long result = Math.round(x); \r
+                               return (int)result;\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("floorDiv", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Integer x = (Integer)environment.getValue(0);\r
+                               Integer y = (Integer)environment.getValue(1);\r
+                               return Math.floorDiv(x, y);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("floorMod", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Integer x = (Integer)environment.getValue(0);\r
+                               Integer y = (Integer)environment.getValue(1);\r
+                               return Math.floorMod(x, y);\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("abs", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Object o = environment.getValue(0);\r
+                               if(o instanceof Double) {\r
+                                       return Math.abs((Double)o);\r
+                               } else if (o instanceof Integer) {\r
+                                       return Math.abs((Integer)o);\r
+                               } else {\r
+                                       throw new IllegalStateException("Unsupported argument to abs: " + o);\r
+                               }\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("min", new Fn1(5) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Object first = environment.getValue(0);\r
+                               if(first instanceof Double) {\r
+                                       Double result = (Double)first;\r
+                                       for(int i=1;i<argc;i++) {\r
+                                               Double d = (Double)environment.getValue(i);\r
+                                               if(d < result) result = d;\r
+                                       }\r
+                                       return result;\r
+                               } else if (first instanceof Integer) {\r
+                                       Integer result = (Integer )first;\r
+                                       for(int i=1;i<argc;i++) {\r
+                                               Integer  d = (Integer )environment.getValue(i);\r
+                                               if(d < result) result = d;\r
+                                       }\r
+                                       return result;\r
+                               } else {\r
+                                       throw new IllegalStateException("Unsupported argument to min: " + first);\r
+                               }\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("max", new Fn1(5) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Object first = environment.getValue(0);\r
+                               if(first instanceof Double) {\r
+                                       Double result = (Double)first;\r
+                                       for(int i=1;i<argc;i++) {\r
+                                               Double d = (Double)environment.getValue(i);\r
+                                               if(d > result) result = d;\r
+                                       }\r
+                                       return result;\r
+                               } else if (first instanceof Integer) {\r
+                                       Integer result = (Integer )first;\r
+                                       for(int i=1;i<argc;i++) {\r
+                                               Integer  d = (Integer )environment.getValue(i);\r
+                                               if(d > result) result = d;\r
+                                       }\r
+                                       return result;\r
+                               } else {\r
+                                       throw new IllegalStateException("Unsupported argument to max: " + first);\r
+                               }\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("ulp", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.ulp(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("signum", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.signum(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("sinh", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.sinh(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("cosh", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.cosh(x);\r
+                       }\r
+                       \r
+               });\r
+               \r
+               model.functions.put("tanh", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.tanh(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("hypot", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               Double y = (Double)environment.getValue(1);\r
+                               return Math.hypot(x, y);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("expm1", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.expm1(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("log1p", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.log1p(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("copySign", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               Double y = (Double)environment.getValue(1);\r
+                               return Math.copySign(x, y);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("getExponent", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.getExponent(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("nextAfter", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               Double y = (Double)environment.getValue(1);\r
+                               return Math.nextAfter(x, y);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("nextUp", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.nextUp(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("nextDown", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.nextDown(x);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("scalb", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               Integer y = (Integer)environment.getValue(1);\r
+                               return Math.scalb(x, y);\r
+                       }\r
+                       \r
+               });\r
+\r
+               model.functions.put("size", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Array array = (Array)environment.getValue(0);\r
+                               Double col = (Double)environment.getValue(1);\r
+                               return Double.valueOf(array.size(col.intValue()));\r
+                       }\r
+                       \r
                });\r
                model.functions.put("zidz", new Fn1(2) {\r
 \r
@@ -248,32 +679,6 @@ final public class Environment implements IEnvironment, ISystem {
                                \r
                        }\r
                        \r
-               });\r
-               model.functions.put("min", new Fn1(5) {\r
-\r
-                       @Override\r
-                       public Object evaluate(IEnvironment environment, int argc) {\r
-                               Double result = (Double)environment.getValue(0);\r
-                               for(int i=1;i<argc;i++) {\r
-                                       Double d = (Double)environment.getValue(i);\r
-                                       if(d < result) result = d;\r
-                               }\r
-                               return result;\r
-                       }\r
-                       \r
-               });\r
-               model.functions.put("max", new Fn1(5) {\r
-\r
-                       @Override\r
-                       public Object evaluate(IEnvironment environment, int argc) {\r
-                               Double result = (Double)environment.getValue(0);\r
-                               for(int i=1;i<argc;i++) {\r
-                                       Double d = (Double)environment.getValue(i);\r
-                                       if(d > result) result = d;\r
-                               }\r
-                               return result;\r
-                       }\r
-                       \r
                });\r
                model.functions.put("integer", new Fn1(1) {\r
 \r
@@ -302,58 +707,6 @@ final public class Environment implements IEnvironment, ISystem {
                                }\r
                        }\r
                        \r
-               });\r
-               model.functions.put("ceil", new Fn1(1) {\r
-\r
-                       @Override\r
-                       public Object evaluate(IEnvironment environment, int argc) {\r
-                               Object value = environment.getValue(0);\r
-                               if(value instanceof Double) {\r
-                                       return Math.ceil((Double)value);\r
-                               } else if(value instanceof Array) {\r
-                                       return ((Array)value).copy(new Modifier() {\r
-\r
-                                               @Override\r
-                                               public Object modify(Object o) {\r
-                                                       if(o instanceof Double) {\r
-                                                               return Math.ceil((Double)o);\r
-                                                       } else {\r
-                                                               throw new IllegalStateException();\r
-                                                       }\r
-                                               }\r
-                                               \r
-                                       });\r
-                               } else {\r
-                                       throw new IllegalStateException();\r
-                               }\r
-                       }\r
-                       \r
-               });\r
-               model.functions.put("floor", new Fn1(1) {\r
-\r
-                       @Override\r
-                       public Object evaluate(IEnvironment environment, int argc) {\r
-                               Object value = environment.getValue(0);\r
-                               if(value instanceof Double) {\r
-                                       return Math.floor((Double)value);\r
-                               } else if(value instanceof Array) {\r
-                                       return ((Array)value).copy(new Modifier() {\r
-\r
-                                               @Override\r
-                                               public Object modify(Object o) {\r
-                                                       if(o instanceof Double) {\r
-                                                               return Math.floor((Double)o);\r
-                                                       } else {\r
-                                                               throw new IllegalStateException();\r
-                                                       }\r
-                                               }\r
-                                               \r
-                                       });\r
-                               } else {\r
-                                       throw new IllegalStateException();\r
-                               }\r
-                       }\r
-                       \r
                });\r
                model.functions.put("sum", new Fn1(1) {\r
 \r