org.apache.commons.math3.linear.RealMatrixPreservingVisitor Java Examples

The following examples show how to use org.apache.commons.math3.linear.RealMatrixPreservingVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static double[] flatten(@Nonnull final RealMatrix[] grid) {
    Preconditions.checkArgument(grid.length >= 1, "The number of rows must be greater than 1");

    final int rows = grid.length;
    RealMatrix grid0 = grid[0];
    Preconditions.checkNotNull(grid0);
    int cellRows = grid0.getRowDimension();
    int cellCols = grid0.getColumnDimension();

    final DoubleArrayList list = new DoubleArrayList(rows * cellRows * cellCols);
    final RealMatrixPreservingVisitor visitor = new DefaultRealMatrixPreservingVisitor() {
        @Override
        public void visit(int row, int column, double value) {
            list.add(value);
        }
    };

    for (int row = 0; row < rows; row++) {
        RealMatrix cell = grid[row];
        cell.walkInRowOrder(visitor);
    }

    return list.toArray();
}
 
Example #2
Source File: WeightedIntDiGraph.java    From pacaya with Apache License 2.0 6 votes vote down vote up
public static WeightedIntDiGraph fromMatrix(RealMatrix m) {
    WeightedIntDiGraph g = new WeightedIntDiGraph();
    m.walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
        
        @Override
        public void visit(int row, int column, double value) {
            if (value != 0.0) {
                g.addEdge(row, column, value);
            }
        }
        
        @Override
        public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {
            // do nothing
        }
        
        @Override
        public double end() {
            // do nothing
            return 0;
        }
    });
    return g;
}
 
Example #3
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static double[] flatten(@Nonnull final RealMatrix[][] grid) {
    Preconditions.checkArgument(grid.length >= 1, "The number of rows must be greater than 1");
    Preconditions.checkArgument(grid[0].length >= 1,
        "The number of cols must be greater than 1");

    final int rows = grid.length;
    final int cols = grid[0].length;
    RealMatrix grid00 = grid[0][0];
    Preconditions.checkNotNull(grid00);
    int cellRows = grid00.getRowDimension();
    int cellCols = grid00.getColumnDimension();

    final DoubleArrayList list = new DoubleArrayList(rows * cols * cellRows * cellCols);
    final RealMatrixPreservingVisitor visitor = new DefaultRealMatrixPreservingVisitor() {
        @Override
        public void visit(int row, int column, double value) {
            list.add(value);
        }
    };

    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            RealMatrix cell = grid[row][col];
            cell.walkInRowOrder(visitor);
        }
    }

    return list.toArray();
}