001: package JSci.tests;
002:
003: import java.lang.reflect.Constructor;
004: import java.util.Map;
005: import java.util.HashMap;
006: import junit.framework.*;
007: import JSci.GlobalSettings;
008: import JSci.maths.*;
009: import JSci.maths.matrices.*;
010: import JSci.maths.vectors.*;
011: import JSci.maths.polynomials.*;
012:
013: /**
014: * Testcase for matrices.
015: * @author Mark Hale
016: */
017: public class MatrixTest extends
018: junitx.extensions.EqualsHashCodeTestCase {
019: private final int N = 10;
020: private final int M = 10;
021: private Constructor constructor;
022: private double array[][];
023: private double array2[][];
024:
025: public static void main(String arg[]) {
026: junit.textui.TestRunner.run(suite());
027: }
028:
029: public static Test suite() {
030: Class[] classes = new Class[] { DoubleMatrix.class,
031: DoubleTridiagonalMatrix.class,
032: DoubleDiagonalMatrix.class, DoubleSparseMatrix.class };
033: TestSuite suite = new TestSuite(MatrixTest.class.toString());
034: for (int i = 0; i < classes.length; i++) {
035: Map properties = new HashMap();
036: properties.put("test.matrix.class", classes[i]);
037: suite.addTest(new junit.extensions.RepeatedTest(
038: new TestProperties(new TestSuite(MatrixTest.class,
039: classes[i].toString()), properties), 5));
040: }
041: return suite;
042: }
043:
044: public MatrixTest(String name) {
045: super (name);
046: }
047:
048: protected void setUp() throws Exception {
049: GlobalSettings.ZERO_TOL = 1.0e-6;
050: array = new double[N][M];
051: array2 = new double[N][M];
052: Class matrixClass = (Class) TestProperties.getProperties().get(
053: "test.matrix.class");
054: constructor = matrixClass
055: .getConstructor(new Class[] { double[][].class });
056: if (DiagonalMatrix.class.isAssignableFrom(matrixClass))
057: setUpDiagonal();
058: else if (TridiagonalMatrix.class.isAssignableFrom(matrixClass))
059: setUpTridiagonal();
060: else
061: setUpRectangular();
062: super .setUp();
063: }
064:
065: private void setUpRectangular() {
066: for (int i = 0; i < N; i++) {
067: for (int j = 0; j < M; j++) {
068: array[i][j] = ExtraMath.random(-1.0, 1.0);
069: array2[i][j] = ExtraMath.random(-1.0, 1.0);
070: }
071: }
072: }
073:
074: private void setUpTridiagonal() {
075: for (int i = 0; i < N; i++) {
076: for (int j = 0; j < M; j++) {
077: if (j >= i - 1 && j <= i + 1) {
078: array[i][j] = ExtraMath.random(-1.0, 1.0);
079: array2[i][j] = ExtraMath.random(-1.0, 1.0);
080: }
081: }
082: }
083: }
084:
085: private void setUpDiagonal() {
086: for (int i = 0; i < N; i++) {
087: array[i][i] = ExtraMath.random(-1.0, 1.0);
088: array2[i][i] = ExtraMath.random(-1.0, 1.0);
089: }
090: }
091:
092: protected Object createInstance() {
093: try {
094: return constructor.newInstance(new Object[] { array });
095: } catch (Exception e) {
096: throw new RuntimeException(e);
097: }
098: }
099:
100: protected Object createNotEqualInstance() {
101: try {
102: return constructor.newInstance(new Object[] { array2 });
103: } catch (Exception e) {
104: throw new RuntimeException(e);
105: }
106: }
107:
108: public void testConstructor() {
109: AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
110: assertEquals(N, mat.rows());
111: assertEquals(M, mat.columns());
112: for (int i = 0; i < mat.rows(); i++) {
113: for (int j = 0; j < mat.columns(); j++)
114: assertEquals(array[i][j], mat.getElement(i, j),
115: GlobalSettings.ZERO_TOL);
116: }
117: }
118:
119: public void testSetGet() {
120: AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
121: for (int i = 0; i < mat.rows(); i++) {
122: for (int j = 0; j < mat.columns(); j++) {
123: try {
124: mat.setElement(i, j, -1.0);
125: assertEquals(-1.0, mat.getElement(i, j),
126: GlobalSettings.ZERO_TOL);
127: mat.setElement(i, j, array[i][j]);
128: } catch (MatrixDimensionException e) {
129: // ignore
130: }
131: }
132: }
133: }
134:
135: public void testZeroTolerance() {
136: AbstractDoubleMatrix delta = (AbstractDoubleMatrix) createInstance();
137: delta = delta.scalarMultiply(Math.random()
138: * GlobalSettings.ZERO_TOL / delta.frobeniusNorm());
139: AbstractDoubleMatrix m = (AbstractDoubleMatrix) createNotEqualInstance();
140: assertTrue("m == m + delta", m.equals(m.add(delta)));
141: }
142:
143: public void testAdd() {
144: AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
145: AbstractDoubleMatrix ans = mat.add(mat);
146: for (int i = 0; i < ans.rows(); i++) {
147: for (int j = 0; j < ans.columns(); j++) {
148: String msg = mat.getClass().toString() + ": (" + i
149: + "," + j + ")";
150: assertEquals(msg, array[i][j] + array[i][j], ans
151: .getElement(i, j), GlobalSettings.ZERO_TOL);
152: }
153: }
154: }
155:
156: public void testSubtract() {
157: AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
158: AbstractDoubleMatrix ans = mat.subtract(mat);
159: for (int i = 0; i < ans.rows(); i++) {
160: for (int j = 0; j < ans.columns(); j++) {
161: String msg = mat.getClass().toString() + ": (" + i
162: + "," + j + ")";
163: assertEquals(msg, array[i][j] - array[i][j], ans
164: .getElement(i, j), GlobalSettings.ZERO_TOL);
165: }
166: }
167: }
168:
169: public void testMultiply() {
170: AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
171: AbstractDoubleMatrix ans = (AbstractDoubleMatrix) mat
172: .multiply(mat.transpose());
173: for (int i = 0; i < ans.rows(); i++) {
174: for (int j = 0; j < ans.columns(); j++) {
175: double sum = 0.0;
176: for (int k = 0; k < mat.columns(); k++)
177: sum += array[i][k] * array[j][k];
178: String msg = mat.getClass().toString() + ": (" + i
179: + "," + j + ")";
180: assertEquals(msg, sum, ans.getElement(i, j),
181: GlobalSettings.ZERO_TOL);
182: }
183: }
184: }
185:
186: public void testMapElements() {
187: AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
188: RealPolynomial map = new RealPolynomial(
189: new double[] { 2.0, 1.0 });
190: AbstractDoubleMatrix ans = mat.mapElements(map);
191: for (int i = 0; i < ans.rows(); i++) {
192: for (int j = 0; j < ans.columns(); j++) {
193: String msg = mat.getClass().toString() + ": (" + i
194: + "," + j + ")";
195: assertEquals(msg, map.map(array[i][j]), ans.getElement(
196: i, j), GlobalSettings.ZERO_TOL);
197: }
198: }
199: }
200:
201: public void testGMRes() {
202: AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
203: AbstractDoubleVector y = JSci.util.VectorToolkit
204: .randomVector(mat.rows());
205: try {
206: AbstractDoubleVector x = LinearMath.solveGMRes(mat, y, 100,
207: GlobalSettings.ZERO_TOL);
208: assertEquals(y, mat.multiply(x));
209: } catch (MaximumIterationsExceededException e) {
210: throw new RuntimeException(e);
211: }
212: }
213: }
|