Reads in a {@link Matrix} from the IO stream.
@return Matrix
@throws IOException If anything goes wrong.
971913
public <M extends FMatrix>M read32() throws IOException {
        List<String> words = extractWords();

        if( words.size() == 3 ) {
            int numRows = Integer.parseInt(words.get(0));
            int numCols = Integer.parseInt(words.get(1));
            boolean real = words.get(2).compareToIgnoreCase("real") == 0;

            if (numRows < 0 || numCols < 0)
                throw new IOException("Invalid number of rows and/or columns: " + numRows + " " + numCols);

            if (real)
                return (M) readFDRM(numRows, numCols);
            else
                return (M) readCDRM(numRows, numCols);
        } else if ( words.size() == 4 ) {
            int numRows = Integer.parseInt(words.get(0));
            int numCols = Integer.parseInt(words.get(1));
            int length = Integer.parseInt(words.get(2));
            boolean real = words.get(3).compareToIgnoreCase("real") == 0;

            if (numRows < 0 || numCols < 0)
                throw new IOException("Invalid number of rows and/or columns: " + numRows + " " + numCols);

            if (real)
                return (M) readFSTR(numRows, numCols, length);
            else
                throw new IllegalArgumentException("Sparse complex not yet supported");
        } else {
            throw new IOException("Unexpected number of words on the first line. Found "+words.size());
        }
    }
461401
private static byte[] readAll(InputStream is) throws IOException {
    ByteArrayOutputStream tmp = new ByteArrayOutputStream();

    byte[] buf = new byte[2000];
    int read = 0;
    do {
      read = is.read(buf);
      tmp.write(buf, 0, read);
    } while ((read > 0) && (is.available() > 0));
    return tmp.toByteArray();
  }
616373
public Matrix getR () {
		Matrix X = new Matrix(n,n);
		double[][] R = X.getArray();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i < j) {
					R[i][j] = QR[i][j];
				} else if (i == j) {
					R[i][j] = Rdiag[i];
				} else {
					R[i][j] = 0.0;
				}
			}
		}
		return X;
	}
1268164
private Matrix loadText(InputStream fileStream) throws IOException {
        Matrix matrix = null;

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

        while ((line = br.readLine()) != null) {
            if (index >= rows)
                throw new IOException("More rows than specified");
            String[] termVectorPair = line.split("\\|");
            String[] values = termVectorPair[1].split("\\s");
            termToIndex.put(termVectorPair[0], index);
            if (values.length != columns) {
                throw new IOException(
                            "improperly formated semantic space file");
            }
            for (int c = 0; c < columns; ++c) {
                double d = Double.parseDouble(values[c]);
                row[c] = d;
                // matrix.set(index, c, d);
            }
            matrix.setRow(index, row);
            index++;
        }
        if (index != rows)
            throw new IOException(String.format(
                "Expected %d rows; saw %d", rows, index));
        return matrix;    
    }
227397
public Matrix getIplus() {
        Matrix Iplus = new Matrix(size, size);
        int i, j;
        for (i = 0; i < size; i++)
            for (j = 0; j < size; j++)
                Iplus.matrix[i][j] = 0d;
        for (i = 1; i < size; i++)
            Iplus.matrix[i - 1][i] = Math.sqrt(J * J + J - (J - i + 1) * (J - i + 1) + (J - i + 1));
        return Iplus;
    }
277697
public Matrix getIminus() {
        Matrix Iminus = new Matrix(size, size);
        int i, j;
        for (i = 0; i < size; i++)
            for (j = 0; j < size; j++)
                Iminus.matrix[i][j] = 0d;
        for (i = 1; i < size; i++)
            Iminus.matrix[i][i - 1] = Math.sqrt(J * J + J - (J - i) * (J - i) - (J - i));
        return Iminus;
    }
1086064
public Matrix4f getViewMatrix() {
        if (updateViewMatrix) {
            rotationMatrixInverse = Matrix4f.createRotation(rotation);
            final Matrix4f rotationMatrix = Matrix4f.createRotation(rotation.invert());
            final Matrix4f positionMatrix = Matrix4f.createTranslation(position.negate());
            viewMatrix = rotationMatrix.mul(positionMatrix);
            updateViewMatrix = false;
        }
        return viewMatrix;
    }
276256
public static Matrix copy(Matrix m) {
		if (m==null) throw new NullPointerException();
		Matrix copy=createMatrix(m.getRowCount(), m.getColumnCount());
		for (int row=0; row<m.getRowCount(); row++) for (int col=0; col<m.getColumnCount(); col++)
			copy.set(row, col, m.getQuick(row, col));
		return copy;
	}

	/**
	 * Creates a copy of a matrix.
	 * @param m the matrix to copy
	 * @return a new matrix with the same dimensions and contents as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Matrix2 copy(Matrix2 m) {
		return (Matrix2) copy((Matrix)m);
	}

	/**
	 * Creates a copy of a matrix.
	 * @param m the matrix to copy
	 * @return a new matrix with the same dimensions and contents as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Matrix3 copy(Matrix3 m) {
		return (Matrix3) copy((Matrix)m);
	}

	/**
	 * Creates a copy of a matrix.
	 * @param m the matrix to copy
	 * @return a new matrix with the same dimensions and contents as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Vector copy(Vector m) {
		return (Vector) copy((Matrix)m);
	}

	/**
	 * Creates a copy of a matrix.
	 * @param m the matrix to copy
	 * @return a new matrix with the same dimensions and contents as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Vector2 copy(Vector2 m) {
		return (Vector2) copy((Matrix)m);
	}

	/**
	 * Creates a copy of a matrix.
	 * @param m the matrix to copy
	 * @return a new matrix with the same dimensions and contents as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Vector3 copy(Vector3 m) {
		return (Vector3) copy((Matrix)m);
	}

	/**
	 * Creates a copy of a matrix.
	 * @param m the matrix to copy
	 * @return a new matrix with the same dimensions and contents as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Vector4 copy(Vector4 m) {
		return (Vector4) copy((Matrix)m);
	}
	
	/**
	 * Creates a matrix with the same dimensions as the argument.
	 * @param matrix the template matrix
	 * @return a new zero matrix with the same dimensions as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Matrix createLike(Matrix matrix) {
		return createMatrix(matrix.getRowCount(), matrix.getColumnCount());
	}
	
	/**
	 * Creates a matrix with the same dimensions as the argument.
	 * @param matrix the template matrix
	 * @return a new zero matrix with the same dimensions as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Matrix2 createLike(Matrix2 matrix) {
		return (Matrix2) createMatrix(matrix.getRowCount(), matrix.getColumnCount());
	}
	
	/**
	 * Creates a matrix with the same dimensions as the argument.
	 * @param matrix the template matrix
	 * @return a new zero matrix with the same dimensions as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Matrix3 createLike(Matrix3 matrix) {
		return (Matrix3) createMatrix(matrix.getRowCount(), matrix.getColumnCount());
	}
	
	/**
	 * Creates a matrix with the same dimensions as the argument.
	 * @param matrix the template matrix
	 * @return a new zero matrix with the same dimensions as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Vector createLike(Vector matrix) {
		return (Vector) createMatrix(matrix.getRowCount(), matrix.getColumnCount());
	}
	
	/**
	 * Creates a matrix with the same dimensions as the argument.
	 * @param matrix the template matrix
	 * @return a new zero matrix with the same dimensions as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Vector2 createLike(Vector2 matrix) {
		return (Vector2) createMatrix(matrix.getRowCount(), matrix.getColumnCount());
	}
	
	/**
	 * Creates a matrix with the same dimensions as the argument.
	 * @param matrix the template matrix
	 * @return a new zero matrix with the same dimensions as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Vector3 createLike(Vector3 matrix) {
		return (Vector3) createMatrix(matrix.getRowCount(), matrix.getColumnCount());
	}
	
	/**
	 * Creates a matrix with the same dimensions as the argument.
	 * @param matrix the template matrix
	 * @return a new zero matrix with the same dimensions as the argument
	 * @throws NullPointerException if {@code matrix} is {@code null}
	 */
	public static Vector4 createLike(Vector4 matrix) {
		return (Vector4) createMatrix(matrix.getRowCount(), matrix.getColumnCount());
	}
	
	/**
	 * Creates an identity matrix of the specified dimension.
	 * @param size the dimension of the matrix
	 * @return a <code>size</code>×<code>size</code> identity matrix
	 * @throws IllegalArgumentException if the size is not positive
	 */
	public static Matrix identity(int size) {
		Matrix m = MatrixFactory.createMatrix(size, size);
		for (int i = 0; i < size; i++)
			m.set(i, i, 1);
		return m;
	}

	/**
	 * Creates a matrix of the specified size whose elements are all 1.
	 * @param rows the number of rows
	 * @param columns the number of columns
	 * @return a <code>rows</code>×<code>columns</code> matrix whose elements are all 1
	 * @throws IllegalArgumentException if either argument is non-positive
	 */
	public static Matrix ones(int rows, int columns) {
		Matrix m = MatrixFactory.createMatrix(rows, columns);
		for (int i = 0; i < m.getRowCount(); i++)
			for (int j = 0; j < m.getColumnCount(); j++)
				m.set(i, j, 1);
		return m;
	}

	/**
	 * Creates a matrix of the specified size whose elements are pseudorandom, uniformly distributed
	 * values between 0 (inclusive) and 1 (exclusive).
	 * @param rows the number of rows
	 * @param columns the number of columns
	 * @return a <code>rows</code>×<code>columns</code> matrix of random elements
	 * @throws IllegalArgumentException if either argument is non-positive
	 */
	public static Matrix random(int rows, int columns) {
		Matrix m = MatrixFactory.createMatrix(rows, columns);
		for (int i = 0; i < m.getRowCount(); i++)
			for (int j = 0; j < m.getColumnCount(); j++)
				m.set(i, j, Math.random());
		return m;
	}
	
	//
	// Matrix creation from arrays
	//
	
	/**
	 * Creates a matrix and initializes its elements from an array. The parameter should be a {@code double[r][c]}
	 * array with {@code r>0} and {@code c>0}. The matrix element at row {@code row} and column {@code col} will be
	 * {@code elements[row][col]}.
	 * @param elements the array containing the elements of the matrix
	 * @return the new matrix initialized from the array
	 * @throws NullPointerException if the array is {@code null}
	 * @throws IllegalArgumentException if the dimensions of the array are not valid
	 */
	public static Matrix createMatrix(double[][] elements) {
		Matrix m=createMatrix(elements.length, elements[0].length);
		for (int row=0; row<m.getRowCount(); row++) {
			double[] rowData=elements[row];
			if (rowData.length!=m.getColumnCount()) throw new IllegalArgumentException("Invalid array");
			for (int col=0; col<m.getColumnCount(); col++) {
				m.set(row, col, rowData[col]);
			}
		}
		return m;
	}
	
	/**
	 * Creates a matrix and initializes its elements from an array. The parameter should be a {@code double[2][2]}
	 * array. The matrix element at row {@code row} and column {@code col} will be
	 * {@code elements[row][col]}.
	 * @param elements the array containing the elements of the matrix
	 * @return the new matrix initialized from the array
	 * @throws NullPointerException if the array is {@code null}
	 * @throws IllegalArgumentException if the dimensions of the array are not correct
	 */
	public static Matrix2 createMatrix2(double[][] elements) {
		Matrix m=createMatrix(elements);
		if (m.getRowCount()!=2 || m.getColumnCount()!=2)
			throw new IllegalArgumentException("Array describes a matrix of size "+m.getRowCount()+"x"+m.getColumnCount()+" instead of 2x2");
		return (Matrix2)m;
	}
	
	/**
	 * Creates a matrix and initializes its elements from an array. The parameter should be a {@code double[3][3]}
	 * array. The matrix element at row {@code row} and column {@code col} will be
	 * {@code elements[row][col]}.
	 * @param elements the array containing the elements of the matrix
	 * @return the new matrix initialized from the array
	 * @throws NullPointerException if the array is {@code null}
	 * @throws IllegalArgumentException if the dimensions of the array are not correct
	 */
	public static Matrix3 createMatrix3(double[][] elements) {
		Matrix m=createMatrix(elements);
		if (m.getRowCount()!=3 || m.getColumnCount()!=3)
			throw new IllegalArgumentException("Array describes a matrix of size "+m.getRowCount()+"x"+m.getColumnCount()+" instead of 3x3");
		return (Matrix3)m;
	}
	
	/**
	 * Creates a vector and initializes its elements from an array.
	 * @param elements the array containing the elements of the vector
	 * @return the new vector initialized from the array
	 * @throws NullPointerException if the array is {@code null}
	 * @throws IllegalArgumentException if the length of the array is zero
	 */
	public static Vector createVector(double[] elements) {
		Vector v=createVector(elements.length);
		for (int i=0; i<elements.length; i++)
			v.setCoordQuick(i, elements[i]);
		return v;
	}
	
	/**
	 * Creates a matrix backed by the specified data structure.
	 * @param core the data structure for the new matrix
	 * @return a matrix of proper type using the specified matrix core for its implementation
	 * @throws NullPointerException if {@code core} is {@code null}
	 */
	public static Matrix create(MatrixCore core) {
		if (core.getColumnCount()==1) {
			switch (core.getRowCount()) {
				case 2: return new CoredVector2Impl(core);
				case 3: return new CoredVector3Impl(core);
				case 4: return new CoredVector4Impl(core);
				default: return new CoredVectorImpl(core);
			}
		} else {
			if (core.getRowCount()==2 && core.getColumnCount()==2)
				return new CoredMatrix2Impl(core);
			if (core.getRowCount()==3 && core.getColumnCount()==3)
				return new CoredMatrix3Impl(core);
			return new CoredMatrixImpl(core);
		}
	}
}
220733
public VectorInfo readVectorInfo() throws IOException {
        String[] component = readTrimmedLine().split(" +");
        if (component.length != 4)
            throw new IOException(
                    "Current line unparsable. It must consist of 4 tokens");

        // Read header
        if (!component[0].equalsIgnoreCase("%%MatrixMarket"))
            throw new IOException("Not in Matrix Market exchange format");

        // This will always be "vector"
        if (!component[1].equalsIgnoreCase("vector"))
            throw new IOException("Expected \"vector\", got " + component[1]);

        // Sparse or dense?
        boolean sparse = false;
        if (component[2].equalsIgnoreCase("coordinate"))
            sparse = true;
        else if (component[2].equalsIgnoreCase("array"))
            sparse = false;
        else
            throw new IOException("Unknown layout " + component[2]);

        // Dataformat
        VectorInfo.VectorField field = null;
        if (component[3].equalsIgnoreCase("real"))
            field = VectorInfo.VectorField.Real;
        else if (component[3].equalsIgnoreCase("integer"))
            field = VectorInfo.VectorField.Integer;
        else if (component[3].equalsIgnoreCase("complex"))
            field = VectorInfo.VectorField.Complex;
        else if (component[3].equalsIgnoreCase("pattern"))
            field = VectorInfo.VectorField.Pattern;
        else
            throw new IOException("Unknown field specification " + component[3]);

        // Pack together. This also verifies the format
        return new VectorInfo(sparse, field);
    }
871994
public MatrixInfo readMatrixInfo() throws IOException {
        String[] component = readTrimmedLine().split(" +");
        if (component.length != 5)
            throw new IOException(
                    "Current line unparsable. It must consist of 5 tokens");

        // Read header
        if (!component[0].equalsIgnoreCase("%%MatrixMarket"))
            throw new IOException("Not in Matrix Market exchange format");

        // This will always be "matrix"
        if (!component[1].equalsIgnoreCase("matrix"))
            throw new IOException("Expected \"matrix\", got " + component[1]);

        // Sparse or dense?
        boolean sparse = false;
        if (component[2].equalsIgnoreCase("coordinate"))
            sparse = true;
        else if (component[2].equalsIgnoreCase("array"))
            sparse = false;
        else
            throw new IOException("Unknown layout " + component[2]);

        // Dataformat
        MatrixInfo.MatrixField field = null;
        if (component[3].equalsIgnoreCase("real"))
            field = MatrixInfo.MatrixField.Real;
        else if (component[3].equalsIgnoreCase("integer"))
            field = MatrixInfo.MatrixField.Integer;
        else if (component[3].equalsIgnoreCase("complex"))
            field = MatrixInfo.MatrixField.Complex;
        else if (component[3].equalsIgnoreCase("pattern"))
            field = MatrixInfo.MatrixField.Pattern;
        else
            throw new IOException("Unknown field specification " + component[3]);

        // Matrix pattern
        MatrixInfo.MatrixSymmetry symmetry = null;
        if (component[4].equalsIgnoreCase("general"))
            symmetry = MatrixInfo.MatrixSymmetry.General;
        else if (component[4].equalsIgnoreCase("symmetric"))
            symmetry = MatrixInfo.MatrixSymmetry.Symmetric;
        else if (component[4].equalsIgnoreCase("skew-symmetric"))
            symmetry = MatrixInfo.MatrixSymmetry.SkewSymmetric;
        else if (component[4].equalsIgnoreCase("Hermitian"))
            symmetry = MatrixInfo.MatrixSymmetry.Hermitian;
        else
            throw new IOException("Unknown symmetry specification "
                    + component[4]);

        // Pack together. This also verifies the format
        return new MatrixInfo(sparse, field, symmetry);
    }
864803
public static byte[] readByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        copy(input, output);
        return output.toByteArray();
    }
352966
public List<MapRow> read() throws IOException
   {
      List<MapRow> result = new ArrayList<MapRow>();
      int fileCount = m_stream.readInt();
      if (fileCount != 0)
      {
         for (int index = 0; index < fileCount; index++)
         {
            // We use a LinkedHashMap to preserve insertion order in iteration
            // Useful when debugging the file format.
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            readBlock(map);
            result.add(new MapRow(map));
         }
      }
      return result;
   }
241017
public Matrix transpose() {
        Matrix result = blankOfShape(columns, rows);
        MatrixIterator it = result.iterator();

        while (it.hasNext()) {
            it.next();
            int i = it.rowIndex();
            int j = it.columnIndex();
            it.set(get(j, i));
        }

        return result;
    }
1170380
public T loadCSV( String fileName )
            throws IOException {
        DMatrix mat = MatrixIO.loadCSV(fileName,true);

        T ret = createMatrix(1,1, mat.getType());

        ret.setMatrix(mat);

        return ret;
    }
802471
public MatrixSize readCoordinateSize() throws IOException {
        int numRows = getInt(), numColumns = getInt(), numEntries = getInt();

        return new MatrixSize(numRows, numColumns, numEntries);
    }
345484
public Object read() throws IOException {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("Reading "+file);
        }
        try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
            return xs.fromXML(in);
        } catch (RuntimeException | Error e) {
            throw new IOException("Unable to read "+file,e);
        }
    }
117919
public Matrix transpose() {
        Matrix A = new Matrix(N, M);
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                A.data[j][i] = data[i][j];
            }
        }
        return A;
    }