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

com.enterprisemath.math.fa.RealToRealFunction Maven / Gradle / Ivy

The newest version!
package com.enterprisemath.math.fa;

import com.enterprisemath.math.algebra.Vector;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.enterprisemath.utils.ValidationUtils;

/**
 * Defines real function of real variable.
 *
 * @author radek.hecl
 *
 */
public class RealToRealFunction implements Function {

    /**
     * Builder object.
     */
    public static class Builder {

        /**
         * Source function.
         */
        private VectorToRealFunction source;

        /**
         * Sets source function.
         *
         * @param source source function
         * @return this instance
         */
        public Builder setSource(VectorToRealFunction source) {
            this.source = source;
            return this;
        }

        /**
         * Builds the result object.
         *
         * @return created object
         */
        public RealToRealFunction build() {
            return new RealToRealFunction(this);
        }
    }

    /**
     * Source function.
     */
    private VectorToRealFunction source;

    /**
     * Creates new instance.
     *
     * @param builder builder object
     */
    public RealToRealFunction(Builder builder) {
        source = builder.source;
        guardInvariants();
    }

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardNotNull(source, "source cannot be null");
    }

    @Override
    public Double getValue(Double x) {
        return source.getValue(Vector.create1D(x));
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    /**
     * Creates new instance.
     *
     * @param expression expression
     * @return created function
     */
    public static RealToRealFunction create(String expression) {
        return new RealToRealFunction.Builder().
                setSource(VectorToRealFunction.create(expression)).
                build();
    }

    /**
     * Creates new instance.
     *
     * @param source source function
     * @return created function
     */
    public static RealToRealFunction create(VectorToRealFunction source) {
        return new RealToRealFunction.Builder().
                setSource(source).
                build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy