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
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
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
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
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
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
\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
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