View Javadoc

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