gt:         FieldMatrix<Fraction> m = createSparseMatrix(testData);
context: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.math.linear;
  
  import org.apache.commons.math.Field;
  import org.apache.commons.math.fraction.Fraction;
  import org.apache.commons.math.fraction.FractionConversionException;
  import org.apache.commons.math.fraction.FractionField;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * Test cases for the {@link SparseFieldMatrix} class.
   *
   * @version $Revision: 728186 $ $Date: 2009-04-20 11:42:11 -0700 (Mon, 20 Apr 2009) $
   */
  public class SparseFieldMatrixTest extends TestCase {
      // 3 x 3 identity matrix
      protected Fraction[][] id = { {new Fraction(1), new Fraction(0), new Fraction(0) }, { new Fraction(0), new Fraction(1), new Fraction(0) }, { new Fraction(0), new Fraction(0), new Fraction(1) } };
      // Test data for group operations
      protected Fraction[][] testData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) },
              { new Fraction(1), new Fraction(0), new Fraction(8) } };
      protected Fraction[][] testDataLU = null;
      protected Fraction[][] testDataPlus2 = { { new Fraction(3), new Fraction(4), new Fraction(5) }, { new Fraction(4), new Fraction(7), new Fraction(5) },
              { new Fraction(3), new Fraction(2), new Fraction(10) } };
      protected Fraction[][] testDataMinus = { { new Fraction(-1), new Fraction(-2), new Fraction(-3) },
              { new Fraction(-2), new Fraction(-5), new Fraction(-3) }, { new Fraction(-1), new Fraction(0), new Fraction(-8) } };
      protected Fraction[] testDataRow1 = { new Fraction(1), new Fraction(2), new Fraction(3) };
      protected Fraction[] testDataCol3 = { new Fraction(3), new Fraction(3), new Fraction(8) };
      protected Fraction[][] testDataInv = { { new Fraction(-40), new Fraction(16), new Fraction(9) }, { new Fraction(13), new Fraction(-5), new Fraction(-3) },
              { new Fraction(5), new Fraction(-2), new Fraction(-1) } };
      protected Fraction[] preMultTest = { new Fraction(8), new Fraction(12), new Fraction(33) };
      protected Fraction[][] testData2 = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) } };
      protected Fraction[][] testData2T = { { new Fraction(1), new Fraction(2) }, { new Fraction(2), new Fraction(5) }, { new Fraction(3), new Fraction(3) } };
      protected Fraction[][] testDataPlusInv = { { new Fraction(-39), new Fraction(18), new Fraction(12) },
              { new Fraction(15), new Fraction(0), new Fraction(0) }, { new Fraction(6), new Fraction(-2), new Fraction(7) } };
  
      // lu decomposition tests
      protected Fraction[][] luData = { { new Fraction(2), new Fraction(3), new Fraction(3) }, { new Fraction(0), new Fraction(5), new Fraction(7) }, { new Fraction(6), new Fraction(9), new Fraction(8) } };
      protected Fraction[][] luDataLUDecomposition = null;
  
      // singular matrices
      protected Fraction[][] singular = { { new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(3) } };
      protected Fraction[][] bigSingular = { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) },
              { new Fraction(2), new Fraction(5), new Fraction(3), new Fraction(4) }, { new Fraction(7), new Fraction(3), new Fraction(256), new Fraction(1930) }, { new Fraction(3), new Fraction(7), new Fraction(6), new Fraction(8) } }; // 4th
  
      // row
      // =
      // 1st
      // +
      // 2nd
      protected Fraction[][] detData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(4), new Fraction(5), new Fraction(6) },
              { new Fraction(7), new Fraction(8), new Fraction(10) } };
      protected Fraction[][] detData2 = { { new Fraction(1), new Fraction(3) }, { new Fraction(2), new Fraction(4) } };
  
      // vectors
      protected Fraction[] testVector = { new Fraction(1), new Fraction(2), new Fraction(3) };
      protected Fraction[] testVector2 = { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) };
  
      // submatrix accessor tests
      protected Fraction[][] subTestData = null;
  
      // array selections
      protected Fraction[][] subRows02Cols13 = { {new Fraction(2), new Fraction(4) }, { new Fraction(4), new Fraction(8) } };
      protected Fraction[][] subRows03Cols12 = { { new Fraction(2), new Fraction(3) }, { new Fraction(5), new Fraction(6) } };
      protected Fraction[][] subRows03Cols123 = { { new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7) } };
  
      // effective permutations
      protected Fraction[][] subRows20Cols123 = { { new Fraction(4), new Fraction(6), new Fraction(8) }, { new Fraction(2), new Fraction(3), new Fraction(4) } };
      protected Fraction[][] subRows31Cols31 = null;
  
      // contiguous ranges
      protected Fraction[][] subRows01Cols23 = null;
      protected Fraction[][] subRows23Cols00 = { { new Fraction(2) }, { new Fraction(4) } };
      protected Fraction[][] subRows00Cols33 = { { new Fraction(4) } };
  
      // row matrices
      protected Fraction[][] subRow0 = { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) } };
      protected Fraction[][] subRow3 = { { new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7) } };
  
      // column matrices
      protected Fraction[][] subColumn1 = null;
      protected Fraction[][] subColumn3 = null;
  
      // tolerances
      protected double entryTolerance = 10E-16;
      protected double normTolerance = 10E-14;
      protected Field<Fraction> field = FractionField.getInstance();
  
      public SparseFieldMatrixTest(String name) {
          super(name);
          setupFractionArrays();
      }
  
      private void setupFractionArrays() {
          try {
              testDataLU = new Fraction[][]{ { new Fraction(2), new Fraction(5), new Fraction(3) }, { new Fraction(.5d), new Fraction(-2.5d), new Fraction(6.5d) },
                      { new Fraction(0.5d), new Fraction(0.2d), new Fraction(.2d) } };
              luDataLUDecomposition = new Fraction[][]{ { new Fraction(6), new Fraction(9), new Fraction(8) },
                  { new Fraction(0), new Fraction(5), new Fraction(7) }, { new Fraction(0.33333333333333), new Fraction(0), new Fraction(0.33333333333333) } };
              subTestData = new Fraction [][]{ { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) },
                      { new Fraction(1.5), new Fraction(2.5), new Fraction(3.5), new Fraction(4.5) }, { new Fraction(2), new Fraction(4), new Fraction(6), new Fraction(8) }, { new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7) } };
              subRows31Cols31 = new Fraction[][]{ { new Fraction(7), new Fraction(5) }, { new Fraction(4.5), new Fraction(2.5) } };
              subRows01Cols23 = new Fraction[][]{ { new Fraction(3), new Fraction(4) }, { new Fraction(3.5), new Fraction(4.5) } };
              subColumn1 = new Fraction [][]{ { new Fraction(2) }, { new Fraction(2.5) }, { new Fraction(4) }, { new Fraction(5) } };
              subColumn3 = new Fraction[][]{ { new Fraction(4) }, { new Fraction(4.5) }, { new Fraction(8) }, { new Fraction(7) } };
          } catch (FractionConversionException e) {
              // ignore, can't happen
          }
          
                  
      }
  
      public static Test suite() {
          TestSuite suite = new TestSuite(SparseFieldMatrixTest.class);
          suite.setName("SparseFieldMatrix<Fraction> Tests");
          return suite;
      }
  
      /** test dimensions */
      public void testDimensions() {
          SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
          SparseFieldMatrix<Fraction> m2 = createSparseMatrix(testData2);
          assertEquals("testData row dimension", 3, m.getRowDimension());
          assertEquals("testData column dimension", 3, m.getColumnDimension());
          assertTrue("testData is square", m.isSquare());
          assertEquals("testData2 row dimension", m2.getRowDimension(), 2);
          assertEquals("testData2 column dimension", m2.getColumnDimension(), 3);
          assertTrue("testData2 is not square", !m2.isSquare());
      }
  
      /** test copy functions */
      public void testCopyFunctions() {
          SparseFieldMatrix<Fraction> m1 = createSparseMatrix(testData);
          FieldMatrix<Fraction> m2 = m1.copy();
          assertEquals(m1.getClass(), m2.getClass());
          assertEquals((m2), m1);
          SparseFieldMatrix<Fraction> m3 = createSparseMatrix(testData);
          FieldMatrix<Fraction> m4 = m3.copy();
          assertEquals(m3.getClass(), m4.getClass());
          assertEquals((m4), m3);
      }
  
      /** test add */
      public void testAdd() {
          SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
          SparseFieldMatrix<Fraction> mInv = createSparseMatrix(testDataInv);
          SparseFieldMatrix<Fraction> mDataPlusInv = createSparseMatrix(testDataPlusInv);
          FieldMatrix<Fraction> mPlusMInv = m.add(mInv);
          for (int row = 0; row < m.getRowDimension(); row++) {
              for (int col = 0; col < m.getColumnDimension(); col++) {
                  assertEquals("sum entry entry", 
                      mDataPlusInv.getEntry(row, col).doubleValue(), mPlusMInv.getEntry(row, col).doubleValue(), 
                      entryTolerance);
              }
          }
      }
  
      /** test add failure */
      public void testAddFail() {
          SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
          SparseFieldMatrix<Fraction> m2 = createSparseMatrix(testData2);
          try {
              m.add(m2);
              fail("IllegalArgumentException expected");
          } catch (IllegalArgumentException ex) {
              // ignored
          }
      }
  
      
      /** test m-n = m + -n */
      public void testPlusMinus() {
          SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
          SparseFieldMatrix<Fraction> n = createSparseMatrix(testDataInv);
          assertClose("m-n = m + -n", m.subtract(n),
              n.scalarMultiply(new Fraction(-1)).add(m), entryTolerance);
          try {
              m.subtract(createSparseMatrix(testData2));
              fail("Expecting illegalArgumentException");
          } catch (IllegalArgumentException ex) {
              // ignored
          }
      }
  
      /** test multiply */
      public void testMultiply() {
          SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
          SparseFieldMatrix<Fraction> mInv = createSparseMatrix(testDataInv);
          SparseFieldMatrix<Fraction> identity = createSparseMatrix(id);
          SparseFieldMatrix<Fraction> m2 = createSparseMatrix(testData2);
          assertClose("inverse multiply", m.multiply(mInv), identity,
                  entryTolerance);
          assertClose("inverse multiply", m.multiply(new Array2DRowFieldMatrix<Fraction>(testDataInv)), identity,
                      entryTolerance);
          assertClose("inverse multiply", mInv.multiply(m), identity,
                  entryTolerance);
          assertClose("identity multiply", m.multiply(identity), m,
                  entryTolerance);
          assertClose("identity multiply", identity.multiply(mInv), mInv,
                  entryTolerance);
          assertClose("identity multiply", m2.multiply(identity), m2,
                  entryTolerance);
          try {
              m.multiply(createSparseMatrix(bigSingular));
              fail("Expecting illegalArgumentException");
          } catch (IllegalArgumentException ex) {
              // ignored
          }
      }
  
      // Additional Test for Array2DRowRealMatrixTest.testMultiply
  
      private Fraction[][] d3 = new Fraction[][] { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8) } };
      private Fraction[][] d4 = new Fraction[][] { { new Fraction(1) }, { new Fraction(2) }, { new Fraction(3) }, { new Fraction(4) } };
      private Fraction[][] d5 = new Fraction[][] { { new Fraction(30) }, { new Fraction(70) } };
  
      public void testMultiply2() {
          FieldMatrix<Fraction> m3 = createSparseMatrix(d3);
          FieldMatrix<Fraction> m4 = createSparseMatrix(d4);
          FieldMatrix<Fraction> m5 = createSparseMatrix(d5);
          assertClose("m3*m4=m5", m3.multiply(m4), m5, entryTolerance);
      }
  
      /** test trace */
      public void testTrace() {
          FieldMatrix<Fraction> m = createSparseMatrix(id);
          assertEquals("identity trace", 3d, m.getTrace().doubleValue(), entryTolerance);
          m = createSparseMatrix(testData2);
          try {
              m.getTrace();
              fail("Expecting NonSquareMatrixException");
          } catch (NonSquareMatrixException ex) {
              // ignored
          }
      }
  
      /** test sclarAdd */
      public void testScalarAdd() {
          FieldMatrix<Fraction> m = createSparseMatrix(testData);
          assertClose("scalar add", createSparseMatrix(testDataPlus2), 
              m.scalarAdd(new Fraction(2)), entryTolerance);
      }
  
      /** test operate */
      public void testOperate() {
          FieldMatrix<Fraction> m = createSparseMatrix(id);
          assertClose("identity operate", testVector, m.operate(testVector),
                  entryTolerance);
          assertClose("identity operate", testVector, m.operate(
                  new ArrayFieldVector<Fraction>(testVector)).getData(), entryTolerance);
          m = createSparseMatrix(bigSingular);
          try {
              m.operate(testVector);
              fail("Expecting illegalArgumentException");
          } catch (IllegalArgumentException ex) {
              // ignored
          }
      }
  
      /** test issue MATH-209 */
      public void testMath209() {
          FieldMatrix<Fraction> a = createSparseMatrix(new Fraction[][] {
                  { new Fraction(1), new Fraction(2) }, { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } });
          Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) });
          assertEquals(a.getRowDimension(), b.length);
          assertEquals(3.0, b[0].doubleValue(), 1.0e-12);
          assertEquals(7.0, b[1].doubleValue(), 1.0e-12);
          assertEquals(11.0, b[2].doubleValue(), 1.0e-12);
      }
  
      /** test transpose */
      public void testTranspose() {
          
          FieldMatrix<Fraction> m = createSparseMatrix(testData); 
          FieldMatrix<Fraction> mIT = new FieldLUDecompositionImpl<Fraction>(m).getSolver().getInverse().transpose();
          FieldMatrix<Fraction> mTI = new FieldLUDecompositionImpl<Fraction>(m.transpose()).getSolver().getInverse();
          assertClose("inverse-transpose", mIT, mTI, normTolerance);
          m = createSparseMatrix(testData2);
          FieldMatrix<Fraction> mt = createSparseMatrix(testData2T);
          assertClose("transpose",mt,m.transpose(),normTolerance);
      }
  
      /** test preMultiply by vector */
      public void testPremultiplyVector() {
  