Java Code Examples for org.apache.commons.math3.analysis.differentiation.DerivativeStructure#add()

The following examples show how to use org.apache.commons.math3.analysis.differentiation.DerivativeStructure#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public DerivativeStructure value(DerivativeStructure[] variables) {
    DerivativeStructure[] y = new DerivativeStructure[factors.getRowDimension()];
    for (int i = 0; i < y.length; ++i) {
        y[i] = variables[0].getField().getZero();
        for (int j = 0; j < factors.getColumnDimension(); ++j) {
            y[i] = y[i].add(variables[j].multiply(factors.getEntry(i, j)));
        }
    }

    DerivativeStructure sum = variables[0].getField().getZero();
    for (int i = 0; i < y.length; ++i) {
        DerivativeStructure ri = y[i].subtract(target[i]);
        sum = sum.add(ri.multiply(ri));
    }
    return sum;
}
 
Example 2
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public DerivativeStructure value(DerivativeStructure[] variables) {
    DerivativeStructure[] y = new DerivativeStructure[factors.getRowDimension()];
    for (int i = 0; i < y.length; ++i) {
        y[i] = variables[0].getField().getZero();
        for (int j = 0; j < factors.getColumnDimension(); ++j) {
            y[i] = y[i].add(variables[j].multiply(factors.getEntry(i, j)));
        }
    }

    DerivativeStructure sum = variables[0].getField().getZero();
    for (int i = 0; i < y.length; ++i) {
        DerivativeStructure ri = y[i].subtract(target[i]);
        sum = sum.add(ri.multiply(ri));
    }
    return sum;
}
 
Example 3
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    DerivativeStructure x3 = variables[2];
    DerivativeStructure tmp1 = variables[0].getField().getZero();
    if (x1.getValue() == 0) {
        tmp1 = tmp1.add((x2.getValue() >= 0) ? 0.25 : -0.25);
    } else {
        tmp1 = x2.divide(x1).atan().divide(twoPi);
        if (x1.getValue() < 0) {
            tmp1 = tmp1.add(0.5);
        }
    }
    DerivativeStructure tmp2 = x1.multiply(x1).add(x2.multiply(x2)).sqrt();
    return new DerivativeStructure[] {
        x3.subtract(tmp1.multiply(10)).multiply(10),
        tmp2.subtract(1).multiply(10),
        x3
    };
}
 
Example 4
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    DerivativeStructure x3 = variables[2];
    DerivativeStructure tmp1 = variables[0].getField().getZero();
    if (x1.getValue() == 0) {
        tmp1 = tmp1.add((x2.getValue() >= 0) ? 0.25 : -0.25);
    } else {
        tmp1 = x2.divide(x1).atan().divide(twoPi);
        if (x1.getValue() < 0) {
            tmp1 = tmp1.add(0.5);
        }
    }
    DerivativeStructure tmp2 = x1.multiply(x1).add(x2.multiply(x2)).sqrt();
    return new DerivativeStructure[] {
        x3.subtract(tmp1.multiply(10)).multiply(10),
        tmp2.subtract(1).multiply(10),
        x3
    };
}
 
Example 5
Source File: CurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public DerivativeStructure[] value(DerivativeStructure[] point) {

    // extract parameters
    final double[] parameters = new double[point.length];
    for (int k = 0; k < point.length; ++k) {
        parameters[k] = point[k].getValue();
    }

    // compute the residuals
    final DerivativeStructure[] values = new DerivativeStructure[observations.size()];
    int i = 0;
    for (WeightedObservedPoint observed : observations) {

        // build the DerivativeStructure by adding first the value as a constant
        // and then adding derivatives
        DerivativeStructure vi = new DerivativeStructure(point.length, 1, f.value(observed.getX(), parameters));
        for (int k = 0; k < point.length; ++k) {
            vi = vi.add(new DerivativeStructure(point.length, 1, k, 0.0));
        }

        values[i++] = vi;

    }

    return values;
}
 
Example 6
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure[] f = new DerivativeStructure[m];
    for (int i = 0; i < (m - 2); ++i) {
        double div = (i + 1) / 29.0;
        DerivativeStructure s1 = variables[0].getField().getZero();
        DerivativeStructure dx = variables[0].getField().getOne();
        for (int j = 1; j < n; ++j) {
            s1 = s1.add(dx.multiply(j).multiply(variables[j]));
            dx = dx.multiply(div);
        }
        DerivativeStructure s2 = variables[0].getField().getZero();
        dx = variables[0].getField().getOne();
        for (int j = 0; j < n; ++j) {
            s2 = s2.add(dx.multiply(variables[j]));
            dx = dx.multiply(div);
        }
        f[i] = s1.subtract(s2.multiply(s2)).subtract(1);
    }

    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    f[m - 2] = x1;
    f[m - 1] = x2.subtract(x1.multiply(x1)).subtract(1);

    return f;

}
 
Example 7
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
  DerivativeStructure sum = variables[0].getField().getZero();
  for (int i = 0; i < n; ++i) {
    sum = sum.add(variables[i]);
  }
  DerivativeStructure t  = sum.multiply(2.0 / m).add(1);
  DerivativeStructure[] f = new DerivativeStructure[m];
  for (int i = 0; i < n; ++i) {
    f[i] = variables[i].subtract(t);
  }
  Arrays.fill(f, n, m, t.negate());
  return f;
}
 
Example 8
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure[] f = new DerivativeStructure[m];
    DerivativeStructure sum = variables[0].getField().getZero();
  for (int i = 1; i < (n - 1); ++i) {
      sum = sum.add(variables[i].multiply(i + 1));
  }
  for (int i = 0; i < (m - 1); ++i) {
    f[i] = sum.multiply(i).subtract(1);
  }
  f[m - 1] = variables[0].getField().getOne().negate();
  return f;
}
 
Example 9
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    DerivativeStructure x3 = variables[2];
    DerivativeStructure x4 = variables[3];
  return new DerivativeStructure[] {
    x1.add(x2.multiply(10)),
    x3.subtract(x4).multiply(sqrt5),
    x2.subtract(x3.multiply(2)).multiply(x2.subtract(x3.multiply(2))),
    x1.subtract(x4).multiply(x1.subtract(x4)).multiply(sqrt10)
  };
}
 
Example 10
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public DerivativeStructure getRadius(DerivativeStructure cx, DerivativeStructure cy) {
    DerivativeStructure r = cx.getField().getZero();
    for (Vector2D point : points) {
        r = r.add(distance(point, cx, cy));
    }
    return r.divide(points.size());
}
 
Example 11
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    DerivativeStructure x3 = variables[2];
    DerivativeStructure x4 = variables[3];
  return new DerivativeStructure[] {
    x1.add(x2.multiply(10)),
    x3.subtract(x4).multiply(sqrt5),
    x2.subtract(x3.multiply(2)).multiply(x2.subtract(x3.multiply(2))),
    x1.subtract(x4).multiply(x1.subtract(x4)).multiply(sqrt10)
  };
}
 
Example 12
Source File: CurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public DerivativeStructure[] value(DerivativeStructure[] point) {

    // extract parameters
    final double[] parameters = new double[point.length];
    for (int k = 0; k < point.length; ++k) {
        parameters[k] = point[k].getValue();
    }

    // compute the residuals
    final DerivativeStructure[] values = new DerivativeStructure[observations.size()];
    int i = 0;
    for (WeightedObservedPoint observed : observations) {

        // build the DerivativeStructure by adding first the value as a constant
        // and then adding derivatives
        DerivativeStructure vi = new DerivativeStructure(point.length, 1, f.value(observed.getX(), parameters));
        for (int k = 0; k < point.length; ++k) {
            vi = vi.add(new DerivativeStructure(point.length, 1, k, 0.0));
        }

        values[i++] = vi;

    }

    return values;
}
 
Example 13
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public DerivativeStructure value(DerivativeStructure[] variables)  {
    DerivativeStructure radius = getRadius(variables[0], variables[1]);

    DerivativeStructure sum = variables[0].getField().getZero();
    for (Vector2D point : points) {
        DerivativeStructure di = distance(point, variables[0], variables[1]).subtract(radius);
        sum = sum.add(di.multiply(di));
    }

    return sum;
}
 
Example 14
Source File: CurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public DerivativeStructure[] value(DerivativeStructure[] point) {

    // extract parameters
    final double[] parameters = new double[point.length];
    for (int k = 0; k < point.length; ++k) {
        parameters[k] = point[k].getValue();
    }

    // compute the residuals
    final DerivativeStructure[] values = new DerivativeStructure[observations.size()];
    int i = 0;
    for (WeightedObservedPoint observed : observations) {

        // build the DerivativeStructure by adding first the value as a constant
        // and then adding derivatives
        DerivativeStructure vi = new DerivativeStructure(point.length, 1, f.value(observed.getX(), parameters));
        for (int k = 0; k < point.length; ++k) {
            vi = vi.add(new DerivativeStructure(point.length, 1, k, 0.0));
        }

        values[i++] = vi;

    }

    return values;
}
 
Example 15
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure[] f = new DerivativeStructure[m];
    DerivativeStructure sum = variables[0].getField().getZero();
  for (int i = 1; i < (n - 1); ++i) {
      sum = sum.add(variables[i].multiply(i + 1));
  }
  for (int i = 0; i < (m - 1); ++i) {
    f[i] = sum.multiply(i).subtract(1);
  }
  f[m - 1] = variables[0].getField().getOne().negate();
  return f;
}
 
Example 16
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure[] f = new DerivativeStructure[m];
    DerivativeStructure sum = variables[0].getField().getZero();
    for (int i = 0; i < n; ++i) {
        sum = sum.add(variables[i].multiply(i + 1));
    }
    for (int i = 0; i < m; ++i) {
        f[i] = sum.multiply(i + 1).subtract(1);
    }
    return f;
}
 
Example 17
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure[] f = new DerivativeStructure[m];
    DerivativeStructure sum = variables[0].getField().getZero();
    for (int i = 0; i < n; ++i) {
        sum = sum.add(variables[i].multiply(i + 1));
    }
    for (int i = 0; i < m; ++i) {
        f[i] = sum.multiply(i + 1).subtract(1);
    }
    return f;
}
 
Example 18
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public DerivativeStructure getRadius(DerivativeStructure cx, DerivativeStructure cy) {
    DerivativeStructure r = cx.getField().getZero();
    for (Vector2D point : points) {
        r = r.add(distance(point, cx, cy));
    }
    return r.divide(points.size());
}
 
Example 19
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public DerivativeStructure value(DerivativeStructure[] variables)  {
    DerivativeStructure radius = getRadius(variables[0], variables[1]);

    DerivativeStructure sum = variables[0].getField().getZero();
    for (Vector2D point : points) {
        DerivativeStructure di = distance(point, variables[0], variables[1]).subtract(radius);
        sum = sum.add(di.multiply(di));
    }

    return sum;
}
 
Example 20
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure[] f = new DerivativeStructure[m];
    DerivativeStructure sum = variables[0].getField().getZero();
    for (int i = 0; i < n; ++i) {
        sum = sum.add(variables[i].multiply(i + 1));
    }
    for (int i = 0; i < m; ++i) {
        f[i] = sum.multiply(i + 1).subtract(1);
    }
    return f;
}