package umontreal.iro.lecuyer.functions;



/**
 * Represents a function computing
 * 
 * (af (x) + b)2 for a user-defined function
 * f (x).
 * 
 */
public class SquareMathFunction implements MathFunctionWithFirstDerivative  {
   private MathFunction func;
   private double a, b;


   /**
    * Constructs a new square function
    *  for function func.
    *  The values of the constants are
    *  a = 1 and b = 0.
    * 
    * @param func the function f (x).
    * 
    * 
    */
   public SquareMathFunction (MathFunction func) {
      this (func, 1, 0);
   }


   /**
    * Constructs a new power function
    *  for function func, and constants
    *  a and b.
    * 
    * @param func the function f (x).
    * 
    *    @param a <
    * #30#>the multiplicative constant.
    *    @param b the additive constant.
    * 
    * 
    */
   public SquareMathFunction (MathFunction func, double a, double b) {
      if (func == null)
         throw new NullPointerException();
      this.func = func;
      this.a = a;
      this.b = b;
   }


   /**
    * Returns the function f (x).
    * 
    * @return the function.
    * 
    */
   public MathFunction getFunction() {
      return func;
   }


   /**
    * Returns the value of a.
    * 
    * @return the value of a.
    * 
    */
   public double getA() {
      return a;
   }


   /**
    * Returns the value of b.
    * 
    * @return the value of b.
    * 
    */
   public double getB() {
      return b;
   }


   public double evaluate (double x) {
      final double v = a*func.evaluate (x) + b;
      return v*v;
   }

   public double derivative (double x) {
      final double fder = MathFunctionUtil.derivative (func, x);
      return 2*a*(a*func.evaluate (x) + b)*fder;
   }
}

 

Ads help maintain this website.