From 7df05015276ebed35552e9fbaa35148a1d10819f Mon Sep 17 00:00:00 2001 From: villberg Date: Wed, 3 Sep 2014 12:24:45 +0000 Subject: [PATCH] Multiple bugfixes for project model refs #5224 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30195 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../fi/semantum/sysdyn/solver/Addition.java | 11 +- .../src/fi/semantum/sysdyn/solver/Array.java | 103 ++++++------------ .../fi/semantum/sysdyn/solver/Derivate.java | 25 +++-- .../fi/semantum/sysdyn/solver/Division.java | 23 ++-- .../sysdyn/solver/ElementwiseDivision.java | 61 ++++++----- .../sysdyn/solver/ElementwisePower.java | 53 ++++++--- .../sysdyn/solver/ElementwiseProduct.java | 63 +++++------ .../semantum/sysdyn/solver/Environment.java | 90 +++++++++++---- .../fi/semantum/sysdyn/solver/Function.java | 2 +- .../semantum/sysdyn/solver/GreaterThan.java | 4 +- .../src/fi/semantum/sysdyn/solver/Model.java | 96 +++++++++++----- .../semantum/sysdyn/solver/Subtraction.java | 11 +- .../semantum/sysdyn/solver/VariableBase.java | 36 +++--- 13 files changed, 349 insertions(+), 229 deletions(-) diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java index 8c09ab6f..6c531d41 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.Array.Modifier2; + public class Addition implements IExpression { public IExpression exp1; @@ -38,7 +40,14 @@ public class Addition implements IExpression { if(o1 instanceof Array && o2 instanceof Array) { Array la = (Array)o1; Array ra = (Array)o2; - return la.add(ra); + return la.copy2(ra, new Modifier2() { + + @Override + public Object modify(Object o1, Object o2) { + if(o1 instanceof Double && o2 instanceof Double) return ((Double)o1)+((Double)o2); + throw new IllegalStateException("Tried to add a non-numerical array"); + } + }); } throw new IllegalStateException(); } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java index eaa4cca1..8c34ffc4 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java @@ -301,75 +301,7 @@ public class Array implements IExpression { throw new IllegalStateException(); } - - public Array add(Array other) { - Array result = new Array(); - Collection lae = elements(); - Collection rae = other.elements(); - if(lae.size() != rae.size()) throw new IllegalStateException(); - Iterator li = lae.iterator(); - Iterator ri = rae.iterator(); - for(int i=0;i lae = elements(); - Collection rae = other.elements(); - if(lae.size() != rae.size()) throw new IllegalStateException(); - Iterator li = lae.iterator(); - Iterator ri = rae.iterator(); - for(int i=0;i lae = elements(); - Collection rae = other.elements(); - if(lae.size() != rae.size()) throw new IllegalStateException(); - Iterator li = lae.iterator(); - Iterator ri = rae.iterator(); - for(int i=0;i es = elements(); + Collection oes = other.elements(); + if(es.size() != oes.size()) throw new IllegalStateException("Array dimensions do not match"); + Iterator e = es.iterator(); + Iterator oe = oes.iterator(); + for(int i=0;i lae = la.elements(); - Collection rae = ra.elements(); - if(lae.size() != rae.size()) throw new UnsupportedOperationException(); - Iterator li = lae.iterator(); - Iterator ri = rae.iterator(); - Array result = new Array(); - for(int i=0;i lae = la.elements(); - Iterator li = lae.iterator(); - Array result = new Array(); - for(int i=0;i lae = la.elements(); - Collection rae = ra.elements(); - if(lae.size() != rae.size()) throw new UnsupportedOperationException(); - Iterator li = lae.iterator(); - Iterator ri = rae.iterator(); - Array result = new Array(); - for(int i=0;i lae = la.elements(); - List rae = ra.elements(); - if(lae.size() != rae.size()) throw new UnsupportedOperationException(); - Array result = new Array(lae.size()); - int index = 0; - for(Object o : lae) { - Double ld = (Double)o; - Double rd = (Double)rae.get(index++); - result.addElement(ld*rd); - } - return result; + return la.copy2(ra, new Modifier2() { + + @Override + public Object modify(Object o1, Object o2) { + if(o1 instanceof Double && o2 instanceof Double) return ((Double)o1)*((Double)o2); + throw new IllegalStateException("Tried to multiply a non-numerical array"); + } + }); } else if (left instanceof Array && right instanceof Double) { Array la = (Array)left; - Double rd = (Double)right; - List lae = la.elements(); - Array result = new Array(lae.size()); - for(Object o : lae) { - Double ld = (Double)o; - result.addElement(ld*rd); - } - return result; + final double rd = (Double)right; + return la.copy(new Modifier() { + + @Override + public Object modify(Object o) { + if(o instanceof Double) return ((Double)o)*rd; + else throw new IllegalStateException("Tried to multiply a non-numerical array"); + } + + }); } else if (left instanceof Double && right instanceof Array) { - Array ra = (Array)right; - Double ld = (Double)left; - List rae = ra.elements(); - Array result = new Array(rae.size()); - for(Object o : rae) { - Double rd = (Double)o; - result.addElement(ld*rd); - } - return result; + Array la = (Array)right; + final double rd = (Double)left; + return la.copy(new Modifier() { + + @Override + public Object modify(Object o) { + if(o instanceof Double) return ((Double)o)*rd; + else throw new IllegalStateException("Tried to multiply a non-numerical array"); + } + + }); } else { throw new UnsupportedOperationException(); } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java index 84d8afed..b4dc80e0 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java @@ -16,6 +16,7 @@ import gnu.trove.list.array.TIntArrayList; import gnu.trove.map.hash.TObjectIntHashMap; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -29,7 +30,7 @@ interface Fn { public Object evaluateInput(IEnvironment environment, int argPosition); public void setLocals(IEnvironment environment); public int offset(); - public Variable[] parameters(); + public Variable[] parameters(int args); } abstract class Fn1 implements Fn { @@ -37,17 +38,25 @@ abstract class Fn1 implements Fn { Variable[] parameters; public Fn1(int pCount) { - parameters = new Variable[pCount]; - for(int i=0;i 0) { + parameters = new Variable[pCount]; + for(int i=0;i ((Double)exp2.evaluate(environment)); + Object left = exp1.evaluate(environment); + Object right = exp2.evaluate(environment); + return ((Double)left) > ((Double)right); } @Override diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java index a5fde8b0..537d238f 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java @@ -13,6 +13,7 @@ package fi.semantum.sysdyn.solver; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -98,7 +99,7 @@ class Model implements IFrame { argh.add(args.args[i].modification.evaluate(environment)); } - Variable[] parameters = fn.parameters(); + Variable[] parameters = fn.parameters(argh.size()); for(int i=0;i arrs = new ArrayList(); for(int i=0;i 0 && dim != vectorDimension) throw new IllegalStateException("Array dimensions do not agree"); + vectorDimension = dim; + vector[i] = true; + arrs.add((Array)ps[i]); + } else { + vector[i] = false; + } } if(vectorDimension > 0) { - - Array result = new Array(); - - for(int i=0;i arrs) { + + Array result = new Array(); + + Array first = arrs.get(0); + + int d = first.elements().size(); + if(d == 0) return result; + + Object firstElement = first.element(0); + boolean subArray = firstElement instanceof Array; + + for(int i=0;i subArrs = new ArrayList(); + for(int j=0;j work, VariableBase target) { VariableBase deep = work.get(target.name); if(deep == null) return target; diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Subtraction.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Subtraction.java index 4ef86db8..e908d74d 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Subtraction.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Subtraction.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.Array.Modifier2; + public class Subtraction implements IExpression { public IExpression exp1; @@ -39,7 +41,14 @@ public class Subtraction implements IExpression { if(o1 instanceof Array && o2 instanceof Array) { Array la = (Array)o1; Array ra = (Array)o2; - return la.sub(ra); + return la.copy2(ra, new Modifier2() { + + @Override + public Object modify(Object o1, Object o2) { + if(o1 instanceof Double && o2 instanceof Double) return ((Double)o1)-((Double)o2); + throw new IllegalStateException("Tried to subtract a non-numerical array"); + } + }); } throw new IllegalStateException(); } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java index e0a57a6c..40c75d2d 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java @@ -219,24 +219,30 @@ public class VariableBase { else return array; } else { - + if(dimension() > 1) { + + Array array = new Array(); + intoArray(environment, index, 0, array); if(subscripts != null) { - - Array[] sub = SolverUtils.parseSubscripts(environment, subscripts); - if(SolverUtils.isSlice(sub)) { - Array arr = new Array(); - intoArray(environment, index, 0, arr); - return arr.slice(sub); - } else { - return environment.getValue(index(environment, subscripts)); - } - - } else { - Array array = new Array(); - intoArray(environment, index, 0, array); - return array; + return array.subscript(environment, subscripts); } + else return array; + +// if(subscripts != null) { +// +// Array[] sub = SolverUtils.parseSubscripts(environment, subscripts); +// if(SolverUtils.isSlice(sub)) { +// Array arr = new Array(); +// intoArray(environment, index, 0, arr); +// return arr.slice(sub); +// } else { +// return environment.getValue(index(environment, subscripts)); +// } +// +// } else { +// return array; +// } } Object result = environment.getNamedValue(name); -- 2.47.1