Search in sources :

Example 26 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class WrapperDoubleMatrix2D method viewPart.

/**
 *Constructs and returns a new <i>sub-range view</i> that is a <tt>height x width</tt> sub matrix starting at <tt>[row,column]</tt>.
 *
 *Operations on the returned view can only be applied to the restricted range.
 *Any attempt to access coordinates not contained in the view will throw an <tt>IndexOutOfBoundsException</tt>.
 *<p>
 *<b>Note that the view is really just a range restriction:</b>
 *The returned matrix is backed by this matrix, so changes in the returned matrix are reflected in this matrix, and vice-versa.
 *<p>
 *The view contains the cells from <tt>[row,column]</tt> to <tt>[row+height-1,column+width-1]</tt>, all inclusive.
 *and has <tt>view.rows() == height; view.columns() == width;</tt>.
 *A view's legal coordinates are again zero based, as usual.
 *In other words, legal coordinates of the view range from <tt>[0,0]</tt> to <tt>[view.rows()-1==height-1,view.columns()-1==width-1]</tt>.
 *As usual, any attempt to access a cell at a coordinate <tt>column&lt;0 || column&gt;=view.columns() || row&lt;0 || row&gt;=view.rows()</tt> will throw an <tt>IndexOutOfBoundsException</tt>.
 *
 *@param     row   The index of the row-coordinate.
 *@param     column   The index of the column-coordinate.
 *@param     height   The height of the box.
 *@param     width   The width of the box.
 *@throws	IndexOutOfBoundsException if <tt>column<0 || width<0 || column+width>columns() || row<0 || height<0 || row+height>rows()</tt>
 *@return the new view.
 */
public DoubleMatrix2D viewPart(final int row, final int column, int height, int width) {
    checkBox(row, column, height, width);
    DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {

        public double getQuick(int i, int j) {
            return content.get(row + i, column + j);
        }

        public void setQuick(int i, int j, double value) {
            content.set(row + i, column + j, value);
        }
    };
    view.rows = height;
    view.columns = width;
    return view;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 27 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class WrapperDoubleMatrix2D method viewDice.

/**
 *Constructs and returns a new <i>dice (transposition) view</i>; Swaps axes; example: 3 x 4 matrix --> 4 x 3 matrix.
 *The view has both dimensions exchanged; what used to be columns become rows, what used to be rows become columns.
 *In other words: <tt>view.get(row,column)==this.get(column,row)</tt>.
 *This is a zero-copy transposition, taking O(1), i.e. constant time.
 *The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
 *Use idioms like <tt>result = viewDice(A).copy()</tt> to generate an independent transposed matrix.
 *<p>
 *<b>Example:</b>
 *<table border="0">
 *  <tr nowrap>
 *	<td valign="top">2 x 3 matrix: <br>
 *	  1, 2, 3<br>
 *	  4, 5, 6 </td>
 *	<td>transpose ==></td>
 *	<td valign="top">3 x 2 matrix:<br>
 *	  1, 4 <br>
 *	  2, 5 <br>
 *	  3, 6</td>
 *	<td>transpose ==></td>
 *	<td valign="top">2 x 3 matrix: <br>
 *	  1, 2, 3<br>
 *	  4, 5, 6 </td>
 *  </tr>
 *</table>
 *
 *@return a new dice view.
 */
public DoubleMatrix2D viewDice() {
    DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {

        public double getQuick(int row, int column) {
            return content.get(column, row);
        }

        public void setQuick(int row, int column, double value) {
            content.set(column, row, value);
        }
    };
    view.rows = columns;
    view.columns = rows;
    return view;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 28 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class WrapperDoubleMatrix2D method viewSelection.

/**
 *Constructs and returns a new <i>selection view</i> that is a matrix holding the indicated cells.
 *There holds <tt>view.rows() == rowIndexes.length, view.columns() == columnIndexes.length</tt> and <tt>view.get(i,j) == this.get(rowIndexes[i],columnIndexes[j])</tt>.
 *Indexes can occur multiple times and can be in arbitrary order.
 *<p>
 *<b>Example:</b>
 *<pre>
 *this = 2 x 3 matrix:
 *1, 2, 3
 *4, 5, 6
 *rowIndexes     = (0,1)
 *columnIndexes  = (1,0,1,0)
 *-->
 *view = 2 x 4 matrix:
 *2, 1, 2, 1
 *5, 4, 5, 4
 *</pre>
 *Note that modifying the index arguments after this call has returned has no effect on the view.
 *The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
 *<p>
 *To indicate "all" rows or "all columns", simply set the respective parameter
 *@param  rowIndexes   The rows of the cells that shall be visible in the new view. To indicate that <i>all</i> rows shall be visible, simply set this parameter to <tt>null</tt>.
 *@param  columnIndexes   The columns of the cells that shall be visible in the new view. To indicate that <i>all</i> columns shall be visible, simply set this parameter to <tt>null</tt>.
 *@return the new view.
 *@throws IndexOutOfBoundsException if <tt>!(0 <= rowIndexes[i] < rows())</tt> for any <tt>i=0..rowIndexes.length()-1</tt>.
 *@throws IndexOutOfBoundsException if <tt>!(0 <= columnIndexes[i] < columns())</tt> for any <tt>i=0..columnIndexes.length()-1</tt>.
 */
public DoubleMatrix2D viewSelection(int[] rowIndexes, int[] columnIndexes) {
    // check for "all"
    if (rowIndexes == null) {
        rowIndexes = new int[rows];
        for (int i = rows; --i >= 0; ) rowIndexes[i] = i;
    }
    if (columnIndexes == null) {
        columnIndexes = new int[columns];
        for (int i = columns; --i >= 0; ) columnIndexes[i] = i;
    }
    checkRowIndexes(rowIndexes);
    checkColumnIndexes(columnIndexes);
    final int[] rix = rowIndexes;
    final int[] cix = columnIndexes;
    DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {

        public double getQuick(int i, int j) {
            return content.get(rix[i], cix[j]);
        }

        public void setQuick(int i, int j, double value) {
            content.set(rix[i], cix[j], value);
        }
    };
    view.rows = rowIndexes.length;
    view.columns = columnIndexes.length;
    return view;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 29 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class QRDecomposition method solve.

/**
 *Least squares solution of <tt>A*X = B</tt>; <tt>returns X</tt>.
 *@param B    A matrix with as many rows as <tt>A</tt> and any number of columns.
 *@return     <tt>X</tt> that minimizes the two norm of <tt>Q*R*X - B</tt>.
 *@exception  IllegalArgumentException  if <tt>B.rows() != A.rows()</tt>.
 *@exception  IllegalArgumentException  if <tt>!this.hasFullRank()</tt> (<tt>A</tt> is rank deficient).
 */
public DoubleMatrix2D solve(DoubleMatrix2D B) {
    cern.jet.math.Functions F = cern.jet.math.Functions.functions;
    if (B.rows() != m) {
        throw new IllegalArgumentException("Matrix row dimensions must agree.");
    }
    if (!this.hasFullRank()) {
        throw new IllegalArgumentException("Matrix is rank deficient.");
    }
    // Copy right hand side
    int nx = B.columns();
    DoubleMatrix2D X = B.copy();
    // Compute Y = transpose(Q)*B
    for (int k = 0; k < n; k++) {
        for (int j = 0; j < nx; j++) {
            double s = 0.0;
            for (int i = k; i < m; i++) {
                s += QR.getQuick(i, k) * X.getQuick(i, j);
            }
            s = -s / QR.getQuick(k, k);
            for (int i = k; i < m; i++) {
                X.setQuick(i, j, X.getQuick(i, j) + s * QR.getQuick(i, k));
            }
        }
    }
    // Solve R*X = Y;
    for (int k = n - 1; k >= 0; k--) {
        for (int j = 0; j < nx; j++) {
            X.setQuick(k, j, X.getQuick(k, j) / Rdiag.getQuick(k));
        }
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < nx; j++) {
                X.setQuick(i, j, X.getQuick(i, j) - X.getQuick(k, j) * QR.getQuick(i, k));
            }
        }
    }
    return X.viewPart(0, 0, n, nx);
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 30 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class TestMatrix2D method doubleTest10.

/**
 */
public static void doubleTest10() {
    int rows = 6;
    // make a 4*5 matrix
    int columns = 7;
    // DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
    DoubleMatrix2D master = Factory2D.ascending(rows, columns);
    // Basic.ascending(master);
    // master.assign(1); // set all cells to 1
    Transform.mult(master, Math.sin(0.3));
    System.out.println("\n" + master);
    // master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
    // System.out.println("\n"+master);
    int[] rowIndexes = { 0, 1, 2, 3 };
    int[] columnIndexes = { 0, 1, 2, 3 };
    int[] rowIndexes2 = { 3, 0, 3 };
    int[] columnIndexes2 = { 3, 0, 3 };
    DoubleMatrix2D view1 = master.viewPart(1, 1, 4, 5).viewSelection(rowIndexes, columnIndexes);
    System.out.println("\nview1=" + view1);
    DoubleMatrix2D view9 = view1.viewStrides(2, 2).viewStrides(2, 1);
    System.out.println("\nview9=" + view9);
    view1 = view1.viewSelection(rowIndexes2, columnIndexes2);
    System.out.println("\nview1=" + view1);
    DoubleMatrix2D view2 = view1.viewPart(1, 1, 2, 2);
    System.out.println("\nview2=" + view2);
    DoubleMatrix2D view3 = view2.viewRowFlip();
    System.out.println("\nview3=" + view3);
    view3.assign(Factory2D.ascending(view3.rows(), view3.columns()));
    // Basic.ascending(view3);
    System.out.println("\nview3=" + view3);
    // view2.assign(-1);
    System.out.println("\nmaster replaced" + master);
    System.out.println("\nview1 replaced" + view1);
    System.out.println("\nview2 replaced" + view2);
    System.out.println("\nview3 replaced" + view3);
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Aggregations

DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)137 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)39 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)37 Algebra (cern.colt.matrix.linalg.Algebra)16 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)13 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)13 Node (edu.cmu.tetrad.graph.Node)11 Graph (edu.cmu.tetrad.graph.Graph)8 Test (org.junit.Test)6 DoubleMatrixReader (ubic.basecode.io.reader.DoubleMatrixReader)6 StringMatrixReader (ubic.basecode.io.reader.StringMatrixReader)6 DataSet (edu.cmu.tetrad.data.DataSet)5 DoubleArrayList (cern.colt.list.DoubleArrayList)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)4 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)4 AbstractFormatter (cern.colt.matrix.impl.AbstractFormatter)3 RobustEigenDecomposition (dr.math.matrixAlgebra.RobustEigenDecomposition)3 Endpoint (edu.cmu.tetrad.graph.Endpoint)3 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)3 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)3