Java Code Examples for org.apache.commons.math3.linear.RealVector#getNorm()

The following examples show how to use org.apache.commons.math3.linear.RealVector#getNorm() . 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: L2NormSumVectorComposer.java    From Indra with MIT License 5 votes vote down vote up
@Override
public RealVector compose(List<RealVector> vectors) {
    RealVector sum = super.compose(vectors);
    if (vectors.size() > 1) {
        //norm is not calculated for single vectors.
        double norm = sum.getNorm();
        return sum.mapDivideToSelf(norm);
    }

    return sum;
}
 
Example 2
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
/**
 * Find the first singular vector/value of a matrix A based on the Power method.
 * 
 * @see http://www.cs.yale.edu/homes/el327/datamining2013aFiles/07_singular_value_decomposition.pdf
 * @param A target matrix
 * @param x0 initial vector
 * @param nIter number of iterations for the Power method
 * @param u 1st left singular vector
 * @param v 1st right singular vector
 * @return 1st singular value
 */
public static double power1(@Nonnull final RealMatrix A, @Nonnull final double[] x0,
        final int nIter, @Nonnull final double[] u, @Nonnull final double[] v) {
    Preconditions.checkArgument(A.getColumnDimension() == x0.length,
        "Column size of A and length of x should be same");
    Preconditions.checkArgument(A.getRowDimension() == u.length,
        "Row size of A and length of u should be same");
    Preconditions.checkArgument(x0.length == v.length, "Length of x and u should be same");
    Preconditions.checkArgument(nIter >= 1, "Invalid number of iterations: " + nIter);

    RealMatrix AtA = A.transpose().multiply(A);

    RealVector x = new ArrayRealVector(x0);
    for (int i = 0; i < nIter; i++) {
        x = AtA.operate(x);
    }

    double xNorm = x.getNorm();
    for (int i = 0, n = v.length; i < n; i++) {
        v[i] = x.getEntry(i) / xNorm;
    }

    RealVector Av = new ArrayRealVector(A.operate(v));
    double s = Av.getNorm();

    for (int i = 0, n = u.length; i < n; i++) {
        u[i] = Av.getEntry(i) / s;
    }

    return s;
}
 
Example 3
Source File: L2Regularizer.java    From samantha with MIT License 5 votes vote down vote up
public double getObjective(double l2coef, List<RealVector> vars) {
    double objVal = 0.0;
    for (RealVector realVector : vars) {
        double l2norm = realVector.getNorm();
        objVal += l2norm * l2norm;
    }
    return objVal * l2coef;
}
 
Example 4
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
/**
 * Lanczos tridiagonalization for a symmetric matrix C to make s * s tridiagonal matrix T.
 *
 * @see http://www.cas.mcmaster.ca/~qiao/publications/spie05.pdf
 * @param C target symmetric matrix
 * @param a initial vector
 * @param T result is stored here
 */
public static void lanczosTridiagonalization(@Nonnull final RealMatrix C,
        @Nonnull final double[] a, @Nonnull final RealMatrix T) {
    Preconditions.checkArgument(Arrays.deepEquals(C.getData(), C.transpose().getData()),
        "Target matrix C must be a symmetric matrix");
    Preconditions.checkArgument(C.getColumnDimension() == a.length,
        "Column size of A and length of a should be same");
    Preconditions.checkArgument(T.getRowDimension() == T.getColumnDimension(),
        "T must be a square matrix");

    int s = T.getRowDimension();

    // initialize T with zeros
    T.setSubMatrix(new double[s][s], 0, 0);

    RealVector a0 = new ArrayRealVector(a.length);
    RealVector r = new ArrayRealVector(a);

    double beta0 = 1.d;

    for (int i = 0; i < s; i++) {
        RealVector a1 = r.mapDivide(beta0);
        RealVector Ca1 = C.operate(a1);

        double alpha1 = a1.dotProduct(Ca1);

        r = Ca1.add(a1.mapMultiply(-1.d * alpha1)).add(a0.mapMultiply(-1.d * beta0));

        double beta1 = r.getNorm();

        T.setEntry(i, i, alpha1);
        if (i - 1 >= 0) {
            T.setEntry(i, i - 1, beta0);
        }
        if (i + 1 < s) {
            T.setEntry(i, i + 1, beta1);
        }

        a0 = a1.copy();
        beta0 = beta1;
    }
}
 
Example 5
Source File: L2Regularizer.java    From samantha with MIT License 4 votes vote down vote up
public double getObjective(double l2coef, RealVector var) {
    double l2norm = var.getNorm();
    return l2coef * l2norm * l2norm;
}
 
Example 6
Source File: MicrosphereInterpolatingFunction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compute the cosine of the angle between 2 vectors.
 *
 * @param v Vector.
 * @param w Vector.
 * @return the cosine of the angle between {@code v} and {@code w}.
 */
private double cosAngle(final RealVector v, final RealVector w) {
    return v.dotProduct(w) / (v.getNorm() * w.getNorm());
}
 
Example 7
Source File: MicrosphereInterpolatingFunction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compute the cosine of the angle between 2 vectors.
 *
 * @param v Vector.
 * @param w Vector.
 * @return the cosine of the angle between {@code v} and {@code w}.
 */
private double cosAngle(final RealVector v, final RealVector w) {
    return v.dotProduct(w) / (v.getNorm() * w.getNorm());
}
 
Example 8
Source File: MicrosphereInterpolatingFunction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compute the cosine of the angle between 2 vectors.
 *
 * @param v Vector.
 * @param w Vector.
 * @return the cosine of the angle between {@code v} and {@code w}.
 */
private double cosAngle(final RealVector v, final RealVector w) {
    return v.dotProduct(w) / (v.getNorm() * w.getNorm());
}
 
Example 9
Source File: MicrosphereInterpolatingFunction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compute the cosine of the angle between 2 vectors.
 *
 * @param v Vector.
 * @param w Vector.
 * @return the cosine of the angle between {@code v} and {@code w}.
 */
private double cosAngle(final RealVector v, final RealVector w) {
    return v.dotProduct(w) / (v.getNorm() * w.getNorm());
}
 
Example 10
Source File: MicrosphereInterpolatingFunction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compute the cosine of the angle between 2 vectors.
 *
 * @param v Vector.
 * @param w Vector.
 * @return the cosine of the angle between {@code v} and {@code w}.
 */
private double cosAngle(final RealVector v, final RealVector w) {
    return v.dotProduct(w) / (v.getNorm() * w.getNorm());
}
 
Example 11
Source File: MicrosphereInterpolatingFunction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compute the cosine of the angle between 2 vectors.
 *
 * @param v Vector.
 * @param w Vector.
 * @return the cosine of the angle between {@code v} and {@code w}.
 */
private double cosAngle(final RealVector v, final RealVector w) {
    return v.dotProduct(w) / (v.getNorm() * w.getNorm());
}
 
Example 12
Source File: MicrosphereInterpolatingFunction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compute the cosine of the angle between 2 vectors.
 *
 * @param v Vector.
 * @param w Vector.
 * @return the cosine of the angle between {@code v} and {@code w}.
 */
private double cosAngle(final RealVector v, final RealVector w) {
    return v.dotProduct(w) / (v.getNorm() * w.getNorm());
}
 
Example 13
Source File: MicrosphereInterpolatingFunction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compute the cosine of the angle between 2 vectors.
 *
 * @param v Vector.
 * @param w Vector.
 * @return the cosine of the angle between {@code v} and {@code w}.
 */
private double cosAngle(final RealVector v, final RealVector w) {
    return v.dotProduct(w) / (v.getNorm() * w.getNorm());
}