def test_scipy_sparse(self) -> None: """Test scipy sparse matrices.""" # Constants. A = numpy.arange(8).reshape((4, 2)) A = sp.csc_matrix(A) A = sp.eye(2).tocsc() key = (slice(0, 1, None), slice(None, None, None)) Aidx = intf.index(A, (slice(0, 2, None), slice(None, None, None))) Aidx = intf.index(Aidx, key) self.assertEqual(Aidx.shape, (1, 2)) self.assertEqual(Aidx[0, 0], 1) self.assertEqual(Aidx[0, 1], 0) # Linear ops. var = Variable((4, 2)) A = numpy.arange(8).reshape((4, 2)) A = sp.csc_matrix(A) B = sp.hstack([A, A]) self.assertExpression(var + A, (4, 2)) self.assertExpression(A + var, (4, 2)) self.assertExpression(B @ var, (4, 2)) self.assertExpression(var - A, (4, 2)) self.assertExpression(A - A - var, (4, 2)) if PY35: self.assertExpression(var.__rmatmul__(B), (4, 2))
def test_scipy_sparse(self): """Test scipy sparse matrices.""" # Constants. A = numpy.matrix( numpy.arange(8).reshape((4,2)) ) A = sp.csc_matrix(A) A = sp.eye(2).tocsc() key = (slice(0, 1, None), slice(None, None, None)) Aidx = intf.index(A, (slice(0, 2, None), slice(None, None, None))) Aidx = intf.index(Aidx, key) self.assertEqual(Aidx.shape, (1, 2)) self.assertEqual(Aidx[0,0], 1) self.assertEqual(Aidx[0,1], 0) # Linear ops. var = Variable(4, 2) A = numpy.matrix( numpy.arange(8).reshape((4,2)) ) A = sp.csc_matrix(A) B = sp.hstack([A, A]) self.assertExpression(var + A, (4, 2)) self.assertExpression(A + var, (4, 2)) self.assertExpression(B * var, (4, 2)) self.assertExpression(var - A, (4, 2)) self.assertExpression(A - A - var, (4, 2)) if PY35: self.assertExpression(var.__rmatmul__(B), (4,2))
def test_lists(self): # index mat = intf.index([[1,2,3,4],[3,4,5,6]], (slice(1,4,2), slice(0,2,None))) self.assertItemsEqual(mat, [[2,4],[4,6]]) mat = intf.index([[1,2,3,4],[3,4,5,6]], (slice(1,4,2), slice(0,1,None))) self.assertItemsEqual(mat, [2,4]) mat = intf.index([[1,2,3,4],[3,4,5,6]], (slice(1,2,None), slice(1,2,None))) self.assertEquals(mat, 4) mat = intf.index([[2],[2]], (slice(0,1,None), slice(0,1,None))) self.assertEquals(mat, 2)
def test_lists(self): # index mat = intf.index([[1,2,3,4],[3,4,5,6]], (slice(1,4,2), slice(0,2,None))) self.assertItemsEqual(mat, [[2,4],[4,6]]) mat = intf.index([[1,2,3,4],[3,4,5,6]], (slice(1,4,2), slice(0,1,None))) self.assertItemsEqual(mat, [2,4]) mat = intf.index([[1,2,3,4],[3,4,5,6]], (slice(1,2,None), slice(1,2,None))) self.assertEquals(mat, 4) mat = intf.index([[2],[2]], (slice(0,1,None), slice(0,1,None))) self.assertEquals(mat, 2) sign = intf.sign([[2],[2]]) assert sign.pos_mat.value[0,0] == True