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

org.nd4j.autodiff.functions.PolynomialTerm Maven / Gradle / Ivy

package org.nd4j.autodiff.functions;

import java.util.List;

import org.nd4j.autodiff.Field;
import org.nd4j.autodiff.samediff.SDGraph;
import org.nd4j.autodiff.samediff.SameDiff;

/**
 *
 * @param 
 */
public class PolynomialTerm> extends AbstractUnaryFunction {

    protected double m_scale;
    protected int m_exponent;

    public PolynomialTerm(SameDiff sameDiff,
                          double i_scale,
                          DifferentialFunction i_v,
                          int i_exponent,
                          boolean inPlace) {
        // scale v^{exponent}
        //note that super handles addEdges
        super(sameDiff,i_v,new Object[]{i_scale,i_exponent,inPlace});
        m_scale = i_scale;
        m_exponent = i_exponent;
    }

    public PolynomialTerm(SameDiff sameDiff,
                          double i_scale,
                          DifferentialFunction i_v,
                          int i_exponent) {
        this(sameDiff,i_scale,i_v,i_exponent,false);
    }

    @Override
    public X doGetValue() {
        return (arg().getValue(true).pow(m_exponent)).mul(m_scale);
    }

    @Override
    public double getReal() {
        return Math.pow(arg().getReal(), m_exponent) * m_scale;
    }

    @Override
    public DifferentialFunction diff(DifferentialFunction i_v) {
        return (new PolynomialTerm<>(sameDiff,m_scale * m_exponent, arg(), m_exponent - 1))
                .mul(arg().diff(i_v));
    }

    @Override
    public String toString() {
        return m_scale + arg().toString() + "^" + m_exponent;
    }

    @Override
    public String doGetFormula(List> variables) {
        return "( " + m_scale + " * Math.pow(" + arg().doGetFormula(variables) + "," + m_exponent
                + ")";
    }

    @Override
    public String functionName() {
        return "pow";
    }

    @Override
    public DifferentialFunction inverse() {
        return new PolynomialTerm<>(sameDiff, m_scale, arg(), -m_exponent);
    }

    @Override
    public DifferentialFunction negate() {
        return new PolynomialTerm<>(sameDiff, -m_scale, arg(), m_exponent);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy