Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
MatrixUtils |
|
| 1.5333333333333334;1.533 |
1 | /* |
|
2 | * Copyright 2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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$ $Date: 2005-07-04 16:29:23 -0700 (Mon, 04 Jul 2005) $ |
|
25 | */ |
|
26 | public class MatrixUtils { |
|
27 | ||
28 | /** |
|
29 | * Default constructor. Package scope to prevent unwanted instantiation. |
|
30 | */ |
|
31 | public MatrixUtils() { |
|
32 | 0 | super(); |
33 | 0 | } |
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 | 18 | return new RealMatrixImpl(data); |
47 | } |
|
48 | ||
49 | /** |
|
50 | * Returns <code>dimension x dimension</code> identity matrix. |
|
51 | * |
|
52 | * @param dimension dimension of identity matrix to generate |
|
53 | * @return identity matrix |
|
54 | * @throws IllegalArgumentException if dimension is not positive |
|
55 | * @since 1.1 |
|
56 | */ |
|
57 | public static RealMatrix createRealIdentityMatrix(int dimension) { |
|
58 | 26 | RealMatrixImpl out = new RealMatrixImpl(dimension, dimension); |
59 | 22 | double[][] d = out.getDataRef(); |
60 | 76 | for (int row = 0; row < dimension; row++) { |
61 | 196 | for (int col = 0; col < dimension; col++) { |
62 | 142 | d[row][col] = row == col ? 1d : 0d; |
63 | } |
|
64 | } |
|
65 | 22 | return out; |
66 | } |
|
67 | ||
68 | /** |
|
69 | * Returns a {@link BigMatrix} whose entries are the the values in the |
|
70 | * the input array. The input array is copied, not referenced. |
|
71 | * |
|
72 | * @param data input array |
|
73 | * @return RealMatrix containing the values of the array |
|
74 | * @throws IllegalArgumentException if <code>data</code> is not rectangular |
|
75 | * (not all rows have the same length) or empty |
|
76 | * @throws NullPointerException if data is null |
|
77 | */ |
|
78 | public static BigMatrix createBigMatrix(double[][] data) { |
|
79 | 20 | return new BigMatrixImpl(data); |
80 | } |
|
81 | ||
82 | /** |
|
83 | * Returns a {@link BigMatrix} whose entries are the the values in the |
|
84 | * the input array. The input array is copied, not referenced. |
|
85 | * |
|
86 | * @param data input array |
|
87 | * @return RealMatrix containing the values of the array |
|
88 | * @throws IllegalArgumentException if <code>data</code> is not rectangular |
|
89 | * (not all rows have the same length) or empty |
|
90 | * @throws NullPointerException if data is null |
|
91 | */ |
|
92 | public static BigMatrix createBigMatrix(BigDecimal[][] data) { |
|
93 | 4 | return new BigMatrixImpl(data); |
94 | } |
|
95 | ||
96 | /** |
|
97 | * Returns a {@link BigMatrix} whose entries are the the values in the |
|
98 | * the input array. The input array is copied, not referenced. |
|
99 | * |
|
100 | * @param data input array |
|
101 | * @return RealMatrix containing the values of the array |
|
102 | * @throws IllegalArgumentException if <code>data</code> is not rectangular |
|
103 | * (not all rows have the same length) or empty |
|
104 | * @throws NullPointerException if data is null |
|
105 | */ |
|
106 | public static BigMatrix createBigMatrix(String[][] data) { |
|
107 | 2 | return new BigMatrixImpl(data); |
108 | } |
|
109 | ||
110 | /** |
|
111 | * Creates a row {@link RealMatrix} using the data from the input |
|
112 | * array. |
|
113 | * |
|
114 | * @param rowData the input row data |
|
115 | * @return a 1 x rowData.length RealMatrix |
|
116 | * @throws IllegalArgumentException if <code>rowData</code> is empty |
|
117 | * @throws NullPointerException if <code>rowData</code>is null |
|
118 | */ |
|
119 | public static RealMatrix createRowRealMatrix(double[] rowData) { |
|
120 | 6 | int nCols = rowData.length; |
121 | 4 | double[][] data = new double[1][nCols]; |
122 | 4 | System.arraycopy(rowData, 0, data[0], 0, nCols); |
123 | 4 | return new RealMatrixImpl(data); |
124 | } |
|
125 | ||
126 | /** |
|
127 | * Creates a row {@link BigMatrix} using the data from the input |
|
128 | * array. |
|
129 | * |
|
130 | * @param rowData the input row data |
|
131 | * @return a 1 x rowData.length BigMatrix |
|
132 | * @throws IllegalArgumentException if <code>rowData</code> is empty |
|
133 | * @throws NullPointerException if <code>rowData</code>is null |
|
134 | */ |
|
135 | public static BigMatrix createRowBigMatrix(double[] rowData) { |
|
136 | 6 | int nCols = rowData.length; |
137 | 4 | double[][] data = new double[1][nCols]; |
138 | 4 | System.arraycopy(rowData, 0, data[0], 0, nCols); |
139 | 4 | return new BigMatrixImpl(data); |
140 | } |
|
141 | ||
142 | /** |
|
143 | * Creates a row {@link BigMatrix} using the data from the input |
|
144 | * array. |
|
145 | * |
|
146 | * @param rowData the input row data |
|
147 | * @return a 1 x rowData.length BigMatrix |
|
148 | * @throws IllegalArgumentException if <code>rowData</code> is empty |
|
149 | * @throws NullPointerException if <code>rowData</code>is null |
|
150 | */ |
|
151 | public static BigMatrix createRowBigMatrix(BigDecimal[] rowData) { |
|
152 | 2 | int nCols = rowData.length; |
153 | 2 | BigDecimal[][] data = new BigDecimal[1][nCols]; |
154 | 2 | System.arraycopy(rowData, 0, data[0], 0, nCols); |
155 | 2 | return new BigMatrixImpl(data); |
156 | } |
|
157 | ||
158 | /** |
|
159 | * Creates a row {@link BigMatrix} using the data from the input |
|
160 | * array. |
|
161 | * |
|
162 | * @param rowData the input row data |
|
163 | * @return a 1 x rowData.length BigMatrix |
|
164 | * @throws IllegalArgumentException if <code>rowData</code> is empty |
|
165 | * @throws NullPointerException if <code>rowData</code>is null |
|
166 | */ |
|
167 | public static BigMatrix createRowBigMatrix(String[] rowData) { |
|
168 | 2 | int nCols = rowData.length; |
169 | 2 | String[][] data = new String[1][nCols]; |
170 | 2 | System.arraycopy(rowData, 0, data[0], 0, nCols); |
171 | 2 | return new BigMatrixImpl(data); |
172 | } |
|
173 | ||
174 | /** |
|
175 | * Creates a column {@link RealMatrix} using the data from the input |
|
176 | * array. |
|
177 | * |
|
178 | * @param columnData the input column data |
|
179 | * @return a columnData x 1 RealMatrix |
|
180 | * @throws IllegalArgumentException if <code>columnData</code> is empty |
|
181 | * @throws NullPointerException if <code>columnData</code>is null |
|
182 | */ |
|
183 | public static RealMatrix createColumnRealMatrix(double[] columnData) { |
|
184 | 6 | int nRows = columnData.length; |
185 | 4 | double[][] data = new double[nRows][1]; |
186 | 10 | for (int row = 0; row < nRows; row++) { |
187 | 6 | data[row][0] = columnData[row]; |
188 | } |
|
189 | 4 | return new RealMatrixImpl(data); |
190 | } |
|
191 | ||
192 | /** |
|
193 | * Creates a column {@link BigMatrix} using the data from the input |
|
194 | * array. |
|
195 | * |
|
196 | * @param columnData the input column data |
|
197 | * @return a columnData x 1 BigMatrix |
|
198 | * @throws IllegalArgumentException if <code>columnData</code> is empty |
|
199 | * @throws NullPointerException if <code>columnData</code>is null |
|
200 | */ |
|
201 | public static BigMatrix createColumnBigMatrix(double[] columnData) { |
|
202 | 6 | int nRows = columnData.length; |
203 | 4 | double[][] data = new double[nRows][1]; |
204 | 10 | for (int row = 0; row < nRows; row++) { |
205 | 6 | data[row][0] = columnData[row]; |
206 | } |
|
207 | 4 | return new BigMatrixImpl(data); |
208 | } |
|
209 | ||
210 | /** |
|
211 | * Creates a column {@link BigMatrix} using the data from the input |
|
212 | * array. |
|
213 | * |
|
214 | * @param columnData the input column data |
|
215 | * @return a columnData x 1 BigMatrix |
|
216 | * @throws IllegalArgumentException if <code>columnData</code> is empty |
|
217 | * @throws NullPointerException if <code>columnData</code>is null |
|
218 | */ |
|
219 | public static BigMatrix createColumnBigMatrix(BigDecimal[] columnData) { |
|
220 | 2 | int nRows = columnData.length; |
221 | 2 | BigDecimal[][] data = new BigDecimal[nRows][1]; |
222 | 8 | for (int row = 0; row < nRows; row++) { |
223 | 6 | data[row][0] = columnData[row]; |
224 | } |
|
225 | 2 | return new BigMatrixImpl(data); |
226 | } |
|
227 | ||
228 | /** |
|
229 | * Creates a column {@link BigMatrix} using the data from the input |
|
230 | * array. |
|
231 | * |
|
232 | * @param columnData the input column data |
|
233 | * @return a columnData x 1 BigMatrix |
|
234 | * @throws IllegalArgumentException if <code>columnData</code> is empty |
|
235 | * @throws NullPointerException if <code>columnData</code>is null |
|
236 | */ |
|
237 | public static BigMatrix createColumnBigMatrix(String[] columnData) { |
|
238 | 2 | int nRows = columnData.length; |
239 | 2 | String[][] data = new String[nRows][1]; |
240 | 8 | for (int row = 0; row < nRows; row++) { |
241 | 6 | data[row][0] = columnData[row]; |
242 | } |
|
243 | 2 | return new BigMatrixImpl(data); |
244 | } |
|
245 | ||
246 | /** |
|
247 | * Returns <code>dimension x dimension</code> identity matrix. |
|
248 | * |
|
249 | * @param dimension dimension of identity matrix to generate |
|
250 | * @return identity matrix |
|
251 | * @throws IllegalArgumentException if dimension is not positive |
|
252 | * @since 1.1 |
|
253 | */ |
|
254 | public static BigMatrix createBigIdentityMatrix(int dimension) { |
|
255 | 20 | BigMatrixImpl out = new BigMatrixImpl(dimension, dimension); |
256 | 20 | BigDecimal[][] d = out.getDataRef(); |
257 | 70 | for (int row = 0; row < dimension; row++) { |
258 | 184 | for (int col = 0; col < dimension; col++) { |
259 | 134 | d[row][col] = row == col ? BigMatrixImpl.ONE : BigMatrixImpl.ZERO; |
260 | } |
|
261 | } |
|
262 | 20 | return out; |
263 | } |
|
264 | ||
265 | } |
|
266 |