com.accelad.math.nilgiri.autodiff.DifferentialFunctionFactory Maven / Gradle / Ivy
The newest version!
package com.accelad.math.nilgiri.autodiff;
import com.accelad.math.nilgiri.AbstractFactory;
import com.accelad.math.nilgiri.DoubleReal;
import com.accelad.math.nilgiri.DoubleRealFactory;
import com.accelad.math.nilgiri.Field;
import java.util.List;
public class DifferentialFunctionFactory> {
protected AbstractFactory mFactory;
public DifferentialFunctionFactory(AbstractFactory mFactory) {
if (mFactory != null) {
this.mFactory = mFactory;
} else {
throw new IllegalArgumentException("Input not null value.");
}
}
public Constant val(X iX) {
return new Constant<>(iX, mFactory);
}
public Variable var(String iName, X iX, PreEvaluator preEvaluator) {
return new Variable<>(iName, iX, mFactory, preEvaluator);
}
public Variable var(String iName, X iX) {
return new Variable<>(iName, iX, mFactory);
}
public Zero zero() {
return new Zero<>(mFactory);
}
public One one() {
return new One<>(mFactory);
}
public DifferentialFunction cos(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.cos(arg().getValue());
}
@Override
public double getReal() {
return Math.cos(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return (sin(arg()).mul(arg().diff(i_v))).negate();
}
@Override
public String toString() {
return "cos(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.cos(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction sin(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.sin(arg().getValue());
}
@Override
public double getReal() {
return Math.sin(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return cos(arg()).mul(arg().diff(i_v));
}
@Override
public String toString() {
return "sin(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.sin(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction tan(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.tan(arg().getValue());
}
@Override
public double getReal() {
return Math.tan(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return (new PolynomialTerm<>(1, cos(arg()), -2)).mul(arg().diff(i_v));
}
@Override
public String toString() {
return "tan(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.tan(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction acos(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.acos(arg().getValue());
}
@Override
public double getReal() {
return Math.acos(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return one().div(sqrt(one().minus(arg().pow(2)))).negate();
}
@Override
public String toString() {
return "acos(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.acos(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction asin(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.asin(arg().getValue());
}
@Override
public double getReal() {
return Math.asin(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return one().div(sqrt(one().minus(arg().pow(2))));
}
@Override
public String toString() {
return "asin(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.asin(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction atan(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.atan(arg().getValue());
}
@Override
public double getReal() {
return Math.atan(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return one().div(one().plus(arg().pow(2)));
}
@Override
public String toString() {
return "atan(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.atan(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction cosh(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.cosh(arg().getValue());
}
@Override
public double getReal() {
return Math.cosh(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return sinh(arg());
}
@Override
public String toString() {
return "cosh(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.cosh(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction sinh(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.sinh(arg().getValue());
}
@Override
public double getReal() {
return Math.sinh(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return cosh(arg());
}
@Override
public String toString() {
return "sinh(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.sinh(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction tanh(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.tanh(arg().getValue());
}
@Override
public double getReal() {
return Math.tanh(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return one().div(cosh(arg())).pow(2);
}
@Override
public String toString() {
return "tanh(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.tanh(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction acosh(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.acosh(arg().getValue());
}
@Override
public double getReal() {
return DoubleRealFactory.instance().acosh(new DoubleReal(arg().getReal())).getReal();
}
@Override
public DifferentialFunction diff(Variable i_v) {
return one().div(sqrt(arg().minus(one())).mul(sqrt(arg().plus(one()))));
}
@Override
public String toString() {
return "acosh(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.acosh(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction asinh(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.asinh(arg().getValue());
}
@Override
public double getReal() {
return DoubleRealFactory.instance().asinh(new DoubleReal(arg().getReal())).getReal();
}
@Override
public DifferentialFunction diff(Variable i_v) {
return one().div(sqrt(arg().pow(2).plus(one())));
}
@Override
public String toString() {
return "asinh(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.asinh(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction atanh(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.atanh(arg().getValue());
}
@Override
public double getReal() {
return DoubleRealFactory.instance().atanh(new DoubleReal(arg().getReal())).getReal();
}
@Override
public DifferentialFunction diff(Variable i_v) {
return one().div(one().minus(arg().pow(2)));
}
@Override
public String toString() {
return "atanh(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.atanh(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction exp(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.exp(arg().getValue());
}
@Override
public double getReal() {
return Math.exp(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return exp(arg()).mul(arg().diff(i_v));
}
@Override
public String toString() {
return "exp(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.exp(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction log(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.log(arg().getValue());
}
@Override
public double getReal() {
return Math.log(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return new Inverse<>(arg()).mul(arg().diff(i_v));
}
@Override
public String toString() {
return "log(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.log(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction pow(DifferentialFunction iX, Constant i_y) {
return new AbstractBinaryFunction(iX, i_y) {
@Override
public X getValue() {
return mFactory.pow(larg().getValue(), rarg().getValue());
}
@Override
public double getReal() {
return Math.pow(larg().getReal(), rarg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
Constant ym1 = DifferentialFunctionFactory.this
.val(rarg().getValue().minus(mFactory.one()));
return rarg().mul(DifferentialFunctionFactory.this.pow(larg(), ym1))
.mul(larg().diff(i_v));
}
@Override
public String toString() {
return "pow(" + larg().toString() + ", " + rarg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.pow(" + larg().getFormula(variables) + ","
+ rarg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction sqrt(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.sqrt(arg().getValue());
}
@Override
public double getReal() {
return Math.sqrt(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
return ((sqrt(arg()).inverse())
.div(DifferentialFunctionFactory.this.val(mFactory.one().mul(2L))))
.mul(arg().diff(i_v));
}
@Override
public String toString() {
return "sqrt(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.sqrt(" + arg().getFormula(variables) + ")";
}
};
}
public DifferentialFunction square(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.square(arg().getValue());
}
@Override
public double getReal() {
return Math.pow(arg().getReal(), 2);
}
@Override
public DifferentialFunction diff(Variable i_v) {
return arg().mul(DifferentialFunctionFactory.this.val(mFactory.one().mul(2L)))
.mul(arg().diff(i_v));
}
@Override
public String toString() {
return "square(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.pow(" + arg().getFormula(variables) + ", 2d )";
}
};
}
public DifferentialFunction floor(DifferentialFunction iX) {
return new AbstractUnaryFunction(iX) {
@Override
public X getValue() {
return mFactory.floor(arg().getValue());
}
@Override
public double getReal() {
return Math.floor(arg().getReal());
}
@Override
public DifferentialFunction diff(Variable i_v) {
throw new RuntimeException("not allowed");
}
@Override
public String toString() {
return "floor(" + arg().toString() + ")";
}
@Override
public String getFormula(List> variables) {
return "Math.floor(" + arg().getFormula(variables) + ")";
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy