001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.linear;
018:
019: import java.math.BigDecimal;
020:
021: /**
022: * A collection of static methods that operate on or return matrices.
023: *
024: * @version $Revision: 209143 $ $Date: 2005-07-04 16:29:23 -0700 (Mon, 04 Jul 2005) $
025: */
026: public class MatrixUtils {
027:
028: /**
029: * Default constructor. Package scope to prevent unwanted instantiation.
030: */
031: public MatrixUtils() {
032: super ();
033: }
034:
035: /**
036: * Returns a {@link RealMatrix} whose entries are the the values in the
037: * the input array. The input array is copied, not referenced.
038: *
039: * @param data input array
040: * @return RealMatrix containing the values of the array
041: * @throws IllegalArgumentException if <code>data</code> is not rectangular
042: * (not all rows have the same length) or empty
043: * @throws NullPointerException if data is null
044: */
045: public static RealMatrix createRealMatrix(double[][] data) {
046: return new RealMatrixImpl(data);
047: }
048:
049: /**
050: * Returns <code>dimension x dimension</code> identity matrix.
051: *
052: * @param dimension dimension of identity matrix to generate
053: * @return identity matrix
054: * @throws IllegalArgumentException if dimension is not positive
055: * @since 1.1
056: */
057: public static RealMatrix createRealIdentityMatrix(int dimension) {
058: RealMatrixImpl out = new RealMatrixImpl(dimension, dimension);
059: double[][] d = out.getDataRef();
060: for (int row = 0; row < dimension; row++) {
061: for (int col = 0; col < dimension; col++) {
062: d[row][col] = row == col ? 1d : 0d;
063: }
064: }
065: return out;
066: }
067:
068: /**
069: * Returns a {@link BigMatrix} whose entries are the the values in the
070: * the input array. The input array is copied, not referenced.
071: *
072: * @param data input array
073: * @return RealMatrix containing the values of the array
074: * @throws IllegalArgumentException if <code>data</code> is not rectangular
075: * (not all rows have the same length) or empty
076: * @throws NullPointerException if data is null
077: */
078: public static BigMatrix createBigMatrix(double[][] data) {
079: return new BigMatrixImpl(data);
080: }
081:
082: /**
083: * Returns a {@link BigMatrix} whose entries are the the values in the
084: * the input array. The input array is copied, not referenced.
085: *
086: * @param data input array
087: * @return RealMatrix containing the values of the array
088: * @throws IllegalArgumentException if <code>data</code> is not rectangular
089: * (not all rows have the same length) or empty
090: * @throws NullPointerException if data is null
091: */
092: public static BigMatrix createBigMatrix(BigDecimal[][] data) {
093: return new BigMatrixImpl(data);
094: }
095:
096: /**
097: * Returns a {@link BigMatrix} whose entries are the the values in the
098: * the input array. The input array is copied, not referenced.
099: *
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: 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[] 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: /**
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: 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: /**
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(
220: 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
261: : BigMatrixImpl.ZERO;
262: }
263: }
264: return out;
265: }
266:
267: }
|