1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/* 2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more 3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements. See the NOTICE file distributed with 4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership. 5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0 6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with 7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License. You may obtain a copy of the License at 8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * http://www.apache.org/licenses/LICENSE-2.0 10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software 12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS, 13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and 15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License. 16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.linear; 19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.IOException; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.ObjectInputStream; 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.ObjectOutputStream; 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.lang.reflect.Array; 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.math.BigDecimal; 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.util.Arrays; 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.Field; 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.FieldElement; 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathRuntimeException; 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats; 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.fraction.BigFraction; 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.fraction.Fraction; 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * A collection of static methods that operate on or return matrices. 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 983921 $ $Date: 2010-08-10 12:46:06 +0200 (mar. 10 août 2010) $ 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class MatrixUtils { 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Private constructor. 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private MatrixUtils() { 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(); 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a {@link RealMatrix} with specified dimensions. 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The type of matrix returned depends on the dimension. Below 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 2<sup>12</sup> elements (i.e. 4096 elements or 64×64 for a 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * square matrix) which can be stored in a 32kB array, a {@link 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Array2DRowRealMatrix} instance is built. Above this threshold a {@link 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * BlockRealMatrix} instance is built.</p> 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The matrix elements are all set to 0.0.</p> 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param rows number of rows of the matrix 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param columns number of columns of the matrix 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return RealMatrix with specified dimensions 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #createRealMatrix(double[][]) 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static RealMatrix createRealMatrix(final int rows, final int columns) { 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return (rows * columns <= 4096) ? 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond new Array2DRowRealMatrix(rows, columns) : new BlockRealMatrix(rows, columns); 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a {@link FieldMatrix} with specified dimensions. 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The type of matrix returned depends on the dimension. Below 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 2<sup>12</sup> elements (i.e. 4096 elements or 64×64 for a 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * square matrix), a {@link FieldMatrix} instance is built. Above 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this threshold a {@link BlockFieldMatrix} instance is built.</p> 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The matrix elements are all set to field.getZero().</p> 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param <T> the type of the field elements 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param field field to which the matrix elements belong 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param rows number of rows of the matrix 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param columns number of columns of the matrix 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return FieldMatrix with specified dimensions 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #createFieldMatrix(FieldElement[][]) 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.0 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static <T extends FieldElement<T>> FieldMatrix<T> createFieldMatrix(final Field<T> field, 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int rows, 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int columns) { 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return (rows * columns <= 4096) ? 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond new Array2DRowFieldMatrix<T>(field, rows, columns) : new BlockFieldMatrix<T>(field, rows, columns); 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a {@link RealMatrix} whose entries are the the values in the 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the input array. 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The type of matrix returned depends on the dimension. Below 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 2<sup>12</sup> elements (i.e. 4096 elements or 64×64 for a 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * square matrix) which can be stored in a 32kB array, a {@link 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Array2DRowRealMatrix} instance is built. Above this threshold a {@link 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * BlockRealMatrix} instance is built.</p> 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The input array is copied, not referenced.</p> 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param data input array 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return RealMatrix containing the values of the array 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>data</code> is not rectangular 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (not all rows have the same length) or empty 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if either <code>data</code> or 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>data[0]</code> is null 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #createRealMatrix(int, int) 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static RealMatrix createRealMatrix(double[][] data) { 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return (data.length * data[0].length <= 4096) ? 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond new Array2DRowRealMatrix(data) : new BlockRealMatrix(data); 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a {@link FieldMatrix} whose entries are the the values in the 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the input array. 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The type of matrix returned depends on the dimension. Below 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 2<sup>12</sup> elements (i.e. 4096 elements or 64×64 for a 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * square matrix), a {@link FieldMatrix} instance is built. Above 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this threshold a {@link BlockFieldMatrix} instance is built.</p> 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The input array is copied, not referenced.</p> 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param <T> the type of the field elements 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param data input array 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return RealMatrix containing the values of the array 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>data</code> is not rectangular 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (not all rows have the same length) or empty 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if either <code>data</code> or 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>data[0]</code> is null 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #createFieldMatrix(Field, int, int) 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.0 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static <T extends FieldElement<T>> FieldMatrix<T> createFieldMatrix(T[][] data) { 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return (data.length * data[0].length <= 4096) ? 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond new Array2DRowFieldMatrix<T>(data) : new BlockFieldMatrix<T>(data); 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns <code>dimension x dimension</code> identity matrix. 136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param dimension dimension of identity matrix to generate 138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return identity matrix 139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if dimension is not positive 140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.1 141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static RealMatrix createRealIdentityMatrix(int dimension) { 143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final RealMatrix m = createRealMatrix(dimension, dimension); 144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < dimension; ++i) { 145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.setEntry(i, i, 1.0); 146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return m; 148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns <code>dimension x dimension</code> identity matrix. 152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param <T> the type of the field elements 154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param field field to which the elements belong 155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param dimension dimension of identity matrix to generate 156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return identity matrix 157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if dimension is not positive 158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.0 159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static <T extends FieldElement<T>> FieldMatrix<T> 161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond createFieldIdentityMatrix(final Field<T> field, final int dimension) { 162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final T zero = field.getZero(); 163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final T one = field.getOne(); 164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @SuppressWarnings("unchecked") // zero is type T 165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final T[][] d = (T[][]) Array.newInstance(zero.getClass(), new int[] { dimension, dimension }); 166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int row = 0; row < dimension; row++) { 167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final T[] dRow = d[row]; 168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond Arrays.fill(dRow, zero); 169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dRow[row] = one; 170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new Array2DRowFieldMatrix<T>(d, false); 172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns <code>dimension x dimension</code> identity matrix. 176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param dimension dimension of identity matrix to generate 178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return identity matrix 179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if dimension is not positive 180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.1 181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0, replaced by {@link #createFieldIdentityMatrix(Field, int)} 182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createBigIdentityMatrix(int dimension) { 185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigDecimal[][] d = new BigDecimal[dimension][dimension]; 186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int row = 0; row < dimension; row++) { 187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigDecimal[] dRow = d[row]; 188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond Arrays.fill(dRow, BigMatrixImpl.ZERO); 189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dRow[row] = BigMatrixImpl.ONE; 190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(d, false); 192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a diagonal matrix with specified elements. 196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param diagonal diagonal elements of the matrix (the array elements 198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * will be copied) 199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return diagonal matrix 200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.0 201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static RealMatrix createRealDiagonalMatrix(final double[] diagonal) { 203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final RealMatrix m = createRealMatrix(diagonal.length, diagonal.length); 204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < diagonal.length; ++i) { 205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.setEntry(i, i, diagonal[i]); 206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return m; 208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a diagonal matrix with specified elements. 212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param <T> the type of the field elements 214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param diagonal diagonal elements of the matrix (the array elements 215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * will be copied) 216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return diagonal matrix 217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.0 218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static <T extends FieldElement<T>> FieldMatrix<T> 220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond createFieldDiagonalMatrix(final T[] diagonal) { 221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final FieldMatrix<T> m = 222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond createFieldMatrix(diagonal[0].getField(), diagonal.length, diagonal.length); 223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < diagonal.length; ++i) { 224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.setEntry(i, i, diagonal[i]); 225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return m; 227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a {@link BigMatrix} whose entries are the the values in the 231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the input array. The input array is copied, not referenced. 232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param data input array 234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return RealMatrix containing the values of the array 235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>data</code> is not rectangular 236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (not all rows have the same length) or empty 237dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if data is null 238dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createFieldMatrix(FieldElement[][])} 239dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 240dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 241dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createBigMatrix(double[][] data) { 242dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data); 243dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 244dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 245dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 246dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a {@link BigMatrix} whose entries are the the values in the 247dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the input array. The input array is copied, not referenced. 248dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 249dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param data input array 250dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return RealMatrix containing the values of the array 251dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>data</code> is not rectangular 252dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (not all rows have the same length) or empty 253dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if data is null 254dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createFieldMatrix(FieldElement[][])} 255dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 256dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 257dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createBigMatrix(BigDecimal[][] data) { 258dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data); 259dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 260dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 261dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 262dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a {@link BigMatrix} whose entries are the the values in the 263dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the input array. 264dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>If an array is built specially in order to be embedded in a 265dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * BigMatrix and not used directly, the <code>copyArray</code> may be 266dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * set to <code>false</code. This will prevent the copying and improve 267dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * performance as no new array will be built and no data will be copied.</p> 268dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param data data for new matrix 269dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param copyArray if true, the input array will be copied, otherwise 270dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * it will be referenced 271dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return BigMatrix containing the values of the array 272dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>data</code> is not rectangular 273dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (not all rows have the same length) or empty 274dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>data</code> is null 275dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #createRealMatrix(double[][]) 276dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createFieldMatrix(FieldElement[][])} 277dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 278dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 279dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createBigMatrix(BigDecimal[][] data, boolean copyArray) { 280dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data, copyArray); 281dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 282dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 283dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 284dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a {@link BigMatrix} whose entries are the the values in the 285dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the input array. The input array is copied, not referenced. 286dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 287dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param data input array 288dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return RealMatrix containing the values of the array 289dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>data</code> is not rectangular 290dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (not all rows have the same length) or empty 291dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if data is null 292dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createFieldMatrix(FieldElement[][])} 293dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 294dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 295dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createBigMatrix(String[][] data) { 296dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data); 297dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 298dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 299dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 300dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a {@link RealVector} using the data from the input array. 301dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 302dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param data the input data 303dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a data.length RealVector 304dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>data</code> is empty 305dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>data</code>is null 306dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 307dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static RealVector createRealVector(double[] data) { 308dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new ArrayRealVector(data, true); 309dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 310dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 311dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 312dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a {@link FieldVector} using the data from the input array. 313dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 314dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param <T> the type of the field elements 315dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param data the input data 316dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a data.length FieldVector 317dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>data</code> is empty 318dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>data</code>is null 319dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 320dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static <T extends FieldElement<T>> FieldVector<T> createFieldVector(final T[] data) { 321dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new ArrayFieldVector<T>(data, true); 322dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 323dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 324dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 325dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a row {@link RealMatrix} using the data from the input 326dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 327dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 328dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param rowData the input row data 329dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a 1 x rowData.length RealMatrix 330dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>rowData</code> is empty 331dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>rowData</code>is null 332dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 333dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static RealMatrix createRowRealMatrix(double[] rowData) { 334dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nCols = rowData.length; 335dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final RealMatrix m = createRealMatrix(1, nCols); 336dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < nCols; ++i) { 337dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.setEntry(0, i, rowData[i]); 338dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 339dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return m; 340dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 341dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 342dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 343dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a row {@link FieldMatrix} using the data from the input 344dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 345dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 346dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param <T> the type of the field elements 347dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param rowData the input row data 348dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a 1 x rowData.length FieldMatrix 349dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>rowData</code> is empty 350dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>rowData</code>is null 351dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 352dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static <T extends FieldElement<T>> FieldMatrix<T> 353dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond createRowFieldMatrix(final T[] rowData) { 354dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nCols = rowData.length; 355dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (nCols == 0) { 356dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.AT_LEAST_ONE_COLUMN); 357dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 358dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final FieldMatrix<T> m = createFieldMatrix(rowData[0].getField(), 1, nCols); 359dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < nCols; ++i) { 360dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.setEntry(0, i, rowData[i]); 361dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 362dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return m; 363dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 364dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 365dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 366dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a row {@link BigMatrix} using the data from the input 367dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 368dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 369dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param rowData the input row data 370dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a 1 x rowData.length BigMatrix 371dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>rowData</code> is empty 372dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>rowData</code>is null 373dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createRowFieldMatrix(FieldElement[])} 374dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 375dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 376dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createRowBigMatrix(double[] rowData) { 377dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nCols = rowData.length; 378dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigDecimal[][] data = new BigDecimal[1][nCols]; 379dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < nCols; ++i) { 380dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data[0][i] = new BigDecimal(rowData[i]); 381dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 382dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data, false); 383dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 384dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 385dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 386dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a row {@link BigMatrix} using the data from the input 387dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 388dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 389dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param rowData the input row data 390dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a 1 x rowData.length BigMatrix 391dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>rowData</code> is empty 392dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>rowData</code>is null 393dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createRowFieldMatrix(FieldElement[])} 394dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 395dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 396dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createRowBigMatrix(BigDecimal[] rowData) { 397dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nCols = rowData.length; 398dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigDecimal[][] data = new BigDecimal[1][nCols]; 399dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond System.arraycopy(rowData, 0, data[0], 0, nCols); 400dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data, false); 401dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 402dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 403dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 404dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a row {@link BigMatrix} using the data from the input 405dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 406dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 407dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param rowData the input row data 408dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a 1 x rowData.length BigMatrix 409dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>rowData</code> is empty 410dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>rowData</code>is null 411dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createRowFieldMatrix(FieldElement[])} 412dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 413dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 414dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createRowBigMatrix(String[] rowData) { 415dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nCols = rowData.length; 416dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigDecimal[][] data = new BigDecimal[1][nCols]; 417dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < nCols; ++i) { 418dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data[0][i] = new BigDecimal(rowData[i]); 419dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 420dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data, false); 421dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 422dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 423dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 424dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a column {@link RealMatrix} using the data from the input 425dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 426dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 427dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param columnData the input column data 428dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a columnData x 1 RealMatrix 429dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>columnData</code> is empty 430dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>columnData</code>is null 431dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 432dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static RealMatrix createColumnRealMatrix(double[] columnData) { 433dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nRows = columnData.length; 434dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final RealMatrix m = createRealMatrix(nRows, 1); 435dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < nRows; ++i) { 436dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.setEntry(i, 0, columnData[i]); 437dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 438dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return m; 439dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 440dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 441dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 442dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a column {@link FieldMatrix} using the data from the input 443dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 444dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 445dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param <T> the type of the field elements 446dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param columnData the input column data 447dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a columnData x 1 FieldMatrix 448dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>columnData</code> is empty 449dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>columnData</code>is null 450dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 451dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static <T extends FieldElement<T>> FieldMatrix<T> 452dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond createColumnFieldMatrix(final T[] columnData) { 453dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nRows = columnData.length; 454dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (nRows == 0) { 455dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.AT_LEAST_ONE_ROW); 456dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 457dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final FieldMatrix<T> m = createFieldMatrix(columnData[0].getField(), nRows, 1); 458dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < nRows; ++i) { 459dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.setEntry(i, 0, columnData[i]); 460dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 461dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return m; 462dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 463dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 464dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 465dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a column {@link BigMatrix} using the data from the input 466dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 467dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 468dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param columnData the input column data 469dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a columnData x 1 BigMatrix 470dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>columnData</code> is empty 471dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>columnData</code>is null 472dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createColumnFieldMatrix(FieldElement[])} 473dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 474dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 475dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createColumnBigMatrix(double[] columnData) { 476dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nRows = columnData.length; 477dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigDecimal[][] data = new BigDecimal[nRows][1]; 478dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int row = 0; row < nRows; row++) { 479dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data[row][0] = new BigDecimal(columnData[row]); 480dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 481dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data, false); 482dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 483dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 484dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 485dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a column {@link BigMatrix} using the data from the input 486dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 487dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 488dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param columnData the input column data 489dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a columnData x 1 BigMatrix 490dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>columnData</code> is empty 491dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>columnData</code>is null 492dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createColumnFieldMatrix(FieldElement[])} 493dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 494dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 495dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createColumnBigMatrix(BigDecimal[] columnData) { 496dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nRows = columnData.length; 497dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigDecimal[][] data = new BigDecimal[nRows][1]; 498dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int row = 0; row < nRows; row++) { 499dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data[row][0] = columnData[row]; 500dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 501dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data, false); 502dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 503dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 504dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 505dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Creates a column {@link BigMatrix} using the data from the input 506dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array. 507dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 508dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param columnData the input column data 509dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a columnData x 1 BigMatrix 510dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if <code>columnData</code> is empty 511dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NullPointerException if <code>columnData</code>is null 512dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated since 2.0 replaced by {@link #createColumnFieldMatrix(FieldElement[])} 513dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 514dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 515dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static BigMatrix createColumnBigMatrix(String[] columnData) { 516dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int nRows = columnData.length; 517dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigDecimal[][] data = new BigDecimal[nRows][1]; 518dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int row = 0; row < nRows; row++) { 519dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data[row][0] = new BigDecimal(columnData[row]); 520dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 521dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BigMatrixImpl(data, false); 522dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 523dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 524dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 525dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Check if a row index is valid. 526dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param m matrix containing the submatrix 527dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param row row index to check 528dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception MatrixIndexException if index is not valid 529dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 530dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void checkRowIndex(final AnyMatrix m, final int row) { 531dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (row < 0 || row >= m.getRowDimension()) { 532dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new MatrixIndexException(LocalizedFormats.ROW_INDEX_OUT_OF_RANGE, 533dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond row, 0, m.getRowDimension() - 1); 534dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 535dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 536dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 537dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 538dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Check if a column index is valid. 539dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param m matrix containing the submatrix 540dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param column column index to check 541dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception MatrixIndexException if index is not valid 542dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 543dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void checkColumnIndex(final AnyMatrix m, final int column) 544dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MatrixIndexException { 545dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (column < 0 || column >= m.getColumnDimension()) { 546dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new MatrixIndexException(LocalizedFormats.COLUMN_INDEX_OUT_OF_RANGE, 547dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond column, 0, m.getColumnDimension() - 1); 548dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 549dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 550dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 551dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 552dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Check if submatrix ranges indices are valid. 553dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Rows and columns are indicated counting from 0 to n-1. 554dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 555dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param m matrix containing the submatrix 556dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param startRow Initial row index 557dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param endRow Final row index 558dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param startColumn Initial column index 559dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param endColumn Final column index 560dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception MatrixIndexException if the indices are not valid 561dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 562dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void checkSubMatrixIndex(final AnyMatrix m, 563dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int startRow, final int endRow, 564dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int startColumn, final int endColumn) { 565dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond checkRowIndex(m, startRow); 566dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond checkRowIndex(m, endRow); 567dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (startRow > endRow) { 568dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new MatrixIndexException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW, 569dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond startRow, endRow); 570dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 571dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 572dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond checkColumnIndex(m, startColumn); 573dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond checkColumnIndex(m, endColumn); 574dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (startColumn > endColumn) { 575dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new MatrixIndexException(LocalizedFormats.INITIAL_COLUMN_AFTER_FINAL_COLUMN, 576dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond startColumn, endColumn); 577dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 578dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 579dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 580dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 581dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 582dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 583dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Check if submatrix ranges indices are valid. 584dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Rows and columns are indicated counting from 0 to n-1. 585dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 586dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param m matrix containing the submatrix 587dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param selectedRows Array of row indices. 588dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param selectedColumns Array of column indices. 589dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception MatrixIndexException if row or column selections are not valid 590dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 591dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void checkSubMatrixIndex(final AnyMatrix m, 592dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int[] selectedRows, final int[] selectedColumns) 593dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MatrixIndexException { 594dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (selectedRows.length * selectedColumns.length == 0) { 595dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (selectedRows.length == 0) { 596dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new MatrixIndexException(LocalizedFormats.EMPTY_SELECTED_ROW_INDEX_ARRAY); 597dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 598dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new MatrixIndexException(LocalizedFormats.EMPTY_SELECTED_COLUMN_INDEX_ARRAY); 599dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 600dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 601dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (final int row : selectedRows) { 602dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond checkRowIndex(m, row); 603dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 604dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (final int column : selectedColumns) { 605dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond checkColumnIndex(m, column); 606dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 607dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 608dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 609dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 610dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Check if matrices are addition compatible 611dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param left left hand side matrix 612dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param right right hand side matrix 613dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IllegalArgumentException if matrices are not addition compatible 614dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 615dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) 616dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IllegalArgumentException { 617dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if ((left.getRowDimension() != right.getRowDimension()) || 618dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond (left.getColumnDimension() != right.getColumnDimension())) { 619dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw MathRuntimeException.createIllegalArgumentException( 620dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond LocalizedFormats.NOT_ADDITION_COMPATIBLE_MATRICES, 621dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond left.getRowDimension(), left.getColumnDimension(), 622dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond right.getRowDimension(), right.getColumnDimension()); 623dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 624dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 625dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 626dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 627dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Check if matrices are subtraction compatible 628dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param left left hand side matrix 629dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param right right hand side matrix 630dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IllegalArgumentException if matrices are not subtraction compatible 631dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 632dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) 633dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IllegalArgumentException { 634dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if ((left.getRowDimension() != right.getRowDimension()) || 635dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond (left.getColumnDimension() != right.getColumnDimension())) { 636dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw MathRuntimeException.createIllegalArgumentException( 637dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond LocalizedFormats.NOT_SUBTRACTION_COMPATIBLE_MATRICES, 638dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond left.getRowDimension(), left.getColumnDimension(), 639dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond right.getRowDimension(), right.getColumnDimension()); 640dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 641dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 642dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 643dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 644dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Check if matrices are multiplication compatible 645dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param left left hand side matrix 646dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param right right hand side matrix 647dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IllegalArgumentException if matrices are not multiplication compatible 648dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 649dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) 650dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IllegalArgumentException { 651dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (left.getColumnDimension() != right.getRowDimension()) { 652dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw MathRuntimeException.createIllegalArgumentException( 653dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond LocalizedFormats.NOT_MULTIPLICATION_COMPATIBLE_MATRICES, 654dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond left.getRowDimension(), left.getColumnDimension(), 655dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond right.getRowDimension(), right.getColumnDimension()); 656dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 657dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 658dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 659dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 660dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Convert a {@link FieldMatrix}/{@link Fraction} matrix to a {@link RealMatrix}. 661dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param m matrix to convert 662dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return converted matrix 663dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 664dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static Array2DRowRealMatrix fractionMatrixToRealMatrix(final FieldMatrix<Fraction> m) { 665dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final FractionMatrixConverter converter = new FractionMatrixConverter(); 666dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.walkInOptimizedOrder(converter); 667dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return converter.getConvertedMatrix(); 668dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 669dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 670dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Converter for {@link FieldMatrix}/{@link Fraction}. */ 671dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private static class FractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor<Fraction> { 672dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 673dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Converted array. */ 674dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private double[][] data; 675dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 676dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Simple constructor. */ 677dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public FractionMatrixConverter() { 678dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(Fraction.ZERO); 679dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 680dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 681dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 682dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 683dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void start(int rows, int columns, 684dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int startRow, int endRow, int startColumn, int endColumn) { 685dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data = new double[rows][columns]; 686dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 687dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 688dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 689dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 690dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void visit(int row, int column, Fraction value) { 691dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data[row][column] = value.doubleValue(); 692dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 693dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 694dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Get the converted matrix. 695dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return converted matrix 696dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 697dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond Array2DRowRealMatrix getConvertedMatrix() { 698dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new Array2DRowRealMatrix(data, false); 699dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 700dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 701dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 702dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 703dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 704dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Convert a {@link FieldMatrix}/{@link BigFraction} matrix to a {@link RealMatrix}. 705dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param m matrix to convert 706dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return converted matrix 707dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 708dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(final FieldMatrix<BigFraction> m) { 709dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final BigFractionMatrixConverter converter = new BigFractionMatrixConverter(); 710dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m.walkInOptimizedOrder(converter); 711dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return converter.getConvertedMatrix(); 712dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 713dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 714dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Converter for {@link FieldMatrix}/{@link BigFraction}. */ 715dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private static class BigFractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor<BigFraction> { 716dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 717dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Converted array. */ 718dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private double[][] data; 719dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 720dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Simple constructor. */ 721dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public BigFractionMatrixConverter() { 722dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(BigFraction.ZERO); 723dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 724dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 725dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 726dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 727dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void start(int rows, int columns, 728dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int startRow, int endRow, int startColumn, int endColumn) { 729dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data = new double[rows][columns]; 730dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 731dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 732dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 733dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 734dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void visit(int row, int column, BigFraction value) { 735dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data[row][column] = value.doubleValue(); 736dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 737dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 738dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Get the converted matrix. 739dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return converted matrix 740dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 741dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond Array2DRowRealMatrix getConvertedMatrix() { 742dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new Array2DRowRealMatrix(data, false); 743dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 744dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 745dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 746dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 747dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Serialize a {@link RealVector}. 748dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 749dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This method is intended to be called from within a private 750dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>writeObject</code> method (after a call to 751dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>oos.defaultWriteObject()</code>) in a class that has a 752dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link RealVector} field, which should be declared <code>transient</code>. 753dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This way, the default handling does not serialize the vector (the {@link 754dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * RealVector} interface is not serializable by default) but this method does 755dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * serialize it specifically. 756dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 757dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 758dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The following example shows how a simple class with a name and a real vector 759dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * should be written: 760dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre><code> 761dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * public class NamedVector implements Serializable { 762dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 763dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * private final String name; 764dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * private final transient RealVector coefficients; 765dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 766dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * // omitted constructors, getters ... 767dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 768dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * private void writeObject(ObjectOutputStream oos) throws IOException { 769dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * oos.defaultWriteObject(); // takes care of name field 770dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * MatrixUtils.serializeRealVector(coefficients, oos); 771dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * } 772dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 773dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { 774dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * ois.defaultReadObject(); // takes care of name field 775dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * MatrixUtils.deserializeRealVector(this, "coefficients", ois); 776dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * } 777dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 778dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * } 779dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </code></pre> 780dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 781dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 782dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param vector real vector to serialize 783dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param oos stream where the real vector should be written 784dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IOException if object cannot be written to stream 785dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #deserializeRealVector(Object, String, ObjectInputStream) 786dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 787dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void serializeRealVector(final RealVector vector, 788dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final ObjectOutputStream oos) 789dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IOException { 790dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int n = vector.getDimension(); 791dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond oos.writeInt(n); 792dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < n; ++i) { 793dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond oos.writeDouble(vector.getEntry(i)); 794dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 795dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 796dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 797dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Deserialize a {@link RealVector} field in a class. 798dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 799dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This method is intended to be called from within a private 800dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>readObject</code> method (after a call to 801dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>ois.defaultReadObject()</code>) in a class that has a 802dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link RealVector} field, which should be declared <code>transient</code>. 803dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This way, the default handling does not deserialize the vector (the {@link 804dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * RealVector} interface is not serializable by default) but this method does 805dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * deserialize it specifically. 806dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 807dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param instance instance in which the field must be set up 808dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param fieldName name of the field within the class (may be private and final) 809dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param ois stream from which the real vector should be read 810dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception ClassNotFoundException if a class in the stream cannot be found 811dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IOException if object cannot be read from the stream 812dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #serializeRealVector(RealVector, ObjectOutputStream) 813dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 814dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void deserializeRealVector(final Object instance, 815dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final String fieldName, 816dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final ObjectInputStream ois) 817dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws ClassNotFoundException, IOException { 818dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond try { 819dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 820dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // read the vector data 821dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int n = ois.readInt(); 822dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] data = new double[n]; 823dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < n; ++i) { 824dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond data[i] = ois.readDouble(); 825dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 826dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 827dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // create the instance 828dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final RealVector vector = new ArrayRealVector(data, false); 829dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 830dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // set up the field 831dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final java.lang.reflect.Field f = 832dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond instance.getClass().getDeclaredField(fieldName); 833dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond f.setAccessible(true); 834dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond f.set(instance, vector); 835dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 836dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } catch (NoSuchFieldException nsfe) { 837dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond IOException ioe = new IOException(); 838dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ioe.initCause(nsfe); 839dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw ioe; 840dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } catch (IllegalAccessException iae) { 841dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond IOException ioe = new IOException(); 842dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ioe.initCause(iae); 843dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw ioe; 844dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 845dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 846dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 847dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 848dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Serialize a {@link RealMatrix}. 849dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 850dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This method is intended to be called from within a private 851dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>writeObject</code> method (after a call to 852dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>oos.defaultWriteObject()</code>) in a class that has a 853dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link RealMatrix} field, which should be declared <code>transient</code>. 854dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This way, the default handling does not serialize the matrix (the {@link 855dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * RealMatrix} interface is not serializable by default) but this method does 856dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * serialize it specifically. 857dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 858dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 859dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The following example shows how a simple class with a name and a real matrix 860dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * should be written: 861dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre><code> 862dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * public class NamedMatrix implements Serializable { 863dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 864dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * private final String name; 865dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * private final transient RealMatrix coefficients; 866dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 867dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * // omitted constructors, getters ... 868dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 869dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * private void writeObject(ObjectOutputStream oos) throws IOException { 870dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * oos.defaultWriteObject(); // takes care of name field 871dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * MatrixUtils.serializeRealMatrix(coefficients, oos); 872dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * } 873dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 874dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { 875dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * ois.defaultReadObject(); // takes care of name field 876dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * MatrixUtils.deserializeRealMatrix(this, "coefficients", ois); 877dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * } 878dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 879dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * } 880dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </code></pre> 881dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 882dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 883dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param matrix real matrix to serialize 884dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param oos stream where the real matrix should be written 885dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IOException if object cannot be written to stream 886dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #deserializeRealMatrix(Object, String, ObjectInputStream) 887dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 888dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void serializeRealMatrix(final RealMatrix matrix, 889dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final ObjectOutputStream oos) 890dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IOException { 891dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int n = matrix.getRowDimension(); 892dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int m = matrix.getColumnDimension(); 893dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond oos.writeInt(n); 894dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond oos.writeInt(m); 895dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < n; ++i) { 896dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < m; ++j) { 897dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond oos.writeDouble(matrix.getEntry(i, j)); 898dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 899dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 900dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 901dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 902dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Deserialize a {@link RealMatrix} field in a class. 903dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 904dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This method is intended to be called from within a private 905dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>readObject</code> method (after a call to 906dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>ois.defaultReadObject()</code>) in a class that has a 907dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link RealMatrix} field, which should be declared <code>transient</code>. 908dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This way, the default handling does not deserialize the matrix (the {@link 909dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * RealMatrix} interface is not serializable by default) but this method does 910dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * deserialize it specifically. 911dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 912dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param instance instance in which the field must be set up 913dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param fieldName name of the field within the class (may be private and final) 914dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param ois stream from which the real matrix should be read 915dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception ClassNotFoundException if a class in the stream cannot be found 916dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IOException if object cannot be read from the stream 917dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see #serializeRealMatrix(RealMatrix, ObjectOutputStream) 918dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 919dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static void deserializeRealMatrix(final Object instance, 920dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final String fieldName, 921dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final ObjectInputStream ois) 922dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws ClassNotFoundException, IOException { 923dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond try { 924dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 925dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // read the matrix data 926dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int n = ois.readInt(); 927dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int m = ois.readInt(); 928dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] data = new double[n][m]; 929dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < n; ++i) { 930dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] dataI = data[i]; 931dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < m; ++j) { 932dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dataI[j] = ois.readDouble(); 933dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 934dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 935dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 936dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // create the instance 937dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final RealMatrix matrix = new Array2DRowRealMatrix(data, false); 938dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 939dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // set up the field 940dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final java.lang.reflect.Field f = 941dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond instance.getClass().getDeclaredField(fieldName); 942dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond f.setAccessible(true); 943dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond f.set(instance, matrix); 944dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 945dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } catch (NoSuchFieldException nsfe) { 946dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond IOException ioe = new IOException(); 947dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ioe.initCause(nsfe); 948dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw ioe; 949dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } catch (IllegalAccessException iae) { 950dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond IOException ioe = new IOException(); 951dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ioe.initCause(iae); 952dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw ioe; 953dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 954dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 955dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 956dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 957dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 958