uk.org.retep.math.simplifier.AbstractSimplifyVisitor Maven / Gradle / Ivy
The newest version!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.org.retep.math.simplifier;
import uk.org.retep.math.MathematicsException;
import uk.org.retep.math.model.DyadicOp;
import uk.org.retep.math.model.MathOp;
import uk.org.retep.math.model.MonadicOp;
import uk.org.retep.math.model.MultipleOp;
import uk.org.retep.math.model.OptimizableOp;
import uk.org.retep.math.visitor.AbstractMathVisitor;
import uk.org.retep.math.visitor.FunctionVisitor;
import uk.org.retep.math.visitor.SpecialFunctionVisitor;
import uk.org.retep.math.visitor.TrigonometryVisitor;
/**
* Stubs for the optional visitors - here we simply optimise the children but always return the actual op as-is
* @author peter
*/
public abstract class AbstractSimplifyVisitor
extends AbstractMathVisitor
implements FunctionVisitor,
TrigonometryVisitor,
SpecialFunctionVisitor
{
protected final Object optimiseChildren( final MonadicOp op )
throws MathematicsException
{
if( op instanceof OptimizableOp )
{
return ((OptimizableOp) op).optimize( this );
}
else
{
op.setValue( (MathOp) op.getValue().accept( this ) );
return op;
}
}
protected final Object optimiseChildren( final DyadicOp op )
throws MathematicsException
{
op.setLeft( (MathOp) op.getLeft().accept( this ) );
op.setRight( (MathOp) op.getRight().accept( this ) );
return op;
}
protected final Object optimiseChildren( final MultipleOp op )
throws MathematicsException
{
boolean run = false;
do
{
restart:
for( int i = 0; i < op.size(); i++ )
{
MathOp o = (MathOp) op.getOperation( i ).accept( this );
if( o == null )
{
op.remove( i );
run = !op.isEmpty();
break restart;
}
}
} while( run );
return op;
}
//
@Override
public Object abs( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object cbrt( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object ceil( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object exp( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object expm1( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object floor( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object exponent( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object log( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object log10( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object log1p( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object rint( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object round( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object squareRoot( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
//
//
@Override
public Object acos( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object asin( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object atan( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object atan2( final DyadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object cos( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object cosh( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object sin( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object sinh( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object tan( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
@Override
public Object tanh( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
//
//
@Override
public Object eratosthenesSieve( final MonadicOp op )
throws MathematicsException
{
return optimiseChildren( op );
}
//
}