View Javadoc
1   package org.djunits.value.vdouble.matrix;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   import static org.junit.Assert.assertNotEquals;
6   import static org.junit.Assert.assertTrue;
7   import static org.junit.Assert.fail;
8   
9   import java.lang.reflect.InvocationTargetException;
10  import java.lang.reflect.Method;
11  
12  import org.djunits.Try;
13  import org.djunits.unit.AbsoluteTemperatureUnit;
14  import org.djunits.unit.AngleUnit;
15  import org.djunits.unit.AreaUnit;
16  import org.djunits.unit.DirectionUnit;
17  import org.djunits.unit.DurationUnit;
18  import org.djunits.unit.LengthUnit;
19  import org.djunits.unit.PositionUnit;
20  import org.djunits.unit.SIUnit;
21  import org.djunits.unit.TemperatureUnit;
22  import org.djunits.unit.TimeUnit;
23  import org.djunits.unit.Unit;
24  import org.djunits.unit.quantity.Quantities;
25  import org.djunits.unit.quantity.Quantity;
26  import org.djunits.unit.util.UnitException;
27  import org.djunits.unit.util.UnitRuntimeException;
28  import org.djunits.value.CLASSNAMES;
29  import org.djunits.value.ValueRuntimeException;
30  import org.djunits.value.storage.StorageType;
31  import org.djunits.value.vdouble.function.DoubleMathFunctions;
32  import org.djunits.value.vdouble.matrix.base.AbstractDoubleMatrixRel;
33  import org.djunits.value.vdouble.matrix.base.DoubleMatrix;
34  import org.djunits.value.vdouble.matrix.base.DoubleSparseValue;
35  import org.djunits.value.vdouble.matrix.data.DoubleMatrixData;
36  import org.djunits.value.vdouble.scalar.AbsoluteTemperature;
37  import org.djunits.value.vdouble.scalar.Area;
38  import org.djunits.value.vdouble.scalar.Direction;
39  import org.djunits.value.vdouble.scalar.Duration;
40  import org.djunits.value.vdouble.scalar.Length;
41  import org.djunits.value.vdouble.scalar.Position;
42  import org.djunits.value.vdouble.scalar.Time;
43  import org.djunits.value.vdouble.vector.AreaVector;
44  import org.djunits.value.vfloat.matrix.FloatAreaMatrix;
45  import org.djunits.value.vfloat.matrix.base.FloatMatrix;
46  import org.djunits.value.vfloat.vector.FLOATVECTOR;
47  import org.junit.Test;
48  
49  /**
50   * ...
51   */
52  public class DoubleMatrixMethodTest
53  {
54  
55      /**
56       * Test the standard methods of all matrix classes.
57       * @throws UnitException on error
58       * @throws ValueRuntimeException on error
59       */
60      @Test
61      @SuppressWarnings("checkstyle:methodlength")
62      public void testMatrixMethods() throws ValueRuntimeException, UnitException
63      {
64          double[][] denseTestData = DOUBLEMATRIX.denseRectArrays(10, 20);
65          double[][] sparseTestData = DOUBLEMATRIX.sparseRectArrays(10, 20);
66          double[][] reverseSparseTestData = new double[sparseTestData.length][];
67          // sparseTestData and reverseSparseTestData should not "run out of values" at the same index
68          for (int index = 0; index < sparseTestData.length; index++)
69          {
70              reverseSparseTestData[reverseSparseTestData.length - 1 - index] = sparseTestData[index];
71          }
72          // Ensure that there are > 50% positions where both have a non-zero value
73          for (int row = 1; row < 8; row++)
74          {
75              for (int col = 2; col < 18; col++)
76              {
77                  sparseTestData[row][col] = 10000.456 + row + 100 * col;
78                  reverseSparseTestData[row][col] = 20000.567 + row + 100 * col;
79              }
80          }
81  
82          for (StorageType storageType : new StorageType[] {StorageType.DENSE, StorageType.SPARSE})
83          {
84              for (AreaUnit au : new AreaUnit[] {AreaUnit.SQUARE_METER, AreaUnit.ACRE})
85              {
86                  double[][] testData = storageType.equals(StorageType.DENSE) ? denseTestData : sparseTestData;
87                  AreaMatrix am =
88                          DoubleMatrix.instantiate(DoubleMatrixData.instantiate(testData, au.getScale(), storageType), au);
89  
90                  // INDEX CHECKING
91                  for (int row : new int[] {-1, 0, denseTestData.length - 1, denseTestData.length})
92                  {
93                      for (int col : new int[] {-1, 0, denseTestData[0].length - 1, denseTestData[0].length})
94                      {
95                          if (row < 0 || col < 0 || row >= denseTestData.length || col >= denseTestData[0].length)
96                          {
97                              try
98                              {
99                                  am.get(row, col);
100                                 fail("bad row or bad column value should have thrown a ValueRuntimeException");
101                             }
102                             catch (ValueRuntimeException vre)
103                             {
104                                 // Ignore expected exception
105                             }
106                         }
107                         else
108                         {
109                             am.get(row, col);
110                         }
111                     }
112                     if (row < 0 || row >= denseTestData.length)
113                     {
114                         try
115                         {
116                             am.getRow(row);
117                             fail("getRow with bad row value should have thrown a ValueRuntimeException");
118                         }
119                         catch (ValueRuntimeException vre)
120                         {
121                             // Ignore expected exception
122                         }
123                     }
124                 }
125                 for (int col : new int[] {-1, 0, denseTestData[0].length - 1, denseTestData[0].length})
126                 {
127                     if (col < 0 || col >= denseTestData[0].length)
128                     {
129                         try
130                         {
131                             am.getColumn(col);
132                             fail("getColumn with bad column value should have thrown a ValueRuntimeException");
133                         }
134                         catch (ValueRuntimeException vre)
135                         {
136                             // Ignore expected exception
137                         }
138                     }
139                     else
140                     {
141                         am.getColumn(col);
142                     }
143                 }
144 
145                 // SPARSE AND DENSE
146                 assertEquals(am, am.toSparse());
147                 assertEquals(am, am.toDense());
148                 assertEquals(am, am.toSparse().toDense());
149                 assertEquals(am, am.toDense().toSparse());
150                 assertEquals(am.hashCode(), am.toSparse().hashCode());
151                 assertEquals(am.hashCode(), am.toDense().hashCode());
152                 assertTrue(am.toDense().isDense());
153                 assertFalse(am.toDense().isSparse());
154                 assertTrue(am.toSparse().isSparse());
155                 assertFalse(am.toSparse().isDense());
156 
157                 // EQUALS
158                 assertEquals(am, am);
159                 assertNotEquals(am, new Object());
160                 assertNotEquals(am, null);
161                 assertNotEquals(am, DoubleMatrix.instantiate(
162                         DoubleMatrixData.instantiate(testData, LengthUnit.METER.getScale(), storageType), LengthUnit.METER));
163                 assertNotEquals(am, am.divide(2.0d));
164 
165                 // MUTABLE
166                 assertFalse(am.isMutable());
167                 AreaMatrix ammut = am.mutable();
168                 assertTrue(ammut.isMutable());
169                 assertFalse(am.isMutable());
170                 AreaMatrix ammut2 = ammut.multiplyBy(1.0);
171                 assertEquals(am, ammut2);
172                 assertTrue(ammut.isMutable());
173                 assertFalse(am.isMutable());
174                 assertTrue(ammut2.isMutable());
175                 ammut2 = ammut2.mutable().divideBy(2.0);
176                 assertEquals(am, ammut);
177                 assertNotEquals(am, ammut2);
178                 AreaMatrix ammut3 = ammut2.mutable().divideBy(0.0);
179                 for (int row = 0; row < ammut3.rows(); row++)
180                 {
181                     for (int col = 0; col < ammut3.cols(); col++)
182                     {
183                         if (ammut2.getSI(row, col) == 0)
184                         {
185                             assertTrue("Value should be NaN", Double.isNaN(ammut3.getSI(row, col)));
186 
187                         }
188                         else
189                         {
190                             assertTrue("Value should be Infinite", Double.isInfinite(ammut3.getSI(row, col)));
191                         }
192                     }
193                 }
194 
195                 // ZSUM and CARDINALITY
196                 Area zSum = am.zSum();
197                 double sum = 0;
198                 int card = 0;
199                 for (int row = 0; row < testData.length; row++)
200                 {
201                     for (int col = 0; col < testData[0].length; col++)
202                     {
203                         sum += testData[row][col];
204                         card += testData[row][col] == 0.0d ? 0 : 1;
205                     }
206                 }
207                 assertEquals("zSum", sum, zSum.getInUnit(), 0.1);
208                 assertEquals("cardinality", card, am.cardinality());
209                 AreaMatrix ammutZero = ammut.multiplyBy(0.0);
210                 assertEquals("cardinality should be 0", 0, ammutZero.cardinality());
211                 assertEquals("zSum should be 0", 0.0, ammutZero.zSum().getSI(), 0);
212 
213                 // INCREMENTBY(SCALAR) and DECREMENTBY(SCALAR)
214                 AreaMatrix amold = am.clone();
215                 Area fa = Area.of(10.0d, "m^2");
216                 AreaMatrix aminc = am.mutable().incrementBy(fa).immutable();
217                 AreaMatrix amdec = am.mutable().decrementBy(fa).immutable();
218                 AreaMatrix amid = aminc.mutable().decrementBy(fa);
219                 assertEquals("immutable matrix should not change when converted to mutable", am, amold);
220                 for (int row = 0; row < testData.length; row++)
221                 {
222                     for (int col = 0; col < testData[0].length; col++)
223                     {
224                         assertEquals("increment and decrement with scalar should result in same matrix", am.getSI(row, col),
225                                 amid.getSI(row, col), 0.1);
226                         assertEquals("m + s = (m+s)", au.getScale().toStandardUnit(testData[row][col]) + 10.0,
227                                 aminc.getSI(row, col), 0.1);
228                         assertEquals("m - s = (m-s)", au.getScale().toStandardUnit(testData[row][col]) - 10.0,
229                                 amdec.getSI(row, col), 0.1);
230                     }
231                 }
232 
233                 // MULTIPLYBY() and DIVIDEBY(), TIMES(), DIVIDE()
234                 AreaMatrix amt5 = am.mutable().multiplyBy(5.0d).immutable();
235                 AreaMatrix amd5 = am.mutable().divideBy(5.0d).immutable();
236                 AreaMatrix amtd = amt5.mutable().divideBy(5.0d);
237                 AreaMatrix amtimD = am.times(5.0d);
238                 AreaMatrix amtimF = am.times(5.0f);
239                 AreaMatrix amdivD = am.divide(5.0d);
240                 AreaMatrix amdivF = am.divide(5.0f);
241                 for (int row = 0; row < testData.length; row++)
242                 {
243                     for (int col = 0; col < testData[0].length; col++)
244                     {
245                         assertEquals("times followed by divide with constant should result in same matrix", am.getSI(row, col),
246                                 amtd.getSI(row, col), 0.1);
247                         assertEquals("m * 5.0 = (m*5.0)", au.getScale().toStandardUnit(testData[row][col]) * 5.0d,
248                                 amt5.getSI(row, col), 0.1);
249                         assertEquals("m / 5.0 = (m/5.0)", au.getScale().toStandardUnit(testData[row][col]) / 5.0d,
250                                 amd5.getSI(row, col), 0.1);
251                         assertEquals("amtimD", amt5.getSI(row, col), amtimD.getSI(row, col), 0.1d);
252                         assertEquals("amtimF", amt5.getSI(row, col), amtimF.getSI(row, col), 0.1d);
253                         assertEquals("amdivD", amd5.getSI(row, col), amdivD.getSI(row, col), 0.01d);
254                         assertEquals("amdivD", amd5.getSI(row, col), amdivF.getSI(row, col), 0.01d);
255                     }
256                 }
257 
258                 // GET(), GETINUNIT()
259                 assertEquals("get()", new Area(testData[2][2], au), am.get(2, 2));
260                 assertEquals("getSI()", au.getScale().toStandardUnit(testData[2][2]), am.getSI(2, 2), 0.1);
261                 assertEquals("getInUnit()", testData[2][2], am.getInUnit(2, 2), 0.1);
262                 assertEquals("getInUnit(unit)",
263                         AreaUnit.SQUARE_YARD.getScale().fromStandardUnit(au.getScale().toStandardUnit(testData[2][2])),
264                         am.getInUnit(2, 2, AreaUnit.SQUARE_YARD), 0.1);
265 
266                 // SET(), SETINUNIT()
267                 Area fasqft = new Area(10.5d, AreaUnit.SQUARE_FOOT);
268                 AreaMatrix famChange = am.clone().mutable();
269                 famChange.set(2, 2, fasqft);
270                 assertEquals("set()", fasqft.si, famChange.get(2, 2).si, 0.1d);
271                 famChange = am.clone().mutable();
272                 famChange.setSI(2, 2, 123.4d);
273                 assertEquals("setSI()", 123.4d, famChange.get(2, 2).si, 0.1d);
274                 famChange = am.clone().mutable();
275                 famChange.setInUnit(2, 2, 1.2d);
276                 assertEquals("setInUnit()", 1.2d, famChange.getInUnit(2, 2), 0.1d);
277                 famChange = am.clone().mutable();
278                 famChange.setInUnit(2, 2, 1.5d, AreaUnit.HECTARE);
279                 assertEquals("setInUnit(unit)", 15000.0d, famChange.get(2, 2).si, 1.0d);
280 
281                 // GETROW(), GETCOLUMN(), GETDIAGONAL
282                 double[][] squareData = storageType.equals(StorageType.DENSE) ? DOUBLEMATRIX.denseRectArrays(12, 12)
283                         : DOUBLEMATRIX.sparseRectArrays(12, 12);
284                 AreaMatrix amSquare =
285                         DoubleMatrix.instantiate(DoubleMatrixData.instantiate(squareData, au.getScale(), storageType), au);
286                 double[] row2si = am.getRowSI(2);
287                 double[] col2si = am.getColumnSI(2);
288                 double[] diagsi = amSquare.getDiagonalSI();
289                 AreaVector row2v = am.getRow(2);
290                 AreaVector col2v = am.getColumn(2);
291                 AreaVector diagv = amSquare.getDiagonal();
292                 Area[] row2scalar = am.getRowScalars(2);
293                 Area[] col2scalar = am.getColumnScalars(2);
294                 Area[] diagscalar = amSquare.getDiagonalScalars();
295                 for (int col = 0; col < testData[0].length; col++)
296                 {
297                     assertEquals("row2si", au.getScale().toStandardUnit(testData[2][col]), row2si[col], 0.1d);
298                     assertEquals("row2v", au.getScale().toStandardUnit(testData[2][col]), row2v.getSI(col), 0.1d);
299                     assertEquals("row2scalar", au.getScale().toStandardUnit(testData[2][col]), row2scalar[col].si, 0.1d);
300                 }
301                 for (int row = 0; row < testData.length; row++)
302                 {
303                     assertEquals("col2si", au.getScale().toStandardUnit(testData[row][2]), col2si[row], 0.1d);
304                     assertEquals("col2v", au.getScale().toStandardUnit(testData[row][2]), col2v.getSI(row), 0.1d);
305                     assertEquals("col2scalar", au.getScale().toStandardUnit(testData[row][2]), col2scalar[row].si, 0.1d);
306                 }
307                 for (int diag = 0; diag < amSquare.rows(); diag++)
308                 {
309                     assertEquals("diag2si", au.getScale().toStandardUnit(squareData[diag][diag]), diagsi[diag], 0.1d);
310                     assertEquals("diag2v", au.getScale().toStandardUnit(squareData[diag][diag]), diagv.getSI(diag), 0.1d);
311                     assertEquals("diag2scalar", au.getScale().toStandardUnit(squareData[diag][diag]), diagscalar[diag].si,
312                             0.1d);
313                 }
314 
315                 // GETVALUES(), GETSCALARS()
316                 double[][] valsi = am.getValuesSI();
317                 double[][] valunit = am.getValuesInUnit();
318                 double[][] valsqft = am.getValuesInUnit(AreaUnit.SQUARE_YARD);
319                 Area[][] valscalars = am.getScalars();
320                 for (int row = 0; row < testData.length; row++)
321                 {
322                     for (int col = 0; col < testData[0].length; col++)
323                     {
324                         assertEquals("getValuesSI()", au.getScale().toStandardUnit(testData[row][col]), valsi[row][col], 0.1);
325                         assertEquals("getValuesInUnit()", testData[row][col], valunit[row][col], 0.1);
326                         assertEquals("getValuesInUnit(unit)", AreaUnit.SQUARE_YARD.getScale()
327                                 .fromStandardUnit(au.getScale().toStandardUnit(testData[row][col])), valsqft[row][col], 0.1);
328                         assertEquals("getValuesInUnit(unit)", au.getScale().toStandardUnit(testData[row][col]),
329                                 valscalars[row][col].si, 0.1);
330                     }
331                 }
332 
333                 // ASSIGN FUNCTION ABS, CEIL, FLOOR, NEG, RINT
334                 AreaMatrix amdiv2 = am.divide(2.0d);
335                 assertEquals(am.getStorageType(), amdiv2.getStorageType());
336                 assertEquals(am.getDisplayUnit(), amdiv2.getDisplayUnit());
337                 AreaMatrix amAbs = amdiv2.mutable().abs().immutable();
338                 assertEquals(am.getStorageType(), amAbs.getStorageType());
339                 assertEquals(am.getDisplayUnit(), amAbs.getDisplayUnit());
340                 AreaMatrix amCeil = amdiv2.mutable().ceil().immutable();
341                 assertEquals(am.getStorageType(), amCeil.getStorageType());
342                 assertEquals(am.getDisplayUnit(), amCeil.getDisplayUnit());
343                 AreaMatrix amFloor = amdiv2.mutable().floor().immutable();
344                 assertEquals(am.getStorageType(), amFloor.getStorageType());
345                 assertEquals(am.getDisplayUnit(), amFloor.getDisplayUnit());
346                 AreaMatrix amNeg = amdiv2.mutable().neg().immutable();
347                 assertEquals(am.getStorageType(), amNeg.getStorageType());
348                 assertEquals(am.getDisplayUnit(), amNeg.getDisplayUnit());
349                 AreaMatrix amRint = amdiv2.mutable().rint().immutable();
350                 assertEquals(am.getStorageType(), amRint.getStorageType());
351                 assertEquals(am.getDisplayUnit(), amRint.getDisplayUnit());
352                 for (int row = 0; row < testData.length; row++)
353                 {
354                     for (int col = 0; col < testData[0].length; col++)
355                     {
356                         // TODO: Should be rounded IN THE UNIT rather than BY SI VALUES
357                         assertEquals("div2", au.getScale().toStandardUnit(testData[row][col]) / 2.0d, amdiv2.getSI(row, col),
358                                 0.1d);
359                         assertEquals("abs", Math.abs(au.getScale().toStandardUnit(testData[row][col]) / 2.0d),
360                                 amAbs.getSI(row, col), 0.1d);
361                         assertEquals("ceil", Math.ceil(au.getScale().toStandardUnit(testData[row][col]) / 2.0d),
362                                 amCeil.getSI(row, col), 0.1d);
363                         assertEquals("floor", Math.floor(au.getScale().toStandardUnit(testData[row][col]) / 2.0d),
364                                 amFloor.getSI(row, col), 0.1d);
365                         assertEquals("neg", -au.getScale().toStandardUnit(testData[row][col]) / 2.0d, amNeg.getSI(row, col),
366                                 0.1d);
367                         assertEquals("rint", Math.rint(au.getScale().toStandardUnit(testData[row][col]) / 2.0d),
368                                 amRint.getSI(row, col), 0.1d);
369                     }
370                 }
371 
372                 double[][] testData4x4 = new double[][] {{2, 3, 5, 7}, {11, 13, 17, 19}, {23, 29, 31, 37}, {41, 43, 47, 49}};
373                 AreaMatrix am4x4 =
374                         DoubleMatrix.instantiate(DoubleMatrixData.instantiate(testData4x4, au.getScale(), storageType), au);
375                 double det = am4x4.determinant();
376                 double detCalc = Determinant.det(am4x4.getValuesSI());
377                 double err = Math.max(det, detCalc) / 10000.0;
378                 assertEquals("Determinant of square matrix with unit " + au.getDefaultTextualAbbreviation() + ", storage = "
379                         + storageType + " = " + det + " but should have been " + detCalc, detCalc, det, err);
380                 new Try()
381                 {
382                     @Override
383                     public void execute()
384                     {
385                         double detErr = am.determinant();
386                         System.out.println(detErr);
387                     }
388                 }.test("Determinant of non-square matrix should have thrown exception");
389 
390                 // TEST METHODS THAT INVOLVE TWO MATRIX INSTANCES
391 
392                 for (StorageType storageType2 : new StorageType[] {StorageType.DENSE, StorageType.SPARSE})
393                 {
394                     double[][] testData2 = storageType2.equals(StorageType.DENSE) ? denseTestData : reverseSparseTestData;
395                     for (AreaUnit au2 : new AreaUnit[] {AreaUnit.SQUARE_METER, AreaUnit.ACRE})
396                     {
397 
398                         // PLUS and INCREMENTBY(MATRIX)
399                         AreaMatrix am2 = DoubleMatrix
400                                 .instantiate(DoubleMatrixData.instantiate(testData2, au2.getScale(), storageType2), au2);
401                         AreaMatrix amSum1 = am.plus(am2);
402                         AreaMatrix amSum2 = am2.plus(am);
403                         AreaMatrix amSum3 = am.mutable().incrementBy(am2).immutable();
404                         AreaMatrix amSum4 = am2.mutable().incrementBy(am).immutable();
405                         assertEquals("a+b == b+a", amSum1, amSum2);
406                         assertEquals("a+b == b+a", amSum1, amSum3);
407                         assertEquals("a+b == b+a", amSum1, amSum4);
408                         for (int row = 0; row < testData.length; row++)
409                         {
410                             for (int col = 0; col < testData[0].length; col++)
411                             {
412                                 double tolerance = Double.isFinite(amSum1.getSI(row, col))
413                                         ? Math.abs(amSum1.getSI(row, col) / 10000.0d) : 0.1d;
414                                 assertEquals("value in matrix matches",
415                                         au.getScale().toStandardUnit(testData[row][col])
416                                                 + au2.getScale().toStandardUnit(testData2[row][col]),
417                                         amSum1.getSI(row, col), tolerance);
418                             }
419                         }
420 
421                         // MINUS and DECREMENTBY(MATRIX)
422                         AreaMatrix amDiff1 = am.minus(am2);
423                         AreaMatrix amDiff2 = am2.minus(am).mutable().neg();
424                         AreaMatrix amDiff3 = am.mutable().decrementBy(am2).immutable();
425                         assertEquals("a-b == -(b-a)", amDiff1, amDiff2);
426                         assertEquals("a-b == -(b-a)", amDiff1, amDiff3);
427                         for (int row = 0; row < testData.length; row++)
428                         {
429                             for (int col = 0; col < testData[0].length; col++)
430                             {
431                                 double tolerance = Double.isFinite(amDiff1.getSI(row, col))
432                                         ? Math.abs(amDiff1.getSI(row, col) / 10000.0d) : 0.1d;
433                                 assertEquals("value in matrix matches",
434                                         au.getScale().toStandardUnit(testData[row][col])
435                                                 - au2.getScale().toStandardUnit(testData2[row][col]),
436                                         amDiff1.getSI(row, col), tolerance);
437                             }
438                         }
439 
440                         // TIMES(MATRIX) and DIVIDE(MATRIX)
441                         SIMatrix amTim = am.times(am2);
442                         SIMatrix amDiv = am.divide(am2);
443                         assertEquals("unit of m2 * m2 should be m4", "m4",
444                                 amTim.getDisplayUnit().getQuantity().getSiDimensions().toString(false, false, false));
445                         assertEquals("unit of m2 / m2 should be 1", "1",
446                                 amDiv.getDisplayUnit().getQuantity().getSiDimensions().toString(false, false, false));
447                         for (int row = 0; row < testData.length; row++)
448                         {
449                             for (int col = 0; col < testData[0].length; col++)
450                             {
451                                 double tolerance = Double.isFinite(amTim.getSI(row, col))
452                                         ? Math.abs(amTim.getSI(row, col) / 10000.0d) : 0.1d;
453                                 if (Math.abs(au.getScale().toStandardUnit(testData[row][col])
454                                         * au2.getScale().toStandardUnit(testData2[row][col])
455                                         - amTim.getSI(row, col)) > tolerance)
456                                 {
457                                     System.out.println(
458                                             "mismatch at row " + row + ", col " + col + ", got " + amTim.getSI(row, col)
459                                                     + ", expected " + au.getScale().toStandardUnit(testData[row][col])
460                                                             * au2.getScale().toStandardUnit(testData2[row][col]));
461                                     am.times(am2);
462                                 }
463                                 assertEquals("value in m2 * m2 matches",
464                                         au.getScale().toStandardUnit(testData[row][col])
465                                                 * au2.getScale().toStandardUnit(testData2[row][col]),
466                                         amTim.getSI(row, col), tolerance);
467                                 tolerance = Double.isFinite(amTim.getSI(row, col)) ? Math.abs(amDiv.getSI(row, col) / 10000.0d)
468                                         : 0.1d;
469                                 assertEquals("value in m2 / m2 matches (could be NaN)",
470                                         au.getScale().toStandardUnit(testData[row][col])
471                                                 / au2.getScale().toStandardUnit(testData2[row][col]),
472                                         amDiv.getSI(row, col), tolerance);
473                             }
474                         }
475                     }
476                 }
477             }
478         }
479     }
480 
481     /**
482      * Test if mutable methods give an error in case the matrix is immutable.
483      */
484     @Test
485     public void testImmutableMatrix()
486     {
487         double[][] denseTestData = DOUBLEMATRIX.denseRectArrays(5, 10);
488         double[][] sparseTestData = DOUBLEMATRIX.sparseRectArrays(5, 10);
489 
490         for (StorageType storageType : new StorageType[] {StorageType.DENSE, StorageType.SPARSE})
491         {
492             for (AreaUnit au : new AreaUnit[] {AreaUnit.SQUARE_METER, AreaUnit.ACRE})
493             {
494                 double[][] testData = storageType.equals(StorageType.DENSE) ? denseTestData : sparseTestData;
495                 AreaMatrix am =
496                         DoubleMatrix.instantiate(DoubleMatrixData.instantiate(testData, au.getScale(), storageType), au);
497                 am = am.immutable();
498                 final AreaMatrix amPtr = am;
499                 Area fa = Area.of(10.0d, "m^2");
500                 new Try()
501                 {
502                     @Override
503                     public void execute()
504                     {
505                         amPtr.assign(DoubleMathFunctions.ABS);
506                     }
507                 }.test("ImmutableMatrix.assign(...) should throw error");
508                 new Try()
509                 {
510                     @Override
511                     public void execute()
512                     {
513                         amPtr.decrementBy(fa);
514                     }
515                 }.test("ImmutableMatrix.decrementBy(scalar) should throw error");
516                 new Try()
517                 {
518                     @Override
519                     public void execute()
520                     {
521                         amPtr.decrementBy(amPtr);
522                     }
523                 }.test("ImmutableMatrix.decrementBy(matrix) should throw error");
524                 new Try()
525                 {
526                     @Override
527                     public void execute()
528                     {
529                         amPtr.incrementBy(fa);
530                     }
531                 }.test("ImmutableMatrix.incrementBy(scalar) should throw error");
532                 new Try()
533                 {
534                     @Override
535                     public void execute()
536                     {
537                         amPtr.incrementBy(amPtr);
538                     }
539                 }.test("ImmutableMatrix.incrementBy(matrix) should throw error");
540                 new Try()
541                 {
542                     @Override
543                     public void execute()
544                     {
545                         amPtr.divideBy(2.0d);
546                     }
547                 }.test("ImmutableMatrix.divideBy(factor) should throw error");
548                 new Try()
549                 {
550                     @Override
551                     public void execute()
552                     {
553                         amPtr.multiplyBy(2.0d);
554                     }
555                 }.test("ImmutableMatrix.multiplyBy(factor) should throw error");
556                 new Try()
557                 {
558                     @Override
559                     public void execute()
560                     {
561                         amPtr.set(1, 1, fa);
562                     }
563                 }.test("ImmutableMatrix.set() should throw error");
564                 new Try()
565                 {
566                     @Override
567                     public void execute()
568                     {
569                         amPtr.setSI(1, 1, 20.1d);
570                     }
571                 }.test("ImmutableMatrix.setSI() should throw error");
572                 new Try()
573                 {
574                     @Override
575                     public void execute()
576                     {
577                         amPtr.setInUnit(1, 1, 15.2d);
578                     }
579                 }.test("ImmutableMatrix.setInUnit(f) should throw error");
580                 new Try()
581                 {
582                     @Override
583                     public void execute()
584                     {
585                         amPtr.setInUnit(1, 1, 15.2d, AreaUnit.ARE);
586                     }
587                 }.test("ImmutableMatrix.setInUnit(f, u) should throw error");
588                 new Try()
589                 {
590                     @Override
591                     public void execute()
592                     {
593                         amPtr.abs();
594                     }
595                 }.test("ImmutableMatrix.abs() should throw error");
596                 new Try()
597                 {
598                     @Override
599                     public void execute()
600                     {
601                         amPtr.ceil();
602                     }
603                 }.test("ImmutableMatrix.ceil() should throw error");
604                 new Try()
605                 {
606                     @Override
607                     public void execute()
608                     {
609                         amPtr.floor();
610                     }
611                 }.test("ImmutableMatrix.floor() should throw error");
612                 new Try()
613                 {
614                     @Override
615                     public void execute()
616                     {
617                         amPtr.neg();
618                     }
619                 }.test("ImmutableMatrix.neg() should throw error");
620                 new Try()
621                 {
622                     @Override
623                     public void execute()
624                     {
625                         amPtr.rint();
626                     }
627                 }.test("ImmutableMatrix.rint() should throw error");
628             }
629         }
630     }
631 
632     /**
633      * Test toString() methods. TODO: expand?
634      */
635     @Test
636     public void testMatrixToString()
637     {
638         double[][] denseTestData = DOUBLEMATRIX.denseRectArrays(5, 10);
639         double[][] sparseTestData = DOUBLEMATRIX.sparseRectArrays(5, 10);
640 
641         for (StorageType storageType : new StorageType[] {StorageType.DENSE, StorageType.SPARSE})
642         {
643             for (AreaUnit au : new AreaUnit[] {AreaUnit.SQUARE_METER, AreaUnit.ACRE})
644             {
645                 double[][] testData = storageType.equals(StorageType.DENSE) ? denseTestData : sparseTestData;
646                 AreaMatrix am =
647                         DoubleMatrix.instantiate(DoubleMatrixData.instantiate(testData, au.getScale(), storageType), au);
648                 String s1 = am.toString(); // non-verbose with unit
649                 assertTrue(s1.contains(au.getDefaultTextualAbbreviation()));
650                 String s2 = am.toString(AreaUnit.SQUARE_INCH); // non-verbose with unit
651                 assertTrue(s2.contains(AreaUnit.SQUARE_INCH.getDefaultTextualAbbreviation()));
652                 String s3 = am.toString(AreaUnit.SQUARE_INCH, true, true); // verbose with unit
653                 assertTrue(s3.contains(AreaUnit.SQUARE_INCH.getDefaultTextualAbbreviation()));
654                 if (storageType.equals(StorageType.DENSE))
655                 {
656                     assertTrue(s3.contains("Dense"));
657                     assertFalse(s3.contains("Sparse"));
658                 }
659                 else
660                 {
661                     assertFalse(s3.contains("Dense"));
662                     assertTrue(s3.contains("Sparse"));
663                 }
664                 assertTrue(s3.contains("Rel"));
665                 assertFalse(s3.contains("Abs"));
666                 assertTrue(s3.contains("Immutable"));
667                 assertFalse(s3.contains("Mutable"));
668                 AreaMatrix ammut = am.mutable();
669                 String smut = ammut.toString(AreaUnit.SQUARE_INCH, true, true); // verbose with unit
670                 assertFalse(smut.contains("Immutable"));
671                 assertTrue(smut.contains("Mutable"));
672                 String sNotVerbose = ammut.toString(false, false);
673                 assertFalse(sNotVerbose.contains("Rel"));
674                 assertFalse(sNotVerbose.contains("Abs"));
675                 assertFalse(sNotVerbose.contains("Immutable"));
676                 assertFalse(sNotVerbose.contains("Mutable"));
677                 assertFalse(sNotVerbose.contains(au.getDefaultTextualAbbreviation()));
678             }
679         }
680         TimeMatrix tm = DoubleMatrix.instantiate(
681                 DoubleMatrixData.instantiate(denseTestData, TimeUnit.DEFAULT.getScale(), StorageType.DENSE), TimeUnit.DEFAULT);
682         String st = tm.toString(TimeUnit.DEFAULT, true, true); // verbose with unit
683         assertFalse(st.contains("Rel"));
684         assertTrue(st.contains("Abs"));
685         LengthMatrix lm = DoubleMatrix.instantiate(
686                 DoubleMatrixData.instantiate(denseTestData, LengthUnit.SI.getScale(), StorageType.DENSE), LengthUnit.SI);
687         String sl = lm.toString(LengthUnit.SI, true, true); // verbose with unit
688         assertTrue(sl.contains("Rel"));
689         assertFalse(sl.contains("Abs"));
690     }
691 
692     /**
693      * Test the extra methods that Absolute and Relative with Absolute matrices implement.
694      */
695     @Test
696     public void testSpecialMatrixMethodsRelWithAbs()
697     {
698         double[][] denseTestData = DOUBLEMATRIX.denseRectArrays(5, 10);
699         TimeMatrix tm = DoubleMatrix.instantiate(
700                 DoubleMatrixData.instantiate(denseTestData, TimeUnit.DEFAULT.getScale(), StorageType.DENSE), TimeUnit.DEFAULT);
701         DurationMatrix dm = DoubleMatrix.instantiate(
702                 DoubleMatrixData.instantiate(denseTestData, DurationUnit.MINUTE.getScale(), StorageType.DENSE),
703                 DurationUnit.SECOND);
704         assertTrue(tm.isAbsolute());
705         assertFalse(dm.isAbsolute());
706         assertFalse(tm.isRelative());
707         assertTrue(dm.isRelative());
708 
709         TimeMatrix absPlusRel = tm.plus(dm);
710         TimeMatrix absMinusRel = tm.minus(dm);
711         double[][] halfDenseData = DOUBLEMATRIX.denseRectArrays(5, 10);
712         for (int row = 0; row < halfDenseData.length; row++)
713         {
714             for (int col = 0; col < halfDenseData[row].length; col++)
715             {
716                 halfDenseData[row][col] *= 0.5;
717             }
718         }
719         TimeMatrix halfTimeMatrix = DoubleMatrix.instantiate(
720                 DoubleMatrixData.instantiate(halfDenseData, TimeUnit.DEFAULT.getScale(), StorageType.DENSE), TimeUnit.DEFAULT);
721         DurationMatrix absMinusAbs = tm.minus(halfTimeMatrix);
722         TimeMatrix absDecByRelS = tm.mutable().decrementBy(Duration.of(1.0d, "min"));
723         TimeMatrix absDecByRelM = tm.mutable().decrementBy(dm.divide(2.0d));
724         TimeMatrix relPlusAbs = dm.plus(tm);
725         for (int row = 0; row < denseTestData.length; row++)
726         {
727             for (int col = 0; col < denseTestData[0].length; col++)
728             {
729                 assertEquals("absPlusRel", 61.0 * denseTestData[row][col], absPlusRel.getSI(row, col), 0.01);
730                 assertEquals("absMinusRel", -59.0 * denseTestData[row][col], absMinusRel.getSI(row, col), 0.01);
731                 assertEquals("absMinusAbs", denseTestData[row][col] / 2.0, absMinusAbs.getSI(row, col), 0.01);
732                 assertEquals("absDecByRelS", denseTestData[row][col] - 60.0, absDecByRelS.getSI(row, col), 0.01);
733                 assertEquals("absDecByRelM", -29.0 * denseTestData[row][col], absDecByRelM.getSI(row, col), 0.01);
734                 assertEquals("relPlusAbs", 61.0 * denseTestData[row][col], relPlusAbs.getSI(row, col), 0.01);
735             }
736         }
737         for (int dRows : new int[] {-1, 0, 1})
738         {
739             for (int dCols : new int[] {-1, 0, 1})
740             {
741                 if (dRows == 0 && dCols == 0)
742                 {
743                     continue;
744                 }
745                 double[][] other = DOUBLEMATRIX.denseRectArrays(denseTestData.length + dRows, denseTestData[0].length + dCols);
746                 TimeMatrix wrongTimeMatrix = DoubleMatrix.instantiate(
747                         DoubleMatrixData.instantiate(other, TimeUnit.DEFAULT.getScale(), StorageType.DENSE), TimeUnit.DEFAULT);
748                 try
749                 {
750                     tm.mutable().minus(wrongTimeMatrix);
751                     fail("Mismatching size should have thrown a ValueRuntimeException");
752                 }
753                 catch (ValueRuntimeException vre)
754                 {
755                     // Ignore expected exception
756                 }
757             }
758         }
759         assertTrue("toString returns something informative",
760                 DoubleMatrixData.instantiate(denseTestData, TimeUnit.DEFAULT.getScale(), StorageType.DENSE).toString()
761                         .startsWith("DoubleMatrixData"));
762     }
763 
764     /**
765      * Execute a mats++-like memory test on a sparse matrix. See
766      * <a href="http://www.eng.auburn.edu/~agrawvd/COURSE/E7250_05/REPORTS_TERM/Raghuraman_Mem.doc">Memory Test</a>.
767      */
768     @Test
769     public void memoryTest()
770     {
771         FloatAreaMatrix am = FloatMatrix.instantiate(new float[5][10], AreaUnit.SI, StorageType.SPARSE);
772         am = am.mutable();
773         float nonZeroValue = 123.456f;
774         // Initially the array is filled with zero values; we can skip the initialization phase
775         for (int compoundIndex = 0; compoundIndex < am.cols() * am.rows(); compoundIndex++)
776         {
777             // Let the row count fastest
778             int row = compoundIndex % am.rows();
779             int col = compoundIndex / am.rows();
780             assertEquals("initial value is 0", 0f, am.getSI(row, col), 0.0001);
781             am.setSI(row, col, nonZeroValue);
782             assertEquals("current value is nonZero", nonZeroValue, am.getSI(row, col), 0.0001);
783         }
784         for (int compoundIndex = am.cols() * am.rows(); --compoundIndex >= 0;)
785         {
786             // Let the row count fastest
787             int row = compoundIndex % am.rows();
788             int col = compoundIndex / am.rows();
789             assertEquals("current value is nonZero", nonZeroValue, am.getSI(row, col), 0.0001);
790             am.setSI(row, col, 0f);
791             assertEquals("final value is 0", 0f, am.getSI(row, col), 0.0001);
792         }
793     }
794 
795     /**
796      * Test the instantiateAbs method and instantiateScalarAbsSI method.
797      */
798     @Test
799     public void testInstantiateAbs()
800     {
801         double[][] denseTestData = DOUBLEMATRIX.denseRectArrays(10, 20);
802         TimeMatrix timeMatrix = DoubleMatrix.instantiate(
803                 DoubleMatrixData.instantiate(denseTestData, TimeUnit.DEFAULT.getScale(), StorageType.DENSE), TimeUnit.DEFAULT);
804         DurationMatrix durationMatrix = DoubleMatrix.instantiate(
805                 DoubleMatrixData.instantiate(denseTestData, DurationUnit.MINUTE.getScale(), StorageType.DENSE),
806                 DurationUnit.SECOND);
807 
808         float[] halfDenseData = FLOATVECTOR.denseArray(105);
809         for (int index = 0; index < halfDenseData.length; index++)
810         {
811             halfDenseData[index] *= 0.5;
812         }
813         TimeMatrix relPlusAbsTime = durationMatrix.plus(timeMatrix);
814         for (int row = 0; row < denseTestData.length; row++)
815         {
816             for (int col = 0; col < denseTestData[0].length; col++)
817             {
818                 assertEquals("relPlusAbsTime", 61.0 * denseTestData[row][col], relPlusAbsTime.getSI(row, col), 0.01);
819             }
820         }
821         Time time = durationMatrix.instantiateScalarAbsSI(123.456f, TimeUnit.EPOCH_DAY);
822         assertEquals("Unit of instantiateScalarAbsSI matches", TimeUnit.EPOCH_DAY, time.getDisplayUnit());
823         assertEquals("Value of instantiateScalarAbsSI matches", 123.456f, time.si, 0.1);
824 
825         AngleMatrix angleMatrix = DoubleMatrix.instantiate(
826                 DoubleMatrixData.instantiate(denseTestData, AngleUnit.DEGREE.getScale(), StorageType.DENSE), AngleUnit.DEGREE);
827         DirectionMatrix directionMatrix = DoubleMatrix.instantiate(
828                 DoubleMatrixData.instantiate(denseTestData, DirectionUnit.EAST_DEGREE.getScale(), StorageType.DENSE),
829                 DirectionUnit.EAST_DEGREE);
830 
831         DirectionMatrix relPlusAbsDirection = angleMatrix.plus(directionMatrix);
832         for (int row = 0; row < denseTestData.length; row++)
833         {
834             for (int col = 0; col < denseTestData[0].length; col++)
835             {
836                 assertEquals("relPlusAbsTime", 2.0 / 180 * Math.PI * denseTestData[row][col],
837                         relPlusAbsDirection.getSI(row, col), 0.01);
838             }
839         }
840         Direction direction = angleMatrix.instantiateScalarAbsSI(123.456f, DirectionUnit.NORTH_RADIAN);
841         assertEquals("Unit of instantiateScalarAbsSI matches", DirectionUnit.NORTH_RADIAN, direction.getDisplayUnit());
842         assertEquals("Value of instantiateScalarAbsSI matches", 123.456f, direction.si, 0.1);
843 
844         TemperatureMatrix temperatureMatrix = DoubleMatrix.instantiate(
845                 DoubleMatrixData.instantiate(denseTestData, TemperatureUnit.DEGREE_FAHRENHEIT.getScale(), StorageType.DENSE),
846                 TemperatureUnit.DEGREE_FAHRENHEIT);
847         AbsoluteTemperatureMatrix absoluteTemperatureMatrix = DoubleMatrix.instantiate(
848                 DoubleMatrixData.instantiate(denseTestData, AbsoluteTemperatureUnit.KELVIN.getScale(), StorageType.DENSE),
849                 AbsoluteTemperatureUnit.KELVIN);
850 
851         AbsoluteTemperatureMatrix relPlusAbsTemperature = temperatureMatrix.plus(absoluteTemperatureMatrix);
852         for (int row = 0; row < denseTestData.length; row++)
853         {
854             for (int col = 0; col < denseTestData[0].length; col++)
855             {
856                 assertEquals("relPlusAbsTime", (1.0 + 5.0 / 9.0) * denseTestData[row][col],
857                         relPlusAbsTemperature.getSI(row, col), 0.01);
858             }
859         }
860         AbsoluteTemperature absoluteTemperature =
861                 temperatureMatrix.instantiateScalarAbsSI(123.456f, AbsoluteTemperatureUnit.DEGREE_FAHRENHEIT);
862         assertEquals("Unit of instantiateScalarAbsSI matches", AbsoluteTemperatureUnit.DEGREE_FAHRENHEIT,
863                 absoluteTemperature.getDisplayUnit());
864         assertEquals("Value of instantiateScalarAbsSI matches", 123.456f, absoluteTemperature.si, 0.1);
865 
866         LengthMatrix lengthMatrix = DoubleMatrix.instantiate(
867                 DoubleMatrixData.instantiate(denseTestData, LengthUnit.MILE.getScale(), StorageType.DENSE), LengthUnit.MILE);
868         PositionMatrix positionMatrix = DoubleMatrix.instantiate(
869                 DoubleMatrixData.instantiate(denseTestData, PositionUnit.KILOMETER.getScale(), StorageType.DENSE),
870                 PositionUnit.KILOMETER);
871 
872         PositionMatrix relPlusAbsPosition = lengthMatrix.plus(positionMatrix);
873         for (int row = 0; row < denseTestData.length; row++)
874         {
875             for (int col = 0; col < denseTestData[0].length; col++)
876             {
877                 assertEquals("relPlusAbsTime", 2609.344 * denseTestData[row][col], relPlusAbsPosition.getSI(row, col), 0.1);
878             }
879         }
880         Position position = lengthMatrix.instantiateScalarAbsSI(123.456f, PositionUnit.ANGSTROM);
881         assertEquals("Unit of instantiateScalarAbsSI matches", PositionUnit.ANGSTROM, position.getDisplayUnit());
882         assertEquals("Value of instantiateScalarAbsSI matches", 123.456f, position.si, 0.01);
883     }
884 
885     /**
886      * Test the <code>as</code> method of the SIMatrix class.
887      * @throws SecurityException on error
888      * @throws NoSuchMethodException on error
889      * @throws InvocationTargetException on error
890      * @throws IllegalArgumentException on error
891      * @throws IllegalAccessException on error
892      * @throws ClassNotFoundException on error
893      * @throws UnitException on error
894      * @param <U> the unit type
895      */
896     @SuppressWarnings("unchecked")
897     @Test
898     public <U extends Unit<U>> void testAsUnit() throws ClassNotFoundException, NoSuchMethodException, SecurityException,
899             IllegalAccessException, IllegalArgumentException, InvocationTargetException, UnitException
900     {
901         double[][] testValues = DOUBLEMATRIX.denseRectArrays(10, 20);
902         for (StorageType storageType : new StorageType[] {StorageType.DENSE, StorageType.SPARSE})
903         {
904             for (String type : CLASSNAMES.REL_LIST)
905             {
906                 Class.forName("org.djunits.unit." + type + "Unit");
907                 Quantity<U> quantity = (Quantity<U>) Quantities.INSTANCE.getQuantity(type + "Unit");
908                 for (U unit : quantity.getUnitsById().values())
909                 {
910                     for (StorageType storageType2 : new StorageType[] {StorageType.DENSE, storageType})
911                     {
912                         SIUnit siUnit = SIUnit.of(unit.getQuantity().getSiDimensions());
913                         SIMatrix matrix = SIMatrix.instantiate(testValues, siUnit, storageType2);
914                         Method asMethod = SIMatrix.class.getDeclaredMethod("as", Unit.class);
915                         AbstractDoubleMatrixRel<U, ?, ?, ?> asMatrix =
916                                 (AbstractDoubleMatrixRel<U, ?, ?, ?>) asMethod.invoke(matrix, siUnit);
917                         assertEquals(matrix.getDisplayUnit().getStandardUnit(), asMatrix.getDisplayUnit());
918                         siUnit = SIUnit.of(AbsoluteTemperatureUnit.KELVIN.getQuantity().getSiDimensions());
919                         for (int row = 0; row < testValues.length; row++)
920                         {
921                             for (int col = 0; col < testValues[0].length; col++)
922                             {
923                                 assertEquals("Values should match", testValues[row][col], matrix.getInUnit(row, col), 0.001);
924                             }
925                         }
926                         try
927                         {
928                             asMethod.invoke(matrix, siUnit);
929                             fail("as method should not be able to cast to unrelated (absoluteTemperature) unit");
930                         }
931                         catch (InvocationTargetException ite)
932                         {
933                             Throwable cause = ite.getCause();
934                             assertEquals("cause is UnitRuntimeException", UnitRuntimeException.class, cause.getClass());
935                             // Otherwise ignore expected exception
936                         }
937                     }
938                 }
939             }
940         }
941     }
942 
943     /**
944      * Test the equals method.
945      */
946     @SuppressWarnings("unlikely-arg-type")
947     @Test
948     public void testEquals()
949     {
950         double[][] testData = DOUBLEMATRIX.denseRectArrays(12, 34);
951         testData[2][2] = 0;
952         for (StorageType storageType : new StorageType[] {StorageType.DENSE, StorageType.SPARSE})
953         {
954             DoubleMatrixData dmd = DoubleMatrixData.instantiate(testData, TemperatureUnit.KELVIN.getScale(), storageType);
955             assertTrue("Double matrix is equal to itself", dmd.equals(dmd));
956             assertFalse("Double matrix is not equal to null", dmd.equals(null));
957             assertFalse("Double matrix data is not equal to some string", dmd.equals("some string"));
958             assertTrue("Double matrix is equal to sparse version of itself", dmd.equals(dmd.toSparse()));
959             assertTrue("Double matrix is equal to dense version of itself", dmd.equals(dmd.toDense()));
960             for (StorageType storageType2 : new StorageType[] {StorageType.DENSE, StorageType.SPARSE})
961             {
962                 DoubleMatrixData dvd2 = DoubleMatrixData.instantiate(testData, TemperatureUnit.KELVIN.getScale(), storageType2);
963                 assertEquals(
964                         "Double matrix data is equal to other double vector containing same values regardless of storage type",
965                         dmd, dvd2);
966                 double[][] testData2 = DOUBLEMATRIX.denseRectArrays(12, 33);
967                 testData2[2][2] = 0;
968                 dvd2 = DoubleMatrixData.instantiate(testData2, TemperatureUnit.KELVIN.getScale(), storageType2);
969                 assertFalse("Double matrix data is not equal to other double vector containing same values except last one",
970                         dmd.equals(dvd2));
971                 testData2 = DOUBLEMATRIX.denseRectArrays(13, 34);
972                 testData2[2][2] = 0;
973                 dvd2 = DoubleMatrixData.instantiate(testData2, TemperatureUnit.KELVIN.getScale(), storageType2);
974                 assertFalse("Double matrix data is not equal to other double vector containing same values except last one",
975                         dmd.equals(dvd2));
976                 testData2 = DOUBLEMATRIX.denseRectArrays(12, 34);
977                 dvd2 = DoubleMatrixData.instantiate(testData2, TemperatureUnit.KELVIN.getScale(), storageType2);
978                 assertFalse("Double matrix data is not equal to other double vector containing same values except for one zero",
979                         dmd.equals(dvd2));
980             }
981         }
982     }
983 
984     /**
985      * Test the sparse value class.
986      */
987     @SuppressWarnings({"rawtypes", "unchecked"})
988     @Test
989     public void sparseValueTest()
990     {
991         try
992         {
993             new DoubleSparseValue(-1, 0, 123.456);
994             fail("Negative row should have caused a ValueRuntimeException");
995         }
996         catch (ValueRuntimeException vre)
997         {
998             // Ignore expected exception
999         }
1000 
1001         try
1002         {
1003             new DoubleSparseValue(0, -1, 123.456);
1004             fail("Negative column should have caused a ValueRuntimeException");
1005         }
1006         catch (ValueRuntimeException vre)
1007         {
1008             // Ignore expected exception
1009         }
1010 
1011         Length length = Length.valueOf("123 km");
1012         DoubleSparseValue dsv = new DoubleSparseValue(2, 3, length);
1013         assertEquals("row matches", 2, dsv.getRow());
1014         assertEquals("column matches", 3, dsv.getColumn());
1015         assertEquals("value matches", 123000, dsv.getValueSI(), 0.1);
1016         dsv = new DoubleSparseValue(2, 3, 123.000, LengthUnit.KILOMETER);
1017         assertEquals("row matches", 2, dsv.getRow());
1018         assertEquals("column matches", 3, dsv.getColumn());
1019         assertEquals("value matches", 123000, dsv.getValueSI(), 0.1);
1020     }
1021 
1022 }