Package edu.ucla.sspace.matrix

Examples of edu.ucla.sspace.matrix.Matrix


            new NonNegativeMatrixFactorizationMultiplicative();
        SparseMatrix matrix = new YaleSparseMatrix(VALUES);

        reducer.factorize(matrix, 2);

        Matrix W = reducer.dataClasses();
        assertEquals(4, W.rows());
        assertEquals(2, W.columns());

        Matrix H = reducer.classFeatures();
        assertEquals(2, H.rows());
        assertEquals(3, H.columns());

        /*
        for (int r = 0; r < 4; ++r) {
            for (int c = 0; c < 3; ++c) {
                double v = 0;
View Full Code Here


     * SimilarityFunction} will be used to build a symmetric adjacency matrix
     * and the {@link ClusterLink} will determine how to udpate similarities
     * between newly nerged clusters.
     */
    public Assignments cluster(Matrix m, int numClusters, Properties props) {
        Matrix adj = new SymmetricMatrix(m.rows(), m.rows());
        for (int r = 0; r < m.rows(); ++r) {
            DoubleVector v = m.getRowVector(r);
            for (int c = r+1; c < m.rows(); ++c)
                adj.set(r,c, simFunc.sim(v, m.getRowVector(c)));
        }
        return clusterAdjacencyMatrix(adj, method, numClusters);
    }
View Full Code Here

     * Without ever completely computing (AX-Y).  This is done by computing
     * (AX-Y) on a row by row basis and then multiplying the row of (AX-Y) by
     * the relevant parts of A' to update the final matrix.
     */
    private Matrix computeGofX(SparseMatrix Y) {
        Matrix G = new ArrayMatrix(numDimensions, X.columns());
        // Iterate through each row of Y, and thux AX-Y.  For each row, compute
        // the difference between the two matrics.  Then multiply the values in
        // the correspond row of A against this difference matrix.  Instead of
        // computing the inner product of A' and (AX-Y), we update each k,m
        // value in G by iterating through the columns of A.
        for (int r = 0; r < Y.rows(); ++r) {
            // Get the row vector of Y.
            SparseDoubleVector v = Y.getRowVector(r);
            double[] vDiff = new double[v.length()];
            // Compute the difference between the row vector of AX and the row
            // vector of Y.  This is the straightforward dot product between A_i
            // and X'_i.
            for (int c = 0; c < X.columns(); c++) {
                double sum = 0;
                for (int k = 0; k < A.columns(); ++k)
                    sum += A.get(r, k) * X.get(k, c);
                vDiff[c] = sum - v.get(c);
            }

            // Now get the row vector A_i and for each column k, multiply it
            // against each column c in vDiff to get the difference in the
            // gradient G_{k, c}.
            for (int k = 0; k < A.columns(); ++k)
                for (int c = 0; c < X.columns(); ++c)
                    G.set(k, c, G.get(k, c) + A.get(r, k) * vDiff[c]);
        }
        return G;
    }
View Full Code Here

     * Without every completely computing (AX-Y).  This computation is
     * straightforward as the rows of (AX-Y) can be fully utilized in the naive
     * matrix multiplication against X' without any inverted traversals.
     */
    private Matrix computeGofA(SparseMatrix Y) {
        Matrix G = new ArrayMatrix(A.rows(), numDimensions);
        // Iterate through each row of Y, and thux AX-Y.  For each row, compute
        // the difference between the two matrics.  Then multiply the values in
        // the correspond column of X' against this difference matrix. 
        for (int r = 0; r < Y.rows(); ++r) {
            // Get the row vector of Y.
            SparseDoubleVector v = Y.getRowVector(r);
            double[] vDiff = new double[v.length()];
            // Compute the difference between the row vector of AX and the row
            // vector of Y.  This is the straightforward dot product between A_i
            // and X'_i.
            for (int c = 0; c < X.columns(); c++) {
                double sum = 0;
                for (int k = 0; k < A.columns(); ++k)
                    sum += A.get(r, k) * X.get(k, c);
                vDiff[c] = sum - v.get(c);
            }

            for (int k = 0; k < X.rows(); ++k) {
                double sum = 0;
                for (int c = 0; c < X.columns(); ++c)
                    sum += vDiff[c] * X.get(k, c);
                G.set(r, k, sum);
            }
        }
        return G;
    }
View Full Code Here

        File outMatFile = new File(options.getPositionalArg(1));
        Format inMatFormat = Format.valueOf(
                options.getStringOption('i').toUpperCase());
        Format outMatFormat = Format.valueOf(
                options.getStringOption('o').toUpperCase());
        Matrix matrix = MatrixIO.readMatrix(inMatFile, inMatFormat);
        MatrixIO.writeMatrix(matrix, outMatFile, outMatFormat);
    }
View Full Code Here

        // NOTE: Use a LinkedHashMap here because this will ensure that the
        // words are returned in the same row-order as the matrix.  This
        // generates better disk I/O behavior for accessing the matrix since
        // each word is directly after the previous on disk.
        termToIndex = new LinkedHashMap<String, Integer>();
        Matrix m = null;
        long start = System.currentTimeMillis();

            switch (format) {
            case TEXT:
                m = Matrices.synchronizedMatrix(loadText(is));
View Full Code Here

     * the space's vectors.
     *
     * @param sspaceFile a file in {@link SSpaceFormat#TEXT text} format
     */
    private Matrix loadText(InputStream fileStream) throws IOException {
        Matrix matrix = null;

        BufferedReader br =
                new BufferedReader(new InputStreamReader(fileStream));
        String line = br.readLine();
        if (line == null)
            throw new IOException("Empty .sspace file");
            // Strip off the 4-byte (2 char) header
        String[] dimensions = line.split("\\s");
        int rows = Integer.parseInt(dimensions[0]);
        int columns = Integer.parseInt(dimensions[1]);
        int index = 0;
       
        // reusable array for writing rows into the matrix
        double[] row = new double[columns];
       
        matrix = new ArrayMatrix(rows, columns);

        while ((line = br.readLine()) != null) {
            if (index >= rows)
                throw new IOException("More rows than specified");
            String[] termVectorPair = line.split("\\|");
            String[] values = termVectorPair[1].split("\\s");
            termToIndex.put(termVectorPair[0], index);
            if (values.length != columns) {
                throw new IOException(
                            "improperly formated semantic space file");
            }
            for (int c = 0; c < columns; ++c) {
                double d = Double.parseDouble(values[c]);
                row[c] = d;
                // matrix.set(index, c, d);
            }
            matrix.setRow(index, row);
            index++;
        }
        if (index != rows)
            throw new IOException(String.format(
                "Expected %d rows; saw %d", rows, index));
View Full Code Here

     * the space's vectors.
     *
     * @param sspaceFile a file in {@link SSpaceFormat#TEXT text} format
     */
    private Matrix loadSparseText(InputStream fileStream) throws IOException {
        Matrix matrix = null;

        BufferedReader br =
                new BufferedReader(new InputStreamReader(fileStream));
        String line = br.readLine();
        if (line == null)
            throw new IOError(new Throwable(
                        "An empty file has been passed in"));
        String[] dimensions = line.split("\\s");
        int rows = Integer.parseInt(dimensions[0]);
        int columns = Integer.parseInt(dimensions[1]);

        int row = 0;
       
        // create a sparse matrix
        matrix = Matrices.create(rows, columns, false);
        while ((line = br.readLine()) != null) {
            String[] termVectorPair = line.split("\\|");
            String[] values = termVectorPair[1].split(",");
            termToIndex.put(termVectorPair[0], row);

            // even indicies are columns, odd are the values
            for (int i = 0; i < values.length; i +=2 ) {
                int col = Integer.parseInt(values[i]);
                double val = Double.parseDouble(values[i+1]);
                matrix.set(row, col, val);
            }
            row++;
        }
        return matrix;   
    }
View Full Code Here

        DataInputStream dis = new DataInputStream(fileStream);
        int rows = dis.readInt();
        int cols = dis.readInt();

        // create a dense matrix
        Matrix m = new ArrayMatrix(rows, cols);
        double[] d = new double[cols];
        for (int row = 0; row < rows; ++row) {
            String word = dis.readUTF();
            termToIndex.put(word, row);

            for (int col = 0; col < cols; ++col) {
                d[col] = dis.readDouble();
            }
            m.setRow(row, d);
        }
        return m;
    }
View Full Code Here

                U = MatrixIO.readMatrix(uOutput, Format.DENSE_TEXT,
                                                  Type.DENSE_IN_MEMORY);
                scaledDataClasses = false;

                // Sigma only has n values for an n^2 matrix, so make it sparse
                Matrix S = MatrixIO.readMatrix(sOutput, Format.DENSE_TEXT,
                                               Type.SPARSE_ON_DISK);
                singularValues = new double[dimensions];
                for (int s = 0; s < dimensions; ++s)
                    singularValues[s] = S.get(s, s);

                // Octave does not transpose V, so transpose it
                V = MatrixIO.readMatrix(vOutput, Format.DENSE_TEXT,
                                        Type.DENSE_ON_DISK, true);
                scaledDataClasses = false;
View Full Code Here

TOP

Related Classes of edu.ucla.sspace.matrix.Matrix

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.