1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.linear;
18
19 import java.math.BigDecimal;
20
21 /***
22 * A collection of static methods that operate on or return matrices.
23 *
24 * @version $Revision: 1.2 $ $Date: 2004/10/25 05:36:15 $
25 */
26 public class MatrixUtils {
27
28 /***
29 * Default constructor. Package scope to prevent unwanted instantiation.
30 */
31 public MatrixUtils() {
32 super();
33 }
34
35 /***
36 * Returns a {@link RealMatrix} whose entries are the the values in the
37 * the input array. The input array is copied, not referenced.
38 *
39 * @param data input array
40 * @return RealMatrix containing the values of the array
41 * @throws IllegalArgumentException if <code>data</code> is not rectangular
42 * (not all rows have the same length) or empty
43 * @throws NullPointerException if data is null
44 */
45 public static RealMatrix createRealMatrix(double[][] data) {
46 return new RealMatrixImpl(data);
47 }
48
49 /***
50 * Returns a {@link BigMatrix} whose entries are the the values in the
51 * the input array. The input array is copied, not referenced.
52 *
53 * @param data input array
54 * @return RealMatrix containing the values of the array
55 * @throws IllegalArgumentException if <code>data</code> is not rectangular
56 * (not all rows have the same length) or empty
57 * @throws NullPointerException if data is null
58 */
59 public static BigMatrix createBigMatrix(double[][] data) {
60 return new BigMatrixImpl(data);
61 }
62
63 /***
64 * Returns a {@link BigMatrix} whose entries are the the values in the
65 * the input array. The input array is copied, not referenced.
66 *
67 * @param data input array
68 * @return RealMatrix containing the values of the array
69 * @throws IllegalArgumentException if <code>data</code> is not rectangular
70 * (not all rows have the same length) or empty
71 * @throws NullPointerException if data is null
72 */
73 public static BigMatrix createBigMatrix(BigDecimal[][] data) {
74 return new BigMatrixImpl(data);
75 }
76
77 /***
78 * Returns a {@link BigMatrix} whose entries are the the values in the
79 * the input array. The input array is copied, not referenced.
80 *
81 * @param data input array
82 * @return RealMatrix containing the values of the array
83 * @throws IllegalArgumentException if <code>data</code> is not rectangular
84 * (not all rows have the same length) or empty
85 * @throws NullPointerException if data is null
86 */
87 public static BigMatrix createBigMatrix(String[][] data) {
88 return new BigMatrixImpl(data);
89 }
90
91 /***
92 * Creates a row {@link RealMatrix} using the data from the input
93 * array.
94 *
95 * @param rowData the input row data
96 * @return a 1 x rowData.length RealMatrix
97 * @throws IllegalArgumentException if <code>rowData</code> is empty
98 * @throws NullPointerException if <code>rowData</code>is null
99 */
100 public static RealMatrix createRowRealMatrix(double[] rowData) {
101 int nCols = rowData.length;
102 double[][] data = new double[1][nCols];
103 System.arraycopy(rowData, 0, data[0], 0, nCols);
104 return new RealMatrixImpl(data);
105 }
106
107 /***
108 * Creates a row {@link BigMatrix} using the data from the input
109 * array.
110 *
111 * @param rowData the input row data
112 * @return a 1 x rowData.length BigMatrix
113 * @throws IllegalArgumentException if <code>rowData</code> is empty
114 * @throws NullPointerException if <code>rowData</code>is null
115 */
116 public static BigMatrix createRowBigMatrix(double[] rowData) {
117 int nCols = rowData.length;
118 double[][] data = new double[1][nCols];
119 System.arraycopy(rowData, 0, data[0], 0, nCols);
120 return new BigMatrixImpl(data);
121 }
122
123 /***
124 * Creates a row {@link BigMatrix} using the data from the input
125 * array.
126 *
127 * @param rowData the input row data
128 * @return a 1 x rowData.length BigMatrix
129 * @throws IllegalArgumentException if <code>rowData</code> is empty
130 * @throws NullPointerException if <code>rowData</code>is null
131 */
132 public static BigMatrix createRowBigMatrix(BigDecimal[] rowData) {
133 int nCols = rowData.length;
134 BigDecimal[][] data = new BigDecimal[1][nCols];
135 System.arraycopy(rowData, 0, data[0], 0, nCols);
136 return new BigMatrixImpl(data);
137 }
138
139 /***
140 * Creates a row {@link BigMatrix} using the data from the input
141 * array.
142 *
143 * @param rowData the input row data
144 * @return a 1 x rowData.length BigMatrix
145 * @throws IllegalArgumentException if <code>rowData</code> is empty
146 * @throws NullPointerException if <code>rowData</code>is null
147 */
148 public static BigMatrix createRowBigMatrix(String[] rowData) {
149 int nCols = rowData.length;
150 String[][] data = new String[1][nCols];
151 System.arraycopy(rowData, 0, data[0], 0, nCols);
152 return new BigMatrixImpl(data);
153 }
154
155 /***
156 * Creates a column {@link RealMatrix} using the data from the input
157 * array.
158 *
159 * @param columnData the input column data
160 * @return a columnData x 1 RealMatrix
161 * @throws IllegalArgumentException if <code>columnData</code> is empty
162 * @throws NullPointerException if <code>columnData</code>is null
163 */
164 public static RealMatrix createColumnRealMatrix(double[] columnData) {
165 int nRows = columnData.length;
166 double[][] data = new double[nRows][1];
167 for (int row = 0; row < nRows; row++) {
168 data[row][0] = columnData[row];
169 }
170 return new RealMatrixImpl(data);
171 }
172
173 /***
174 * Creates a column {@link BigMatrix} using the data from the input
175 * array.
176 *
177 * @param columnData the input column data
178 * @return a columnData x 1 BigMatrix
179 * @throws IllegalArgumentException if <code>columnData</code> is empty
180 * @throws NullPointerException if <code>columnData</code>is null
181 */
182 public static BigMatrix createColumnBigMatrix(double[] columnData) {
183 int nRows = columnData.length;
184 double[][] data = new double[nRows][1];
185 for (int row = 0; row < nRows; row++) {
186 data[row][0] = columnData[row];
187 }
188 return new BigMatrixImpl(data);
189 }
190
191 /***
192 * Creates a column {@link BigMatrix} using the data from the input
193 * array.
194 *
195 * @param columnData the input column data
196 * @return a columnData x 1 BigMatrix
197 * @throws IllegalArgumentException if <code>columnData</code> is empty
198 * @throws NullPointerException if <code>columnData</code>is null
199 */
200 public static BigMatrix createColumnBigMatrix(BigDecimal[] columnData) {
201 int nRows = columnData.length;
202 BigDecimal[][] data = new BigDecimal[nRows][1];
203 for (int row = 0; row < nRows; row++) {
204 data[row][0] = columnData[row];
205 }
206 return new BigMatrixImpl(data);
207 }
208
209 /***
210 * Creates a column {@link BigMatrix} using the data from the input
211 * array.
212 *
213 * @param columnData the input column data
214 * @return a columnData x 1 BigMatrix
215 * @throws IllegalArgumentException if <code>columnData</code> is empty
216 * @throws NullPointerException if <code>columnData</code>is null
217 */
218 public static BigMatrix createColumnBigMatrix(String[] columnData) {
219 int nRows = columnData.length;
220 String[][] data = new String[nRows][1];
221 for (int row = 0; row < nRows; row++) {
222 data[row][0] = columnData[row];
223 }
224 return new BigMatrixImpl(data);
225 }
226
227 }
228