Java Code Examples for org.apache.commons.math3.util.MathArrays#linearCombination()

The following examples show how to use org.apache.commons.math3.util.MathArrays#linearCombination() . 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: DerivativeStructure.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc}
 * @exception DimensionMismatchException if number of free parameters
 * or orders do not match
 * @since 3.2
 */
public DerivativeStructure linearCombination(final double[] a, final DerivativeStructure[] b)
    throws DimensionMismatchException {

    // compute an accurate value, taking care of cancellations
    final double[] bDouble = new double[b.length];
    for (int i = 0; i < b.length; ++i) {
        bDouble[i] = b[i].getValue();
    }
    final double accurateValue = MathArrays.linearCombination(a, bDouble);

    // compute a simple value, with all partial derivatives
    DerivativeStructure simpleValue = b[0].getField().getZero();
    for (int i = 0; i < a.length; ++i) {
        simpleValue = simpleValue.add(b[i].multiply(a[i]));
    }

    // create a result with accurate value and all derivatives (not necessarily as accurate as the value)
    final double[] all = simpleValue.getAllDerivatives();
    all[0] = accurateValue;
    return new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all);

}
 
Example 2
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc}
 * @exception DimensionMismatchException if number of free parameters
 * or orders do not match
 * @since 3.2
 */
public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1,
                                             final double a2, final DerivativeStructure b2)
    throws DimensionMismatchException {

    // compute an accurate value, taking care of cancellations
    final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(),
                                                              a2, b2.getValue());

    // compute a simple value, with all partial derivatives
    final DerivativeStructure simpleValue = b1.multiply(a1).add(b2.multiply(a2));

    // create a result with accurate value and all derivatives (not necessarily as accurate as the value)
    final double[] all = simpleValue.getAllDerivatives();
    all[0] = accurateValue;
    return new DerivativeStructure(getFreeParameters(), getOrder(), all);

}
 
Example 3
Source File: SparseGradient.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public SparseGradient linearCombination(final double a1, final SparseGradient b1,
                                          final double a2, final SparseGradient b2,
                                          final double a3, final SparseGradient b3) {

    // compute a simple value, with all partial derivatives
    SparseGradient out = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3));

    // recompute an accurate value, taking care of cancellations
    out.value = MathArrays.linearCombination(a1, b1.value,
                                             a2, b2.value,
                                             a3, b3.value);

    return out;

}
 
Example 4
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc}
 * @exception DimensionMismatchException if number of free parameters
 * or orders do not match
 * @since 3.2
 */
public DerivativeStructure linearCombination(final double[] a, final DerivativeStructure[] b)
    throws DimensionMismatchException {

    // compute an accurate value, taking care of cancellations
    final double[] bDouble = new double[b.length];
    for (int i = 0; i < b.length; ++i) {
        bDouble[i] = b[i].getValue();
    }
    final double accurateValue = MathArrays.linearCombination(a, bDouble);

    // compute a simple value, with all partial derivatives
    DerivativeStructure simpleValue = b[0].getField().getZero();
    for (int i = 0; i < a.length; ++i) {
        simpleValue = simpleValue.add(b[i].multiply(a[i]));
    }

    // create a result with accurate value and all derivatives (not necessarily as accurate as the value)
    final double[] all = simpleValue.getAllDerivatives();
    all[0] = accurateValue;
    return new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all);

}
 
Example 5
Source File: SparseGradient.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public SparseGradient linearCombination(final double a1, final SparseGradient b1,
                                          final double a2, final SparseGradient b2,
                                          final double a3, final SparseGradient b3,
                                          final double a4, final SparseGradient b4) {

    // compute a simple value, with all partial derivatives
    SparseGradient out = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)).add(b4.multiply(a4));

    // recompute an accurate value, taking care of cancellations
    out.value = MathArrays.linearCombination(a1, b1.value,
                                             a2, b2.value,
                                             a3, b3.value,
                                             a4, b4.value);

    return out;

}
 
Example 6
Source File: DefaultVolumeMesh.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private double signedVolumeOfTriangle(//
	final double p0x, final double p0y, final double p0z, //
	final double p1x, final double p1y, final double p1z, //
	final double p2x, final double p2y, final double p2z)
{
	// cross product
	final double cpx = MathArrays.linearCombination(p1y, p2z, -p1z, p2y);
	final double cpy = MathArrays.linearCombination(p1z, p2x, -p1x, p2z);
	final double cpz = MathArrays.linearCombination(p1x, p2y, -p1y, p2x);

	// dot product
	return MathArrays.linearCombination(p0x, cpx, p0y, cpy, p0z, cpz) / 6.0f;
}
 
Example 7
Source File: ConvexHullGenerator2DAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected final boolean isConvex(final ConvexHull2D hull, final boolean includesCollinearPoints,
                                 final double tolerance) {

    final Vector2D[] points = hull.getVertices();
    int sign = 0;

    for (int i = 0; i < points.length; i++) {
        Vector2D p1 = points[i == 0 ? points.length - 1 : i - 1];
        Vector2D p2 = points[i];
        Vector2D p3 = points[i == points.length - 1 ? 0 : i + 1];

        Vector2D d1 = p2.subtract(p1);
        Vector2D d2 = p3.subtract(p2);

        Assert.assertTrue(d1.getNorm() > 1e-10);
        Assert.assertTrue(d2.getNorm() > 1e-10);

        final double cross = MathArrays.linearCombination(d1.getX(), d2.getY(), -d1.getY(), d2.getX());
        final int cmp = Precision.compareTo(cross, 0.0, tolerance);

        if (sign != 0 && cmp != sign) {
            if (includesCollinearPoints && cmp == 0) {
                // in case of collinear points the cross product will be zero
            } else {
                return false;
            }
        }
        
        sign = cmp;
    }
    
    return true;
}
 
Example 8
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc}
 * @exception DimensionMismatchException if number of free parameters
 * or orders do not match
 */
public DerivativeStructure linearCombination(final DerivativeStructure[] a, final DerivativeStructure[] b)
    throws DimensionMismatchException {

    // compute an accurate value, taking care of cancellations
    final double[] aDouble = new double[a.length];
    for (int i = 0; i < a.length; ++i) {
        aDouble[i] = a[i].getValue();
    }
    final double[] bDouble = new double[b.length];
    for (int i = 0; i < b.length; ++i) {
        bDouble[i] = b[i].getValue();
    }
    final double accurateValue = MathArrays.linearCombination(aDouble, bDouble);

    // compute a simple value, with all partial derivatives
    DerivativeStructure simpleValue = a[0].getField().getZero();
    for (int i = 0; i < a.length; ++i) {
        simpleValue = simpleValue.add(a[i].multiply(b[i]));
    }

    // create a result with accurate value and all derivatives (not necessarily as accurate as the value)
    final double[] all = simpleValue.getAllDerivatives();
    all[0] = accurateValue;
    return new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all);

}
 
Example 9
Source File: Line.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Get one point from the plane.
 * @param abscissa desired abscissa for the point
 * @param offset desired offset for the point
 * @return one point in the plane, with given abscissa and offset
 * relative to the line
 */
public Vector2D getPointAt(final Vector1D abscissa, final double offset) {
    final double x       = abscissa.getX();
    final double dOffset = offset - originOffset;
    return new Vector2D(MathArrays.linearCombination(x, cos,  dOffset, sin),
                        MathArrays.linearCombination(x, sin, -dOffset, cos));
}
 
Example 10
Source File: Line.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Get one point from the plane.
 * @param abscissa desired abscissa for the point
 * @param offset desired offset for the point
 * @return one point in the plane, with given abscissa and offset
 * relative to the line
 */
public Vector2D getPointAt(final Vector1D abscissa, final double offset) {
    final double x       = abscissa.getX();
    final double dOffset = offset - originOffset;
    return new Vector2D(MathArrays.linearCombination(x, cos,  dOffset, sin),
                        MathArrays.linearCombination(x, sin, -dOffset, cos));
}
 
Example 11
Source File: Line.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public boolean sameOrientationAs(final Hyperplane<Euclidean2D> other) {
    final Line otherL = (Line) other;
    return MathArrays.linearCombination(sin, otherL.sin, cos, otherL.cos) >= 0.0;
}
 
Example 12
Source File: Vector3D.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/** Linear constructor
 * Build a vector from four other ones and corresponding scale factors.
 * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4
 * @param a1 first scale factor
 * @param u1 first base (unscaled) vector
 * @param a2 second scale factor
 * @param u2 second base (unscaled) vector
 * @param a3 third scale factor
 * @param u3 third base (unscaled) vector
 * @param a4 fourth scale factor
 * @param u4 fourth base (unscaled) vector
 */
public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2,
                double a3, Vector3D u3, double a4, Vector3D u4) {
    this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x, a3, u3.x, a4, u4.x);
    this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y, a3, u3.y, a4, u4.y);
    this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z, a3, u3.z, a4, u4.z);
}
 
Example 13
Source File: Cardumen_0040_s.java    From coming with MIT License 3 votes vote down vote up
/** Linear constructor
 * Build a vector from three other ones and corresponding scale factors.
 * The vector built will be a1 * u1 + a2 * u2 + a3 * u3
 * @param a1 first scale factor
 * @param u1 first base (unscaled) vector
 * @param a2 second scale factor
 * @param u2 second base (unscaled) vector
 * @param a3 third scale factor
 * @param u3 third base (unscaled) vector
 */
public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2,
                double a3, Vector3D u3) {
    this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x, a3, u3.x);
    this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y, a3, u3.y);
    this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z, a3, u3.z);
}
 
Example 14
Source File: Vector3D.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/** Linear constructor
 * Build a vector from four other ones and corresponding scale factors.
 * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4
 * @param a1 first scale factor
 * @param u1 first base (unscaled) vector
 * @param a2 second scale factor
 * @param u2 second base (unscaled) vector
 * @param a3 third scale factor
 * @param u3 third base (unscaled) vector
 * @param a4 fourth scale factor
 * @param u4 fourth base (unscaled) vector
 */
public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2,
                double a3, Vector3D u3, double a4, Vector3D u4) {
    this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x, a3, u3.x, a4, u4.x);
    this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y, a3, u3.y, a4, u4.y);
    this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z, a3, u3.z, a4, u4.z);
}
 
Example 15
Source File: Cardumen_00110_t.java    From coming with MIT License 3 votes vote down vote up
/** Linear constructor
 * Build a vector from three other ones and corresponding scale factors.
 * The vector built will be a1 * u1 + a2 * u2 + a3 * u3
 * @param a1 first scale factor
 * @param u1 first base (unscaled) vector
 * @param a2 second scale factor
 * @param u2 second base (unscaled) vector
 * @param a3 third scale factor
 * @param u3 third base (unscaled) vector
 */
public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2,
                double a3, Vector3D u3) {
    this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x, a3, u3.x);
    this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y, a3, u3.y);
    this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z, a3, u3.z);
}
 
Example 16
Source File: Math_10_DSCompiler_t.java    From coming with MIT License 3 votes vote down vote up
/** Compute linear combination.
 * The derivative structure built will be a1 * ds1 + a2 * ds2 + a3 * ds3 + a4 * ds4
 * @param a1 first scale factor
 * @param c1 first base (unscaled) component
 * @param offset1 offset of first operand in its array
 * @param a2 second scale factor
 * @param c2 second base (unscaled) component
 * @param offset2 offset of second operand in its array
 * @param a3 third scale factor
 * @param c3 third base (unscaled) component
 * @param offset3 offset of third operand in its array
 * @param result array where result must be stored (it may be
 * one of the input arrays)
 * @param resultOffset offset of the result in its array
 */
public void linearCombination(final double a1, final double[] c1, final int offset1,
                              final double a2, final double[] c2, final int offset2,
                              final double a3, final double[] c3, final int offset3,
                              final double[] result, final int resultOffset) {
    for (int i = 0; i < getSize(); ++i) {
        result[resultOffset + i] =
                MathArrays.linearCombination(a1, c1[offset1 + i],
                                             a2, c2[offset2 + i],
                                             a3, c3[offset3 + i]);
    }
}
 
Example 17
Source File: Vector3D.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/** Linear constructor
 * Build a vector from three other ones and corresponding scale factors.
 * The vector built will be a1 * u1 + a2 * u2 + a3 * u3
 * @param a1 first scale factor
 * @param u1 first base (unscaled) vector
 * @param a2 second scale factor
 * @param u2 second base (unscaled) vector
 * @param a3 third scale factor
 * @param u3 third base (unscaled) vector
 */
public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2,
                double a3, Vector3D u3) {
    this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x, a3, u3.x);
    this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y, a3, u3.y);
    this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z, a3, u3.z);
}
 
Example 18
Source File: 1_Vector3D.java    From SimFix with GNU General Public License v2.0 3 votes vote down vote up
/** Linear constructor
 * Build a vector from four other ones and corresponding scale factors.
 * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4
 * @param a1 first scale factor
 * @param u1 first base (unscaled) vector
 * @param a2 second scale factor
 * @param u2 second base (unscaled) vector
 * @param a3 third scale factor
 * @param u3 third base (unscaled) vector
 * @param a4 fourth scale factor
 * @param u4 fourth base (unscaled) vector
 */
public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2,
                double a3, Vector3D u3, double a4, Vector3D u4) {
    this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x, a3, u3.x, a4, u4.x);
    this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y, a3, u3.y, a4, u4.y);
    this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z, a3, u3.z, a4, u4.z);
}
 
Example 19
Source File: JGenProg2017_0061_t.java    From coming with MIT License 2 votes vote down vote up
/** Linear constructor
 * Build a vector from two other ones and corresponding scale factors.
 * The vector built will be a1 * u1 + a2 * u2
 * @param a1 first scale factor
 * @param u1 first base (unscaled) vector
 * @param a2 second scale factor
 * @param u2 second base (unscaled) vector
 */
public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2) {
    this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x);
    this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y);
    this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z);
}
 
Example 20
Source File: Vector3D.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/** Linear constructor
 * Build a vector from two other ones and corresponding scale factors.
 * The vector built will be a1 * u1 + a2 * u2
 * @param a1 first scale factor
 * @param u1 first base (unscaled) vector
 * @param a2 second scale factor
 * @param u2 second base (unscaled) vector
 */
public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2) {
    this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x);
    this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y);
    this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z);
}