public OObject auto_simplify(){
// first remove quotients
for( java.util.ListIterator<Expression> i = divisorList.listIterator();
i.hasNext(); ){
Expression e = i.next();
OObject o = new Power( e, new Complex( -1 ) ).auto_simplify();
if( o instanceof Complex ){
complex = complex.divide( (Complex)o );
} else if( o instanceof Expression ){
expressionList.add( (Expression)o );
} else {
return new jscicalc.Error( "Product.auto_simplify() error" );
}
}
divisorList.clear();
// simplify remaining 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 product
java.util.ListIterator<Expression> i = expressionList.listIterator();
Expression f = null; // initialise for loop
for( Expression e = i.next(); i.hasNext(); e = f ){
OObject base_e = e;
OObject exponent_e = new Complex( 1 );
if( e instanceof Power ){
base_e = ((Power)e).base();
exponent_e = ((Power)e).exponent();
}
f = i.next();
OObject base_f = f;
OObject exponent_f = new Complex( 1 );
if( f instanceof Power ){
base_f = ((Power)f).base();
exponent_f = ((Power)f).exponent();
}
if( base_e.compareTo( base_f ) == 0 ){
OObject exponent = exponent_e.add( exponent_f );
if( exponent instanceof Expression )
exponent = ((Expression)exponent).auto_simplify();
OObject expression = new Power( base_e, exponent ).auto_simplify();
if( expression instanceof Complex ){
complex = complex.multiply( (Complex)expression );
i.remove(); // remove f
i.previous();
i.remove(); // remove e