KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > linear > MatrixUtils


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 JavaDoc;
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         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 <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         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     /**
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         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 JavaDoc[][] data) {
93         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 JavaDoc[][] data) {
107         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         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     /**
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         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     /**
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 JavaDoc[] rowData) {
152         int nCols = rowData.length;
153         BigDecimal JavaDoc[][] data = new BigDecimal JavaDoc[1][nCols];
154         System.arraycopy(rowData, 0, data[0], 0, nCols);
155         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 JavaDoc[] rowData) {
168         int nCols = rowData.length;
169         String JavaDoc[][] data = new String JavaDoc[1][nCols];
170         System.arraycopy(rowData, 0, data[0], 0, nCols);
171         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         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     /**
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         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     /**
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 JavaDoc[] columnData) {
220         int nRows = columnData.length;
221         BigDecimal JavaDoc[][] data = new BigDecimal JavaDoc[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     /**
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 JavaDoc[] columnData) {
238         int nRows = columnData.length;
239         String JavaDoc[][] data = new String JavaDoc[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     /**
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         BigMatrixImpl out = new BigMatrixImpl(dimension, dimension);
256         BigDecimal JavaDoc[][] 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