Example usage for org.apache.commons.math.linear RealMatrix getFrobeniusNorm

List of usage examples for org.apache.commons.math.linear RealMatrix getFrobeniusNorm

Introduction

In this page you can find the example usage for org.apache.commons.math.linear RealMatrix getFrobeniusNorm.

Prototype

double getFrobeniusNorm();

Source Link

Document

Returns the <a href="http://mathworld.wolfram.com/FrobeniusNorm.html"> Frobenius norm</a> of the matrix.

Usage

From source file:net.sf.jtmt.clustering.Cluster.java

/**
 * Gets the similarity.//from  w w  w  . j a v a  2  s . c om
 *
 * @param doc the doc
 * @return the similarity
 */
public double getSimilarity(RealMatrix doc) {
    if (centroid != null) {
        double dotProduct = centroid.transpose().multiply(doc).getNorm();
        double normProduct = centroid.getFrobeniusNorm() * doc.getFrobeniusNorm();
        return dotProduct / normProduct;
    }
    return 0.0D;
}

From source file:net.sf.jtmt.similarity.matrix.CosineSimilarity.java

@Override
public double computeSimilarity(RealMatrix sourceDoc, RealMatrix targetDoc) {
    if (sourceDoc.getRowDimension() != targetDoc.getRowDimension()
            || sourceDoc.getColumnDimension() != targetDoc.getColumnDimension()
            || sourceDoc.getColumnDimension() != 1) {
        throw new IllegalArgumentException("Matrices are not column matrices or not of the same size");
    }// www. j a  v a  2s.c o  m
    // max col sum, only 1 col, so...
    double dotProduct = dot(sourceDoc, targetDoc);
    // sqrt of sum of squares of all elements, only one col, so...
    double eucledianDist = sourceDoc.getFrobeniusNorm() * targetDoc.getFrobeniusNorm();
    return dotProduct / eucledianDist;
}