]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
refs #5224
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 26 Aug 2014 05:49:22 +0000 (05:49 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 26 Aug 2014 05:49:22 +0000 (05:49 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30134 ac1ea38d-2e2b-0410-8846-a27921b304fc

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

index d0ceff4f2b3a340c33dcf6a75e5c5a78bd4c0246..4e65b150382f8435459eb25b10ff8aa6fb2b6c32 100644 (file)
@@ -433,6 +433,23 @@ public class Array implements IExpression {
                return result;\r
        }\r
        \r
+       interface Modifier {\r
+               Object modify(Object o);\r
+       }\r
+       \r
+       public Array copy(Modifier modifier) {\r
+               Array result = new Array();\r
+               for(Object element : elements) {\r
+                       if(element instanceof Array) {\r
+                               Array sub = (Array)element;\r
+                               result.addElement(sub.copy(modifier));\r
+                       } else {\r
+                               result.addElement(modifier.modify(element));\r
+                       }\r
+               }\r
+               return result;\r
+       }\r
+       \r
        public boolean isVector() {\r
                return isVector;\r
        }\r
index d3cfce2490c8b009ac9880d231619e39f3c29b91..d9ab2c96630ef623222d8586fe94d3a28b638a74 100644 (file)
@@ -11,6 +11,7 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import fi.semantum.sysdyn.solver.Array.Modifier;\r
 import gnu.trove.list.array.TIntArrayList;\r
 import gnu.trove.map.hash.TObjectIntHashMap;\r
 \r
@@ -271,9 +272,76 @@ final public class Environment implements IEnvironment, ISystem {
                        @Override\r
                        public Object evaluate(IEnvironment environment, int argc) {\r
                                Object value = environment.getValue(0);\r
-                               Double result = (Double)value;\r
-                               double res = result.intValue(); \r
-                               return res;\r
+                               if(value instanceof Double) {\r
+                                       return ((Double)value).intValue();\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 ((Double)o).intValue();\r
+                                                       } else {\r
+                                                               throw new IllegalStateException();\r
+                                                       }\r
+                                               }\r
+                                               \r
+                                       });\r
+                               } else {\r
+                                       throw new IllegalStateException();\r
+                               }\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
@@ -289,6 +357,30 @@ final public class Environment implements IEnvironment, ISystem {
                                return res;\r
                        }\r
                        \r
+               });\r
+               model.functions.put("ones", new Fn1(1) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Object value = environment.getValue(0);\r
+                               Double size = (Double)value;\r
+                               Array res = new Array();\r
+                               for(int i=0;i<size.intValue();i++) res.addElement(1.0);\r
+                               return res;\r
+                       }\r
+                       \r
+               });\r
+               model.functions.put("zeros", new Fn1(1) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Object value = environment.getValue(0);\r
+                               Double size = (Double)value;\r
+                               Array res = new Array();\r
+                               for(int i=0;i<size.intValue();i++) res.addElement(0.0);\r
+                               return res;\r
+                       }\r
+                       \r
                });\r
                model.functions.put("delay", new Fn1(4) {\r
                        \r