1 16 17 package org.apache.commons.math.linear; 18 19 import java.math.BigDecimal ; 20 21 26 public class MatrixUtils { 27 28 31 public MatrixUtils() { 32 super(); 33 } 34 35 45 public static RealMatrix createRealMatrix(double[][] data) { 46 return new RealMatrixImpl(data); 47 } 48 49 57 public static RealMatrix createRealIdentityMatrix(int dimension) { 58 RealMatrixImpl out = new RealMatrixImpl(dimension, dimension); 59 double[][] d = out.getDataRef(); 60 for (int row = 0; row < dimension; row++) { 61 for (int col = 0; col < dimension; col++) { 62 d[row][col] = row == col ? 1d : 0d; 63 } 64 } 65 return out; 66 } 67 68 78 public static BigMatrix createBigMatrix(double[][] data) { 79 return new BigMatrixImpl(data); 80 } 81 82 92 public static BigMatrix createBigMatrix(BigDecimal [][] data) { 93 return new BigMatrixImpl(data); 94 } 95 96 106 public static BigMatrix createBigMatrix(String [][] data) { 107 return new BigMatrixImpl(data); 108 } 109 110 119 public static RealMatrix createRowRealMatrix(double[] rowData) { 120 int nCols = rowData.length; 121 double[][] data = new double[1][nCols]; 122 System.arraycopy(rowData, 0, data[0], 0, nCols); 123 return new RealMatrixImpl(data); 124 } 125 126 135 public static BigMatrix createRowBigMatrix(double[] rowData) { 136 int nCols = rowData.length; 137 double[][] data = new double[1][nCols]; 138 System.arraycopy(rowData, 0, data[0], 0, nCols); 139 return new BigMatrixImpl(data); 140 } 141 142 151 public static BigMatrix createRowBigMatrix(BigDecimal [] rowData) { 152 int nCols = rowData.length; 153 BigDecimal [][] data = new BigDecimal [1][nCols]; 154 System.arraycopy(rowData, 0, data[0], 0, nCols); 155 return new BigMatrixImpl(data); 156 } 157 158 167 public static BigMatrix createRowBigMatrix(String [] rowData) { 168 int nCols = rowData.length; 169 String [][] data = new String [1][nCols]; 170 System.arraycopy(rowData, 0, data[0], 0, nCols); 171 return new BigMatrixImpl(data); 172 } 173 174 183 public static RealMatrix createColumnRealMatrix(double[] columnData) { 184 int nRows = columnData.length; 185 double[][] data = new double[nRows][1]; 186 for (int row = 0; row < nRows; row++) { 187 data[row][0] = columnData[row]; 188 } 189 return new RealMatrixImpl(data); 190 } 191 192 201 public static BigMatrix createColumnBigMatrix(double[] columnData) { 202 int nRows = columnData.length; 203 double[][] data = new double[nRows][1]; 204 for (int row = 0; row < nRows; row++) { 205 data[row][0] = columnData[row]; 206 } 207 return new BigMatrixImpl(data); 208 } 209 210 219 public static BigMatrix createColumnBigMatrix(BigDecimal [] columnData) { 220 int nRows = columnData.length; 221 BigDecimal [][] data = new BigDecimal [nRows][1]; 222 for (int row = 0; row < nRows; row++) { 223 data[row][0] = columnData[row]; 224 } 225 return new BigMatrixImpl(data); 226 } 227 228 237 public static BigMatrix createColumnBigMatrix(String [] columnData) { 238 int nRows = columnData.length; 239 String [][] data = new String [nRows][1]; 240 for (int row = 0; row < nRows; row++) { 241 data[row][0] = columnData[row]; 242 } 243 return new BigMatrixImpl(data); 244 } 245 246 254 public static BigMatrix createBigIdentityMatrix(int dimension) { 255 BigMatrixImpl out = new BigMatrixImpl(dimension, dimension); 256 BigDecimal [][] d = out.getDataRef(); 257 for (int row = 0; row < dimension; row++) { 258 for (int col = 0; col < dimension; col++) { 259 d[row][col] = row == col ? BigMatrixImpl.ONE : BigMatrixImpl.ZERO; 260 } 261 } 262 return out; 263 } 264 265 } 266 267 | Popular Tags |