*/
public OObject auto_simplify(){
// recursively simplify expressions
for( java.util.ListIterator<Expression> i = expressionList.listIterator();
i.hasNext(); ){
OObject o = i.next().auto_simplify();
if( o instanceof Complex ){
complex = complex.multiply( (Complex)o );
i.remove();
} else if( o instanceof Expression ){
i.set( (Expression)o );
} else {
return new jscicalc.Error( "Product.auto_simplify() error" );
}
}
sort();
if( expressionList.isEmpty() ) return unBox(); // should be a Complex
// now work through expressions in sum
java.util.ListIterator<Expression> i = expressionList.listIterator();
Expression f = null; // initialise for loop
for( Expression e = i.next(); i.hasNext(); e = f ){
Product product_e = null;
if( e instanceof Product ){
product_e = (Product)e;
} else {
product_e = new Product( e, false );
}
f = i.next();
Product product_f = null;
if( f instanceof Product ){
product_f = (Product)f;
} else {
product_f = new Product( f, false );
}
if( product_e.compareTo( product_f ) == 0 ){
System.out.println( "Adding comparable expressions" );
Product product = new Product( product_e, false ); // start with e.
product.setComplex( product_e.getComplex()
.add( product_f.getComplex() ) );
OObject o = product.unBox();
if( o instanceof Complex ){
complex = complex.add( (Complex)o );
i.remove(); // remove f
i.previous();
i.remove(); // remove e