KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > util > MatrixToolkit


1 package JSci.util;
2
3 import JSci.maths.Complex;
4 import JSci.maths.matrices.*;
5
6 /**
7 * This is a useful collection of matrix related methods.
8 * @author Mark Hale
9 */

10 public final class MatrixToolkit {
11         private MatrixToolkit() {}
12
13         /**
14         * Creates a random generated square matrix.
15         */

16         public static DoubleSquareMatrix randomSquareMatrix(int size) {
17                 return (DoubleSquareMatrix)new DoubleSquareMatrix(size).mapElements(RandomMap.MAP);
18         }
19         /**
20         * Creates a random generated tridiagonal matrix.
21         */

22         public static DoubleTridiagonalMatrix randomTridiagonalMatrix(int size) {
23                 return new DoubleTridiagonalMatrix(toArray(randomSquareMatrix(size)));
24         }
25         /**
26         * Creates a random generated diagonal matrix.
27         */

28         public static DoubleDiagonalMatrix randomDiagonalMatrix(int size) {
29                 return new DoubleDiagonalMatrix(toArray(randomSquareMatrix(size)));
30         }
31
32         /**
33         * Creates a random generated square matrix.
34         */

35         public static ComplexSquareMatrix randomComplexSquareMatrix(int size) {
36                 return (ComplexSquareMatrix)new ComplexSquareMatrix(size).mapElements(RandomMap.MAP);
37         }
38         /**
39         * Creates a random generated tridiagonal matrix.
40         */

41         public static ComplexTridiagonalMatrix randomComplexTridiagonalMatrix(int size) {
42                 return new ComplexTridiagonalMatrix(toArray(randomComplexSquareMatrix(size)));
43         }
44         /**
45         * Creates a random generated diagonal matrix.
46         */

47         public static ComplexDiagonalMatrix randomComplexDiagonalMatrix(int size) {
48                 return new ComplexDiagonalMatrix(toArray(randomComplexSquareMatrix(size)));
49         }
50
51         /**
52         * Converts a matrix to an array.
53         */

54         public static double[][] toArray(AbstractDoubleMatrix v) {
55                 double array[][]=new double[v.rows()][v.columns()];
56                 for(int j,i=0;i<array.length;i++) {
57                         for(j=0;j<array[0].length;j++)
58                                 array[i][j]=v.getElement(i,j);
59                 }
60                 return array;
61         }
62         /**
63         * Converts a matrix to an array.
64         */

65         public static Complex[][] toArray(AbstractComplexMatrix v) {
66                 Complex array[][]=new Complex[v.rows()][v.columns()];
67                 for(int j,i=0;i<array.length;i++) {
68                         for(j=0;j<array[0].length;j++)
69                                 array[i][j]=v.getElement(i,j);
70                 }
71                 return array;
72         }
73 }
74
Popular Tags