]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Ones was broken, transpose was missing, multiplication operator did not handle all...
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 26 Sep 2014 12:56:14 +0000 (12:56 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 26 Sep 2014 12:56:14 +0000 (12:56 +0000)
refs #5224

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30323 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
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Multiplication.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java

index e7b1959d63865cc68fc0489ab514f626c70b8fc0..6cdc5b83ae9fa9be1e9732e9c3ddf2768118f9b3 100644 (file)
@@ -16,6 +16,9 @@ import java.util.Iterator;
 import java.util.List;\r
 import java.util.Map;\r
 \r
+/*\r
+ * For matrices first dimension is rows and second is columns i.e. top level elements are rows\r
+ */\r
 public class Array implements IExpression {\r
 \r
        public static final Array FULL = new Array();\r
@@ -309,7 +312,7 @@ public class Array implements IExpression {
                double result = 0;\r
                Collection<Object> lae = elements();\r
                Collection<Object> rae = other.elements();\r
-               if(lae.size() != rae.size()) throw new IllegalStateException();\r
+               if(lae.size() != rae.size()) throw new IllegalStateException("inner product: vector sizes do not match: " + this + " vs. " + other);\r
                Iterator<Object> li = lae.iterator();\r
                Iterator<Object> ri = rae.iterator();\r
                for(int i=0;i<lae.size();i++) {\r
@@ -324,6 +327,37 @@ public class Array implements IExpression {
                return result;\r
        }\r
 \r
+       /*\r
+        * Other array is vector (this is matrix)\r
+        */\r
+       public Array matrixMulVector(Array other) {\r
+               Array result = new Array();\r
+               for(Object o : elements) {\r
+                       // Objects are rows\r
+                       Array a = (Array)o;\r
+                       result.addElement(a.inner(other));\r
+               }\r
+               return result;\r
+       }\r
+\r
+       /*\r
+        * Other array is matrix (this is matrix)\r
+        */\r
+       public Array matrixMulMatrix(Array other) {\r
+               Array result = new Array();\r
+               Array transposed = other.transposed();\r
+               for(Object rightColumn_ : transposed.elements()) {\r
+                       Array rightColumn = (Array)rightColumn_;\r
+                       Array resultColumn = new Array();\r
+                       for(Object leftRow_ : elements) {\r
+                               Array leftRow = (Array)leftRow_;\r
+                               resultColumn.addElement(leftRow.inner(rightColumn));\r
+                       }\r
+                       result.addElement(resultColumn);\r
+               }\r
+               return result.transposed();\r
+       }\r
+\r
        public Array pow(Array other) {\r
                Array result = new Array();\r
                Collection<Object> lae = elements();\r
@@ -535,5 +569,30 @@ public class Array implements IExpression {
        public void applyPartial(Array[] indices, Array value) {\r
                applyPartial(indices, value, 0);\r
        }\r
+       \r
+       public Array transposed() {\r
+               \r
+               List<Object> elements = elements();\r
+               if(elements.isEmpty()) return this;\r
+               \r
+               Array first = (Array)elements.get(0);\r
+               \r
+               Array[] arrs = new Array[first.elements().size()];\r
+               for(int i=0;i<arrs.length;i++) arrs[i] = new Array();\r
+               \r
+               for(int i=0;i<elements.size();i++) {\r
+                       Array a = (Array)elements.get(i);\r
+                       for(int j=0;j<a.elements().size();j++) {\r
+                               Object o = a.element(j);\r
+                               arrs[j].addElement(o);\r
+                       }\r
+               }\r
+\r
+               Array result = new Array();\r
+               for(Array a : arrs) result.addElement(a);\r
+\r
+               return result;\r
+               \r
+       }\r
 \r
 }\r
index 34f6319bca78256d78babaabdc839e3156abc310..135410d7283d6e4f1abd23901ffdc2232c0e5ab2 100644 (file)
@@ -397,7 +397,7 @@ final public class Environment implements IEnvironment, ISystem {
                        public Object evaluate(IEnvironment environment, int argc) {\r
                                int[] dims = new int[argc];\r
                                for(int i=0;i<argc;i++) {\r
-                                       Double d = (Double)environment.getValue(0);\r
+                                       Double d = (Double)environment.getValue(i);\r
                                        dims[i] = d.intValue();\r
                                }\r
                                return make(dims, 0);\r
@@ -427,6 +427,17 @@ final public class Environment implements IEnvironment, ISystem {
                                return make(dims, 0);\r
                        }\r
                        \r
+               });\r
+               model.functions.put("transpose", new Fn1(1) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               \r
+                               Array array = (Array)environment.getValue(0);\r
+                               return array.transposed();\r
+                               \r
+                       }\r
+                       \r
                });\r
                model.functions.put("delay", new Fn1(4) {\r
                        \r
index 154d4e91b4268ab4dfbc12ddebba1150f2be03a1..97791824bc142a658d0136f550db7dcd0b19317a 100644 (file)
@@ -40,6 +40,10 @@ public class Multiplication implements IExpression {
                        Array ra = (Array)right;\r
                        if(la.isVector && ra.isVector) {\r
                                return la.inner(ra);\r
+                       } else if(ra.isVector) {\r
+                               return la.matrixMulVector(ra);\r
+                       } else {\r
+                               return la.matrixMulMatrix(ra);\r
                        }\r
                }\r
                else if (left instanceof Array && right instanceof Double) return ((Array)left).mul((Double)right);\r
index 4d083f77e4dfc63077d3513d2f7975f641193ebd..d66eb549b8ea846ea5c434ecb91f12623b6a5c2a 100644 (file)
@@ -28,7 +28,7 @@ import fi.semantum.sysdyn.solver.parser.SimpleNode;
 \r
 public class Solver {\r
 \r
-       private static final boolean VALIDATE = false;  \r
+       private static final boolean VALIDATE = true;   \r
        private static final boolean PRINT_EXCEPTIONS = false;  \r
        private static final int STACK_SIZE = 1000;\r
        private static final int MAX_LOOPS = 50;\r
@@ -115,8 +115,13 @@ public class Solver {
                if(VALIDATE) {\r
                        if(value instanceof Double) {\r
                                if(Double.isNaN((Double)value)) {\r
+                                       System.err.println("value is invalid (NaN): " + var.base.name);\r
                                        throw new IllegalStateException("value is invalid (NaN)");\r
                                }\r
+                               if(Double.isInfinite((Double)value)) {\r
+                                       System.err.println("value is invalid (Infinite): " + var.base.name);\r
+                                       throw new IllegalStateException("value is invalid (Infinite)");\r
+                               }\r
                        }\r
                        if(value instanceof Array) {\r
                                for(Object o : ((Array)value).elements()) validate(var, o);\r