@Test public void testSum() { Value s; try { s = simple.sum(null, simple); } catch (SetlException ex) { System.err.println(ex.getMessage()); fail("Simple sum error: SetlException on .sum"); return; } assertTrue("Simple sum error: instanceof", s instanceof SetlVector); ArrayList<Double> sbase = ((SetlVector) s).getVectorCopy(); assertTrue("Simple sum error: wrong result: " + sbase + " vs [2,4,6]", sbase.get(0).equals(2.0) && sbase.get(1).equals(4.0) && sbase.get(2).equals(6.0)); try { s = simple.sum(null, new SetlMatrix(null, simple)); } catch (SetlException ex) { System.err.println(ex.getMessage()); fail("Simple sum matrix error: SetlException on .sum"); return; } assertTrue("Simple sum matrix error: instanceof", s instanceof SetlVector); sbase = ((SetlVector) s).getVectorCopy(); assertTrue("Simple sum matrix error: wrong result: " + sbase + " vs [2,4,6]", sbase.get(0).equals(2.0) && sbase.get(1).equals(4.0) && sbase.get(2).equals(6.0)); }
@Override public Value sum(final State state, final Value summand) throws SetlException { boolean isVector = summand instanceof SetlVector; if(isVector || summand instanceof SetlMatrix) { SetlVector sumd = isVector ? (SetlVector) summand : ((SetlMatrix)summand).toVector(); if(this.size() != sumd.size()) { throw new IncompatibleTypeException("Vectors with different number of dimensions cannot be added to one another."); } ArrayList<Double> result = new ArrayList<>(this.size()); for(int i = 0; i < this.size(); i++) { result.add(vector.get(i) + sumd.vector.get(i)); } return new SetlVector(result); } else if(summand instanceof Term) { return ((Term)summand).sumFlipped(state, this); } else { throw new IncompatibleTypeException("A sum cannot have a vector parameter and a parameter of another type."); } }
SetlVector extends IndexedCollectionValue { @Override public Value sum(final State state, final Value summand) throws SetlException { boolean isVector = summand instanceof SetlVector; if(isVector || summand instanceof SetlMatrix) { SetlVector sumd = isVector ? (SetlVector) summand : ((SetlMatrix)summand).toVector(); if(this.size() != sumd.size()) { throw new IncompatibleTypeException("Vectors with different number of dimensions cannot be added to one another."); } ArrayList<Double> result = new ArrayList<>(this.size()); for(int i = 0; i < this.size(); i++) { result.add(vector.get(i) + sumd.vector.get(i)); } return new SetlVector(result); } else if(summand instanceof Term) { return ((Term)summand).sumFlipped(state, this); } else { throw new IncompatibleTypeException("A sum cannot have a vector parameter and a parameter of another type."); } } }
SetlVector extends IndexedCollectionValue { @Override public Value sum(final State state, final Value summand) throws SetlException { boolean isVector = summand instanceof SetlVector; if(isVector || summand instanceof SetlMatrix) { SetlVector sumd = isVector ? (SetlVector) summand : ((SetlMatrix)summand).toVector(); if(this.size() != sumd.size()) { throw new IncompatibleTypeException("Vectors with different number of dimensions cannot be added to one another."); } ArrayList<Double> result = new ArrayList<>(this.size()); for(int i = 0; i < this.size(); i++) { result.add(vector.get(i) + sumd.vector.get(i)); } return new SetlVector(result); } else if(summand instanceof Term) { return ((Term)summand).sumFlipped(state, this); } else { throw new IncompatibleTypeException("A sum cannot have a vector parameter and a parameter of another type."); } } SetlVector(final ArrayList<Double> vector);  SetlVector(final State state, final CollectionValue init); }
SetlVector extends IndexedCollectionValue { @Override public Value sum(final State state, final Value summand) throws SetlException { boolean isVector = summand instanceof SetlVector; if(isVector || summand instanceof SetlMatrix) { SetlVector sumd = isVector ? (SetlVector) summand : ((SetlMatrix)summand).toVector(); if(this.size() != sumd.size()) { throw new IncompatibleTypeException("Vectors with different number of dimensions cannot be added to one another."); } ArrayList<Double> result = new ArrayList<>(this.size()); for(int i = 0; i < this.size(); i++) { result.add(vector.get(i) + sumd.vector.get(i)); } return new SetlVector(result); } else if(summand instanceof Term) { return ((Term)summand).sumFlipped(state, this); } else { throw new IncompatibleTypeException("A sum cannot have a vector parameter and a parameter of another type."); } } SetlVector(final ArrayList<Double> vector);  SetlVector(final State state, final CollectionValue init); @Override Value clone(); @Override Iterator<Value> iterator(); @Override Iterator<Value> descendingIterator(); @Override SetlBoolean isVector(); @Override Value difference(final State state, final Value subtrahend); @Override Value product(final State state, final Value multiplier); @Override Value quotient(final State state, final Value multiplier); @Override Value sum(final State state, final Value summand); @Override Value cartesianProduct(State state, Value other); @Override void addMember(State state, Value element); @Override SetlBoolean containsMember(State state, Value element); @Override Value firstMember(); @Override Value getMember(int index); @Override Value getMember(State state, Value index); @Override Value getMembers(final State state, final Value low, final Value high); @Override Value lastMember(); @Override Value maximumMember(State state); @Override Value minimumMember(State state); @Override Value removeFirstMember(); @Override Value removeLastMember(); @Override void removeMember(State state, Value element); @Override void setMember(final State state, final Value index, final Value value); @Override void setMember(final State state, int index, final Value value); @Override int size(); @Override void appendString(State state, StringBuilder sb, int tabs); @Override void canonical(State state, StringBuilder sb); @Override MatchResult matchesTerm(State state, Value other); @Override int compareTo(CodeFragment other); @Override long compareToOrdering(); @Override boolean equalTo(Object other); @Override int hashCode(); }
SetlVector extends IndexedCollectionValue { @Override public Value sum(final State state, final Value summand) throws SetlException { boolean isVector = summand instanceof SetlVector; if(isVector || summand instanceof SetlMatrix) { SetlVector sumd = isVector ? (SetlVector) summand : ((SetlMatrix)summand).toVector(); if(this.size() != sumd.size()) { throw new IncompatibleTypeException("Vectors with different number of dimensions cannot be added to one another."); } ArrayList<Double> result = new ArrayList<>(this.size()); for(int i = 0; i < this.size(); i++) { result.add(vector.get(i) + sumd.vector.get(i)); } return new SetlVector(result); } else if(summand instanceof Term) { return ((Term)summand).sumFlipped(state, this); } else { throw new IncompatibleTypeException("A sum cannot have a vector parameter and a parameter of another type."); } } SetlVector(final ArrayList<Double> vector);  SetlVector(final State state, final CollectionValue init); @Override Value clone(); @Override Iterator<Value> iterator(); @Override Iterator<Value> descendingIterator(); @Override SetlBoolean isVector(); @Override Value difference(final State state, final Value subtrahend); @Override Value product(final State state, final Value multiplier); @Override Value quotient(final State state, final Value multiplier); @Override Value sum(final State state, final Value summand); @Override Value cartesianProduct(State state, Value other); @Override void addMember(State state, Value element); @Override SetlBoolean containsMember(State state, Value element); @Override Value firstMember(); @Override Value getMember(int index); @Override Value getMember(State state, Value index); @Override Value getMembers(final State state, final Value low, final Value high); @Override Value lastMember(); @Override Value maximumMember(State state); @Override Value minimumMember(State state); @Override Value removeFirstMember(); @Override Value removeLastMember(); @Override void removeMember(State state, Value element); @Override void setMember(final State state, final Value index, final Value value); @Override void setMember(final State state, int index, final Value value); @Override int size(); @Override void appendString(State state, StringBuilder sb, int tabs); @Override void canonical(State state, StringBuilder sb); @Override MatchResult matchesTerm(State state, Value other); @Override int compareTo(CodeFragment other); @Override long compareToOrdering(); @Override boolean equalTo(Object other); @Override int hashCode();  }