Package cern.colt.matrix

Examples of cern.colt.matrix.DoubleMatrix1D


  public void solveUsingColt()
  {
    LUDecompositionQuick solver = new LUDecompositionQuick();
    DoubleMatrix2D matrix = toDoubleMatrix2D();
    solver.decompose(matrix);solver.setLU(matrix);
    DoubleMatrix1D result = toDoubleMatrix1D();
    solver.solve(result);result.toArray(j_x);   
  }
View Full Code Here


  /** Converts the "b" vector to Colt format, for a fall back on Colt when
   * external solver is not found as well as for testing.
   */
  public DoubleMatrix1D toDoubleMatrix1D()
  {
    DoubleMatrix1D result = DoubleFactory1D.dense.make(j_b);
    return result;
  }
View Full Code Here

  @Test
  public final void testExternalSolver_random()
  {
    final DoubleMatrix2D matrix = DoubleFactory2D.sparse.make(size,size);
    matrix.assign(randomGenerator);
    final DoubleMatrix1D b = DoubleFactory1D.dense.make(size);
    b.assign(randomGenerator);
    final LSolver solver = new LSolver(matrix,DoubleFactory1D.dense.make(matrix.rows(), 0));

    boolean singular = false;
    try
    {
      solver.solveExternally();
    }
    catch(IllegalArgumentException ex)
    {
      if (ex.getMessage().contains("singular"))
        singular = true;
      else
        throw ex;
    }
   
    if (!singular)
      TestSolver.verifyAxb(solver);
   
    LUDecompositionQuick coltSolver = new LUDecompositionQuick();
    coltSolver.decompose(matrix);coltSolver.setLU(matrix);
    DoubleMatrix1D vector = DoubleFactory1D.dense.make(matrix.rows(), 0);
   
    try
    {
      coltSolver.solve(vector);
    }
    catch(IllegalArgumentException ex)
    {
      if (ex.getMessage().contains("singular"))
        Assert.assertTrue(singular);
      else
        throw ex;
    }

    if (!singular)
    {
      for(int i=0;i<matrix.rows();++i)
        Assert.assertEquals(solver.j_x[i], vector.getQuick(i),TestSolver.comparisonAccuracy);
      TestSolver.verifyAxb(matrix,solver.toDoubleMatrix1D(),vector);
    }
   
    try
    {
      for(int i=0;i<matrix.rows();++i) solver.j_x[i]=0;
      solver.solveUsingColt();
    }
    catch(IllegalArgumentException ex)
    {
      if (ex.getMessage().contains("singular"))
        Assert.assertTrue(singular);
      else
        throw ex;
    }
   
    if (!singular)
    {
      for(int i=0;i<matrix.rows();++i)
        Assert.assertEquals(solver.j_x[i], vector.getQuick(i),TestSolver.comparisonAccuracy);
      TestSolver.verifyAxb(solver);
    }
  }
View Full Code Here

   * @param b
   * @param x
   */
  public static final void verifyAxb(DoubleMatrix2D A, DoubleMatrix1D b, DoubleMatrix1D x)
  {
    DoubleMatrix1D result = A.zMult(x, b);for(int i=0;i<b.size();++i) Assert.assertEquals(b.getQuick(i),result.getQuick(i),comparisonAccuracy);
  }
View Full Code Here

 
  @Test
  public final void testExternalSolver1()
  {
    LSolver s = new LSolver(testMatrix,DoubleFactory1D.dense.make(new double[]{8., 45., -3., 3., 19.}));
    DoubleMatrix1D x = DoubleFactory1D.dense.make(testMatrix.rows());
    for(int i=0;i<s.j_b.length;++i) x.setQuick(i, s.j_b[i]);
   
    // Test 1
    s.solveExternally();
    for(int i=0;i<testMatrix.rows();++i)
      Assert.assertEquals(i+1, s.j_x[i],comparisonAccuracy);
    verifyAxb(s);
   
    // Test 2
    LUDecompositionQuick solver = new LUDecompositionQuick();
    solver.decompose(testMatrix);solver.setLU(testMatrix);
    solver.solve(x);
    for(int i=0;i<testMatrix.rows();++i)
      Assert.assertEquals(i+1, x.getQuick(i),comparisonAccuracy);
    verifyAxb(s);

    // Test 3
    for(int i=0;i<testMatrix.rows();++i) s.j_x[i]=0;
    s.solveUsingColt();
View Full Code Here

  public final void testExternalSolver_fail6()
  {
    final int size = testMatrix.rows();
    final DoubleMatrix2D matrix = DoubleFactory2D.sparse.make(size,size);
    final LSolver solver = new LSolver(matrix,DoubleFactory1D.dense.make(new double[]{8., 45., -3., 3., 19.}));
    final DoubleMatrix1D x = DoubleFactory1D.dense.make(testMatrix.rows());
   
    for(int i=0;i<solver.j_b.length;++i) x.setQuick(i, solver.j_b[i]);
   
    TestAbstractExperiment.checkForCorrectException(new whatToRun() {
      public void run() throws NumberFormatException {
        solver.solveExternally();
      }
View Full Code Here

    for(int cnt=0;cnt < 14000;++cnt)
    {
      int x = rnd.nextInt(size), y = rnd.nextInt(size);
      matrix.setQuick(x, y, 0.5);
    }
    final DoubleMatrix1D b = DoubleFactory1D.dense.make(size);
    b.assign(randomGenerator);

    final LSolver solver = new LSolver(matrix,b);
    long tmStarted = new Date().getTime();
    LUDecompositionQuick coltSolver = new LUDecompositionQuick();
    coltSolver.decompose(matrix);coltSolver.setLU(matrix);
   
    DoubleMatrix1D vector = b.copy();
    coltSolver.solve(vector);
    long tmFinished = new Date().getTime();
    System.out.println(" time taken: "+((double)tmFinished-tmStarted)/1000);
    verifyAxb(matrix, b, vector);
   
    tmStarted = new Date().getTime();
    solver.solveExternally();
    tmFinished = new Date().getTime();
    System.out.println(" time taken: "+((double)tmFinished-tmStarted)/1000);   
    for(int i=0;i<matrix.rows();++i)
      Assert.assertEquals(solver.j_x[i], vector.getQuick(i),comparisonAccuracy);
    verifyAxb(solver);
  }
View Full Code Here

   * @return the textual representation of the system of equations stored in <em>solver</em>.
   */
  public String dumpEquations(LSolver solver,final int [] incompatiblePairs, Map<CmpVertex,CmpVertex> newToOrig)
  {
    DoubleMatrix2D matrix = solver.toDoubleMatrix2D();
    DoubleMatrix1D b = solver.toDoubleMatrix1D();
    StringBuffer result = new StringBuffer();
   
    for(int row=0;row<matrix.rows();++row)
    {
      int indexRow = findIndexOf(incompatiblePairs, row);
      assert indexRow >= 0;
      PairScore pairRow = getPairScore(indexRow, 0,0);
     
      boolean firstValue = true;
      for(int column = 0;column < matrix.columns();++column)
      {
        double value = matrix.getQuick(row, column);
        if (value != 0) // this is not a computed value, hence it is reasonable to compare this double to zero in this way.
        {
          int indexColumn = findIndexOf(incompatiblePairs, column);
          assert indexColumn >= 0;
          PairScore pairColumn = getPairScore(indexColumn, 0,0);
          if (!firstValue) result.append(" + ");else firstValue = false;
          result.append(value);result.append("(");appendPair(result,pairRow, newToOrig);result.append(":");appendPair(result,pairColumn, newToOrig);result.append(")");

        }
      }
      result.append(" = ");result.append(b.getQuick(row));result.append("\n");
    }
   
    return result.toString();
  }
View Full Code Here

  }
 
  protected DoubleMatrix1D getExpectedMatrix1DSlowly(LearnerGraph gr)
  {
    int size=gr.getStateNumber()*(gr.getStateNumber()+1)/2;
    DoubleMatrix1D result = DoubleFactory1D.dense.make(size);
    StatesToConsider filter = LearnerGraphND.ignoreRejectStates;
    GDLearnerGraph ndGraph = new GDLearnerGraph(gr,filter, false);
    DetermineDiagonalAndRightHandSide ddrhInstance = ndGraph.new DDRH_default();
    for(Entry<CmpVertex,Map<String,List<CmpVertex>>> entryA:ndGraph.matrixForward.transitionMatrix.entrySet())
    {
      // Now iterate through states
      Iterator<Entry<CmpVertex,Map<String,List<CmpVertex>>>> stateB_It = ndGraph.matrixForward.transitionMatrix.entrySet().iterator();
      while(stateB_It.hasNext())
      {
        Entry<CmpVertex,Map<String,List<CmpVertex>>> stateB = stateB_It.next();

        int currentStatePair = ndGraph.vertexToIntNR(stateB.getKey(),entryA.getKey());
        ddrhInstance.compute(entryA.getKey(),stateB.getKey(),entryA.getValue(),stateB.getValue());
        result.setQuick(currentStatePair, ddrhInstance.getRightHandSide());
       
        if (stateB.getKey().equals(entryA.getKey())) break; // we only process a triangular subset.
      }
    }
   
View Full Code Here

    GDLearnerGraph ndGraph = new GDLearnerGraph(gr,LearnerGraphND.ignoreRejectStates, false);
    LSolver solver = ndGraph.buildMatrix(ThreadNumber);
    DoubleMatrix2D Ax=solver.toDoubleMatrix2D();
    Assert.assertEquals(getExpectedMatrix2DSlowly(gr),Ax);
    if (expectedAx != null) Assert.assertEquals(expectedAx, Ax);
    DoubleMatrix1D b=solver.toDoubleMatrix1D();
    if (expectedB != null) Assert.assertEquals(expectedB, b);Assert.assertEquals(getExpectedMatrix1DSlowly(gr),b);
    solver.solveExternally();// check if we have a solution, just in case it fails.

    // Now check consistency.
    gr.config.setAttenuationK_testOnly(1);DoubleMatrix2D Ax1 = ndGraph.buildMatrix(ThreadNumber).toDoubleMatrix2D();
    gr.config.setAttenuationK(0);DoubleMatrix2D Ax0 = ndGraph.buildMatrix(ThreadNumber).toDoubleMatrix2D();
    DoubleMatrix1D one = DoubleFactory1D.dense.make(Ax1.rows(), 1), a=DoubleFactory1D.dense.make(Ax.rows(), 0);
   
    // check A(1)*one >= 0
    Ax1.zMult(one, a);for(int i=0;i<a.size();++i) Assert.assertTrue(a.getQuick(i)>=0);
   
    // check (A(1)-A(0))*one = b
View Full Code Here

TOP

Related Classes of cern.colt.matrix.DoubleMatrix1D

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.