Logo Search packages:      
Sourcecode: jblas version File versions  Download package

FloatMatrix.java

// --- BEGIN LICENSE BLOCK ---
/* 
 * Copyright (c) 2009, Mikio L. Braun
 * Copyright (c) 2008, Johannes Schaback
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 * 
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 * 
 *     * Neither the name of the Technische Universit├Ąt Berlin nor the
 *       names of its contributors may be used to endorse or promote
 *       products derived from this software without specific prior
 *       written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
// --- END LICENSE BLOCK ---

package org.jblas.la;

import org.jblas.la.exceptions.SizeException;
import org.jblas.la.ranges.Range;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Comparator;

/**
 * A general matrix class for <tt>float</tt> typed values.
 * 
 * Don't be intimidated by the large number of methods this function defines. Most
 * are overloads provided for ease of use. For example, for each arithmetic operation,
 * up to six overloaded versions exist to handle in-place computations, and
 * scalar arguments.
 * 
 * <h3>Construction</h3>
 * 
 * <p>To construct a two-dimensional matrices, you can use the following constructors
 * and static methods.</p>
 * 
 * <table class="my">
 * <tr><th>Method<th>Description
 * <tr><td>FloatMatrix(m,n, [value1, value2, value3...])<td>Values are filled in row by row.
 * <tr><td>FloatMatrix(new float[][] {{value1, value2, ...}, ...}<td>Inner arrays are columns.
 * <tr><td>FloatMatrix.zeros(m,n) <td>Initial values set to 0.0f.
 * <tr><td>FloatMatrix.ones(m,n) <td>Initial values set to 1.0f.
 * <tr><td>FloatMatrix.rand(m,n) <td>Values drawn at random between 0.0f and 1.0f.
 * <tr><td>FloatMatrix.randn(m,n) <td>Values drawn from normal distribution.
 * <tr><td>FloatMatrix.eye(n) <td>Unit matrix (values 0.0f except for 1.0f on the diagonal).
 * <tr><td>FloatMatrix.diag(array) <td>Diagonal matrix with given diagonal elements.
 * </table>
 * 
 * <p>Alternatively, you can construct (column) vectors, if you just supply the length
 * using the following constructors and static methods.</p>
 * 
 * <table class="my">
 * <tr><th>Method<th>Description
 * <tr><td>FloatMatrix(m)<td>Constructs a column vector.
 * <tr><td>FloatMatrix(new float[] {value1, value2, ...})<td>Constructs a column vector.
 * <tr><td>FloatMatrix.zeros(m) <td>Initial values set to 1.0f.
 * <tr><td>FloatMatrix.ones(m) <td>Initial values set to 0.0f.
 * <tr><td>FloatMatrix.rand(m) <td>Values drawn at random between 0.0f and 1.0f.
 * <tr><td>FloatMatrix.randn(m) <td>Values drawn from normal distribution.
 * </table>
 * 
 * <p>You can also construct new matrices by concatenating matrices either horziontally
 * or vertically:</p>
 * 
 * <table class="my">
 * <tr><th>Method<th>Description
 * <tr><td>x.concatHorizontally(y)<td>New matrix will be x next to y.
 * <tr><td>x.concatVertically(y)<td>New matrix will be x atop y.
 * </table>
 * 
 * <h3>Element Access, Copying and Duplication</h3>
 * 
 * <p>To access individual elements, or whole rows and columns, use the following
 * methods:<p>
 * 
 * <table class="my">
 * <tr><th>x.Method<th>Description
 * <tr><td>x.get(i,j)<td>Get element in row i and column j.
 * <tr><td>x.put(i, j, v)<td>Set element in row i and column j to value v
 * <tr><td>x.get(i)<td>Get the ith element of the matrix (traversing rows first).
 * <tr><td>x.put(i, v)<td>Set the ith element of the matrix (traversing rows first).
 * <tr><td>x.getColumn(i)<td>Get a copy of column i.
 * <tr><td>x.putColumn(i, c)<td>Put matrix c into column i.
 * <tr><td>x.getRow(i)<td>Get a copy of row i.
 * <tr><td>x.putRow(i, c)<td>Put matrix c into row i.
 * <tr><td>x.swapColumns(i, j)<td>Swap the contents of columns i and j.
 * <tr><td>x.swapRows(i, j)<td>Swap the contents of columns i and j.
 * </table>
 * 
 * <p>For <tt>get</tt> and <tt>put</tt>, you can also pass integer arrays,
 * FloatMatrix objects, or Range objects, which then specify the indices used 
 * as follows:
 * 
 * <ul>
 * <li><em>integer array:</em> the elements will be used as indices.
 * <li><em>FloatMatrix object:</em> non-zero entries specify the indices.
 * <li><em>Range object:</em> see below.
 * </ul>
 * 
 * <p>When using <tt>put</tt> with multiple indices, the assigned object must
 * have the correct size or be a scalar.</p>
 *
 * <p>There exist the following Range objects. The Class <tt>RangeUtils</tt> also
 * contains the a number of handy helper methods for constructing these ranges.</p>
 * <table class="my">
 * <tr><th>Class <th>RangeUtils method <th>Indices
 * <tr><td>AllRange <td>all() <td>All legal indices.
 * <tr><td>PointRange <td>point(i) <td> A single point.
 * <tr><td>IntervalRange <td>interval(a, b)<td> All indices from a to b (inclusive)
 * <tr><td rowspan=3>IndicesRange <td>indices(int[])<td> The specified indices.
 * <tr><td>indices(FloatMatrix)<td>The specified indices.
 * <tr><td>find(FloatMatrix)<td>The non-zero entries of the matrix.
 * </table>
 * 
 * <p>The following methods can be used for duplicating and copying matrices.</p>
 * 
 * <table class="my">
 * <tr><th>Method<th>Description
 * <tr><td>x.dup()<td>Get a copy of x.
 * <tr><td>x.copy(y)<td>Copy the contents of y to x (possible resizing x).
 * </table>
 *    
 * <h3>Size and Shape</h3>
 * 
 * <p>The following methods permit to acces the size of a matrix and change its size or shape.</p>
 * 
 * <table class="my">
 * <tr><th>x.Method<th>Description
 * <tr><td>x.rows<td>Number of rows.
 * <tr><td>x.columns<td>Number of columns.
 * <tr><td>x.length<td>Total number of elements.
 * <tr><td>x.isEmpty()<td>Checks whether rows == 0 and columns == 0.
 * <tr><td>x.isRowVector()<td>Checks whether rows == 1.
 * <tr><td>x.isColumnVector()<td>Checks whether columns == 1.
 * <tr><td>x.isVector()<td>Checks whether rows == 1 or columns == 1.
 * <tr><td>x.isSquare()<td>Checks whether rows == columns.
 * <tr><td>x.isScalar()<td>Checks whether length == 1.
 * <tr><td>x.resize(r, c)<td>Resize the matrix to r rows and c columns, discarding the content.
 * <tr><td>x.reshape(r, c)<td>Resize the matrix to r rows and c columns.<br> Number of elements must not change.
 * </table>
 * 
 * <p>The size is stored in the <tt>rows</tt> and <tt>columns</tt> member variables.
 * The total number of elements is stored in <tt>length</tt>. Do not change these
 * values unless you know what you're doing!</p>
 * 
 * <h3>Arithmetics</h3>
 * 
 * <p>The usual arithmetic operations are implemented. Each operation exists in a
 * in-place version, recognizable by the suffix <tt>"i"</tt>, to which you can supply
 * the result matrix (or <tt>this</tt> is used, if missing). Using in-place operations
 * can also lead to a smaller memory footprint, as the number of temporary objects
 * which are directly garbage collected again is reduced.</p>
 * 
 * <p>Whenever you specify a result vector, the result vector must already have the
 * correct dimensions.</p>
 * 
 * <p>For example, you can add two matrices using the <tt>add</tt> method. If you want
 * to store the result in of <tt>x + y</tt> in <tt>z</tt>, type
 * <span class=code>
 * x.addi(y, z)   // computes x = y + z.
 * </span>
 * Even in-place methods return the result, such that you can easily chain in-place methods,
 * for example:
 * <span class=code>
 * x.addi(y).addi(z) // computes x += y; x += z
 * </span></p> 
 *
 * <p>Methods which operate element-wise only make sure that the length of the matrices
 * is correct. Therefore, you can add a 3 * 3 matrix to a 1 * 9 matrix, for example.</p>
 * 
 * <p>Finally, there exist versions which take floats instead of FloatMatrix Objects
 * as arguments. These then compute the operation with the same value as the
 * right-hand-side. The same effect can be achieved by passing a FloatMatrix with
 * exactly one element.</p>
 * 
 * <table class="my">
 * <tr><th>Operation <th>Method <th>Comment
 * <tr><td>x + y <td>x.add(y)             <td>
 * <tr><td>x - y <td>x.sub(y), y.rsub(x) <td>rsub subtracts left from right hand side
 * <tr><td rowspan=3>x * y    <td>x.mul(y) <td>element-wise multiplication 
 * <tr>                                   <td>x.mmul(y)<td>matrix-matrix multiplication
 * <tr>                                   <td>x.dot(y) <td>scalar-product
 * <tr><td>x / y <td>x.div(y), y.rdiv(x) <td>rdiv divides right hand side by left hand side.
 * <tr><td>- x    <td>x.neg()                   <td>
 * </table>
 * 
 * <p>There also exist operations which work on whole columns or rows.</p>
 * 
 * <table class="my">
 * <tr><th>Method <th>Description
 * <tr><td>x.addRowVector<td>adds a vector to each row (addiRowVector works in-place)
 * <tr><td>x.addColumnVector<td>adds a vector to each column
 * <tr><td>x.subRowVector<td>subtracts a vector from each row
 * <tr><td>x.subColumnVector<td>subtracts a vector from each column
 * <tr><td>x.mulRow<td>Multiplies a row by a scalar
 * <tr><td>x.mulColumn<td>multiplies a row by a column
 * </table>
 * 
 * <p>In principle, you could achieve the same result by first calling getColumn(), 
 * adding, and then calling putColumn, but these methods are much faster.</p>
 * 
 * <p>The following comparison operations are available</p>
 *  
 * <table class="my">
 * <tr><th>Operation <th>Method
 * <tr><td>x &lt; y           <td>x.lt(y)
 * <tr><td>x &lt;= y    <td>x.le(y)
 * <tr><td>x &gt; y           <td>x.gt(y)
 * <tr><td>x &gt;= y    <td>x.ge(y)
 * <tr><td>x == y       <td>x.eq(y)
 * <tr><td>x != y       <td>x.ne(y)
 * </table>
 *
 * <p> Logical operations are also supported. For these operations, a value different from
 * zero is treated as "true" and zero is treated as "false". All operations are carried
 * out elementwise.</p>
 * 
 * <table class="my">
 * <tr><th>Operation <th>Method
 * <tr><td>x & y  <td>x.and(y)
 * <tr><td>x | y  <td>x.or(y)
 * <tr><td>x ^ y  <td>x.xor(y)
 * <tr><td>! x          <td>x.not()
 * </table>
 * 
 * <p>Finally, there are a few more methods to compute various things:</p>
 * 
 * <table class="my">
 * <tr><th>Method <th>Description
 * <tr><td>x.max() <td>Return maximal element
 * <tr><td>x.argmax() <td>Return index of largest element
 * <tr><td>x.min() <td>Return minimal element
 * <tr><td>x.argmin() <td>Return index of largest element
 * <tr><td>x.columnMins() <td>Return column-wise minima
 * <tr><td>x.columnArgmins() <td>Return column-wise index of minima
 * <tr><td>x.columnMaxs() <td>Return column-wise maxima
 * <tr><td>x.columnArgmaxs() <td>Return column-wise index of maxima
 * </table>
 * 
 * @author Mikio Braun, Johannes Schaback
 */
00274 public class FloatMatrix {

    /** Number of rows. */
00277     public int rows;
    /** Number of columns. */
00279     public int columns;
    /** Total number of elements (for convenience). */
00281     public int length;
    /** The actual data stored by rows (that is, row 0, row 1...). */
00283     public float[] data = null; // rows are contiguous
    public static final FloatMatrix EMPTY = new FloatMatrix();

    /**************************************************************************
     *
     * Constructors and factory functions
     *
     **************************************************************************/
    /** Create a new matrix with <i>newRows</i> rows, <i>newColumns</i> columns
     * using <i>newData></i> as the data. The length of the data is not checked!
     */
00294     public FloatMatrix(int newRows, int newColumns, float... newData) {
        rows = newRows;
        columns = newColumns;
        length = rows * columns;

        if (newData != null && newData.length != newRows * newColumns) {
            throw new IllegalArgumentException(
                    "Passed data must match matrix dimensions.");
        }

        data = newData;
    //System.err.printf("%d * %d matrix created\n", rows, columns);
    }

    /**
     * Creates a new <i>n</i> times <i>m</i> <tt>FloatMatrix</tt>.
     * @param newRows the number of rows (<i>n</i>) of the new matrix.
     * @param newColumns the number of columns (<i>m</i>) of the new matrix.
     */
00313     public FloatMatrix(int newRows, int newColumns) {
        this(newRows, newColumns, new float[newRows * newColumns]);
    }

    /**
     * Creates a new <tt>FloatMatrix</tt> of size 0 times 0.
     */
00320     public FloatMatrix() {
        this(0, 0, (float[]) null);
    }

    /**
     * Create a Matrix of length <tt>len</tt>. By default, this creates a row vector.
     * @param len
     */
00328     public FloatMatrix(int len) {
        this(len, 1, new float[len]);
    }

    public FloatMatrix(float[] newData) {
        this(newData.length);
        data = newData;
    }

    /**
     * Creates a new matrix by reading it from a file.
     * @param filename the path and name of the file to read the matrix from
     * @throws IOException
     */
00342     public FloatMatrix(String filename) throws IOException {
        load(filename);
    }

    /**
     * Creates a new <i>n</i> times <i>m</i> <tt>FloatMatrix</tt> from
     * the given <i>n</i> times <i>m</i> 2D data array. The first dimension of the array makes the
     * rows (<i>n</i>) and the second dimension the columns (<i>m</i>). For example, the
     * given code <br/><br/>
     * <code>new FloatMatrix(new float[][]{{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}}).print();</code><br/><br/>
     * will constructs the following matrix:
     * <pre>
     * 1.0f 2.0f  3.0f
     * 4.0f 5.0f  6.0f
     * 7.0f 8.0f  9.0f
     * </pre>.
     * @param data <i>n</i> times <i>m</i> data array
     */
00360     public FloatMatrix(float[][] data) {
        this(data.length, data[0].length);

        for (int r = 0; r < rows; r++) {
            assert (data[r].length == columns);
        }

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                put(r, c, data[r][c]);
            }
        }
    }

    /** Create matrix with random values uniformly in 0..1. */
00375     public static FloatMatrix rand(int rows, int columns) {
        FloatMatrix m = new FloatMatrix(rows, columns);

        java.util.Random r = new java.util.Random();
        for (int i = 0; i < rows * columns; i++) {
            m.data[i] = r.nextFloat();
        }

        return m;
    }

    /** Creates a row vector with random values uniformly in 0..1. */
00387     public static FloatMatrix rand(int len) {
        return rand(len, 1);
    }

    /** Create matrix with normally distributed random values. */
00392     public static FloatMatrix randn(int rows, int columns) {
        FloatMatrix m = new FloatMatrix(rows, columns);

        java.util.Random r = new java.util.Random();
        for (int i = 0; i < rows * columns; i++) {
            m.data[i] = (float) r.nextGaussian();
        }

        return m;
    }

    /** Create row vector with normally distributed random values. */
00404     public static FloatMatrix randn(int len) {
        return randn(len, 1);
    }

    /** Creates a new matrix in which all values are equal 0. */
00409     public static FloatMatrix zeros(int rows, int columns) {
        return new FloatMatrix(rows, columns);
    }

    /** Creates a row vector of given length. */
00414     public static FloatMatrix zeros(int length) {
        return zeros(length, 1);
    }

    /** Creates a new matrix in which all values are equal 1. */
00419     public static FloatMatrix ones(int rows, int columns) {
        FloatMatrix m = new FloatMatrix(rows, columns);

        for (int i = 0; i < rows * columns; i++) {
            m.put(i, 1.0f);
        }

        return m;
    }

    /** Creates a row vector with all elements equal to 1. */
00430     public static FloatMatrix ones(int length) {
        return ones(length, 1);
    }

    /** Construct a new n-by-n identity matrix. */
00435     public static FloatMatrix eye(int n) {
        FloatMatrix m = new FloatMatrix(n, n);

        for (int i = 0; i < n; i++) {
            m.put(i, i, 1.0f);
        }

        return m;
    }

    /**
     * Creates a new matrix where the values of the given vector are the diagonal values of
     * the matrix.
     */
00449     public static FloatMatrix diag(FloatMatrix x) {
        FloatMatrix m = new FloatMatrix(x.length, x.length);

        for (int i = 0; i < x.length; i++) {
            m.put(i, i, x.get(i));
        }

        return m;
    }

    /**
     * Create a 1-by-1 matrix. For many operations, this matrix functions like a
     * normal float.
     */
00463     public static FloatMatrix scalar(float s) {
        FloatMatrix m = new FloatMatrix(1, 1);
        m.put(0, 0, s);
        return m;
    }

    /** Test whether a matrix is scalar. */
00470     public boolean isScalar() {
        return length == 1;
    }

    /** Return the first element of the matrix. */
00475     public float scalar() {
        return get(0);
    }

    /**
     * Concatenates two matrices horizontally. Matrices must have identical
     * numbers of rows.
     */
00483     public static FloatMatrix concatHorizontally(FloatMatrix A, FloatMatrix B) {
        if (A.rows != B.rows) {
            throw new SizeException("Matrices don't have same number of rows.");
        }

        FloatMatrix result = new FloatMatrix(A.rows, A.columns + B.columns);
        SimpleBlas.copy(A, result);
        JavaBlas.rcopy(B.length, B.data, 0, 1, result.data, A.length, 1);
        return result;
    }

    /**
     * Concatenates two matrices vertically. Matrices must have identical
     * numbers of columns.
     */
00498     public static FloatMatrix concatVertically(FloatMatrix A, FloatMatrix B) {
        if (A.columns != B.columns) {
            throw new SizeException("Matrices don't have same number of columns (" + A.columns + " != " + B.columns + ".");
        }

        FloatMatrix result = new FloatMatrix(A.rows + B.rows, A.columns);

        for (int i = 0; i < A.columns; i++) {
            JavaBlas.rcopy(A.rows, A.data, A.index(0, i), 1, result.data, result.index(0, i), 1);
            JavaBlas.rcopy(B.rows, B.data, B.index(0, i), 1, result.data, result.index(A.rows, i), 1);
        }

        return result;
    }

    /**************************************************************************
     * Working with slices (Man! 30+ methods just to make this a bit flexible...)
     */
    /** Get all elements specified by the linear indices. */
00517     public FloatMatrix get(int[] indices) {
        FloatMatrix result = new FloatMatrix(indices.length);

        for (int i = 0; i < indices.length; i++) {
            result.put(i, get(indices[i]));
        }

        return result;
    }

    /** Get all elements for a given row and the specified columns. */
00528     public FloatMatrix get(int r, int[] indices) {
        FloatMatrix result = new FloatMatrix(1, indices.length);

        for (int i = 0; i < indices.length; i++) {
            result.put(i, get(r, indices[i]));
        }

        return result;
    }

    /** Get all elements for a given column and the specified rows. */
00539     public FloatMatrix get(int[] indices, int c) {
        FloatMatrix result = new FloatMatrix(indices.length, c);

        for (int i = 0; i < indices.length; i++) {
            result.put(i, get(indices[i], c));
        }

        return result;
    }

    /** Get all elements from the specified rows and columns. */
00550     public FloatMatrix get(int[] rindices, int[] cindices) {
        FloatMatrix result = new FloatMatrix(rindices.length, cindices.length);

        for (int i = 0; i < rindices.length; i++) {
            for (int j = 0; j < cindices.length; j++) {
                result.put(i, j, get(rindices[i], cindices[j]));
            }
        }

        return result;
    }

    /** Get elements from specified rows and columns. */
00563     public FloatMatrix get(Range rs, Range cs) {
        rs.init(0, rows - 1);
        cs.init(0, columns - 1);
        FloatMatrix result = new FloatMatrix(rs.length(), cs.length());

        for (; !rs.hasMore(); rs.next()) {
            for (; !cs.hasMore(); cs.next()) {
                result.put(rs.index(), cs.index(), get(rs.value(), cs.value()));
            }
        }

        return result;
    }

    /** Get elements specified by the non-zero entries of the passed matrix. */
00578     public FloatMatrix get(FloatMatrix indices) {
        return get(indices.findIndices());
    }

    /**
     * Get elements from a row and columns as specified by the non-zero entries of
     * a matrix.
     */
00586     public FloatMatrix get(int r, FloatMatrix indices) {
        return get(r, indices.findIndices());
    }

    /**
     * Get elements from a column and rows as specified by the non-zero entries of
     * a matrix.
     */
00594     public FloatMatrix get(FloatMatrix indices, int c) {
        return get(indices.findIndices(), c);
    }

    /**
     * Get elements from columns and rows as specified by the non-zero entries of
     * the passed matrices.
     */
00602     public FloatMatrix get(FloatMatrix rindices, FloatMatrix cindices) {
        return get(rindices.findIndices(), cindices.findIndices());
    }

    /** Return all elements with linear index a, a + 1, ..., b - 1.*/
00607     public FloatMatrix getRange(int a, int b) {
        FloatMatrix result = new FloatMatrix(b - a);

        for (int k = 0; k < b - a; k++) {
            result.put(k, get(a + k));
        }

        return result;
    }

    /** Get elements from a row and columns <tt>a</tt> to <tt>b</tt>. */
00618     public FloatMatrix getColumnRange(int r, int a, int b) {
        FloatMatrix result = new FloatMatrix(1, b - a);

        for (int k = 0; k < b - a; k++) {
            result.put(k, get(r, a + k));
        }

        return result;
    }

    /** Get elements from a column and rows <tt>a/tt> to <tt>b</tt>. */
00629     public FloatMatrix getRowRange(int a, int b, int c) {
        FloatMatrix result = new FloatMatrix(b - a);

        for (int k = 0; k < b - a; k++) {
            result.put(k, get(a + k, c));
        }

        return result;
    }

    /**
     * Get elements from rows <tt>ra</tt> to <tt>rb</tt> and
     * columns <tt>ca</tt> to <tt>cb</tt>.
     */
00643     public FloatMatrix getRange(int ra, int rb, int ca, int cb) {
        FloatMatrix result = new FloatMatrix(rb - ra, cb - ca);

        for (int i = 0; i < rb - ra; i++) {
            for (int j = 0; j < cb - ca; j++) {
                result.put(i, j, get(ra + i, ca + j));
            }
        }

        return result;
    }

    /** Get whole rows from the passed indices. */
00656     public FloatMatrix getRows(int[] rindices) {
        FloatMatrix result = new FloatMatrix(rindices.length, columns);
        for (int i = 0; i < rindices.length; i++) {
            JavaBlas.rcopy(columns, data, index(rindices[i], 0), rows, result.data, result.index(i, 0), result.rows);
        }
        return result;
    }

    /** Get whole rows as specified by the non-zero entries of a matrix. */
00665     public FloatMatrix getRows(FloatMatrix rindices) {
        return getRows(rindices.findIndices());
    }

    /** Get whole columns from the passed indices. */
00670     public FloatMatrix getColumns(int[] cindices) {
        FloatMatrix result = new FloatMatrix(rows, cindices.length);
        for (int i = 0; i < cindices.length; i++) {
            JavaBlas.rcopy(rows, data, index(0, cindices[i]), 1, result.data, result.index(0, i), 1);
        }
        return result;
    }

    /** Get whole columns as specified by the non-zero entries of a matrix. */
00679     public FloatMatrix getColumns(FloatMatrix cindices) {
        return getColumns(cindices.findIndices());
    }

    /**
     * Assert that the matrix has a certain length.
     * @throws SizeException
     */
00687     public void checkLength(int l) {
        if (length != l) {
            throw new SizeException("Matrix does not have the necessary length (" + length + " != " + l + ").");
        }
    }

    /**
     * Asserts that the matrix has a certain number of rows.
     * @throws SizeException
     */
00697     public void checkRows(int r) {
        if (rows != r) {
            throw new SizeException("Matrix does not have the necessary number of rows (" + rows + " != " + r + ").");
        }
    }

    /**
     * Asserts that the amtrix has a certain number of columns.
     * @throws SizeException
     */
00707     public void checkColumns(int c) {
        if (columns != c) {
            throw new SizeException("Matrix does not have the necessary number of columns (" + columns + " != " + c + ").");
        }
    }

    /** Set elements in linear ordering in the specified indices. */
00714     public FloatMatrix put(int[] indices, FloatMatrix x) {
        if (x.isScalar()) {
            return put(indices, x.scalar());
        }
        x.checkLength(indices.length);

        for (int i = 0; i < indices.length; i++) {
            put(indices[i], x.get(i));
        }

        return this;
    }

    /** Set multiple elements in a row. */
00728     public FloatMatrix put(int r, int[] indices, FloatMatrix x) {
        if (x.isScalar()) {
            return put(r, indices, x.scalar());
        }
        x.checkColumns(indices.length);

        for (int i = 0; i < indices.length; i++) {
            put(r, indices[i], x.get(i));
        }

        return this;
    }

    /** Set multiple elements in a row. */
00742     public FloatMatrix put(int[] indices, int c, FloatMatrix x) {
        if (x.isScalar()) {
            return put(indices, c, x.scalar());
        }
        x.checkRows(indices.length);

        for (int i = 0; i < indices.length; i++) {
            put(indices[i], c, x.get(i));
        }

        return this;
    }

    /** Put a sub-matrix as specified by the indices. */
00756     public FloatMatrix put(int[] rindices, int[] cindices, FloatMatrix x) {
        if (x.isScalar()) {
            return put(rindices, cindices, x.scalar());
        }
        x.checkRows(rindices.length);
        x.checkColumns(cindices.length);

        for (int i = 0; i < rindices.length; i++) {
            for (int j = 0; j < cindices.length; j++) {
                put(rindices[i], cindices[j], x.get(i, j));
            }
        }

        return this;
    }

    /** Put a matrix into specified indices. */
00773     public FloatMatrix put(Range rs, Range cs, FloatMatrix x) {
        rs.init(0, rows - 1);
        cs.init(0, columns - 1);

        x.checkRows(rs.length());
        x.checkColumns(cs.length());

        for (; rs.hasMore(); rs.next()) {
            for (; cs.hasMore(); cs.next()) {
                put(rs.value(), cs.value(), x.get(rs.index(), cs.index()));
            }
        }

        return this;
    }

    /** Put a single value into the specified indices (linear adressing). */
00790     public FloatMatrix put(int[] indices, float v) {
        for (int i = 0; i < indices.length; i++) {
            put(indices[i], v);
        }

        return this;
    }

    /** Put a single value into a row and the specified columns. */
00799     public FloatMatrix put(int r, int[] indices, float v) {
        for (int i = 0; i < indices.length; i++) {
            put(r, indices[i], v);
        }

        return this;
    }

    /** Put a single value into the specified rows of a column. */
00808     public FloatMatrix put(int[] indices, int c, float v) {
        for (int i = 0; i < indices.length; i++) {
            put(indices[i], c, v);
        }

        return this;
    }

    /** Put a single value into the specified rows and columns. */
00817     public FloatMatrix put(int[] rindices, int[] cindices, float v) {
        for (int i = 0; i < rindices.length; i++) {
            for (int j = 0; j < cindices.length; j++) {
                put(rindices[i], cindices[j], v);
            }
        }

        return this;
    }

    /**
     * Put a sub-matrix into the indices specified by the non-zero entries
     * of <tt>indices</tt> (linear adressing).
     */
00831     public FloatMatrix put(FloatMatrix indices, FloatMatrix v) {
        return put(indices.findIndices(), v);
    }

    /** Put a sub-vector into the specified columns (non-zero entries of <tt>indices</tt>) of a row. */
00836     public FloatMatrix put(int r, FloatMatrix indices, FloatMatrix v) {
        return put(r, indices.findIndices(), v);
    }

    /** Put a sub-vector into the specified rows (non-zero entries of <tt>indices</tt>) of a column. */
00841     public FloatMatrix put(FloatMatrix indices, int c, FloatMatrix v) {
        return put(indices.findIndices(), c, v);
    }

    /**
     * Put a sub-matrix into the specified rows and columns (non-zero entries of
     * <tt>rindices</tt> and <tt>cindices</tt>.
     */
00849     public FloatMatrix put(FloatMatrix rindices, FloatMatrix cindices, FloatMatrix v) {
        return put(rindices.findIndices(), cindices.findIndices(), v);
    }

    /**
     * Put a single value into the elements specified by the non-zero
     * entries of <tt>indices</tt> (linear adressing).
     */
00857     public FloatMatrix put(FloatMatrix indices, float v) {
        return put(indices.findIndices(), v);
    }

    /**
     * Put a single value into the specified columns (non-zero entries of
     * <tt>indices</tt>) of a row.
     */
00865     public FloatMatrix put(int r, FloatMatrix indices, float v) {
        return put(r, indices.findIndices(), v);
    }

    /**
     * Put a single value into the specified rows (non-zero entries of
     * <tt>indices</tt>) of a column.
     */
00873     public FloatMatrix put(FloatMatrix indices, int c, float v) {
        return put(indices.findIndices(), c, v);
    }

    /**
     * Put a single value in the specified rows and columns (non-zero entries
     * of <tt>rindices</tt> and <tt>cindices</tt>.
     */
00881     public FloatMatrix put(FloatMatrix rindices, FloatMatrix cindices, float v) {
        return put(rindices.findIndices(), cindices.findIndices(), v);
    }

    /** Find the linear indices of all non-zero elements. */
00886     public int[] findIndices() {
        int len = 0;
        for (int i = 0; i < length; i++) {
            if (get(i) != 0.0f) {
                len++;
            }
        }

        int[] indices = new int[len];
        int c = 0;

        for (int i = 0; i < length; i++) {
            if (get(i) != 0.0f) {
                indices[c++] = i;
            }
        }

        return indices;
    }

    /**************************************************************************
     * Basic operations (copying, resizing, element access)
     */
    /** Return transposed copy of this matrix. */
00910     public FloatMatrix transpose() {
        FloatMatrix result = new FloatMatrix(columns, rows);

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                result.put(j, i, get(i, j));
            }
        }

        return result;
    }

    /**
     * Compare two matrices. Returns true if and only if other is also a
     * FloatMatrix which has the same size and the maximal absolute
     * difference in matrix elements is smaller thatn 1e-6.
     */
00927     public boolean equals(Object o) {
        if (!(o instanceof FloatMatrix)) {
            return false;
        }

        FloatMatrix other = (FloatMatrix) o;

        if (!sameSize(other)) {
            return false;
        }

        FloatMatrix diff = MatrixFunctions.absi(sub(other));

        return diff.max() / (rows * columns) < 1e-6;
    }

    /** Resize the matrix. All elements will be set to zero. */
00944     public void resize(int newRows, int newColumns) {
        rows = newRows;
        columns = newColumns;
        length = newRows * newColumns;
        data = new float[rows * columns];
    }

    /** Reshape the matrix. Number of elements must not change. */
00952     public FloatMatrix reshape(int newRows, int newColumns) {
        if (length != newRows * newColumns) {
            throw new IllegalArgumentException(
                    "Number of elements must not change.");
        }

        rows = newRows;
        columns = newColumns;

        return this;
    }

    /** Generate a new matrix which has the given number of replications of this. */
00965     public FloatMatrix repmat(int rowMult, int columnMult) {
        FloatMatrix result = new FloatMatrix(rows * rowMult, columns * columnMult);

        for (int c = 0; c < columnMult; c++)
            for (int r = 0; r < rowMult; r++)
                for (int i = 0; i < rows; i++)
                    for (int j = 0; j < columns; j++)
                        result.put(r * rows + i, c * columns + j, get(i, j));
        return result;
    }

    /** Checks whether two matrices have the same size. */
00977     public boolean sameSize(FloatMatrix a) {
        return rows == a.rows && columns == a.columns;
    }

    /** Throws SizeException unless two matrices have the same size. */
00982     public void assertSameSize(FloatMatrix a) {
        if (!sameSize(a)) {
            throw new SizeException("Matrices must have the same size.");
        }
    }

    /** Checks whether two matrices can be multiplied (that is, number of columns of
     * this must equal number of rows of a. */
00990     public boolean multipliesWith(FloatMatrix a) {
        return columns == a.rows;
    }

    /** Throws SizeException unless matrices can be multiplied with one another. */
00995     public void assertMultipliesWith(FloatMatrix a) {
        if (!multipliesWith(a)) {
            throw new SizeException("Number of columns of left matrix must be equal to number of rows of right matrix.");
        }
    }

    /** Checks whether two matrices have the same length. */
01002     public boolean sameLength(FloatMatrix a) {
        return length == a.length;
    }

    /** Throws SizeException unless matrices have the same length. */
01007     public void assertSameLength(FloatMatrix a) {
        if (!sameLength(a)) {
            throw new SizeException("Matrices must have same length (is: " + length + " and " + a.length + ")");
        }
    }

    /** Copy FloatMatrix a to this. this a is resized if necessary. */
01014     public FloatMatrix copy(FloatMatrix a) {
        if (!sameSize(a)) {
            resize(a.rows, a.columns);
        }

        System.arraycopy(data, 0, a.data, 0, length);
        return a;
    }

    /**
     * Returns a duplicate of this matrix. Geometry is the same (including offsets, transpose, etc.),
     * but the buffer is not shared.
     */
01027     public FloatMatrix dup() {
        FloatMatrix out = new FloatMatrix(rows, columns);

        JavaBlas.rcopy(length, data, 0, 1, out.data, 0, 1);

        return out;
    }

    /** Swap two columns of a matrix. */
01036     public FloatMatrix swapColumns(int i, int j) {
        Blas.sswap(rows, data, index(0, i), 1, data, index(0, j), 1);
        return this;
    }

    /** Swap two rows of a matrix. */
01042     public FloatMatrix swapRows(int i, int j) {
        Blas.sswap(columns, data, index(i, 0), rows, data, index(j, 0), rows);
        return this;
    }

    /** Set matrix element */
01048     public FloatMatrix put(int rowIndex, int columnIndex, float value) {
        data[index(rowIndex, columnIndex)] = value;
        return this;
    }

    /** Retrieve matrix element */
01054     public float get(int rowIndex, int columnIndex) {
        return data[index(rowIndex, columnIndex)];
    }

    /** Get index of an element */
01059     public int index(int rowIndex, int columnIndex) {
        return rowIndex + rows * columnIndex;
    }

    /** Compute the row index of a linear index. */
01064     public int indexRows(int i) {
        return i / rows;
    }

    /** Compute the column index of a linear index. */
01069     public int indexColumns(int i) {
        return i - indexRows(i) * rows;
    }

    /** Get a matrix element (linear indexing). */
01074     public float get(int i) {
        return data[i];
    }

    /** Set a matrix element (linear indexing). */
01079     public FloatMatrix put(int i, float v) {
        data[i] = v;
        return this;
    }

    /** Set all elements to a value. */
01085     public FloatMatrix fill(float value) {
        for (int i = 0; i < length; i++) {
            put(i, value);
        }
        return this;
    }

    /** Get number of rows. */
01093     public int getRows() {
        return rows;
    }

    /** Get number of columns. */
01098     public int getColumns() {
        return columns;
    }

    /** Get total number of elements. */
01103     public int getLength() {
        return length;
    }

    /** Checks whether the matrix is empty. */
01108     public boolean isEmpty() {
        return columns == 0 || rows == 0;
    }

    /** Checks whether the matrix is square. */
01113     public boolean isSquare() {
        return columns == rows;
    }

    /** Throw SizeException unless matrix is square. */
01118     public void assertSquare() {
        if (!isSquare()) {
            throw new SizeException("Matrix must be square!");
        }
    }

    /** Checks whether the matrix is a vector. */
01125     public boolean isVector() {
        return columns == 1 || rows == 1;
    }

    /** Checks whether the matrix is a row vector. */
01130     public boolean isRowVector() {
        return rows == 1;
    }

    /** Checks whether the matrix is a column vector. */
01135     public boolean isColumnVector() {
        return columns == 1;
    }

    /** Returns the diagonal of the matrix. */
01140     public FloatMatrix diag() {
        assertSquare();
        FloatMatrix d = new FloatMatrix(rows);
        JavaBlas.rcopy(rows, data, 0, rows + 1, d.data, 0, 1);
        return d;
    }

    /** Pretty-print this matrix to <tt>System.out</tt>. */
01148     public void print() {
        System.out.println(toString());
    }

    /** Generate string representation of the matrix. */
    @Override
01154     public String toString() {
        StringBuilder s = new StringBuilder();

        s.append("[");

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                s.append(get(i, j));
                if (j < columns - 1) {
                    s.append(", ");
                }
            }
            if (i < rows - 1) {
                s.append("; ");
            }
        }

        s.append("]");

        return s.toString();
    }

    /**
     * Generate string representation of the matrix, with specified
     * format for the entries. For example, <code>x.toString("%.1f")</code>
     * generates a string representations having only one position after the
     * decimal point.
     */
01182     public String toString(String fmt) {
        StringWriter s = new StringWriter();
        PrintWriter p = new PrintWriter(s);

        p.print("[");

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                p.printf(fmt, get(r, c));
                if (c < columns - 1) {
                    p.print(", ");
                }
            }
            if (r < rows - 1) {
                p.print("; ");
            }
        }

        p.print("]");

        return s.toString();
    }

    /** Converts the matrix to a one-dimensional array of floats. */
01206     public float[] toArray() {
        float[] array = new float[length];

        System.arraycopy(data, 0, array, 0, length);

        return array;
    }

    /** Converts the matrix to a two-dimensional array of floats. */
01215     public float[][] toArray2() {
        float[][] array = new float[rows][columns];

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                array[r][c] = get(r, c);
            }
        }

        return array;
    }

    /** Converts the matrix to a one-dimensional array of integers. */
01228     public int[] toIntArray() {
        int[] array = new int[length];

        for (int i = 0; i < length; i++) {
            array[i] = (int) Math.rint(get(i));
        }

        return array;
    }

    /** Convert the matrix to a two-dimensional array of integers. */
01239     public int[][] toIntArray2() {
        int[][] array = new int[rows][columns];

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                array[r][c] = (int) Math.rint(get(r, c));
            }
        }

        return array;
    }

    /** Convert the matrix to a one-dimensional array of boolean values. */
01252     public boolean[] toBooleanArray() {
        boolean[] array = new boolean[length];

        for (int i = 0; i < length; i++) {
            array[i] = get(i) != 0.0f ? true : false;
        }

        return array;
    }

    /** Convert the matrix to a two-dimensional array of boolean values. */
01263     public boolean[][] toBooleanArray2() {
        boolean[][] array = new boolean[rows][columns];

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                array[r][c] = get(r, c) != 0.0f ? true : false;
            }
        }

        return array;
    }

    /** Convert matrix to FloatMatrix. */
01276     public FloatMatrix toFloatMatrix() {
        FloatMatrix result = new FloatMatrix(rows, columns);

        for (int c = 0; c < columns; c++) {
            for (int r = 0; r < rows; r++) {
                result.put(r, c, (float) get(r, c));
            }
        }

        return result;
    }

    /**************************************************************************
     * Arithmetic Operations
     */
    /**
     * Ensures that the result vector has the same length as this. If not,
     * resizing result is tried, which fails if result == this or result == other.
     */
01295     private void ensureResultLength(FloatMatrix other, FloatMatrix result) {
        if (!sameLength(result)) {
            if (result == this || result == other) {
                throw new SizeException("Cannot resize result matrix because it is used in-place.");
            }
            result.resize(rows, columns);
        }
    }

    /** Add two matrices (in-place). */
01305     public FloatMatrix addi(FloatMatrix other, FloatMatrix result) {
        if (other.isScalar()) {
            return addi(other.scalar(), result);
        }
        if (isScalar()) {
            return other.addi(scalar(), result);
        }

        assertSameLength(other);
        ensureResultLength(other, result);

        if (result == this) {
            SimpleBlas.axpy(1.0f, other, result);
        } else if (result == other) {
            SimpleBlas.axpy(1.0f, this, result);
        } else {
            /*SimpleBlas.copy(this, result);
            SimpleBlas.axpy(1.0f, other, result);*/
            JavaBlas.rzgxpy(length, result.data, data, other.data);
        }

        return result;
    }

    /** Add a scalar to a matrix (in-place). */
01330     public FloatMatrix addi(float v, FloatMatrix result) {
        ensureResultLength(null, result);

        for (int i = 0; i < length; i++) {
            result.put(i, get(i) + v);
        }
        return result;
    }

    /** Subtract two matrices (in-place). */
01340     public FloatMatrix subi(FloatMatrix other, FloatMatrix result) {
        if (other.isScalar()) {
            return subi(other.scalar(), result);
        }
        if (isScalar()) {
            return other.rsubi(scalar(), result);
        }

        assertSameLength(other);
        ensureResultLength(other, result);

        if (result == this) {
            SimpleBlas.axpy(-1.0f, other, result);
        } else if (result == other) {
            SimpleBlas.scal(-1.0f, result);
            SimpleBlas.axpy(1.0f, this, result);
        } else {
            SimpleBlas.copy(this, result);
            SimpleBlas.axpy(-1.0f, other, result);
        }
        return result;
    }

    /** Subtract a scalar from a matrix (in-place). */
01364     public FloatMatrix subi(float v, FloatMatrix result) {
        ensureResultLength(null, result);

        for (int i = 0; i < length; i++) {
            result.put(i, get(i) - v);
        }
        return result;
    }

    /**
     * Subtract two matrices, but subtract first from second matrix, that is,
     * compute <em>result = other - this</em> (in-place).
     * */
01377     public FloatMatrix rsubi(FloatMatrix other, FloatMatrix result) {
        return other.subi(this, result);
    }

    /** Subtract a matrix from a scalar (in-place). */
01382     public FloatMatrix rsubi(float a, FloatMatrix result) {
        ensureResultLength(null, result);

        for (int i = 0; i < length; i++) {
            result.put(i, a - get(i));
        }
        return result;
    }

    /** Elementwise multiplication (in-place). */
01392     public FloatMatrix muli(FloatMatrix other, FloatMatrix result) {
        if (other.isScalar()) {
            return muli(other.scalar(), result);
        }
        if (isScalar()) {
            return other.muli(scalar(), result);
        }

        assertSameLength(other);
        ensureResultLength(other, result);

        for (int i = 0; i < length; i++) {
            result.put(i, get(i) * other.get(i));
        }
        return result;
    }

    /** Elementwise multiplication with a scalar (in-place). */
01410     public FloatMatrix muli(float v, FloatMatrix result) {
        ensureResultLength(null, result);

        for (int i = 0; i < length; i++) {
            result.put(i, get(i) * v);
        }
        return result;
    }

    /** Matrix-matrix multiplication (in-place). */
01420     public FloatMatrix mmuli(FloatMatrix other, FloatMatrix result) {
        if (other.isScalar()) {
            return muli(other.scalar(), result);
        }
        if (isScalar()) {
            return other.muli(scalar(), result);
        }

        /* check sizes and resize if necessary */
        assertMultipliesWith(other);
        if (result.rows != rows || result.columns != other.columns) {
            if (result != this && result != other) {
                result.resize(rows, other.columns);
            } else {
                throw new SizeException("Cannot resize result matrix because it is used in-place.");
            }
        }

        if (result == this || result == other) {
            /* actually, blas cannot do multiplications in-place. Therefore, we will fake by
             * allocating a temporary object on the side and copy the result later.
             */
            FloatMatrix temp = new FloatMatrix(result.rows, result.columns);
            if (other.columns == 1) {
                SimpleBlas.gemv(1.0f, this, other, 0.0f, temp);
            } else {
                SimpleBlas.gemm(1.0f, this, other, 0.0f, temp);
            }
            SimpleBlas.copy(temp, result);
        } else {
            if (other.columns == 1) {
                SimpleBlas.gemv(1.0f, this, other, 0.0f, result);
            } else {
                SimpleBlas.gemm(1.0f, this, other, 0.0f, result);
            }
        }
        return result;
    }

    /** Matrix-matrix multiplication with a scalar (for symmetry, does the
     * same as <code>muli(scalar)</code> (in-place).
     */
01462     public FloatMatrix mmuli(float v, FloatMatrix result) {
        return muli(v, result);
    }

    /** Elementwise division (in-place). */
01467     public FloatMatrix divi(FloatMatrix other, FloatMatrix result) {
        if (other.isScalar()) {
            return divi(other.scalar(), result);
        }
        if (isScalar()) {
            return other.rdivi(scalar(), result);
        }

        assertSameLength(other);
        ensureResultLength(other, result);

        for (int i = 0; i < length; i++) {
            result.put(i, get(i) / other.get(i));
        }
        return result;
    }

    /** Elementwise division with a scalar (in-place). */
01485     public FloatMatrix divi(float a, FloatMatrix result) {
        ensureResultLength(null, result);

        for (int i = 0; i < length; i++) {
            result.put(i, get(i) / a);
        }
        return result;
    }

    /**
     * Elementwise division, with operands switched. Computes
     * <code>result = other / this</code> (in-place). */
01497     public FloatMatrix rdivi(FloatMatrix other, FloatMatrix result) {
        return other.divi(this, result);
    }

    /** (Elementwise) division with a scalar, with operands switched. Computes
     * <code>result = a / this</code> (in-place). */
01503     public FloatMatrix rdivi(float a, FloatMatrix result) {
        ensureResultLength(null, result);

        for (int i = 0; i < length; i++) {
            result.put(i, a / get(i));
        }
        return result;
    }

    /** Negate each element (in-place). */
01513     public FloatMatrix negi() {
        for (int i = 0; i < length; i++) {
            put(i, -get(i));
        }
        return this;
    }

    /** Negate each element. */
01521     public FloatMatrix neg() {
        return dup().negi();
    }

    /** Maps zero to 1.0f and all non-zero values to 0.0f (in-place). */
01526     public FloatMatrix noti() {
        for (int i = 0; i < length; i++) {
            put(i, get(i) == 0.0f ? 1.0f : 0.0f);
        }
        return this;
    }

    /** Maps zero to 1.0f and all non-zero values to 0.0f. */
01534     public FloatMatrix not() {
        return dup().noti();
    }

    /** Maps zero to 0.0f and all non-zero values to 1.0f (in-place). */
01539     public FloatMatrix truthi() {
        for (int i = 0; i < length; i++) {
            put(i, get(i) == 0.0f ? 0.0f : 1.0f);
        }
        return this;
    }

    /** Maps zero to 0.0f and all non-zero values to 1.0f. */
01547     public FloatMatrix truth() {
        return dup().truthi();
    }

    /****************************************************************
     * Rank one-updates
     */
    /** Computes a rank-1-update A = A + alpha * x * y'. */
01555     public FloatMatrix rankOneUpdate(float alpha, FloatMatrix x, FloatMatrix y) {
        if (rows != x.length) {
            throw new SizeException("Vector x has wrong length (" + x.length + " != " + rows + ").");
        }
        if (columns != y.length) {
            throw new SizeException("Vector y has wrong length (" + x.length + " != " + columns + ").");
        }

        SimpleBlas.ger(alpha, x, y, this);
        return this;
    }

    /** Computes a rank-1-update A = A + alpha * x * x'. */
01568     public FloatMatrix rankOneUpdate(float alpha, FloatMatrix x) {
        return rankOneUpdate(alpha, x, x);
    }

    /** Computes a rank-1-update A = A + x * x'. */
01573     public FloatMatrix rankOneUpdate(FloatMatrix x) {
        return rankOneUpdate(1.0f, x, x);
    }

    /** Computes a rank-1-update A = A + x * y'. */
01578     public FloatMatrix rankOneUpdate(FloatMatrix x, FloatMatrix y) {
        return rankOneUpdate(1.0f, x, y);
    }

    /****************************************************************
     * Logical operations
     */
    /** Returns the minimal element of the matrix. */
01586     public float min() {
        if (isEmpty()) {
            return Float.POSITIVE_INFINITY;
        }
        float v = Float.POSITIVE_INFINITY;
        for (int i = 0; i < length; i++) {
            if (!Float.isNaN(get(i)) && get(i) < v) {
                v = get(i);
            }
        }

        return v;
    }

    /**
     * Returns the linear index of the minimal element. If there are
     * more than one elements with this value, the first one is returned.
     */
01604     public int argmin() {
        if (isEmpty()) {
            return -1;
        }
        float v = Float.POSITIVE_INFINITY;
        int a = -1;
        for (int i = 0; i < length; i++) {
            if (!Float.isNaN(get(i)) && get(i) < v) {
                v = get(i);
                a = i;
            }
        }

        return a;
    }

    /**
     * Computes the minimum between two matrices. Returns the smaller of the
     * corresponding elements in the matrix (in-place).
     */
01624     public FloatMatrix mini(FloatMatrix other, FloatMatrix result) {
        if (result == this) {
            for (int i = 0; i < length; i++) {
                if (get(i) > other.get(i)) {
                    put(i, other.get(i));
                }
            }
        } else {
            for (int i = 0; i < length; i++) {
                if (get(i) > other.get(i)) {
                    result.put(i, other.get(i));
                } else {
                    result.put(i, get(i));
                }
            }
        }
        return this;
    }

    /**
     * Computes the minimum between two matrices. Returns the smaller of the
     * corresponding elements in the matrix (in-place on this).
     */
01647     public FloatMatrix mini(FloatMatrix other) {
        return mini(other, this);
    }

    /**
     * Computes the minimum between two matrices. Returns the smaller of the
     * corresponding elements in the matrix (in-place on this).
     */
01655     public FloatMatrix min(FloatMatrix other) {
        return mini(other, new FloatMatrix(rows, columns));
    }

    public FloatMatrix mini(float v, FloatMatrix result) {
        if (result == this) {
            for (int i = 0; i < length; i++) {
                if (get(i) > v) {
                    result.put(i, v);
                }
            }
        } else {
            for (int i = 0; i < length; i++) {
                if (get(i) > v) {
                    result.put(i, v);
                } else {
                    result.put(i, get(i));
                }
            }

        }
        return this;
    }

    public FloatMatrix mini(float v) {
        return mini(v, this);
    }

    public FloatMatrix min(float v) {
        return mini(v, new FloatMatrix(rows, columns));
    }

    /** Returns the maximal element of the matrix. */
01688     public float max() {
        if (isEmpty()) {
            return Float.NEGATIVE_INFINITY;
        }
        float v = Float.NEGATIVE_INFINITY;
        for (int i = 0; i < length; i++) {
            if (!Float.isNaN(get(i)) && get(i) > v) {
                v = get(i);
            }
        }
        return v;
    }

    /**
     * Returns the linear index of the maximal element of the matrix. If
     * there are more than one elements with this value, the first one
     * is returned.
     */
01706     public int argmax() {
        if (isEmpty()) {
            return -1;
        }
        float v = Float.NEGATIVE_INFINITY;
        int a = -1;
        for (int i = 0; i < length; i++) {
            if (!Float.isNaN(get(i)) && get(i) > v) {
                v = get(i);
                a = i;
            }
        }

        return a;
    }

    /**
     * Computes the maximum between two matrices. Returns the larger of the
     * corresponding elements in the matrix (in-place).
     */
01726     public FloatMatrix maxi(FloatMatrix other, FloatMatrix result) {
        if (result == this) {
            for (int i = 0; i < length; i++) {
                if (get(i) < other.get(i)) {
                    put(i, other.get(i));
                }
            }
        } else {
            for (int i = 0; i < length; i++) {
                if (get(i) < other.get(i)) {
                    result.put(i, other.get(i));
                } else {
                    result.put(i, get(i));
                }
            }
        }
        return this;
    }

    /**
     * Computes the maximum between two matrices. Returns the smaller of the
     * corresponding elements in the matrix (in-place on this).
     */
01749     public FloatMatrix maxi(FloatMatrix other) {
        return maxi(other, this);
    }

    /**
     * Computes the maximum between two matrices. Returns the larger of the
     * corresponding elements in the matrix (in-place on this).
     */
01757     public FloatMatrix max(FloatMatrix other) {
        return maxi(other, new FloatMatrix(rows, columns));
    }

    public FloatMatrix maxi(float v, FloatMatrix result) {
        if (result == this) {
            for (int i = 0; i < length; i++) {
                if (get(i) < v) {
                    result.put(i, v);
                }
            }
        } else {
            for (int i = 0; i < length; i++) {
                if (get(i) < v) {
                    result.put(i, v);
                } else {
                    result.put(i, get(i));
                }
            }

        }
        return this;
    }

    public FloatMatrix maxi(float v) {
        return maxi(v, this);
    }

    public FloatMatrix max(float v) {
        return maxi(v, new FloatMatrix(rows, columns));
    }

    /** Computes the sum of all elements of the matrix. */
01790     public float sum() {
        float s = 0.0f;
        for (int i = 0; i < length; i++) {
            s += get(i);
        }
        return s;
    }

    /**
     * Computes the mean value of all elements in the matrix,
     * that is, <code>x.sum() / x.length</code>.
     */
01802     public float mean() {
        return sum() / length;
    }

    /**
     * Computes the cumulative sum, that is, the sum of all elements
     * of the matrix up to a given index in linear addressing (in-place).
     */
01810     public FloatMatrix cumulativeSumi() {
        float s = 0.0f;
        for (int i = 0; i < length; i++) {
            s += get(i);
            put(i, s);
        }
        return this;
    }

    /**
     * Computes the cumulative sum, that is, the sum of all elements
     * of the matrix up to a given index in linear addressing.
     */
01823     public FloatMatrix cumulativeSum() {
        return dup().cumulativeSumi();
    }

    /** The scalar product of this with other. */
01828     public float dot(FloatMatrix other) {
        return SimpleBlas.dot(this, other);
    }

    /**
     * The Euclidean norm of the matrix as vector, also the Frobenius
     * norm of the matrix.
     */
01836     public float norm2() {
        return SimpleBlas.nrm2(this);
    }

    /**
     * The maximum norm of the matrix (maximal absolute value of the elements).
     */
01843     public float normmax() {
        int i = SimpleBlas.iamax(this);
        return Math.abs(get(i));
    }

    /**
     * The 1-norm of the matrix as vector (sum of absolute values of elements).
     */
01851     public float norm1() {
        return SimpleBlas.asum(this);
    }

    /**
     * Return a new matrix with all elements sorted.
     */
01858     public FloatMatrix sort() {
        float array[] = toArray();
        java.util.Arrays.sort(array);
        return new FloatMatrix(rows, columns, array);
    }

    /**
     * Sort elements in-place.
     */
01867     public FloatMatrix sorti() {
        Arrays.sort(data);
        return this;
    }

    /**
     * Get the sorting permutation.
     *
     * @return an int[] array such that which indexes the elements in sorted
     * order.
     */
01878     public int[] sortingPermutation() {
        Integer[] indices = new Integer[length];

        for (int i = 0; i < length; i++) {
            indices[i] = i;
        }

        final float[] array = data;

        Arrays.sort(indices, new Comparator() {

            public int compare(Object o1, Object o2) {
                int i = (Integer) o1;
                int j = (Integer) o2;
                if (array[i] < array[j]) {
                    return -1;
                } else if (array[i] == array[j]) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });

        int[] result = new int[length];

        for (int i = 0; i < length; i++) {
            result[i] = indices[i];
        }

        return result;
    }

    /**
     * Sort columns (in-place).
     */
01914     public FloatMatrix sortColumnsi() {
        for (int i = 0; i < length; i += rows) {
            Arrays.sort(data, i, i + rows);
        }
        return this;
    }

    /** Sort columns. */
01922     public FloatMatrix sortColumns() {
        return dup().sortColumnsi();
    }

    /** Return matrix of indices which sort all columns. */
01927     public int[][] columnSortingPermutations() {
        int[][] result = new int[columns][];

        FloatMatrix temp = new FloatMatrix(rows);
        for (int c = 0; c < columns; c++) {
            result[c] = getColumn(c, temp).sortingPermutation();
        }

        return result;
    }

    /** Sort rows (in-place). */
01939     public FloatMatrix sortRowsi() {
        // actually, this is much harder because the data is not consecutive
        // in memory...
        FloatMatrix temp = new FloatMatrix(columns);
        for (int r = 0; r < rows; r++) {
            putRow(r, getRow(r, temp).sorti());
        }
        return this;
    }

    /** Sort rows. */
01950     public FloatMatrix sortRows() {
        return dup().sortRowsi();
    }

    /** Return matrix of indices which sort all columns. */
01955     public int[][] rowSortingPermutations() {
        int[][] result = new int[rows][];

        FloatMatrix temp = new FloatMatrix(columns);
        for (int r = 0; r < rows; r++) {
            result[r] = getRow(r, temp).sortingPermutation();
        }

        return result;
    }

    /** Return a vector containing the sums of the columns (having number of columns many entries) */
01967     public FloatMatrix columnSums() {
        if (rows == 1) {
            return dup();
        } else {
            FloatMatrix v = new FloatMatrix(1, columns);

            for (int c = 0; c < columns; c++) {
                for (int r = 0; r < rows; r++) {
                    v.put(c, v.get(c) + get(r, c));
                }
            }

            return v;
        }
    }

    /** Return a vector containing the means of all columns. */
01984     public FloatMatrix columnMeans() {
        return columnSums().divi(rows);
    }

    /** Return a vector containing the sum of the rows. */
01989     public FloatMatrix rowSums() {
        if (columns == 1) {
            return dup();
        } else {
            FloatMatrix v = new FloatMatrix(rows);

            for (int c = 0; c < columns; c++) {
                for (int r = 0; r < rows; r++) {
                    v.put(r, v.get(r) + get(r, c));
                }
            }

            return v;
        }
    }

    /** Return a vector containing the means of the rows. */
02006     public FloatMatrix rowMeans() {
        return rowSums().divi(columns);
    }

    /** Get a copy of a column. */
02011     public FloatMatrix getColumn(int c) {
        return getColumn(c, new FloatMatrix(rows, 1));
    }

    /** Copy a column to the given vector. */
02016     public FloatMatrix getColumn(int c, FloatMatrix result) {
        result.checkLength(rows);
        JavaBlas.rcopy(rows, data, index(0, c), 1, result.data, 0, 1);
        return result;
    }

    /** Copy a column back into the matrix. */
02023     public void putColumn(int c, FloatMatrix v) {
        JavaBlas.rcopy(rows, v.data, 0, 1, data, index(0, c), 1);
    }

    /** Get a copy of a row. */
02028     public FloatMatrix getRow(int r) {
        return getRow(r, new FloatMatrix(1, columns));
    }

    /** Copy a row to a given vector. */
02033     public FloatMatrix getRow(int r, FloatMatrix result) {
        result.checkLength(columns);
        JavaBlas.rcopy(columns, data, index(r, 0), rows, result.data, 0, 1);
        return result;
    }

    /** Copy a row back into the matrix. */
02040     public void putRow(int r, FloatMatrix v) {
        JavaBlas.rcopy(columns, v.data, 0, 1, data, index(r, 0), rows);
    }

    /** Return column-wise minimums. */
02045     public FloatMatrix columnMins() {
        FloatMatrix mins = new FloatMatrix(1, columns);
        for (int c = 0; c < columns; c++) {
            mins.put(c, getColumn(c).min());
        }
        return mins;
    }

    /** Return index of minimal element per column. */
02054     public int[] columnArgmins() {
        int[] argmins = new int[columns];
        for (int c = 0; c < columns; c++) {
            argmins[c] = getColumn(c).argmin();
        }
        return argmins;
    }

    /** Return column-wise maximums. */
02063     public FloatMatrix columnMaxs() {
        FloatMatrix maxs = new FloatMatrix(1, columns);
        for (int c = 0; c < columns; c++) {
            maxs.put(c, getColumn(c).max());
        }
        return maxs;
    }

    /** Return index of minimal element per column. */
02072     public int[] columnArgmaxs() {
        int[] argmaxs = new int[columns];
        for (int c = 0; c < columns; c++) {
            argmaxs[c] = getColumn(c).argmax();
        }
        return argmaxs;
    }

    /** Return row-wise minimums. */
02081     public FloatMatrix rowMins() {
        FloatMatrix mins = new FloatMatrix(rows);
        for (int c = 0; c < rows; c++) {
            mins.put(c, getRow(c).min());
        }
        return mins;
    }

    /** Return index of minimal element per row. */
02090     public int[] rowArgmins() {
        int[] argmins = new int[rows];
        for (int c = 0; c < rows; c++) {
            argmins[c] = getRow(c).argmin();
        }
        return argmins;
    }

    /** Return row-wise maximums. */
02099     public FloatMatrix rowMaxs() {
        FloatMatrix maxs = new FloatMatrix(rows);
        for (int c = 0; c < rows; c++) {
            maxs.put(c, getRow(c).max());
        }
        return maxs;
    }

    /** Return index of minimal element per row. */
02108     public int[] rowArgmaxs() {
        int[] argmaxs = new int[rows];
        for (int c = 0; c < rows; c++) {
            argmaxs[c] = getRow(c).argmax();
        }
        return argmaxs;
    }

    /**************************************************************************
     * Elementwise Functions
     */
    /** Add a row vector to all rows of the matrix (in place). */
02120     public FloatMatrix addiRowVector(FloatMatrix x) {
        x.checkLength(columns);
        for (int r = 0; r < rows; r++) {
            JavaBlas.raxpy(columns, 1.0f, x.data, 0, 1, data, index(r, 0), rows);
        }
        return this;
    }

    /** Add a row to all rows of the matrix. */
02129     public FloatMatrix addRowVector(FloatMatrix x) {
        return dup().addiRowVector(x);
    }

    /** Add a vector to all columns of the matrix (in-place). */
02134     public FloatMatrix addiColumnVector(FloatMatrix x) {
        x.checkLength(rows);
        for (int c = 0; c < columns; c++) {
            JavaBlas.raxpy(rows, 1.0f, x.data, 0, 1, data, index(0, c), 1);
        }
        return this;
    }

    /** Add a vector to all columns of the matrix. */
02143     public FloatMatrix addColumnVector(FloatMatrix x) {
        return dup().addiColumnVector(x);
    }

    /** Subtract a row vector from all rows of the matrix (in-place). */
02148     public FloatMatrix subiRowVector(FloatMatrix x) {
        // This is a bit crazy, but a row vector must have as length as the columns of the matrix.
        x.checkLength(columns);
        for (int r = 0; r < rows; r++) {
            JavaBlas.raxpy(columns, -1.0f, x.data, 0, 1, data, index(r, 0), rows);
        }
        return this;
    }

    /** Subtract a row vector from all rows of the matrix. */
02158     public FloatMatrix subRowVector(FloatMatrix x) {
        return dup().subiRowVector(x);
    }

    /** Subtract a column vector from all columns of the matrix (in-place). */
02163     public FloatMatrix subiColumnVector(FloatMatrix x) {
        x.checkLength(rows);
        for (int c = 0; c < columns; c++) {
            JavaBlas.raxpy(rows, -1.0f, x.data, 0, 1, data, index(0, c), 1);
        }
        return this;
    }

    /** Subtract a vector from all columns of the matrix. */
02172     public FloatMatrix subColumnVector(FloatMatrix x) {
        return dup().subiColumnVector(x);
    }

    /** Multiply a row by a scalar. */
02177     public FloatMatrix mulRow(int r, float scale) {
        Blas.sscal(columns, scale, data, index(r, 0), rows);
        return this;
    }

    /** Multiply a column by a scalar. */
02183     public FloatMatrix mulColumn(int c, float scale) {
        Blas.sscal(rows, scale, data, index(0, c), 1);
        return this;
    }

    /** Multiply all columns with a column vector (in-place). */
02189     public FloatMatrix muliColumnVector(FloatMatrix x) {
        x.checkLength(rows);
        for (int c = 0; c < columns; c++) {
            for (int r = 0; r < rows; r++)
                put(r, c, get(r, c) * x.get(r));
        }
        return this;
    }

    /** Multiply all columns with a column vector. */
02199     public FloatMatrix mulColumnVector(FloatMatrix x) {
        return dup().muliColumnVector(x);
    }

    /** Multiply all rows with a row vector (in-place). */
02204     public FloatMatrix muliRowVector(FloatMatrix x) {
        x.checkLength(columns);
        for (int c = 0; c < columns; c++)
            for (int r = 0; r < rows; r++)
                put(r, c, get(r, c) * x.get(c));
        return this;
    }

    /** Multiply all rows with a row vector. */
02213     public FloatMatrix mulRowVector(FloatMatrix x) {
        return dup().muliRowVector(x);
    }

    /**
     * Writes out this matrix to the given data stream.
     * @param dos the data output stream to write to.
     * @throws IOException
     */
02222     public void out(DataOutputStream dos) throws IOException {
        dos.writeUTF("float");
        dos.writeInt(columns);
        dos.writeInt(rows);

        dos.writeInt(data.length);
        for (int i = 0; i < data.length; i++) {
            dos.writeDouble(data[i]);
        }
    }

    /**
     * Reads in a matrix from the given data stream. Note
     * that the old data of this matrix will be discarded.
     * @param dis the data input stream to read from.
     * @throws IOException
     */
02239     public void in(DataInputStream dis) throws IOException {
        if (!dis.readUTF().equals("float")) {
            throw new IllegalStateException("The matrix in the specified file is not of the correct type!");
        }

        this.columns = dis.readInt();
        this.rows = dis.readInt();

        final int MAX = dis.readInt();
        data = new float[MAX];
        for (int i = 0; i < MAX; i++) {
            data[i] = dis.readFloat();
        }
    }

    /**
     * Saves this matrix to the specified file.
     * @param filename the file to write the matrix in.
     * @throws IOException thrown on errors while writing the matrix to the file
     */
02259     public void save(String filename) throws IOException {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename, false));
        this.out(dos);
    }

    /**
     * Loads a matrix from a file into this matrix. Note that the old data
     * of this matrix will be discarded.
     * @param filename the file to read the matrix from
     * @throws IOException thrown on errors while reading the matrix
     */
02270     public void load(String filename) throws IOException {
        DataInputStream dis = new DataInputStream(new FileInputStream(filename));
        this.in(dis);
    }

    public static FloatMatrix loadAsciiFile(String filename) throws IOException {
        BufferedReader is = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));

        // Go through file and count columns and rows. What makes this endeavour a bit difficult is
        // that files can have leading or trailing spaces leading to spurious fields
        // after String.split().
        String line;
        int rows = 0;
        int columns = -1;
        while ((line = is.readLine()) != null) {
            String[] elements = line.split("\\s+");
            int numElements = elements.length;
            if (elements[0].length() == 0) {
                numElements--;
            }
            if (elements[elements.length - 1].length() == 0) {
                numElements--;
            }

            if (columns == -1) {
                columns = numElements;
            } else {
                if (columns != numElements) {
                    throw new IOException("Number of elements changes in line " + line + ".");
                }
            }

            rows++;
        }
        is.close();

        // Go through file a second time process the actual data.
        is = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        FloatMatrix result = new FloatMatrix(rows, columns);
        int r = 0;
        while ((line = is.readLine()) != null) {
            String[] elements = line.split("\\s+");
            int firstElement = (elements[0].length() == 0) ? 1 : 0;
            for (int c = 0, cc = firstElement; c < columns; c++, cc++) {
                result.put(r, c, Float.valueOf(elements[cc]));
            }
            r++;
        }
        return result;
    }

    /****************************************************************
     * Autogenerated code
     */
    /***** Code for operators ***************************************/

    /* Overloads for the usual arithmetic operations */
    /*#
    def gen_overloads(base, result_rows, result_cols, verb=''); <<-EOS
    #{doc verb.capitalize + " a matrix (in place)."}
    public FloatMatrix #{base}i(FloatMatrix other) {
    return #{base}i(other, this);
    }

    #{doc verb.capitalize + " a matrix (in place)."}
    public FloatMatrix #{base}(FloatMatrix other) {
    return #{base}i(other, new FloatMatrix(#{result_rows}, #{result_cols}));
    }

    #{doc verb.capitalize + " a scalar (in place)."}
    public FloatMatrix #{base}i(float v) {
    return #{base}i(v, this);
    }

    #{doc verb.capitalize + " a scalar."}
    public FloatMatrix #{base}(float v) {
    return #{base}i(v, new FloatMatrix(rows, columns));
    }
    EOS
    end
    #*/

    /* Generating code for logical operators. This not only generates the stubs
     * but really all of the code.
     */
    /*#
    def gen_compare(name, op, cmp); <<-EOS
    #{doc 'Test for ' + cmp + ' (in-place).'}
    public FloatMatrix #{name}i(FloatMatrix other, FloatMatrix result) {
    if (other.isScalar())
    return #{name}i(other.scalar(), result);

    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, get(i) #{op} other.get(i) ? 1.0f : 0.0f);
    return result;
    }

    #{doc 'Test for ' + cmp + ' (in-place).'}
    public FloatMatrix #{name}i(FloatMatrix other) {
    return #{name}i(other, this);
    }

    #{doc 'Test for ' + cmp + '.'}
    public FloatMatrix #{name}(FloatMatrix other) {
    return #{name}i(other, new FloatMatrix(rows, columns));
    }

    #{doc 'Test for ' + cmp + ' against a scalar (in-place).'}
    public FloatMatrix #{name}i(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    for (int i = 0; i < length; i++)
    result.put(i, get(i) #{op} value ? 1.0f : 0.0f);
    return result;
    }

    #{doc 'Test for ' + cmp + ' against a scalar (in-place).'}
    public FloatMatrix #{name}i(float value) {
    return #{name}i(value, this);
    }

    #{doc 'test for ' + cmp + ' against a scalar.'}
    public FloatMatrix #{name}(float value) {
    return #{name}i(value, new FloatMatrix(rows, columns));
    }
    EOS
    end
    #*/
    /*#
    def gen_logical(name, op, cmp); <<-EOS
    #{doc 'Compute elementwise ' + cmp + ' (in-place).'}
    public FloatMatrix #{name}i(FloatMatrix other, FloatMatrix result) {
    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, (get(i) != 0.0f) #{op} (other.get(i) != 0.0f) ? 1.0f : 0.0f);
    return result;
    }

    #{doc 'Compute elementwise ' + cmp + ' (in-place).'}
    public FloatMatrix #{name}i(FloatMatrix other) {
    return #{name}i(other, this);
    }

    #{doc 'Compute elementwise ' + cmp + '.'}
    public FloatMatrix #{name}(FloatMatrix other) {
    return #{name}i(other, new FloatMatrix(rows, columns));
    }

    #{doc 'Compute elementwise ' + cmp + ' against a scalar (in-place).'}
    public FloatMatrix #{name}i(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    boolean val = (value != 0.0f);
    for (int i = 0; i < length; i++)
    result.put(i, (get(i) != 0.0f) #{op} val ? 1.0f : 0.0f);
    return result;
    }

    #{doc 'Compute elementwise ' + cmp + ' against a scalar (in-place).'}
    public FloatMatrix #{name}i(float value) {
    return #{name}i(value, this);
    }

    #{doc 'Compute elementwise ' + cmp + ' against a scalar.'}
    public FloatMatrix #{name}(float value) {
    return #{name}i(value, new FloatMatrix(rows, columns));
    }
    EOS
    end
    #*/

    /*# collect(gen_overloads('add', 'rows', 'columns', 'add'),
    gen_overloads('sub', 'rows', 'columns', 'subtract'),
    gen_overloads('rsub', 'rows', 'columns', '(right-)subtract'),
    gen_overloads('div', 'rows', 'columns', 'elementwise divide by'),
    gen_overloads('rdiv', 'rows', 'columns', '(right-)elementwise divide by'),
    gen_overloads('mul', 'rows', 'columns', 'elementwise multiply by'),
    gen_overloads('mmul', 'rows', 'other.columns', 'matrix-multiply by'),
    gen_compare('lt', '<', '"less than"'),
    gen_compare('gt', '>', '"greater than"'),
    gen_compare('le', '<=', '"less than or equal"'),
    gen_compare('ge', '>=', '"greater than or equal"'),
    gen_compare('eq', '==', 'equality'),
    gen_compare('ne', '!=', 'inequality'),
    gen_logical('and', '&', 'logical and'),
    gen_logical('or', '|', 'logical or'),
    gen_logical('xor', '^', 'logical xor'))
    #*/
//RJPP-BEGIN------------------------------------------------------------
    /** Add a matrix (in place). */
02463     public FloatMatrix addi(FloatMatrix other) {
    return addi(other, this);
    }

    /** Add a matrix (in place). */
02468     public FloatMatrix add(FloatMatrix other) {
    return addi(other, new FloatMatrix(rows, columns));
    }

    /** Add a scalar (in place). */
02473     public FloatMatrix addi(float v) {
    return addi(v, this);
    }

    /** Add a scalar. */
02478     public FloatMatrix add(float v) {
    return addi(v, new FloatMatrix(rows, columns));
    }

    /** Subtract a matrix (in place). */
02483     public FloatMatrix subi(FloatMatrix other) {
    return subi(other, this);
    }

    /** Subtract a matrix (in place). */
02488     public FloatMatrix sub(FloatMatrix other) {
    return subi(other, new FloatMatrix(rows, columns));
    }

    /** Subtract a scalar (in place). */
02493     public FloatMatrix subi(float v) {
    return subi(v, this);
    }

    /** Subtract a scalar. */
02498     public FloatMatrix sub(float v) {
    return subi(v, new FloatMatrix(rows, columns));
    }

    /** (right-)subtract a matrix (in place). */
02503     public FloatMatrix rsubi(FloatMatrix other) {
    return rsubi(other, this);
    }

    /** (right-)subtract a matrix (in place). */
02508     public FloatMatrix rsub(FloatMatrix other) {
    return rsubi(other, new FloatMatrix(rows, columns));
    }

    /** (right-)subtract a scalar (in place). */
02513     public FloatMatrix rsubi(float v) {
    return rsubi(v, this);
    }

    /** (right-)subtract a scalar. */
02518     public FloatMatrix rsub(float v) {
    return rsubi(v, new FloatMatrix(rows, columns));
    }

    /** Elementwise divide by a matrix (in place). */
02523     public FloatMatrix divi(FloatMatrix other) {
    return divi(other, this);
    }

    /** Elementwise divide by a matrix (in place). */
02528     public FloatMatrix div(FloatMatrix other) {
    return divi(other, new FloatMatrix(rows, columns));
    }

    /** Elementwise divide by a scalar (in place). */
02533     public FloatMatrix divi(float v) {
    return divi(v, this);
    }

    /** Elementwise divide by a scalar. */
02538     public FloatMatrix div(float v) {
    return divi(v, new FloatMatrix(rows, columns));
    }

    /** (right-)elementwise divide by a matrix (in place). */
02543     public FloatMatrix rdivi(FloatMatrix other) {
    return rdivi(other, this);
    }

    /** (right-)elementwise divide by a matrix (in place). */
02548     public FloatMatrix rdiv(FloatMatrix other) {
    return rdivi(other, new FloatMatrix(rows, columns));
    }

    /** (right-)elementwise divide by a scalar (in place). */
02553     public FloatMatrix rdivi(float v) {
    return rdivi(v, this);
    }

    /** (right-)elementwise divide by a scalar. */
02558     public FloatMatrix rdiv(float v) {
    return rdivi(v, new FloatMatrix(rows, columns));
    }

    /** Elementwise multiply by a matrix (in place). */
02563     public FloatMatrix muli(FloatMatrix other) {
    return muli(other, this);
    }

    /** Elementwise multiply by a matrix (in place). */
02568     public FloatMatrix mul(FloatMatrix other) {
    return muli(other, new FloatMatrix(rows, columns));
    }

    /** Elementwise multiply by a scalar (in place). */
02573     public FloatMatrix muli(float v) {
    return muli(v, this);
    }

    /** Elementwise multiply by a scalar. */
02578     public FloatMatrix mul(float v) {
    return muli(v, new FloatMatrix(rows, columns));
    }

    /** Matrix-multiply by a matrix (in place). */
02583     public FloatMatrix mmuli(FloatMatrix other) {
    return mmuli(other, this);
    }

    /** Matrix-multiply by a matrix (in place). */
02588     public FloatMatrix mmul(FloatMatrix other) {
    return mmuli(other, new FloatMatrix(rows, other.columns));
    }

    /** Matrix-multiply by a scalar (in place). */
02593     public FloatMatrix mmuli(float v) {
    return mmuli(v, this);
    }

    /** Matrix-multiply by a scalar. */
02598     public FloatMatrix mmul(float v) {
    return mmuli(v, new FloatMatrix(rows, columns));
    }

    /** Test for "less than" (in-place). */
02603     public FloatMatrix lti(FloatMatrix other, FloatMatrix result) {
    if (other.isScalar())
    return lti(other.scalar(), result);

    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, get(i) < other.get(i) ? 1.0f : 0.0f);
    return result;
    }

    /** Test for "less than" (in-place). */
02616     public FloatMatrix lti(FloatMatrix other) {
    return lti(other, this);
    }

    /** Test for "less than". */
02621     public FloatMatrix lt(FloatMatrix other) {
    return lti(other, new FloatMatrix(rows, columns));
    }

    /** Test for "less than" against a scalar (in-place). */
02626     public FloatMatrix lti(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    for (int i = 0; i < length; i++)
    result.put(i, get(i) < value ? 1.0f : 0.0f);
    return result;
    }

    /** Test for "less than" against a scalar (in-place). */
02634     public FloatMatrix lti(float value) {
    return lti(value, this);
    }

    /** test for "less than" against a scalar. */
02639     public FloatMatrix lt(float value) {
    return lti(value, new FloatMatrix(rows, columns));
    }

    /** Test for "greater than" (in-place). */
02644     public FloatMatrix gti(FloatMatrix other, FloatMatrix result) {
    if (other.isScalar())
    return gti(other.scalar(), result);

    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, get(i) > other.get(i) ? 1.0f : 0.0f);
    return result;
    }

    /** Test for "greater than" (in-place). */
02657     public FloatMatrix gti(FloatMatrix other) {
    return gti(other, this);
    }

    /** Test for "greater than". */
02662     public FloatMatrix gt(FloatMatrix other) {
    return gti(other, new FloatMatrix(rows, columns));
    }

    /** Test for "greater than" against a scalar (in-place). */
02667     public FloatMatrix gti(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    for (int i = 0; i < length; i++)
    result.put(i, get(i) > value ? 1.0f : 0.0f);
    return result;
    }

    /** Test for "greater than" against a scalar (in-place). */
02675     public FloatMatrix gti(float value) {
    return gti(value, this);
    }

    /** test for "greater than" against a scalar. */
02680     public FloatMatrix gt(float value) {
    return gti(value, new FloatMatrix(rows, columns));
    }

    /** Test for "less than or equal" (in-place). */
02685     public FloatMatrix lei(FloatMatrix other, FloatMatrix result) {
    if (other.isScalar())
    return lei(other.scalar(), result);

    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, get(i) <= other.get(i) ? 1.0f : 0.0f);
    return result;
    }

    /** Test for "less than or equal" (in-place). */
02698     public FloatMatrix lei(FloatMatrix other) {
    return lei(other, this);
    }

    /** Test for "less than or equal". */
02703     public FloatMatrix le(FloatMatrix other) {
    return lei(other, new FloatMatrix(rows, columns));
    }

    /** Test for "less than or equal" against a scalar (in-place). */
02708     public FloatMatrix lei(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    for (int i = 0; i < length; i++)
    result.put(i, get(i) <= value ? 1.0f : 0.0f);
    return result;
    }

    /** Test for "less than or equal" against a scalar (in-place). */
02716     public FloatMatrix lei(float value) {
    return lei(value, this);
    }

    /** test for "less than or equal" against a scalar. */
02721     public FloatMatrix le(float value) {
    return lei(value, new FloatMatrix(rows, columns));
    }

    /** Test for "greater than or equal" (in-place). */
02726     public FloatMatrix gei(FloatMatrix other, FloatMatrix result) {
    if (other.isScalar())
    return gei(other.scalar(), result);

    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, get(i) >= other.get(i) ? 1.0f : 0.0f);
    return result;
    }

    /** Test for "greater than or equal" (in-place). */
02739     public FloatMatrix gei(FloatMatrix other) {
    return gei(other, this);
    }

    /** Test for "greater than or equal". */
02744     public FloatMatrix ge(FloatMatrix other) {
    return gei(other, new FloatMatrix(rows, columns));
    }

    /** Test for "greater than or equal" against a scalar (in-place). */
02749     public FloatMatrix gei(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    for (int i = 0; i < length; i++)
    result.put(i, get(i) >= value ? 1.0f : 0.0f);
    return result;
    }

    /** Test for "greater than or equal" against a scalar (in-place). */
02757     public FloatMatrix gei(float value) {
    return gei(value, this);
    }

    /** test for "greater than or equal" against a scalar. */
02762     public FloatMatrix ge(float value) {
    return gei(value, new FloatMatrix(rows, columns));
    }

    /** Test for equality (in-place). */
02767     public FloatMatrix eqi(FloatMatrix other, FloatMatrix result) {
    if (other.isScalar())
    return eqi(other.scalar(), result);

    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, get(i) == other.get(i) ? 1.0f : 0.0f);
    return result;
    }

    /** Test for equality (in-place). */
02780     public FloatMatrix eqi(FloatMatrix other) {
    return eqi(other, this);
    }

    /** Test for equality. */
02785     public FloatMatrix eq(FloatMatrix other) {
    return eqi(other, new FloatMatrix(rows, columns));
    }

    /** Test for equality against a scalar (in-place). */
02790     public FloatMatrix eqi(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    for (int i = 0; i < length; i++)
    result.put(i, get(i) == value ? 1.0f : 0.0f);
    return result;
    }

    /** Test for equality against a scalar (in-place). */
02798     public FloatMatrix eqi(float value) {
    return eqi(value, this);
    }

    /** test for equality against a scalar. */
02803     public FloatMatrix eq(float value) {
    return eqi(value, new FloatMatrix(rows, columns));
    }

    /** Test for inequality (in-place). */
02808     public FloatMatrix nei(FloatMatrix other, FloatMatrix result) {
    if (other.isScalar())
    return nei(other.scalar(), result);

    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, get(i) != other.get(i) ? 1.0f : 0.0f);
    return result;
    }

    /** Test for inequality (in-place). */
02821     public FloatMatrix nei(FloatMatrix other) {
    return nei(other, this);
    }

    /** Test for inequality. */
02826     public FloatMatrix ne(FloatMatrix other) {
    return nei(other, new FloatMatrix(rows, columns));
    }

    /** Test for inequality against a scalar (in-place). */
02831     public FloatMatrix nei(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    for (int i = 0; i < length; i++)
    result.put(i, get(i) != value ? 1.0f : 0.0f);
    return result;
    }

    /** Test for inequality against a scalar (in-place). */
02839     public FloatMatrix nei(float value) {
    return nei(value, this);
    }

    /** test for inequality against a scalar. */
02844     public FloatMatrix ne(float value) {
    return nei(value, new FloatMatrix(rows, columns));
    }

    /** Compute elementwise logical and (in-place). */
02849     public FloatMatrix andi(FloatMatrix other, FloatMatrix result) {
    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, (get(i) != 0.0f) & (other.get(i) != 0.0f) ? 1.0f : 0.0f);
    return result;
    }

    /** Compute elementwise logical and (in-place). */
02859     public FloatMatrix andi(FloatMatrix other) {
    return andi(other, this);
    }

    /** Compute elementwise logical and. */
02864     public FloatMatrix and(FloatMatrix other) {
    return andi(other, new FloatMatrix(rows, columns));
    }

    /** Compute elementwise logical and against a scalar (in-place). */
02869     public FloatMatrix andi(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    boolean val = (value != 0.0f);
    for (int i = 0; i < length; i++)
    result.put(i, (get(i) != 0.0f) & val ? 1.0f : 0.0f);
    return result;
    }

    /** Compute elementwise logical and against a scalar (in-place). */
02878     public FloatMatrix andi(float value) {
    return andi(value, this);
    }

    /** Compute elementwise logical and against a scalar. */
02883     public FloatMatrix and(float value) {
    return andi(value, new FloatMatrix(rows, columns));
    }

    /** Compute elementwise logical or (in-place). */
02888     public FloatMatrix ori(FloatMatrix other, FloatMatrix result) {
    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, (get(i) != 0.0f) | (other.get(i) != 0.0f) ? 1.0f : 0.0f);
    return result;
    }

    /** Compute elementwise logical or (in-place). */
02898     public FloatMatrix ori(FloatMatrix other) {
    return ori(other, this);
    }

    /** Compute elementwise logical or. */
02903     public FloatMatrix or(FloatMatrix other) {
    return ori(other, new FloatMatrix(rows, columns));
    }

    /** Compute elementwise logical or against a scalar (in-place). */
02908     public FloatMatrix ori(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    boolean val = (value != 0.0f);
    for (int i = 0; i < length; i++)
    result.put(i, (get(i) != 0.0f) | val ? 1.0f : 0.0f);
    return result;
    }

    /** Compute elementwise logical or against a scalar (in-place). */
02917     public FloatMatrix ori(float value) {
    return ori(value, this);
    }

    /** Compute elementwise logical or against a scalar. */
02922     public FloatMatrix or(float value) {
    return ori(value, new FloatMatrix(rows, columns));
    }

    /** Compute elementwise logical xor (in-place). */
02927     public FloatMatrix xori(FloatMatrix other, FloatMatrix result) {
    assertSameLength(other);
    ensureResultLength(other, result);

    for (int i = 0; i < length; i++)
    result.put(i, (get(i) != 0.0f) ^ (other.get(i) != 0.0f) ? 1.0f : 0.0f);
    return result;
    }

    /** Compute elementwise logical xor (in-place). */
02937     public FloatMatrix xori(FloatMatrix other) {
    return xori(other, this);
    }

    /** Compute elementwise logical xor. */
02942     public FloatMatrix xor(FloatMatrix other) {
    return xori(other, new FloatMatrix(rows, columns));
    }

    /** Compute elementwise logical xor against a scalar (in-place). */
02947     public FloatMatrix xori(float value, FloatMatrix result) {
    ensureResultLength(null, result);
    boolean val = (value != 0.0f);
    for (int i = 0; i < length; i++)
    result.put(i, (get(i) != 0.0f) ^ val ? 1.0f : 0.0f);
    return result;
    }

    /** Compute elementwise logical xor against a scalar (in-place). */
02956     public FloatMatrix xori(float value) {
    return xori(value, this);
    }

    /** Compute elementwise logical xor against a scalar. */
02961     public FloatMatrix xor(float value) {
    return xori(value, new FloatMatrix(rows, columns));
    }
//RJPP-END--------------------------------------------------------------
}

Generated by  Doxygen 1.6.0   Back to index