public Object evaluate(IEnvironment environment) {\r
Object o1 = exp1.evaluate(environment);\r
Object o2 = exp2.evaluate(environment);\r
+ if(o1 == null || o2 == null) return null; \r
if(o1 instanceof Double && o2 instanceof Double) {\r
return (Double)o1 + (Double)o2;\r
}\r
\r
public class Array implements IExpression {\r
\r
+ public static final Array FULL = new Array();\r
+ \r
private ArrayList<Object> elements = new ArrayList<Object>();\r
\r
public Array() {\r
return true;\r
}\r
\r
- public Array slice(Slice[] indices) {\r
+ public Array slice(Array[] indices) {\r
\r
if(indices.length == 1) {\r
Array result = new Array();\r
- for(int i=indices[0].start;i<=indices[0].end;i++) {\r
- result.addElement(element(i));\r
+ for(Object r : indices[0].elements) {\r
+ Double d = (Double)r;\r
+ int index = d.intValue();\r
+ result.addElement(element(index));\r
}\r
return result;\r
}\r
\r
- if(indices.length != 2) throw new IllegalStateException();\r
+ if(indices.length == 2) {\r
+ if(indices[1] == Array.FULL) {\r
+ Double d = (Double)indices[0].element(0);\r
+ int index = d.intValue();\r
+ return (Array)elements.get(index);\r
+ } else {\r
+ Array result = new Array();\r
+ for(Object o : elements) {\r
+ Array a = (Array)o;\r
+ Double d = (Double)indices[1].element(0);\r
+ int index = d.intValue();\r
+ result.addElement(a.element(index));\r
+ }\r
+ return result;\r
+ }\r
+ }\r
\r
- if(indices[1] == Slice.FULL) {\r
- return (Array)elements.get(indices[0].start);\r
- } else {\r
+ if(indices.length == 3) {\r
Array result = new Array();\r
- for(Object o : elements) {\r
- Array a = (Array)o;\r
- result.addElement(a.element(indices[1].start));\r
+ for(int i=0;i<indices[0].dimension();i++) {\r
+ Double d0 = (Double)indices[0].element(i);\r
+ int index0 = d0.intValue();\r
+ Array a0 = (Array)element(index0);\r
+ for(int j=0;j<indices[1].dimension();j++) {\r
+ Double d1 = (Double)indices[1].element(j);\r
+ int index1 = d1.intValue();\r
+ Array a1 = (Array)a0.element(index1);\r
+ for(int k=0;k<indices[2].dimension();k++) {\r
+ Double d2 = (Double)indices[2].element(k);\r
+ int index2 = d2.intValue();\r
+ result.addElement(a1.element(index2));\r
+ }\r
+ }\r
}\r
return result;\r
}\r
\r
+ throw new IllegalStateException();\r
+ \r
}\r
\r
public Array add(Array other) {\r
}\r
}\r
\r
+ public static Array singleton(double value) {\r
+ Array result = new Array();\r
+ result.addElement(value);\r
+ return result;\r
+ }\r
+\r
+ public static Array slice(int start, int end) {\r
+ Array result = new Array();\r
+ for(int i=start;i<=end;i++) {\r
+ double d = i;\r
+ result.addElement(d);\r
+ }\r
+ return result;\r
+ }\r
+\r
}\r
try {\r
value = Double.parseDouble(name);\r
} catch (NumberFormatException e) {\r
- value = name;\r
+ if("true".equals(name))\r
+ value = Boolean.TRUE;\r
+ else if("false".equals(name))\r
+ value = Boolean.FALSE;\r
+ else\r
+ value = name;\r
}\r
}\r
\r
public Object evaluate(IEnvironment environment) {\r
Object left = exp1.evaluate(environment);\r
Object right = exp2.evaluate(environment);\r
+ if(left == null || right == null) return null;\r
if(left instanceof Double && right instanceof Double) return ((Double)left)/((Double)right);\r
else if (left instanceof Array && right instanceof Double) return arrayDiv((Array)left, (Double)right);\r
else if (left instanceof Double && right instanceof Array) return arrayDiv((Array)right, (Double)left);\r
return Double.valueOf(array.size(col.intValue()));\r
}\r
\r
+ });\r
+ model.functions.put("exp", new Fn1(2) {\r
+\r
+ @Override\r
+ public Object evaluate(IEnvironment environment, int argc) {\r
+ Double x = (Double)environment.getValue(0);\r
+ return Math.exp(x);\r
+ }\r
+ \r
});\r
model.functions.put("zidz", new Fn1(2) {\r
\r
}\r
\r
fn.setLocals(frame);\r
-\r
return fn.evaluate(frame, args.args.length);\r
\r
}\r
public Object evaluate(IEnvironment environment) {\r
Object left = exp1.evaluate(environment);\r
Object right = exp2.evaluate(environment);\r
+ if(left == null || right == null) return null;\r
if(left instanceof Double && right instanceof Double) {\r
return ((Double)left)*((Double)right);\r
} \r
\r
@Override\r
public Object evaluate(IEnvironment environment) {\r
- return -((Double)exp.evaluate(environment));\r
+ Double d = (Double)exp.evaluate(environment);\r
+ if(d == null) return null;\r
+ return -d;\r
}\r
\r
@Override\r
\r
// public IFrame currentFrame;\r
\r
- private int PRINT = 1;\r
+ private int PRINT = 0;\r
\r
public Object walk(SimpleNode n, int indent, IFrame model) {\r
Object result = walk_(n, indent, model);\r
try {\r
if(!ass.assigned) {\r
Object value = ass.expression.evaluate(env);\r
- ass.target.assign(env, ass.subscripts, value);\r
- ass.assigned = true;\r
+ if(value != null) {\r
+ ass.target.assign(env, ass.subscripts, value);\r
+ ass.assigned = true;\r
+ }\r
}\r
} catch (Exception e) {\r
condition++;\r
newValues[i] = derivatives[i].expression.evaluate(env);\r
}\r
for(int i=0;i<model.derivatives.size();i++) {\r
- derivatives[i].target.assign(env, assignments[i].subscripts, newValues[i]);\r
+ derivatives[i].target.assign(env, derivatives[i].subscripts, newValues[i]);\r
}\r
\r
// Increment time\r
return dimensions != null && dimensions != VariableBase.UNINIT;\r
}\r
\r
- public static Slice[] parseSubscripts(IEnvironment env, IExpression[] subscripts) {\r
- Slice[] result = new Slice[subscripts.length];\r
+ public static Array[] parseSubscripts(IEnvironment env, IExpression[] subscripts) {\r
+ Array[] result = new Array[subscripts.length];\r
for(int i=0;i<subscripts.length;i++) {\r
IExpression e = subscripts[i];\r
if(e instanceof ArraySliceExpression) {\r
ArraySliceExpression ase = (ArraySliceExpression)e;\r
Double start = (Double)ase.start.evaluate(env);\r
Double end = (Double)ase.end.evaluate(env);\r
- result[i] = new Slice(start.intValue(), end.intValue());\r
+ result[i] = Array.slice(start.intValue()-1, end.intValue()-1);\r
+ } else if (e instanceof Array) {\r
+ Array array = (Array)e;\r
+ Array r = new Array();\r
+ for(Object o : array.elements()) {\r
+ Double d = (Double)o;\r
+ r.addElement(d-1);\r
+ }\r
+ result[i] = r;\r
} else {\r
Double v = (Double)e.evaluate(env);\r
int index = v.intValue()-1;\r
- if(index == -2) result[i] = Slice.FULL;\r
- else result[i] = new Slice(index,index);\r
+ if(index == -2) result[i] = Array.FULL;\r
+ else result[i] = Array.singleton(index);\r
}\r
}\r
return result;\r
}\r
\r
- public static boolean isSlice(Slice[] subs) {\r
- for(Slice s : subs) {\r
- if(s.start != s.end) return true;\r
+ public static boolean isSlice(Array[] subs) {\r
+ for(Array s : subs) {\r
+ if(s.dimension() != 1) return true;\r
}\r
return false;\r
}\r
\r
@Override\r
public Object evaluate(IEnvironment environment) {\r
- return ((Double)exp1.evaluate(environment)) - ((Double)exp2.evaluate(environment));\r
+ Double d1 = (Double)exp1.evaluate(environment);\r
+ Double d2 = (Double)exp2.evaluate(environment);\r
+ if(d1 == null || d2 == null) return null;\r
+ return d1-d2;\r
}\r
\r
@Override\r
\r
private void validateSize(IEnvironment env, IExpression[] subscripts, Object value) {\r
\r
+ if(value == null) return;\r
+ \r
if(base.isStoredAsArray()) return;\r
\r
int fullDimension = base.dimension();\r
}\r
} else {\r
if(subscripts == null) {\r
+ if(base.dimensions.length == this.subscripts.length && value instanceof Double) \r
+ return;\r
if(!(value instanceof Array))\r
throw new IllegalStateException();\r
Array arr = (Array)value;\r
if(!arr.validateDimensions(base.dimensions, 0))\r
throw new IllegalStateException();\r
} else {\r
+ if(base.dimensions.length == subscripts.length) {\r
+ if(value instanceof Double) return;\r
+ }\r
throw new IllegalStateException();\r
}\r
}\r
\r
@Override\r
public Object evaluate(IEnvironment environment) {\r
- return base.evaluate(environment, subscripts);\r
+ Object result = base.evaluate(environment, subscripts);\r
+ if(result == null) System.err.println("returned null for " + base.name);\r
+ return result;\r
}\r
\r
\r
if(dimension() > 1) {\r
if(subscripts != null) {\r
\r
- Slice[] sub = SolverUtils.parseSubscripts(environment, subscripts);\r
+ Array[] sub = SolverUtils.parseSubscripts(environment, subscripts);\r
if(SolverUtils.isSlice(sub)) {\r
Array arr = new Array();\r
intoArray(environment, index, 0, arr);\r