Example usage for org.apache.commons.math3.linear RealVector getNorm

List of usage examples for org.apache.commons.math3.linear RealVector getNorm

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealVector getNorm.

Prototype

public double getNorm() 

Source Link

Document

Returns the L2 norm of the vector.

Usage

From source file:hivemall.utils.math.MatrixUtils.java

@Nonnull
static RealVector unitL2norm(@Nonnull final RealVector x) {
    double x0 = x.getEntry(0);
    double sign = MathUtils.sign(x0);
    x.setEntry(0, x0 + sign * x.getNorm());
    return x.unitVector();
}

From source file:hivemall.utils.math.MatrixUtils.java

/**
 * Find the first singular vector/value of a matrix A based on the Power method.
 *
 * @see http//from  w  w  w .  j  a  v  a 2  s.c om
 *      ://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;
}

From source file:hivemall.utils.math.MatrixUtils.java

/**
 * 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/*from   w  w  w.jav a  2 s . co  m*/
 * @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;
    }
}

From source file:com.mothsoft.alexis.engine.textual.DocumentFeatures.java

public double cosine(final RealVector v1, final RealVector v2) {
    final double norm = v1.getNorm();
    final double norm2 = v2.getNorm();
    final double divisor = norm * norm2;

    if (divisor == 0.0) {
        return 0;
    } else {//from  w  w  w .  j  a v  a 2s .co  m
        return v1.dotProduct(v2) / (divisor);
    }
}

From source file:edu.oregonstate.eecs.mcplan.ml.SequentialProjectionHashLearner.java

@Override
public void run() {
    final RealMatrix cov_reg = MatrixUtils.createRealIdentityMatrix(X.getRowDimension())
            .scalarMultiply(shrinkage);/*from  w  ww .j a  v a2 s  . co  m*/
    for (int k = 0; k < K; ++k) {
        System.out.println("k = " + k);
        System.out.println("\tCovariance");
        final RealMatrix cov = Xi_.multiply(Xi_.transpose()).add(cov_reg);
        //         System.out.println( cov );
        System.out.println("\tM");
        final RealMatrix M = cov; // XL.multiply( Si_ ).multiply( XLt ).add( cov.scalarMultiply( eta ) );
        // TODO: You only need the largest eigenvalue, so the full
        // decomposition is a ton of unnecessary work.
        System.out.println("\tM[" + M.getRowDimension() + "x" + M.getColumnDimension() + "]");
        final EigenDecomposition evd = new EigenDecomposition(M);
        final RealVector w = evd.getEigenvector(0);
        w.mapMultiplyToSelf(1.0 / w.getNorm());
        //         if( Math.abs( w.getNorm() - 1.0 ) > 1e-2 ) {
        //            System.out.println( "! Non-unit eigenvector: ||w|| = " + w.getNorm() );
        //            System.out.println( Math.abs( w.getNorm() - 1.0 ) );
        //            assert( false );
        //         }
        W.add(w);
        final RealMatrix w_wt = w.outerProduct(w);
        final RealMatrix S_tilde = XLt.multiply(w_wt).multiply(XL);
        T(S_tilde, Si_);
        Si_ = Si_.subtract(S_tilde.scalarMultiply(alpha));
        Xi_ = Xi_.subtract(w_wt.multiply(Xi_));
    }
}

From source file:com.opengamma.strata.math.impl.matrix.CommonsMatrixAlgebra.java

@Override
public double getNorm2(Matrix m) {
    ArgChecker.notNull(m, "m");
    if (m instanceof DoubleArray) {
        RealVector temp = CommonsMathWrapper.wrap((DoubleArray) m);
        return temp.getNorm();
    } else if (m instanceof DoubleMatrix) {
        RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m);
        SingularValueDecomposition svd = new SingularValueDecomposition(temp);
        return svd.getNorm();
    }//from  w  w  w. j a  va  2  s .c  o  m
    throw new IllegalArgumentException("Can only find norm2 of DoubleMatrix; have " + m.getClass());
}

From source file:ir.project.TFIDFBookVector.java

public double cosineSimilarity(TFIDFBookVector other) {
    RealVector otherVector = other.getVector();

    // get dot product
    double dotProduct = this.vector.dotProduct(otherVector);

    // get Euclidian norms
    double norm1 = this.vector.getNorm();
    double norm2 = otherVector.getNorm();

    // calculate cosine similarity
    double sim = dotProduct / (norm1 * norm2);

    return sim;//from  w w  w.jav a2  s.  c  om

}

From source file:com.cloudera.oryx.kmeans.computation.cluster.KSketchIndex.java

public void add(RealVector vec, int centerId) {
    points.get(centerId).add(vec);//from  w  w  w  .  j  a  v  a 2 s  .c om
    double length = vec.getNorm();
    lengthSquared.get(centerId).add(length * length);
    pointsPerFold[centerId]++;
    updated = true;
}

From source file:com.joptimizer.util.MPSParserNetlibTest.java

/**
 * Tests the parsing of a netlib problem.
 */// w  ww. j  a  v a  2  s. c  o m
public void xxxtestSingleNetlib() throws Exception {
    log.debug("testSingleNetlib");
    //String problemName = "afiro";
    //String problemName = "afiroPresolved";
    //String problemName = "adlittle";
    //String problemName = "kb2";
    //String problemName = "sc50a";
    //String problemName = "sc50b";
    //String problemName = "blend";
    //String problemName = "scorpion";
    //String problemName = "recipe";
    //String problemName = "recipePresolved";
    //String problemName = "sctap1";
    //String problemName = "fit1d";
    //String problemName = "israel";
    //String problemName = "grow15";
    //String problemName = "etamacro";
    //String problemName = "pilot";
    //String problemName = "pilot4";
    //String problemName = "osa-14";
    //String problemName = "brandyPresolved";
    String problemName = "maros";

    File f = Utils.getClasspathResourceAsFile("lp" + File.separator + "netlib" + File.separator + problemName
            + File.separator + problemName + ".mps");
    MPSParser mpsParser = new MPSParser();
    mpsParser.parse(f);

    Properties expectedSolProps = null;
    try {
        //this is the solution of the mps problem given by Mathematica
        expectedSolProps = load(Utils.getClasspathResourceAsFile(
                "lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "sol.txt"));
    } catch (Exception e) {
    }

    log.debug("name: " + mpsParser.getName());
    log.debug("n   : " + mpsParser.getN());
    log.debug("meq : " + mpsParser.getMeq());
    log.debug("mieq: " + mpsParser.getMieq());
    log.debug("meq+mieq: " + (mpsParser.getMeq() + mpsParser.getMieq()));
    List<String> variablesNames = mpsParser.getVariablesNames();
    log.debug("x: " + ArrayUtils.toString(variablesNames));
    //      log.debug("c: " + ArrayUtils.toString(p.getC()));
    //      log.debug("G: " + ArrayUtils.toString(p.getG()));
    //      log.debug("h: " + ArrayUtils.toString(p.getH()));
    //      log.debug("A: " + ArrayUtils.toString(p.getA()));
    //      log.debug("b: " + ArrayUtils.toString(p.getB()));
    //      log.debug("lb:" + ArrayUtils.toString(p.getLb()));
    //      log.debug("ub:" + ArrayUtils.toString(p.getUb()));

    //check consistency: if the problem was correctly parsed, the expectedSol must be its solution
    double delta = 1.e-7;
    if (expectedSolProps != null) {
        //key = variable name
        //value = sol value
        assertEquals(expectedSolProps.size(), variablesNames.size());
        RealVector expectedSol = new ArrayRealVector(variablesNames.size());
        for (int i = 0; i < variablesNames.size(); i++) {
            expectedSol.setEntry(i, Double.parseDouble(expectedSolProps.getProperty(variablesNames.get(i))));
        }
        log.debug("expectedSol: " + ArrayUtils.toString(expectedSol.toArray()));

        //check objective function value
        Map<String, LPNetlibProblem> problemsMap = LPNetlibProblem.loadAllProblems();
        LPNetlibProblem problem = problemsMap.get(problemName);
        RealVector c = new ArrayRealVector(mpsParser.getC().toArray());
        double value = c.dotProduct(expectedSol);
        log.debug("optimalValue: " + problem.optimalValue);
        log.debug("value       : " + value);
        assertEquals(problem.optimalValue, value, delta);

        //check G.x < h
        if (mpsParser.getG() != null) {
            RealMatrix G = new Array2DRowRealMatrix(mpsParser.getG().toArray());
            RealVector h = new ArrayRealVector(mpsParser.getH().toArray());
            RealVector Gxh = G.operate(expectedSol).subtract(h);
            double maxGxh = -Double.MAX_VALUE;
            for (int i = 0; i < Gxh.getDimension(); i++) {
                //log.debug(i);
                maxGxh = Math.max(maxGxh, Gxh.getEntry(i));
                assertTrue(Gxh.getEntry(i) <= 0);
            }
            log.debug("max(G.x - h): " + maxGxh);
        }

        //check A.x = b
        if (mpsParser.getA() != null) {
            RealMatrix A = new Array2DRowRealMatrix(mpsParser.getA().toArray());
            RealVector b = new ArrayRealVector(mpsParser.getB().toArray());
            RealVector Axb = A.operate(expectedSol).subtract(b);
            double norm = Axb.getNorm();
            log.debug("||A.x -b||: " + norm);
            assertEquals(0., norm, delta * mpsParser.getN());//some more tolerance
        }

        //check upper and lower bounds
        for (int i = 0; i < mpsParser.getLb().size(); i++) {
            double di = Double.isNaN(mpsParser.getLb().getQuick(i)) ? -Double.MAX_VALUE
                    : mpsParser.getLb().getQuick(i);
            assertTrue(di <= expectedSol.getEntry(i));
        }
        for (int i = 0; i < mpsParser.getUb().size(); i++) {
            double di = Double.isNaN(mpsParser.getUb().getQuick(i)) ? Double.MAX_VALUE
                    : mpsParser.getUb().getQuick(i);
            assertTrue(di >= expectedSol.getEntry(i));
        }
    }

    Utils.writeDoubleArrayToFile(mpsParser.getC().toArray(), "target" + File.separator + "c.txt");
    Utils.writeDoubleMatrixToFile(mpsParser.getG().toArray(), "target" + File.separator + "G.csv");
    Utils.writeDoubleArrayToFile(mpsParser.getH().toArray(), "target" + File.separator + "h.txt");
    Utils.writeDoubleMatrixToFile(mpsParser.getA().toArray(), "target" + File.separator + "A.csv");
    Utils.writeDoubleArrayToFile(mpsParser.getB().toArray(), "target" + File.separator + "b.txt");
    Utils.writeDoubleArrayToFile(mpsParser.getLb().toArray(), "target" + File.separator + "lb.txt");
    Utils.writeDoubleArrayToFile(mpsParser.getUb().toArray(), "target" + File.separator + "ub.txt");
}

From source file:edu.oregonstate.eecs.mcplan.ml.ConstrainedKMeans.java

private boolean mstep() {
    final double[][] mu_prime = new double[k][d];
    final int[] nh = new int[k];
    for (int i = 0; i < N; ++i) {
        final int h = assignments_[i];
        nh[h] += 1;/*from  w  w w  .  ja  v a2 s .  c o  m*/
        final RealVector xi = X_.get(i);
        //         mu_prime[h] = mu_prime[h].add( xi.subtract( mu_prime[h] ).mapDivideToSelf( i + 1 ) );
        for (int j = 0; j < d; ++j) {
            // Incremental mean calculation
            mu_prime[h][j] += (xi.getEntry(j) - mu_prime[h][j]) / nh[h];
        }
    }

    double Jprime = 0.0;
    for (int i = 0; i < N; ++i) {
        final int h = assignments_[i];
        Jprime += J(i, h);
    }

    double delta = 0.0;
    for (int h = 0; h < k; ++h) {
        final RealVector v = new ArrayRealVector(mu_prime[h], false); // No copy
        //         System.out.println( mu_[h] );
        //         System.out.println( v );
        final RealVector diff = mu_[h].subtract(v);
        delta += diff.getNorm();
        //         delta += mu_[h].getDistance( v );
        //         delta += distance( mu_[h], v );
        mu_[h] = v;
    }
    //      System.out.println( "\tdelta = " + delta );
    final double deltaJ = J_ - Jprime;
    //      System.out.println( "\tdeltaJ = " + deltaJ );
    J_ = Jprime;
    return deltaJ > convergence_tolerance_;
}