All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.org.retep.math.builder.BasicFunctionBuilder 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.builder;

import uk.org.retep.math.model.Nop;
import uk.org.retep.math.model.NumberOp;
import uk.org.retep.annotations.NoInstance;
import uk.org.retep.math.MathematicsException;
import uk.org.retep.math.model.MathOp;
import uk.org.retep.math.model.MonadicFunctionOp;
import uk.org.retep.math.model.MonadicOp;
import uk.org.retep.math.model.OptimizableOp;
import uk.org.retep.math.visitor.ArithmeticVisitor;
import uk.org.retep.math.visitor.FunctionVisitor;
import uk.org.retep.util.math.MathUtils;

import static uk.org.retep.math.model.OpUtils.*;

/**
 *
 * @author peter
 */
@NoInstance
public class BasicFunctionBuilder
{

    private BasicFunctionBuilder()
    {
    }

    public static MonadicBuilder abs( MathBuilder builder )
    {
        return abs().add( builder );
    }

    public static MonadicBuilder abs()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op2()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.abs( this );
                    }

                    @Override
                    protected Number optimize( final Number value )
                            throws MathematicsException
                    {
                        return MathUtils.abs( value );
                    }
                };
            }
        };
    }

    public static MonadicBuilder cubeRoot( MathBuilder builder )
    {
        return cubeRoot().add( builder );
    }

    public static MonadicBuilder cubeRoot()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.cbrt( this );
                    }
                };
            }
        };
    }

    public static MonadicBuilder ceil( MathBuilder builder )
    {
        return ceil().add( builder );
    }

    public static MonadicBuilder ceil()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op2()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.ceil( this );
                    }

                    @Override
                    protected Number optimize( final Number value )
                            throws MathematicsException
                    {
                        return MathUtils.ceil( value );
                    }
                };
            }
        };
    }

    public static MonadicBuilder exp( MathBuilder builder )
    {
        return exp().add( builder );
    }

    public static MonadicBuilder exp()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.exp( this );
                    }
                };
            }
        };
    }

    public static MonadicBuilder expm1( MathBuilder builder )
    {
        return expm1().add( builder );
    }

    public static MonadicBuilder expm1()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.expm1( this );
                    }
                };
            }
        };
    }

    public static MonadicBuilder floor( MathBuilder builder )
    {
        return floor().add( builder );
    }

    public static MonadicBuilder floor()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op2()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.floor( this );
                    }

                    @Override
                    protected Number optimize( final Number value )
                            throws MathematicsException
                    {
                        return MathUtils.floor( value );
                    }
                };
            }
        };
    }

    public static MonadicBuilder exponent( MathBuilder builder )
    {
        return exponent().add( builder );
    }

    public static MonadicBuilder exponent()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.exponent( this );
                    }
                };
            }
        };
    }

    public static MonadicBuilder log( MathBuilder builder )
    {
        return log().add( builder );
    }

    public static MonadicBuilder log()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.log( this );
                    }
                };
            }
        };
    }

    public static MonadicBuilder log10( MathBuilder builder )
    {
        return log10().add( builder );
    }

    public static MonadicBuilder log10()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.log10( this );
                    }
                };
            }
        };
    }

    public static MonadicBuilder log1p( MathBuilder builder )
    {
        return log1p().add( builder );
    }

    public static MonadicBuilder log1p()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.log1p( this );
                    }
                };
            }
        };
    }

    public static MonadicBuilder rint( MathBuilder builder )
    {
        return rint().add( builder );
    }

    public static MonadicBuilder rint()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.rint( this );
                    }
                };
            }
        };
    }

    public static MonadicBuilder round( MathBuilder builder )
    {
        return round().add( builder );
    }

    public static MonadicBuilder round()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op2()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.round( this );
                    }

                    @Override
                    protected Number optimize( final Number value )
                            throws MathematicsException
                    {
                        return MathUtils.integer( value );
                    }
                };
            }
        };
    }

    public static MonadicBuilder sqrt( MathBuilder builder )
    {
        return sqrt().add( builder );
    }

    public static MonadicBuilder sqrt()
    {
        return new MonadicBuilder()
        {

            @Override
            protected MonadicOp createOp()
            {
                return new Op()
                {

                    @Override
                    protected Object acceptImpl( final FunctionVisitor visitor )
                            throws MathematicsException
                    {
                        return visitor.squareRoot( this );
                    }
                };
            }
        };
    }

    private static abstract class Op
            extends MonadicFunctionOp
    {

        @Override
        protected Class getVisitor()
        {
            return FunctionVisitor.class;
        }
    }

    private static abstract class Op2
            extends Op
            implements OptimizableOp
    {

        @Override
        public MathOp optimize( final ArithmeticVisitor visitor )
                throws MathematicsException
        {
            final MathOp child = (MathOp) getValue().accept( visitor );

            if( isNumber( child ) )
            {
                final Number v = optimize( ((NumberOp) child).getValue() );
                if( v == null )
                {
                    return Nop.getInstance();
                }
                else
                {
                    return NumberOp.create( v );
                }
            }

            setValue( child );
            return this;
        }

        protected abstract Number optimize( Number value )
                throws MathematicsException;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy