/** * Returns a {@link MatrixBuilder} in the default format of the fastest * available {@link SVD.Algorithm SVD algorithm}. * * @return a matrix builder to be used in creating a matrix for use with the * {@link SVD} class */ public static MatrixBuilder getMatrixBuilderForSVD() { return getMatrixBuilderForSVD(false); }
public static void main(String[] args) throws Exception { Matrix m = MatrixIO.readMatrix(new File(args[0]), MatrixIO.Format.DENSE_TEXT); m = Matrices.transpose(m); File out = new File(args[1]); MatrixIO.writeMatrix(m, out, MatrixIO.Format.DENSE_TEXT); }
return Matrices.asSparseMatrix(Arrays.asList(rowVectors));
/** * Returns a copied version of a given matrix. The returned matrix will * have the same dimensionality, values, and sparsity, but it may not have * the same exact sub-type. * * @param matrix the matrix to be copied * * @throws IllegalArgumentException when the dimensionality of matrix and * output do not match * * @return a copied version of matrix */ public static Matrix copy(Matrix matrix) { Matrix copiedMatrix = null; if (matrix instanceof SparseMatrix) copiedMatrix = Matrices.create( matrix.rows(), matrix.columns(), Type.SPARSE_IN_MEMORY); else copiedMatrix = Matrices.create( matrix.rows(), matrix.columns(), Type.DENSE_IN_MEMORY); return copyTo(matrix, copiedMatrix); }
if (m2 instanceof DiagonalMatrix) { if (m1 instanceof DiagonalMatrix) return multiplyBothDiag(m1, m2); else return multiplyRightDiag(m1, m2); } else if (m1 instanceof DiagonalMatrix) { return multiplyLeftDiag(m1, m2); return multiplyBothDiag(m1, m2); else return multiplyRightDiag(m1, m2); } else if (m1 instanceof DiagonalMatrix) { return multiplyLeftDiag(m1, m2); Matrix resultMatrix = create(m1.rows(), m2.columns(), true); for (int r = 0; r < m1.rows(); ++r) { double[] row = m1.getRow(r);
/** * Returns a scaled {@link Matrix}, where each row is the unit * magnitude version of the corresponding row vector in {@link matrix}. * This is required so that dot product computations over a large number of * data points can be distributed, wherease the cosine similarity cannot be. */ private Matrix scaleMatrix(Matrix matrix) { // Scale every data point such that it has a dot product of 1 with // itself. This will make further calculations easier since the dot // product distrubutes when the cosine similarity does not. if (matrix instanceof SparseMatrix) { List<SparseDoubleVector> scaledVectors = new ArrayList<SparseDoubleVector>(matrix.rows()); SparseMatrix sm = (SparseMatrix) matrix; for (int r = 0; r < matrix.rows(); ++r) { SparseDoubleVector v = sm.getRowVector(r); scaledVectors.add(new ScaledSparseDoubleVector( v, 1/v.magnitude())); } return Matrices.asSparseMatrix(scaledVectors); } else { List<DoubleVector> scaledVectors = new ArrayList<DoubleVector>(matrix.rows()); for (int r = 0; r < matrix.rows(); ++r) { DoubleVector v = matrix.getRowVector(r); scaledVectors.add(new ScaledDoubleVector(v, 1/v.magnitude())); } return Matrices.asMatrix(scaledVectors); } }
Vectors.subview(e.getValue(), 0, getVectorLength()))); Matrix m = Matrices.asMatrix(vectorsInOrder);
@SuppressWarnings("deprecation") double[][] m = MatrixIO.readMatrixArray(matrix, format); return coltSVD(m, Matrices.isDense(format), dimensions);
/** * {@inheritDoc} */ public Matrix transform(Matrix input, Matrix output) { Matrices.copyTo(input, output); return output; }
if (m2 instanceof DiagonalMatrix) { if (m1 instanceof DiagonalMatrix) return multiplyBothDiag(m1, m2); else return multiplyRightDiag(m1, m2); } else if (m1 instanceof DiagonalMatrix) { return multiplyLeftDiag(m1, m2); return multiplyBothDiag(m1, m2); else return multiplyRightDiag(m1, m2); } else if (m1 instanceof DiagonalMatrix) { return multiplyLeftDiag(m1, m2); Matrix resultMatrix = create(m1.rows(), m2.columns(), true); for (int r = 0; r < m1.rows(); ++r) { double[] row = m1.getRow(r);
/** * Returns a scaled {@link Matrix}, where each row is the unit * magnitude version of the corresponding row vector in {@link matrix}. * This is required so that dot product computations over a large number of * data points can be distributed, wherease the cosine similarity cannot be. */ private Matrix scaleMatrix(Matrix matrix) { // Scale every data point such that it has a dot product of 1 with // itself. This will make further calculations easier since the dot // product distrubutes when the cosine similarity does not. if (matrix instanceof SparseMatrix) { List<SparseDoubleVector> scaledVectors = new ArrayList<SparseDoubleVector>(matrix.rows()); SparseMatrix sm = (SparseMatrix) matrix; for (int r = 0; r < matrix.rows(); ++r) { SparseDoubleVector v = sm.getRowVector(r); scaledVectors.add(new ScaledSparseDoubleVector( v, 1/v.magnitude())); } return Matrices.asSparseMatrix(scaledVectors); } else { List<DoubleVector> scaledVectors = new ArrayList<DoubleVector>(matrix.rows()); for (int r = 0; r < matrix.rows(); ++r) { DoubleVector v = matrix.getRowVector(r); scaledVectors.add(new ScaledDoubleVector(v, 1/v.magnitude())); } return Matrices.asMatrix(scaledVectors); } }
/** * Returns a copied version of a given matrix. The returned matrix will * have the same dimensionality, values, and sparsity, but it may not have * the same exact sub-type. * * @param matrix the matrix to be copied * * @throws IllegalArgumentException when the dimensionality of matrix and * output do not match * * @return a copied version of matrix */ public static Matrix copy(Matrix matrix) { Matrix copiedMatrix = null; if (matrix instanceof SparseMatrix) copiedMatrix = Matrices.create( matrix.rows(), matrix.columns(), Type.SPARSE_IN_MEMORY); else copiedMatrix = Matrices.create( matrix.rows(), matrix.columns(), Type.DENSE_IN_MEMORY); return copyTo(matrix, copiedMatrix); }
Matrix clusterToSplit = Matrices.asMatrix(originalCluster); Assignment[] newAssignments = clustering.cluster(clusterToSplit, 2, props).assignments();
@SuppressWarnings("deprecation") double[][] m = MatrixIO.readMatrixArray(matrix, format); return coltSVD(m, Matrices.isDense(format), dimensions);
/** * Returns a {@link MatrixBuilder} in the default format of the fastest * available {@link SVD.Algorithm SVD algorithm}. * * @return a matrix builder to be used in creating a matrix for use with the * {@link SVD} class */ public static MatrixBuilder getMatrixBuilderForSVD() { return getMatrixBuilderForSVD(false); }
public static void main(String[] args) throws Exception { Matrix m = MatrixIO.readMatrix(new File(args[0]), MatrixIO.Format.DENSE_TEXT); m = Matrices.transpose(m); File out = new File(args[1]); MatrixIO.writeMatrix(m, out, MatrixIO.Format.DENSE_TEXT); }
Assignments assignments = null; if (sparseVectors.size() > 0) { SparseMatrix matrix = Matrices.asSparseMatrix(sparseVectors); assignments = (numClusters > 0) ? clustering.cluster(matrix, numClusters, props) : clustering.cluster(matrix, props); } else { Matrix matrix = Matrices.asMatrix(vectors); assignments = (numClusters > 0) ? clustering.cluster(matrix, numClusters, props)