KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > matrices > RingMatrix


1 package JSci.maths.matrices;
2
3 import JSci.GlobalSettings;
4 import JSci.maths.groups.AbelianGroup;
5 import JSci.maths.algebras.*;
6 import JSci.maths.fields.*;
7
8 /**
9 * The RingMatrix class provides an object for encapsulating matrices over an arbitrary ring.
10 * @version 1.1
11 * @author Mark Hale
12 */

13 public class RingMatrix extends Matrix {
14         /**
15         * Array containing the elements of the matrix.
16         */

17         protected Ring.Member matrix[][];
18         /**
19         * Constructs a matrix.
20         */

21         protected RingMatrix(final int rows, final int cols) {
22                 super(rows,cols);
23         }
24         /**
25         * Constructs a matrix by wrapping an array.
26         * @param array an assigned value
27         */

28         public RingMatrix(final Ring.Member array[][]) {
29                 this(array.length,array[0].length);
30                 matrix=array;
31         }
32         /**
33         * Compares two matrices for equality.
34         * @param m a matrix
35         */

36         public boolean equals(Object JavaDoc m) {
37                 if(m!=null && (m instanceof RingMatrix) &&
38                 numRows==((RingMatrix)m).rows() && numCols==((RingMatrix)m).columns()) {
39                         final RingMatrix rm=(RingMatrix)m;
40                         for(int j,i=0;i<numRows;i++) {
41                                 for(j=0;j<numCols;j++) {
42                                         if(!matrix[i][j].equals(rm.getElement(i,j)))
43                                                 return false;
44                                 }
45                         }
46                         return true;
47                 } else
48                         return false;
49         }
50         /**
51         * Returns a string representing this matrix.
52         */

53         public String JavaDoc toString() {
54                 final StringBuffer JavaDoc buf=new StringBuffer JavaDoc(5*numRows*numCols);
55                 for(int j,i=0;i<numRows;i++) {
56                         for(j=0;j<numCols;j++) {
57                                 buf.append(matrix[i][j].toString());
58                                 buf.append(' ');
59                         }
60                         buf.append('\n');
61                 }
62                 return buf.toString();
63         }
64         /**
65         * Returns an element of the matrix.
66         * @param i row index of the element
67         * @param j column index of the element
68         * @exception MatrixDimensionException If attempting to access an invalid element.
69         */

70         public Ring.Member getElement(final int i, final int j) {
71                 if(i>=0 && i<numRows && j>=0 && j<numCols)
72                         return matrix[i][j];
73                 else
74                         throw new MatrixDimensionException(getInvalidElementMsg(i,j));
75         }
76         /**
77         * Sets the value of an element of the matrix.
78         * @param i row index of the element
79         * @param j column index of the element
80         * @param r a ring element
81         * @exception MatrixDimensionException If attempting to access an invalid element.
82         */

83         public void setElement(final int i, final int j, final Ring.Member r) {
84                 if(i>=0 && i<numRows && j>=0 && j<numCols)
85                         matrix[i][j]=r;
86                 else
87                         throw new MatrixDimensionException(getInvalidElementMsg(i,j));
88         }
89     public Object JavaDoc getSet() {
90         throw new RuntimeException JavaDoc("Not yet implemented: please file bug report");
91     }
92
93 //============
94
// OPERATIONS
95
//============
96

97         /**
98         * Returns the negative of this matrix.
99         */

100         public AbelianGroup.Member negate() {
101                 final Ring.Member array[][]=new Ring.Member[numRows][numCols];
102                 for(int j,i=0;i<numRows;i++) {
103                         array[i][0]=(Ring.Member) matrix[i][0].negate();
104                         for(j=1;j<numCols;j++)
105                                 array[i][j]=(Ring.Member) matrix[i][j].negate();
106                 }
107                 return new RingMatrix(array);
108         }
109
110 // ADDITION
111

112         /**
113         * Returns the addition of this matrix and another.
114         */

115         public final AbelianGroup.Member add(final AbelianGroup.Member m) {
116                 if(m instanceof RingMatrix)
117                         return add((RingMatrix)m);
118                 else
119                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
120         }
121         /**
122         * Returns the addition of this matrix and another.
123         * @param m a matrix
124         * @exception MatrixDimensionException If the matrices are different sizes.
125         */

126         public RingMatrix add(final RingMatrix m) {
127                 if(numRows==m.numRows && numCols==m.numCols) {
128                         final Ring.Member array[][]=new Ring.Member[numRows][numCols];
129                         for(int j,i=0;i<numRows;i++) {
130                                 array[i][0]=(Ring.Member) matrix[i][0].add(m.getElement(i,0));
131                                 for(j=1;j<numCols;j++)
132                                         array[i][j]=(Ring.Member) matrix[i][j].add(m.getElement(i,j));
133                         }
134                         return new RingMatrix(array);
135                 } else
136                         throw new MatrixDimensionException("Matrices are different sizes.");
137         }
138
139 // SUBTRACTION
140

141         /**
142         * Returns the subtraction of this matrix and another.
143         */

144         public final AbelianGroup.Member subtract(final AbelianGroup.Member m) {
145                 if(m instanceof RingMatrix)
146                         return subtract((RingMatrix)m);
147                 else
148                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
149         }
150         /**
151         * Returns the subtraction of this matrix and another.
152         * @param m a matrix
153         * @exception MatrixDimensionException If the matrices are different sizes.
154         */

155         public RingMatrix subtract(final RingMatrix m) {
156                 if(numRows==m.numRows && numCols==m.numCols) {
157                         final Ring.Member array[][]=new Ring.Member[numRows][numCols];
158                         for(int j,i=0;i<numRows;i++) {
159                                 array[i][0]=(Ring.Member) matrix[i][0].subtract(m.getElement(i,0));
160                                 for(j=1;j<numCols;j++)
161                                         array[i][j]=(Ring.Member) matrix[i][j].subtract(m.getElement(i,j));
162                         }
163                         return new RingMatrix(array);
164                 } else
165                         throw new MatrixDimensionException("Matrices are different sizes.");
166         }
167
168 // SCALAR MULTIPLICATION
169

170         /**
171         * Returns the multiplication of this matrix by a scalar.
172         * @param r a ring element.
173         */

174         public Module.Member scalarMultiply(Ring.Member r) {
175                 final Ring.Member array[][]=new Ring.Member[numRows][numCols];
176                 for(int j,i=0;i<numRows;i++) {
177                         array[i][0]=r.multiply(matrix[i][0]);
178                         for(j=1;j<numCols;j++)
179                                 array[i][j]=r.multiply(matrix[i][j]);
180                 }
181                 return new RingMatrix(array);
182         }
183
184 // SCALAR DIVISON
185

186         /**
187         * Returns the division of this matrix by a scalar.
188         * @param x a field element.
189         */

190         public VectorSpace.Member scalarDivide(Field.Member x) {
191                 final Ring.Member array[][]=new Ring.Member[numRows][numCols];
192                 for(int j,i=0;i<numRows;i++) {
193                         array[i][0]=((Field.Member)matrix[i][0]).divide(x);
194                         for(j=1;j<numCols;j++)
195                                 array[i][j]=((Field.Member)matrix[i][j]).divide(x);
196                 }
197                 return new RingMatrix(array);
198         }
199
200 // MATRIX MULTIPLICATION
201

202         /**
203         * Returns the multiplication of this matrix and another.
204         */

205         public final Ring.Member multiply(final Ring.Member m) {
206                 if(m instanceof RingMatrix)
207                         return multiply((RingMatrix)m);
208                 else
209                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
210         }
211         /**
212         * Returns the multiplication of this matrix and another.
213         * @param m a matrix
214         * @exception MatrixDimensionException If the matrices are incompatible.
215         */

216         public RingMatrix multiply(final RingMatrix m) {
217                 if(numCols==m.numRows) {
218                         int n,k;
219                         final Ring.Member array[][]=new Ring.Member[numRows][m.numCols];
220                         for(int j=0;j<numRows;j++) {
221                                 for(k=0;k<m.numCols;k++) {
222                                         AbelianGroup.Member g=matrix[j][0].multiply(m.getElement(0,k));
223                                         for(n=1;n<numCols;n++)
224                                                 g=g.add(matrix[j][n].multiply(m.getElement(n,k)));
225                                         array[j][k]=(Ring.Member) g;
226                                 }
227                         }
228                         return new RingMatrix(array);
229                 } else
230                         throw new MatrixDimensionException("Incompatible matrices.");
231         }
232
233 // DIRECT SUM
234

235         /**
236         * Returns the direct sum of this matrix and another.
237         */

238         public RingMatrix directSum(final RingMatrix m) {
239                 final Ring.Member array[][]=new Ring.Member[numRows+m.numRows][numCols+m.numCols];
240                 for(int j,i=0;i<numRows;i++) {
241                         for(j=0;j<numCols;j++)
242                                 array[i][j]=matrix[i][j];
243                 }
244                 for(int j,i=0;i<m.numRows;i++) {
245                         for(j=0;j<m.numCols;j++)
246                                 array[i+numRows][j+numCols]=m.getElement(i,j);
247                 }
248                 return new RingMatrix(array);
249         }
250
251 // TENSOR PRODUCT
252

253         /**
254         * Returns the tensor product of this matrix and another.
255         */

256         public RingMatrix tensor(final RingMatrix m) {
257                 final Ring.Member array[][]=new Ring.Member[numRows*m.numRows][numCols*m.numCols];
258                 for(int i=0;i<numRows;i++) {
259                         for(int j=0;j<numCols;j++) {
260                                 for(int k=0;k<m.numRows;j++) {
261                                         for(int l=0;l<m.numCols;l++)
262                                                 array[i*m.numRows+k][j*m.numCols+l]=matrix[i][j].multiply(m.getElement(k,l));
263                                 }
264                         }
265                 }
266                 return new RingMatrix(array);
267         }
268
269 // TRANSPOSE
270

271         /**
272         * Returns the transpose of this matrix.
273         * @return a matrix
274         */

275         public Matrix transpose() {
276                 final Ring.Member array[][]=new Ring.Member[numCols][numRows];
277                 for(int j,i=0;i<numRows;i++) {
278                         array[0][i]=matrix[i][0];
279                         for(j=1;j<numCols;j++)
280                                 array[j][i]=matrix[i][j];
281                 }
282                 return new RingMatrix(array);
283         }
284 }
285
286
Popular Tags