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

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

Introduction

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

Prototype

public double getLInfNorm() 

Source Link

Document

Returns the L norm of the vector.

Usage

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

@Override
public double getNormInfinity(Matrix m) {
    ArgChecker.notNull(m, "m");
    if (m instanceof DoubleArray) {
        RealVector temp = CommonsMathWrapper.wrap((DoubleArray) m);
        return temp.getLInfNorm();
    } else if (m instanceof DoubleMatrix) {
        RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m);
        //REVIEW Commons getNorm() is wrong - it returns the column norm
        // TODO find if commons implements this anywhere, so we are not doing it
        // by hand
        double max = 0.0;
        for (int row = temp.getRowDimension() - 1; row >= 0; row--) {
            max = Math.max(max, temp.getRowVector(row).getL1Norm());
        }//  w  w w .  j a v  a2s. c  om
        return max;
    }
    throw new IllegalArgumentException("Can only find normInfinity of DoubleMatrix; have " + m.getClass());
}

From source file:org.apache.predictionio.examples.java.recommendations.tutorial4.FeatureBasedAlgorithm.java

public FeatureBasedModel train(PreparedData data) {
    Map<Integer, RealVector> userFeatures = new HashMap<Integer, RealVector>();
    Map<Integer, Integer> userActions = new HashMap<Integer, Integer>();

    for (Integer uid : data.userInfo.keySet()) {
        userFeatures.put(uid, new ArrayRealVector(data.featureCount));
        userActions.put(uid, 0);/*from www. j  a va2 s.  c  om*/
    }

    for (TrainingData.Rating rating : data.ratings) {
        final int uid = rating.uid;
        final int iid = rating.iid;
        final double rate = rating.rating;

        // Skip features outside the range.
        if (!(params.min <= rate && rate <= params.max))
            continue;

        final double actualRate = (rate - params.drift) * params.scale;
        final RealVector userFeature = userFeatures.get(uid);
        final RealVector itemFeature = data.itemFeatures.get(iid);
        userFeature.combineToSelf(1, actualRate, itemFeature);

        userActions.put(uid, userActions.get(uid) + 1);
    }

    // Normalize userFeatures by l-inf-norm
    for (Integer uid : userFeatures.keySet()) {
        final RealVector feature = userFeatures.get(uid);
        feature.mapDivideToSelf(feature.getLInfNorm());
    }

    // Normalize itemFeatures by weight
    Map<Integer, RealVector> itemFeatures = new HashMap<Integer, RealVector>();
    for (Integer iid : data.itemFeatures.keySet()) {
        final RealVector feature = data.itemFeatures.get(iid);
        final RealVector normalizedFeature = feature.mapDivide(feature.getL1Norm());
        itemFeatures.put(iid, normalizedFeature);
    }

    return new FeatureBasedModel(userFeatures, userActions, itemFeatures);
}