001 package com.croftsoft.core.math.matrix;
002
003 /***********************************************************************
004 * Implementation of interface MatrixMut.
005 *
006 * @version
007 * $Id: MatrixImp.java,v 1.5 2008/05/09 18:35:56 croft Exp $
008 * @since
009 * 2008-04-25
010 * @author
011 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
012 ***********************************************************************/
013
014 public class MatrixImp
015 implements MatrixMut
016 ////////////////////////////////////////////////////////////////////////
017 ////////////////////////////////////////////////////////////////////////
018 {
019
020 protected final int
021 rowCount,
022 columnCount;
023
024 protected final double [ ] [ ] values;
025
026 ////////////////////////////////////////////////////////////////////////
027 ////////////////////////////////////////////////////////////////////////
028
029 public MatrixImp (
030 final int rowCount,
031 final int columnCount )
032 ////////////////////////////////////////////////////////////////////////
033 {
034 if ( rowCount < 1 )
035 {
036 throw new IllegalArgumentException ( "rowCount < 1" );
037 }
038
039 if ( columnCount < 1 )
040 {
041 throw new IllegalArgumentException ( "columnCount < 1" );
042 }
043
044 this.rowCount = rowCount;
045
046 this.columnCount = columnCount;
047
048 values = new double [ rowCount ] [ columnCount ];
049 }
050
051 public MatrixImp ( final Matrix matrix )
052 ////////////////////////////////////////////////////////////////////////
053 {
054 this ( matrix.getRowCount ( ), matrix.getColumnCount ( ) );
055
056 for ( int row = 0; row < rowCount; row++ )
057 {
058 for ( int column = 0; column < columnCount; column++ )
059 {
060 set ( row, column, matrix.get ( row, column ) );
061 }
062 }
063 }
064
065 public MatrixImp ( final double [ ] [ ] values )
066 ////////////////////////////////////////////////////////////////////////
067 {
068 rowCount = values.length;
069
070 columnCount = values [ 0 ].length;
071
072 if ( rowCount < 1 )
073 {
074 throw new IllegalArgumentException ( "rowCount < 1" );
075 }
076
077 if ( columnCount < 1 )
078 {
079 throw new IllegalArgumentException ( "columnCount < 1" );
080 }
081
082 this.values = new double [ rowCount ] [ columnCount ];
083
084 for ( int row = 0; row < rowCount; row++ )
085 {
086 if ( values [ row ].length != columnCount )
087 {
088 throw new IllegalArgumentException (
089 "values[" + row + "].length != values[0].length" );
090 }
091
092 for ( int column = 0; column < columnCount; column++ )
093 {
094
095 this.values [ row ] [ column ] = values [ row ] [ column ];
096 }
097 }
098 }
099
100 ////////////////////////////////////////////////////////////////////////
101 // accessor methods
102 ////////////////////////////////////////////////////////////////////////
103
104 public double get (
105 final int row,
106 final int column )
107 ////////////////////////////////////////////////////////////////////////
108 {
109 return values [ row ] [ column ];
110 }
111
112 public int getRowCount ( ) { return rowCount; }
113
114 public int getColumnCount ( ) { return columnCount; }
115
116 ////////////////////////////////////////////////////////////////////////
117 // operand methods
118 ////////////////////////////////////////////////////////////////////////
119
120 public boolean isIdentity ( )
121 ////////////////////////////////////////////////////////////////////////
122 {
123 if ( rowCount != columnCount )
124 {
125 return false;
126 }
127
128 for ( int row = 0; row < rowCount; row++ )
129 {
130 for ( int column = 0; column < columnCount; column++ )
131 {
132 final double value = values [ row ] [ column ];
133
134 if ( row == column )
135 {
136 if ( value != 1 )
137 {
138 return false;
139 }
140 }
141 else if ( value != 0 )
142 {
143 return false;
144 }
145 }
146 }
147
148 return true;
149 }
150
151 public boolean isSquare ( )
152 ////////////////////////////////////////////////////////////////////////
153 {
154 return rowCount == columnCount;
155 }
156
157 public boolean matches ( final Matrix matrix )
158 ////////////////////////////////////////////////////////////////////////
159 {
160 return MatrixLib.matches ( this, matrix );
161 }
162
163 public boolean matches (
164 final Matrix matrix,
165 final double tolerance )
166 ////////////////////////////////////////////////////////////////////////
167 {
168 return MatrixLib.matches ( this, matrix, tolerance );
169 }
170
171 ////////////////////////////////////////////////////////////////////////
172 ////////////////////////////////////////////////////////////////////////
173
174 public MatrixMut copy ( )
175 ////////////////////////////////////////////////////////////////////////
176 {
177 return new MatrixImp ( this );
178 }
179
180 public MatrixMut multiply ( final Matrix matrix )
181 ////////////////////////////////////////////////////////////////////////
182 {
183 return MatrixLib.multiply ( this, matrix );
184 }
185
186 public MatrixMut multiply ( final double scalar )
187 ////////////////////////////////////////////////////////////////////////
188 {
189 return MatrixLib.multiply ( this, scalar );
190 }
191
192 ////////////////////////////////////////////////////////////////////////
193 // mutator methods
194 ////////////////////////////////////////////////////////////////////////
195
196 public void copyToSelf ( final Matrix copy )
197 ////////////////////////////////////////////////////////////////////////
198 {
199 MatrixLib.copyToSelf ( this, copy );
200 }
201
202 public void multiplyToSelf ( final double scalar )
203 ////////////////////////////////////////////////////////////////////////
204 {
205 for ( int row = 0; row < rowCount; row++ )
206 {
207 for ( int column = 0; column < columnCount; column++ )
208 {
209 values [ row ] [ column ] = values [ row ] [ column ] * scalar;
210 }
211 }
212 }
213
214 public void multiplyToSelf ( final Matrix matrix )
215 ////////////////////////////////////////////////////////////////////////
216 {
217 MatrixLib.multiplyToSelf ( this, matrix );
218 }
219
220 public void set (
221 final int row,
222 final int column,
223 final double value )
224 ////////////////////////////////////////////////////////////////////////
225 {
226 values [ row ] [ column ] = value;
227 }
228
229 public void setIdentity ( )
230 ////////////////////////////////////////////////////////////////////////
231 {
232 if ( rowCount != columnCount )
233 {
234 throw new IllegalArgumentException ( "rowCount != columnCount" );
235 }
236
237 for ( int row = 0; row < rowCount; row++ )
238 {
239 for ( int column = 0; column < columnCount; column++ )
240 {
241 values [ row ] [ column ] = row == column ? 1 : 0;
242 }
243 }
244 }
245
246 public MatrixMut transpose ( )
247 ////////////////////////////////////////////////////////////////////////
248 {
249 return MatrixLib.transpose ( this );
250 }
251
252 public void transposeSelf ( )
253 ////////////////////////////////////////////////////////////////////////
254 {
255 MatrixLib.transposeSelf ( this );
256 }
257
258 ////////////////////////////////////////////////////////////////////////
259 // overridden Object methods
260 ////////////////////////////////////////////////////////////////////////
261
262 @Override
263 public String toString ( )
264 ////////////////////////////////////////////////////////////////////////
265 {
266 return MatrixLib.toString ( this );
267 }
268
269 ////////////////////////////////////////////////////////////////////////
270 ////////////////////////////////////////////////////////////////////////
271 }