Debian Codegraph @

src/dr/matrix/MutableMatrix.java

raw symbols SCIP
1 /*
2 * MutableMatrix.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.matrix;
27
28 /**
29 * An inteface for a mutable matrix.
30 *
31 * @version $Id: MutableMatrix.java,v 1.3 2005/05/24 20:26:01 rambaut Exp $
32 *
33 * @author Andrew Rambaut
34 */
35 public interface MutableMatrix extends Matrix {
36
37 //***************************************************
38 // Setter methods
39 //***************************************************
40
41 /**
42 * sets the dimension of a square matrix
43 */
44 void setDimension(int rows);
45
46 /**
47 * sets the dimension of a rectangular matrix
48 */
49 void setDimension(int rows, int columns);
50
51 /**
52 * sets the matrix from another matrix object
53 */
54 void setMatrix(Matrix matrix);
55
56 /**
57 * sets the matrix from an 2D array of doubles
58 */
59 void setElements(double[][] values);
60
61 /**
62 * sets the matrix from an 1D array of doubles
63 */
64 void setElements(double[] values);
65
66 /**
67 * sets all elements in a matrix with a single value
68 */
69 void setElements(double value);
70
71 /**
72 * sets a single element
73 */
74 void setElement(int row, int column, double value);
75
76 /**
77 * sets the ith element
78 */
79 void setElement(int index, double value);
80
81 /**
82 * sets a single row
83 */
84 void setRow(int row, double[] values);
85
86 /**
87 * sets a single column
88 */
89 void setColumn(int column, double[] values);
90
91 /**
92 * sets the upper triangle
93 */
94 void setUpperTriangle(double[] values) throws Matrix.NotSquareException;
95
96 /**
97 * sets the lower triangle
98 */
99 void setLowerTriangle(double[] values) throws Matrix.NotSquareException;
100
101 /**
102 * sets the diagonal
103 */
104 void setDiagonal(double[] values) throws Matrix.NotSquareException;
105
106 //***************************************************
107 // Manipulation methods
108 //***************************************************
109
110 /**
111 * transposes this matrix
112 */
113 void makeTransposed();
114
115 /**
116 * makes the matrix symmetrical from the upper triangle
117 */
118 void makeSymmetricFromUpperTriangle() throws Matrix.NotSquareException;
119
120 /**
121 * makes the matrix symmetrical from the lower triangle
122 */
123 void makeSymmetricFromLowerTriangle() throws Matrix.NotSquareException;
124
125 /**
126 * makes the this matrix into an identity matrix
127 */
128 void makeIdentity() throws Matrix.NotSquareException;
129
130 //***************************************************
131 // AbstractMutableMatrix class
132 //***************************************************
133
134 /**
135 * Abstract class providing a base for mutable matrix implementations. At the
136 * very least, classes derived from this need only implement
137 * setDimension(int, int), getRowCount(), getColumnCount(),
138 * setElement(int, int) and getElement(int, int).
139 *
140 * By using these access methods, it will not be very efficient.
141 */
142 public abstract class AbstractMutableMatrix extends Matrix.AbstractMatrix
143 implements MutableMatrix {
144
145 /**
146 * Empty constructor
147 */
148 public AbstractMutableMatrix() {
149 }
150
151 /**
152 * Construct a square matrix
153 */
154 public AbstractMutableMatrix(int rows) {
155 setDimension(rows);
156 }
157
158 /**
159 * Construct a rectangular matrix
160 */
161 public AbstractMutableMatrix(int rows, int columns) {
162 setDimension(rows, columns);
163 }
164
165 /**
166 * Constructor which copies from matrix
167 */
168 public AbstractMutableMatrix(Matrix matrix) {
169 setDimension(matrix.getRowCount(), matrix.getColumnCount());
170 setMatrix(matrix);
171 }
172
173 /**
174 * Constructor which copies from 2D array of doubles
175 */
176 public AbstractMutableMatrix(double[][] values) {
177 setDimension(values.length, values[0].length);
178 setElements(values);
179 }
180
181 /**
182 * Constructor which copies from 1D array of doubles.
183 */
184 public AbstractMutableMatrix(int rows, int columns, double[] values) {
185 setDimension(rows, columns);
186 setElements(values);
187 }
188
189 /**
190 * Constructor which initialises every element to value.
191 */
192 public AbstractMutableMatrix(int rows, int columns, double value) {
193 setDimension(rows, columns);
194 setElements(value);
195 }
196
197 //***************************************************
198 // Setter methods
199 //***************************************************
200
201 /**
202 * sets the dimension of a square matrix
203 */
204 public void setDimension(int rows) {
205 setDimension(rows, rows);
206 }
207
208 /**
209 * sets the matrix from another matrix object. Both matrices
210 * must be the same size.
211 */
212 public void setMatrix(Matrix matrix) {
213 int rc = matrix.getRowCount();
214 int cc = matrix.getColumnCount();
215 for (int r = 0; r < rc; r++) {
216 for (int c = 0; c < cc; c++) {
217 setElement(r, c, matrix.getElement(r, c));
218 }
219 }
220 }
221
222 /**
223 * sets the matrix from an 2D array of doubles. Both matrices
224 * must be the same size.
225 */
226 public void setElements(double[][] values) {
227 setDimension(values.length, values[0].length);
228 for (int r = 0; r < values.length; r++) {
229 for (int c = 0; c < values[0].length; c++) {
230 setElement(r, c, values[r][c]);
231 }
232 }
233 }
234
235 /**
236 * sets the matrix from an 1D array of doubles. The dimension of
237 * the matrix must have already be set appropriately.
238 */
239 public void setElements(double[] values) {
240 int k = 0;
241 int rc = getRowCount();
242 int cc = getColumnCount();
243 for (int r = 0; r < rc; r++) {
244 for (int c = 0; c < cc; c++) {
245 setElement(r, c, values[k]);
246 k++;
247 }
248 }
249 }
250
251 /**
252 * sets all elements in a matrix with a single value
253 */
254 public void setElements(double value) {
255 int rc = getRowCount();
256 int cc = getColumnCount();
257 for (int r = 0; r < rc; r++) {
258 for (int c = 0; c < cc; c++) {
259 setElement(r, c, value);
260 }
261 }
262 }
263
264 /**
265 * sets the ith element
266 */
267 public void setElement(int index, double value) {
268 int r = index / getColumnCount();
269 int c = index % getColumnCount();
270 setElement(r, c, value);
271 }
272
273 /**
274 * sets a single row
275 */
276 public void setRow(int row, double[] values) {
277 int cc = getColumnCount();
278 for (int c = 0; c < cc; c++) {
279 setElement(row, c, values[c]);
280 }
281 }
282
283
284 /**
285 * sets a single column
286 */
287 public void setColumn(int column, double[] values) {
288 int rc = getRowCount();
289 for (int r = 0; r < rc; r++) {
290 setElement(r, column, values[r]);
291 }
292 }
293
294 /**
295 * sets the upper triangle
296 */
297 public void setUpperTriangle(double[] values) throws Matrix.NotSquareException {
298 if (!getIsSquare())
299 throw new Matrix.NotSquareException();
300
301 int k = 0;
302 int dim = getRowCount();
303 for (int r = 0; r < dim; r++) {
304 for (int c = r + 1; c < dim; c++) {
305 setElement(r, c, values[k]);
306 k++;
307 }
308 }
309 }
310
311 /**
312 * sets the lower triangle
313 */
314 public void setLowerTriangle(double[] values) throws Matrix.NotSquareException {
315 if (!getIsSquare())
316 throw new Matrix.NotSquareException();
317
318 int k = 0;
319 int dim = getRowCount();
320 for (int r = 0; r < dim; r++) {
321 for (int c = 0; c < r; c++) {
322 setElement(r, c, values[k]);
323 k++;
324 }
325 }
326 }
327
328 /**
329 * sets the diagonal
330 */
331 public void setDiagonal(double[] values) throws Matrix.NotSquareException {
332 if (!getIsSquare())
333 throw new Matrix.NotSquareException();
334
335 int dim = getRowCount();
336 for (int r = 0; r < dim; r++) {
337 setElement(r, r, values[r]);
338 }
339 }
340
341 //***************************************************
342 // Manipulation methods
343 //***************************************************
344
345 /**
346 * transposes this matrix
347 */
348 public void makeTransposed() {
349 double[][] values = getElements2D();
350
351 int cc = getRowCount();
352 int rc = getColumnCount();
353 setDimension(rc, cc);
354 for (int r = 0; r < rc; r++) {
355 for (int c = 0; c < cc; c++) {
356 setElement(r, c, values[c][r]);
357 }
358 }
359 }
360
361 /**
362 * makes the matrix symmetrical from the upper triangle
363 */
364 public void makeSymmetricFromUpperTriangle() throws Matrix.NotSquareException {
365 if (!getIsSquare())
366 throw new Matrix.NotSquareException();
367
368 double[] values = getUpperTriangle();
369 setLowerTriangle(values);
370 }
371
372 /**
373 * makes the matrix symmetrical from the lower triangle
374 */
375 public void makeSymmetricFromLowerTriangle() throws Matrix.NotSquareException {
376 if (!getIsSquare())
377 throw new Matrix.NotSquareException();
378
379 double[] values = getUpperTriangle();
380 setLowerTriangle(values);
381 }
382
383 /**
384 * makes the this matrix into an identity matrix
385 */
386 public void makeIdentity() throws Matrix.NotSquareException {
387 if (!getIsSquare())
388 throw new Matrix.NotSquareException();
389
390 setElements(0.0);
391 for (int i = 0; i < getRowCount(); i++)
392 setElement(i, i, 1.0);
393 }
394
395 }
396
397 }