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:org.grouplens.lenskit.diffusion.Iterative.IterativeDiffusionItemScorer.java

/**
 * Score items by computing predicted ratings.
 *
 * @see ItemScoreAlgorithm#scoreItems(ItemItemModel, org.grouplens.lenskit.vectors.SparseVector, org.grouplens.lenskit.vectors.MutableSparseVector, NeighborhoodScorer)
 *//*from  w  w  w .j a v  a2  s .  c om*/
@Override
public void score(long user, @Nonnull MutableSparseVector scores) {
    UserHistory<? extends Event> history = dao.getEventsForUser(user, summarizer.eventTypeWanted());
    if (history == null) {
        history = History.forUser(user);
    }
    SparseVector summary = summarizer.summarize(history);
    VectorTransformation transform = normalizer.makeTransformation(user, summary);
    MutableSparseVector normed = summary.mutableCopy();
    transform.apply(normed);
    scores.clear();
    int numItems = 1682;
    //algorithm.scoreItems(model, normed, scores, scorer);
    int num_updates = 300;
    double update_rate = 1;
    double threshold = 0.01;
    RealVector z_out = diffusionMatrix.preMultiply(VectorUtils.toRealVector(numItems, normed));
    boolean updated = true;
    LongSortedSet known = normed.keySet();
    int count_iter = 0;
    for (int i = 0; i < num_updates && updated; i++) {
        updated = false;
        RealVector temp = diffusionMatrix.preMultiply(z_out);
        temp.mapMultiplyToSelf(z_out.getNorm() / temp.getNorm());
        RealVector temp_diff = z_out.add(temp.mapMultiplyToSelf(-1.0));
        for (int j = 0; j < numItems; j++) {
            if (!known.contains((long) (j + 1))) {
                //if the rating is not one of the known ones
                if (Math.abs(temp_diff.getEntry(j)) > threshold) {
                    // if difference is large enough, update
                    updated = true;
                    z_out.setEntry(j, (1.0 - update_rate) * z_out.getEntry(j) + update_rate * temp.getEntry(j));
                }
            }
        }
        count_iter++;
    }
    System.out.println(count_iter);
    LongSortedSet testDomain = scores.keyDomain();
    //fill up the score vector
    for (int i = 0; i < numItems; i++) {
        if (testDomain.contains((long) (i + 1))) {
            scores.set((long) (i + 1), z_out.getEntry(i));
        }
    }

    // untransform the scores
    transform.unapply(scores);
    System.out.println(scores);
}

From source file:org.grouplens.samantha.modeler.solver.L2Regularizer.java

public double getObjective(double l2coef, RealVector var) {
    double l2norm = var.getNorm();
    return l2coef * l2norm * l2norm;
}

From source file:org.grouplens.samantha.modeler.solver.L2Regularizer.java

public double getObjective(double l2coef, List<RealVector> vars) {
    double objVal = 0.0;
    for (RealVector realVector : vars) {
        double l2norm = realVector.getNorm();
        objVal += l2norm * l2norm;// www. jav a2 s.  c  o m
    }
    return objVal * l2coef;
}

From source file:org.lambda3.indra.composition.L2NormSumVectorComposer.java

@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);
    }//from w w  w  . j a  va 2 s  . c  o  m

    return sum;
}

From source file:org.lenskit.mf.funksvd.FunkSVDModelBuilder.java

/**
 * Add a feature's summary to the feature info builder.
 *
 * @param ufv The user values.//from   w  w w.j a  va2  s .  c o  m
 * @param ifv The item values.
 * @param fib  The feature info builder.
 */
protected void summarizeFeature(RealVector ufv, RealVector ifv, FeatureInfo.Builder fib) {
    fib.setUserAverage(realVectorSum(ufv) / ufv.getDimension())
            .setItemAverage(realVectorSum(ifv) / ifv.getDimension())
            .setSingularValue(ufv.getNorm() * ifv.getNorm());
}

From source file:rcdemo.physics.FrictionForceModel.java

@Override
public RealVector getForce(RealVector x, RealVector v) {
    double rho = 1;
    double Cd = 1;
    double A = 1;
    double factor = 0.5 * rho * v.getNorm() * Cd * A;
    return v.mapMultiply(-factor);
}

From source file:trustframework.utils.Similarity.java

/**
 * Calculates cosine similarity between two frequencies
 * @param <T> Hash key class/*from   w  w w  .j  a  v a 2s  .  com*/
 * @param freq1 frequency 1
 * @param freq2 frequency 2
 * @return cosine similarity [0,1]
 */
public static <T> double cosineSimilarity(Map<T, Integer> freq1, Map<T, Integer> freq2) {
    Set<T> terms = new HashSet<>();
    terms.addAll(freq1.keySet());
    terms.addAll(freq2.keySet());
    RealVector v1 = buildRealVector(freq1, terms);
    RealVector v2 = buildRealVector(freq2, terms);
    return (v1.dotProduct(v2)) / (v1.getNorm() * v2.getNorm());
}