Example #1
0
  def test_solve(self):
    import random
    
    spA = [
      Sparsity.dense(1,1)
    ]
    
    for n in range(2,5):
      spA+= [
        Sparsity.diag(n),
        Sparsity.dense(n,n),
        Sparsity.lower(n),
        Sparsity.lower(n).T,
        Sparsity.banded(n,1),
        diagcat(*[Sparsity.diag(n),Sparsity.dense(n,n)]),
        diagcat(*[Sparsity.diag(n),Sparsity.lower(n)]),
        diagcat(*[Sparsity.diag(n),Sparsity.lower(n).T]),
        diagcat(*[Sparsity.lower(n),Sparsity.lower(n).T]),
        Sparsity.diag(n)+Sparsity.rowcol([0],[n-1],n,n),
        Sparsity.diag(n)+Sparsity.rowcol([0,n-1],[n-1,0],n,n),
        Sparsity.diag(n)+Sparsity.triplet(n,n,[0],[n-1]),
        Sparsity.diag(n)+Sparsity.triplet(n,n,[0,n-1],[n-1,0]),
      ]
    
    for sA in spA:

      random.seed(1)
      a = DM(sA,[random.random() for i in range(sA.nnz())])
      A = SX.sym("a",a.sparsity())
      for sB in [ Sparsity.dense(a.size1(),1), vertcat(Sparsity.dense(1,1),Sparsity(a.size1()-1,1)),Sparsity.lower(a.size1()),Sparsity.lower(a.size1()).T]:

        b = DM(sB,[random.random() for i in range(sB.nnz())])
        B = SX.sym("B",b.sparsity())
        C = solve(A,B)
        
        f = Function("f", [A,B],[C])
        

        c = f(a,b)
            
        c_ref = DM(linalg.solve(a,b))
        c_ref = sparsify(c_ref)
                
        print sA.dim(), sB.dim()

        try:
          self.checkarray(c,c_ref)
          self.assertTrue(min((IM.ones(c_ref.sparsity())-IM.ones(c.sparsity())).nonzeros())==0)
        except Exception as e:
          c.print_dense()
          print "sol:"
          c.sparsity().spy()
          print "ref:"
          c_ref.sparsity().spy()
          c_ref.print_dense()
          a.sparsity().sanity_check()
          a.print_dense()
          raise e
Example #2
0
  def test_sparsityindex(self):
    self.message("sparsity indexing")
    nza = set([  (0,0),
             (0,1),
             (2,0),
             (2,3),
             (3,3),
             (2,4),
             (3,1),
             (4,1)])

    a = Sparsity(5,5)
    for i in nza:
      a.add_nz(i[0],i[1])

    b = MX.sym("b",a)

    self.assertRaises(Exception,lambda: b[Sparsity.diag(3)])

    d = Sparsity.diag(5)
    c = b[d]

    self.assertTrue(c.sparsity()==d)

    f = Function('f', [b],[c])
    fin = DM(b.sparsity(),list(range(1,len(nza)+1)))
    f_out = f(fin)

    self.checkarray(DM(f_out.nonzeros()),DM([1,0,0,7,0]),"sparsity index")
Example #3
0
  def test_sparsityindex(self):
    self.message("sparsity indexing")
    nza = set([  (0,0),
             (0,1),
             (2,0),
             (2,3),
             (3,3),
             (2,4),
             (3,1),
             (4,1)])
    
    a = Sparsity(5,5)
    for i in nza:
      a.addNZ(i[0],i[1])
      
    b = MX.sym("b",a)
    
    self.assertRaises(Exception,lambda: b[Sparsity.diag(3)])
    
    d = Sparsity.diag(5)
    c = b[d]

    self.assertTrue(c.sparsity()==d)
    
    f = MXFunction([b],[c])
    f.init()
    f.setInput(range(1,len(nza)+1))
    f.evaluate()
    
    self.checkarray(DMatrix(f.getOutput().nonzeros()),DMatrix([1,0,0,7,0]),"sparsity index")
Example #4
0
  def test_sparsityindex(self):
    self.message("sparsity indexing")
    nza = set([  (0,0),
             (0,1),
             (2,0),
             (2,3),
             (3,3),
             (2,4),
             (3,1),
             (4,1)])
    
    a = CRSSparsity(5,5)
    for i in nza:
      a.getNZ(i[0],i[1])
      
    b = msym("b",a)
    
    self.assertRaises(Exception,lambda: b[sp_diag(3)])
    
    d = sp_diag(5)
    c = b[d]

    self.assertTrue(c.sparsity()==d)
    
    f = MXFunction([b],[c])
    f.init()
    f.input().set(range(1,len(nza)+1))
    f.evaluate()
    
    self.checkarray(DMatrix(f.output().data()),DMatrix([1,0,0,7,0]),"sparsity index")
Example #5
0
  def test_sparsityindex(self):
    self.message("sparsity indexing")
    nza = set([  (0,0),
             (0,1),
             (2,0),
             (2,3),
             (3,3),
             (2,4),
             (3,1),
             (4,1)])
    
    a = CRSSparsity(5,5)
    for i in nza:
      a.getNZ(i[0],i[1])
      
    b = ssym("b",a)
    
    self.assertRaises(Exception,lambda: b[sp_diag(3)])
    
    d = sp_diag(5)
    c = b[d]

    self.assertTrue(c.sparsity()==d)
    
    f = SXFunction([b],[c])
    f.init()
    f.input().set(range(1,len(nza)+1))
    f.evaluate()
    
    self.checkarray(DMatrix(f.output().data()),DMatrix([1,0,0,7,0]),"sparsity index")
    
    self.assertTrue(f.output().data()[1]==0)
Example #6
0
    def test_sparsityindex(self):
        self.message("sparsity indexing")
        nza = set([(0, 0), (0, 1), (2, 0), (2, 3), (3, 3), (2, 4), (3, 1),
                   (4, 1)])

        a = Sparsity(5, 5)
        for i in nza:
            a.addNZ(i[0], i[1])

        b = SX.sym("b", a)

        self.assertRaises(Exception, lambda: b[Sparsity.diag(3)])

        d = Sparsity.diag(5)
        c = b[d]

        self.assertTrue(c.sparsity() == d)

        f = SXFunction('f', [b], [c])
        f.setInput(range(1, len(nza) + 1))
        f.evaluate()

        self.checkarray(DMatrix(f.getOutput().nonzeros()),
                        DMatrix([1, 0, 0, 7, 0]), "sparsity index")

        self.assertTrue(f.getOutput().nonzeros()[1] == 0)
Example #7
0
  def test_sparsityindex(self):
    self.message("sparsity indexing")
    nza = set([  (0,0),
             (0,1),
             (2,0),
             (2,3),
             (3,3),
             (2,4),
             (3,1),
             (4,1)])

    a = Sparsity(5,5)
    for i in nza:
      a.add_nz(i[0],i[1])

    b = MX.sym("b",a)

    self.assertRaises(Exception,lambda: b[Sparsity.diag(3)])

    d = Sparsity.diag(5)
    c = b[d]

    self.assertTrue(c.sparsity()==d)

    f = Function('f', [b],[c])
    fin = DM(b.sparsity(),list(range(1,len(nza)+1)))
    f_out = f(fin)

    self.checkarray(DM(f_out.nonzeros()),DM([1,0,0,7,0]),"sparsity index")
Example #8
0
  def test_solve(self):
    import random
    
    spA = [
      Sparsity.dense(1,1)
    ]
    
    for n in range(2,5):
      spA+= [
        Sparsity.diag(n),
        Sparsity.dense(n,n),
        Sparsity.tril(n),
        Sparsity.tril(n).T,
        Sparsity.banded(n,1),
        blkdiag([Sparsity.diag(n),Sparsity.dense(n,n)]),
        blkdiag([Sparsity.diag(n),Sparsity.tril(n)]),
        blkdiag([Sparsity.diag(n),Sparsity.tril(n).T]),
        blkdiag([Sparsity.tril(n),Sparsity.tril(n).T]),
        Sparsity.diag(n)+Sparsity.rowcol([0],[n-1],n,n),
        Sparsity.diag(n)+Sparsity.rowcol([0,n-1],[n-1,0],n,n),
        Sparsity.diag(n)+Sparsity.triplet(n,n,[0],[n-1]),
        Sparsity.diag(n)+Sparsity.triplet(n,n,[0,n-1],[n-1,0]),
      ]
    
    for sA in spA:

      random.seed(1)
      a = DMatrix(sA,[random.random() for i in range(sA.size())])
      A = SX.sym("a",a.sparsity())
      for sB in [ Sparsity.dense(a.size1(),1), vertcat([Sparsity.dense(1,1),Sparsity.sparse(a.size1()-1,1)]),Sparsity.tril(a.size1()),Sparsity.tril(a.size1()).T]:

        b = DMatrix(sB,[random.random() for i in range(sB.size())])
        B = SX.sym("B",b.sparsity())
        C = solve(A,B)
        
        f = SXFunction([A,B],[C])
        f.init()
        
        f.setInput(a,0)
        f.setInput(b,1)
       
        f.evaluate()
            
        c_ref = DMatrix(linalg.solve(a,b))
        c_ref = sparse(c_ref)
        
        c = f.getOutput()
        
        print sA.dimString(), sB.dimString()

        try:
          self.checkarray(c,c_ref)
          self.assertTrue(min(IMatrix(c_ref.sparsity(),1)-IMatrix(c.sparsity(),1))==0)
        except Exception as e:
          c.printDense()
          print "sol:"
          c.sparsity().spy()
          print "ref:"
          c_ref.sparsity().spy()
          c_ref.printDense()
          a.sparsity().sanityCheck()
          a.printDense()
          raise e
Example #9
0
    def test_solve(self):
        import random

        spA = [Sparsity.dense(1, 1)]

        for n in range(2, 5):
            spA += [
                Sparsity.diag(n),
                Sparsity.dense(n, n),
                Sparsity.tril(n),
                Sparsity.tril(n).T,
                Sparsity.banded(n, 1),
                blkdiag([Sparsity.diag(n),
                         Sparsity.dense(n, n)]),
                blkdiag([Sparsity.diag(n), Sparsity.tril(n)]),
                blkdiag([Sparsity.diag(n),
                         Sparsity.tril(n).T]),
                blkdiag([Sparsity.tril(n),
                         Sparsity.tril(n).T]),
                Sparsity.diag(n) + Sparsity.rowcol([0], [n - 1], n, n),
                Sparsity.diag(n) +
                Sparsity.rowcol([0, n - 1], [n - 1, 0], n, n),
                Sparsity.diag(n) + Sparsity.triplet(n, n, [0], [n - 1]),
                Sparsity.diag(n) +
                Sparsity.triplet(n, n, [0, n - 1], [n - 1, 0]),
            ]

        for sA in spA:

            random.seed(1)
            a = DMatrix(sA, [random.random() for i in range(sA.size())])
            A = SX.sym("a", a.sparsity())
            for sB in [
                    Sparsity.dense(a.size1(), 1),
                    vertcat([
                        Sparsity.dense(1, 1),
                        Sparsity.sparse(a.size1() - 1, 1)
                    ]),
                    Sparsity.tril(a.size1()),
                    Sparsity.tril(a.size1()).T
            ]:

                b = DMatrix(sB, [random.random() for i in range(sB.size())])
                B = SX.sym("B", b.sparsity())
                C = solve(A, B)

                f = SXFunction([A, B], [C])
                f.init()

                f.setInput(a, 0)
                f.setInput(b, 1)

                f.evaluate()

                c_ref = DMatrix(linalg.solve(a, b))
                c_ref = sparse(c_ref)

                c = f.getOutput()

                print sA.dimString(), sB.dimString()

                try:
                    self.checkarray(c, c_ref)
                    self.assertTrue(
                        min(
                            IMatrix(c_ref.sparsity(), 1) -
                            IMatrix(c.sparsity(), 1)) == 0)
                except Exception as e:
                    c.printDense()
                    print "sol:"
                    c.sparsity().spy()
                    print "ref:"
                    c_ref.sparsity().spy()
                    c_ref.printDense()
                    a.sparsity().sanityCheck()
                    a.printDense()
                    raise e
Example #10
0
    def test_solve(self):
        import random

        spA = [Sparsity.dense(1, 1)]

        for n in range(2, 5):
            spA += [
                Sparsity.diag(n),
                Sparsity.dense(n, n),
                Sparsity.lower(n),
                Sparsity.lower(n).T,
                Sparsity.banded(n, 1),
                diagcat(
                    *[Sparsity.diag(n), Sparsity.dense(n, n)]),
                diagcat(
                    *[Sparsity.diag(n), Sparsity.lower(n)]),
                diagcat(
                    *[Sparsity.diag(n), Sparsity.lower(n).T]),
                diagcat(*[Sparsity.lower(n),
                          Sparsity.lower(n).T]),
                Sparsity.diag(n) + Sparsity.rowcol([0], [n - 1], n, n),
                Sparsity.diag(n) +
                Sparsity.rowcol([0, n - 1], [n - 1, 0], n, n),
                Sparsity.diag(n) + Sparsity.triplet(n, n, [0], [n - 1]),
                Sparsity.diag(n) +
                Sparsity.triplet(n, n, [0, n - 1], [n - 1, 0]),
            ]

        for sA in spA:

            random.seed(1)
            a = DM(sA, [random.random() for i in range(sA.nnz())])
            A = SX.sym("a", a.sparsity())
            for sB in [
                    Sparsity.dense(a.size1(), 1),
                    vertcat(Sparsity.dense(1, 1), Sparsity(a.size1() - 1, 1)),
                    Sparsity.lower(a.size1()),
                    Sparsity.lower(a.size1()).T
            ]:

                b = DM(sB, [random.random() for i in range(sB.nnz())])
                B = SX.sym("B", b.sparsity())
                C = solve(A, B)

                f = Function("f", [A, B], [C])

                c = f(a, b)

                c_ref = DM(linalg.solve(a, b))
                c_ref = sparsify(c_ref)

                print sA.dim(), sB.dim()

                try:
                    self.checkarray(c, c_ref)
                    self.assertTrue(
                        min((IM.ones(c_ref.sparsity()) -
                             IM.ones(c.sparsity())).nonzeros()) == 0)
                except Exception as e:
                    c.print_dense()
                    print "sol:"
                    c.sparsity().spy()
                    print "ref:"
                    c_ref.sparsity().spy()
                    c_ref.print_dense()
                    a.sparsity().sanity_check()
                    a.print_dense()
                    raise e