class LLMatFrobeniusNormBenchmark(benchmark.Benchmark): label = "Generalize norm with 100 elements and size = 1,000 for a symmetrical matrix" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T, is_symmetric=True) construct_sym_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat_sym(self.size, self.size, self.nbr_elements) construct_sym_sparse_matrix(self.A_p, self.size, self.nbr_elements) #def tearDown(self): # assert self.A_c.nnz == self.A_p.nnz # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.generalize() return def test_cysparse(self): self.A_c.generalize() return
class LLMatColScaleBenchmark(benchmark.Benchmark): label = "Simple col_scale with 100 elements and size = 1,000" each = 10 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.v = np.arange(0, self.size, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.col_scale(self.v) return def test_cysparse(self): self.A_c.col_scale(self.v) return
class LLMatColScaleBenchmark(benchmark.Benchmark): label = "Simple col_scale with 100 elements and size = 1,000" each = 10 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.v = np.arange(0, self.size, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.col_scale(self.v) return def test_cysparse(self): self.A_c.col_scale(self.v) return
class LLMatFrobeniusNormBenchmark(benchmark.Benchmark): label = "Generalize norm with 100 elements and size = 1,000 for a symmetrical matrix" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T, is_symmetric=True) construct_sym_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat_sym(self.size, self.size, self.nbr_elements) construct_sym_sparse_matrix(self.A_p, self.size, self.nbr_elements) #def tearDown(self): # assert self.A_c.nnz == self.A_p.nnz # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.generalize() return def test_cysparse(self): self.A_c.generalize() return
class LLMatShiftBenchmark(benchmark.Benchmark): label = "Simple shift with 100 elements and size = 1,000 (sigma = 10.47)" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.sigma = 10.47 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_c2 = self.A_c.copy() self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.A_p2 = self.A_p.copy() #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.shift(self.sigma, self.A_p2) return def test_cysparse(self): self.A_c.shift(self.sigma, self.A_c2) return
def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.non_contiguity = 10 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSC_c = self.A_c.to_csc() self.CSC_s = self.A_s.tocsc() self.v = np.arange(0, self.size * self.non_contiguity, dtype=np.float64)
def factorize(self): u"""Factorize matrix A as limited-memory LDLᵀ. :returns: :L: L as a llmat matrix :d: as a Numpy array """ super(CySparseLLDLSolver, self).factorize() nnz = len(self.lvals) row = np.empty(nnz, dtype=np.int64) col = np.empty(nnz, dtype=np.int64) val = np.empty(nnz, dtype=np.float64) elem = 0 for j in xrange(len(self.colptr) - 1): for k in xrange(self.colptr[j], self.colptr[j + 1]): row[elem] = self.rowind[k] col[elem] = j val[elem] = self.lvals[k] elem += 1 L = LLSparseMatrix(size=self.n, itype=INT64_T, dtype=FLOAT64_T) L.put_triplet(row, col, val) return (L, self.d)
def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSR_c = self.A_c.to_csr() self.CSR_s = self.A_s.tocsr() self.B_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.B_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.B_c) self.list_of_matrices.append(self.B_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSC_c = self.B_c.to_csc() self.CSC_s = self.B_s.tocsc() self.v = np.arange(0, self.size, dtype=np.float64)
class LLMatRowScaleStridedBenchmark(benchmark.Benchmark): label = "Simple row_scale with 100 elements and size = 1,000 and a strided NumPy vector (stride = 10)" each = 10 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.stride = 10 self.v = np.arange(0, self.size * self.stride, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.row_scale(self.v[::self.stride]) return def test_cysparse(self): self.A_c.row_scale(self.v[::self.stride]) return
class LLMatMatVecBenchmark(benchmark.Benchmark): label = "CSR * CSC * v with 1000 elements and size = 10,000" each = 100 def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSR_c = self.A_c.to_csr() self.CSR_s = self.A_s.tocsr() self.B_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.B_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.B_c) self.list_of_matrices.append(self.B_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSC_c = self.B_c.to_csc() self.CSC_s = self.B_s.tocsc() self.v = np.arange(0, self.size, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # assert self.w_c[i] == self.w_p[i] # assert self.w_c[i] == self.w_s[i] def test_cysparse(self): self.w_c = self.CSR_c * self.CSC_c * self.v return def test_scipy_sparse(self): self.w_s = self.CSR_s * self.CSC_s * self.v return
def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements)
def jac(self, *args, **kwargs): """Evaluate constraints Jacobian at x.""" vals, rows, cols = super(CySparseNLPModel, self).jac(*args, **kwargs) J = LLSparseMatrix(nrow=self.ncon, ncol=self.nvar, size_hint=vals.size, store_symmetric=False, itype=types.INT64_T, dtype=types.FLOAT64_T) J.put_triplet(rows, cols, vals) return J
class LLMatMatVecBenchmark(benchmark.Benchmark): label = "CSR * CSC * v with 1000 elements and size = 10,000" each = 100 def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSR_c = self.A_c.to_csr() self.CSR_s = self.A_s.tocsr() self.B_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.B_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.B_c) self.list_of_matrices.append(self.B_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSC_c = self.B_c.to_csc() self.CSC_s = self.B_s.tocsc() self.v = np.arange(0, self.size, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # assert self.w_c[i] == self.w_p[i] # assert self.w_c[i] == self.w_s[i] def test_cysparse(self): self.w_c = self.CSR_c * self.CSC_c * self.v return def test_scipy_sparse(self): self.w_s = self.CSR_s * self.CSC_s * self.v return
class LLMatMatVecBenchmark(benchmark.Benchmark): label = "matvec with 1000 elements and size = 10,000" each = 100 def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_p) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.v = np.arange(0, self.size, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # assert self.w_c[i] == self.w_p[i] # assert self.w_c[i] == self.w_s[i] def test_pysparse(self): self.w_p = np.empty(self.size, dtype=np.float64) self.A_p.matvec(self.v, self.w_p) return def test_cysparse(self): self.w_c = self.A_c * self.v return def test_cysparse2(self): self.A_c.matvec(self.v) return def test_scipy_sparse(self): self.w_s = self.A_s * self.v return def test_scipy_sparse2(self): self.A_s._mul_vector(self.v)
class LLMatMatVecBenchmark(benchmark.Benchmark): label = "matvec with 1000 elements and size = 10,000" each = 100 def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_p) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.v = np.arange(0, self.size, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # assert self.w_c[i] == self.w_p[i] # assert self.w_c[i] == self.w_s[i] def test_pysparse(self): self.w_p = np.empty(self.size, dtype=np.float64) self.A_p.matvec(self.v, self.w_p) return def test_cysparse(self): self.w_c = self.A_c * self.v return def test_cysparse2(self): self.A_c.matvec(self.v) return def test_scipy_sparse(self): self.w_s = self.A_s * self.v return def test_scipy_sparse2(self): self.A_s._mul_vector(self.v)
def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.stride = 10 self.v = np.arange(0, self.size * self.stride, dtype=np.float64)
def hess(self, *args, **kwargs): """Evaluate Lagrangian Hessian at (x, z). Note that `rows`, `cols` and `vals` must represent a LOWER triangular sparse matrix in the coordinate format (COO). """ vals, rows, cols = super(CySparseNLPModel, self).hess(*args, **kwargs) H = LLSparseMatrix(size=self.nvar, size_hint=vals.size, store_symmetric=True, itype=types.INT64_T, dtype=types.FLOAT64_T) H.put_triplet(rows, cols, vals) return H
class LLMatPutTripletBenchmark(benchmark.Benchmark): label = "Simple put_triplet with 100 elements, size = 1,000 and put_size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.put_size = 1000 assert self.put_size <= self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.A_sppy = None self.id1 = np.arange(0, self.put_size, dtype=np.int32) self.id2 = np.full(self.put_size, 37, dtype=np.int32) self.b = np.arange(0, self.put_size, dtype=np.float64) def eachSetUp(self): self.A_sppy = csarray((self.size, self.size), dtype=np.float64, storagetype='row') #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i, j] == self.A_p[i, j] def test_pysparse(self): self.A_p.put(self.b, self.id1, self.id2) return def test_cysparse(self): self.A_c.put_triplet(self.id1, self.id2, self.b) return def test_sppy(self): self.A_sppy.put(self.b, self.id1, self.id2, init=True)
class LLMatInfiniteNormBenchmark(benchmark.Benchmark): label = "Infinite norm with 100 elements and size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) def tearDown(self): assert self.p_norm_inf == self.c_norm_inf #assert self.w_c[i] == self.w_s[i] def test_pysparse(self): self.p_norm_inf = self.A_p.norm('inf') return def test_cysparse(self): self.c_norm_inf = self.A_c.norm('inf') return
class LLMatToCSRBenchmark(benchmark.Benchmark): label = "to_csr() with 100 elements and size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.csr_c[i, j] == self.A_c[i, j] def test_pysparse(self): self.csr_p = self.A_p.to_csr() return def test_cysparse(self): self.csr_c = self.A_c.to_csr() return
class LLMatMatVecBenchmark_2(LLMatMatVecBenchmark): label = "matvec with 10,000 elements and size = 100,000" each = 100 def setUp(self): self.nbr_elements = 10000 self.size = 100000 self.non_contiguity = 10 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSC_c = self.A_c.to_csc() self.CSC_s = self.A_s.tocsc() self.v = np.arange(0, self.size * self.non_contiguity, dtype=np.float64)
class LLMatFrobeniusNormBenchmark(benchmark.Benchmark): label = "Frobenius norm with 100 elements and size = 1,000 for a symmetrical matrix" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T, store_symmetric=True) construct_sym_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat_sym(self.size, self.size, self.nbr_elements) construct_sym_sparse_matrix(self.A_p, self.size, self.nbr_elements) def tearDown(self): assert self.p_norm == self.c_norm #assert self.w_c[i] == self.w_s[i] def test_pysparse(self): self.p_norm = self.A_p.norm('fro') return def test_cysparse(self): self.c_norm = self.A_c.norm('frob') return
class LLMatMatVecBenchmark_4(LLMatMatVecBenchmark): label = "matvec with 5000 elements and size = 1,000,000" each = 100 def setUp(self): self.nbr_elements = 5000 self.size = 1000000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_p) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSR_c = self.A_c.to_csr() self.CSR_p = self.A_p.to_csr() self.CSR_s = self.A_s.tocsr() self.v = np.arange(0, self.size, dtype=np.float64)
class LLMatMatVecBenchmark_2(LLMatMatVecBenchmark): label = "matvec with 10,000 elements and size = 100,000" each = 100 def setUp(self): self.nbr_elements = 10000 self.size = 100000 self.non_contiguity = 10 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.CSC_c = self.A_c.to_csc() self.CSC_s = self.A_s.tocsc() self.v = np.arange(0, self.size * self.non_contiguity, dtype=np.float64)
def A(self, *args, **kwargs): """ Evaluate sparse Jacobian of the linear part of the constraints. Useful to obtain constraint matrix when problem is a linear programming problem. """ vals, rows, cols = super(CySparseAmplModel, self).A(*args, **kwargs) A = LLSparseMatrix(nrow=self.ncon, ncol=self.nvar, size_hint=vals.size, store_symmetric=False, type=types.INT64_T, dtype=types.FLOAT64_T) A.put_triplet(rows, cols, vals) return A
class LLMatFrobeniusNormBenchmark(benchmark.Benchmark): label = "Frobenius norm with 100 elements and size = 1,000 for a symmetrical matrix" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T, store_symmetric=True) construct_sym_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat_sym(self.size, self.size, self.nbr_elements) construct_sym_sparse_matrix(self.A_p, self.size, self.nbr_elements) def tearDown(self): assert self.p_norm == self.c_norm #assert self.w_c[i] == self.w_s[i] def test_pysparse(self): self.p_norm = self.A_p.norm('fro') return def test_cysparse(self): self.c_norm = self.A_c.norm('frob') return
class LLMatCopyBenchmark(benchmark.Benchmark): label = "Copy matrix with 100 elements and size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c2[i,j] == self.A_p2[i,j] def test_pysparse(self): self.A_p2 = self.A_p.copy() return def test_cysparse(self): self.A_c2 = self.A_c.copy() return
class LLMatInfiniteNormBenchmark(benchmark.Benchmark): label = "Infinite norm with 100 elements and size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) def tearDown(self): assert self.p_norm_inf == self.c_norm_inf #assert self.w_c[i] == self.w_s[i] def test_pysparse(self): self.p_norm_inf = self.A_p.norm('inf') return def test_cysparse(self): self.c_norm_inf = self.A_c.norm('inf') return
class LLMatPutTripletBenchmark(benchmark.Benchmark): label = "Simple put_triplet with 100 elements, size = 1,000 and put_size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.put_size = 1000 assert self.put_size <= self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.A_sppy = None self.id1 = np.arange(0, self.put_size, dtype=np.int32) self.id2 = np.full(self.put_size, 37, dtype=np.int32) self.b = np.arange(0, self.put_size,dtype=np.float64) def eachSetUp(self): self.A_sppy = csarray((self.size, self.size), dtype=np.float64, storagetype='row') #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i, j] == self.A_p[i, j] def test_pysparse(self): self.A_p.put(self.b, self.id1, self.id2) return def test_cysparse(self): self.A_c.put_triplet(self.id1, self.id2, self.b) return def test_sppy(self): self.A_sppy.put(self.b, self.id1, self.id2, init=True)
def construct_cysparse_matrix(n, nbr_elements): # int is 32 bits on my machine A = LLSparseMatrix(size=n, size_hint=nbr_elements, itype=INT32_T, dtype=FLOAT64_T) for i in xrange(nbr_elements): A[i % n, (2 * i + 1) % n] = i / 3 return A
def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements)
def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_p) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.v = np.arange(0, self.size, dtype=np.float64)
class LLMatUpdateAddAtBenchmark(benchmark.Benchmark): label = "Simple update_add_at with 100 elements and size = 1,000 and 100 elements to add" each = 10 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.nbr_elements_to_add = 100 assert self.nbr_elements_to_add < self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.ones(self.nbr_elements_to_add, dtype=np.int32) self.id2 = self.v = np.arange(0, self.nbr_elements_to_add, dtype=np.int32) self.val = np.empty(self.nbr_elements_to_add, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.update_add_at(self.val, self.id1, self.id2) return def test_cysparse(self): self.A_c.update_add_at(self.id1, self.id2, self.val) return
def setUp(self): self.nbr_elements = 100000 self.size = 1000000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix( (self.size, self.size), dtype=np.float64) # how do we reserve space in advance?
def setUp(self): self.nbr_elements = 100 self.size = 1000 self.take_size = 1000 assert self.take_size <= self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.arange(0, self.take_size, dtype=np.int32) self.id2 = np.full(self.take_size, 37, dtype=np.int32) self.b_c = np.empty((self.take_size, ), dtype=np.float64) self.b_p = np.empty((self.take_size, ), dtype=np.float64)
def _jac(self, x, lp=False): """Helper method to assemble the Jacobian matrix. See the documentation of :meth:`jac` for more information. The positional argument `lp` should be set to `True` only if the problem is known to be a linear program. In this case, the evaluation of the constraint matrix is cheaper and the argument `x` is ignored. """ m = self.m model = self.model on = self.original_n lowerC = np.array(model.lowerC, dtype=np.int64) nlowerC = model.nlowerC upperC = np.array(model.upperC, dtype=np.int64) nupperC = model.nupperC rangeC = np.array(model.rangeC, dtype=np.int64) nrangeC = model.nrangeC # Initialize sparse Jacobian nnzJ = self.model.nnzj + m J = LLSparseMatrix(nrow=self.ncon, ncol=self.nvar, size_hint=nnzJ, store_symmetric=False, itype=types.INT64_T, dtype=types.FLOAT64_T) # Insert contribution of general constraints if lp: J[:on, :on] = self.model.A() else: J[:on, :on] = self.model.jac(x[:on]) # Create a few index lists rlowerC = np.array(range(nlowerC), dtype=np.int64) rupperC = np.array(range(nupperC), dtype=np.int64) rrangeC = np.array(range(nrangeC), dtype=np.int64) # Insert contribution of slacks on general constraints J.put_triplet(lowerC, on + rlowerC, -1.0 * np.ones(nlowerC, dtype=np.float64)) J.put_triplet(upperC, on + nlowerC + rupperC, -1.0 * np.ones(nupperC, dtype=np.float64)) J.put_triplet(rangeC, on + nlowerC + nupperC + rrangeC, -1.0 * np.ones(nrangeC, dtype=np.float64)) return J
def setUp(self): self.nbr_elements = 80000 self.size = 100000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T, store_symmetric=True) construct_sym_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat_sym(self.size, self.size, self.nbr_elements) construct_sym_sparse_matrix(self.A_p, self.size, self.nbr_elements)
class LLMatTakeTripletBenchmark(benchmark.Benchmark): label = "Simple take_triplet with 100 elements, size = 1,000 and take_size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.take_size = 1000 assert self.take_size <= self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.arange(0, self.take_size, dtype=np.int32) self.id2 = np.full(self.take_size, 37, dtype=np.int32) self.b_c = np.empty((self.take_size, ), dtype=np.float64) self.b_p = np.empty((self.take_size, ), dtype=np.float64) def tearDown(self): for i in xrange(self.take_size): assert self.b_c[i] == self.b_p[i] def test_pysparse(self): self.A_p.take(self.b_p, self.id1, self.id2) return def test_cysparse(self): self.A_c.take_triplet(self.id1, self.id2, self.b_c) return
class LLMatTakeTripletBenchmark(benchmark.Benchmark): label = "Simple take_triplet with 100 elements, size = 1,000 and take_size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.take_size = 1000 assert self.take_size <= self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.arange(0, self.take_size, dtype=np.int32) self.id2 = np.full(self.take_size, 37, dtype=np.int32) self.b_c = np.empty((self.take_size,),dtype=np.float64) self.b_p = np.empty((self.take_size,),dtype=np.float64) def tearDown(self): for i in xrange(self.take_size): assert self.b_c[i] == self.b_p[i] def test_pysparse(self): self.A_p.take(self.b_p, self.id1, self.id2) return def test_cysparse(self): self.A_c.take_triplet(self.id1, self.id2, self.b_c) return
class LLMatUpdateAddAtBenchmark(benchmark.Benchmark): label = "Simple update_add_at with 100 elements and size = 1,000 and 100 elements to add" each = 10 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.nbr_elements_to_add = 100 assert self.nbr_elements_to_add < self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.ones(self.nbr_elements_to_add, dtype=np.int32) self.id2 = self.v = np.arange(0, self.nbr_elements_to_add, dtype=np.int32) self.val = np.empty(self.nbr_elements_to_add, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.update_add_at(self.val, self.id1, self.id2) return def test_cysparse(self): self.A_c.update_add_at(self.id1, self.id2, self.val) return
def setUp(self): self.nbr_elements = 100 self.size = 1000 self.nbr_elements_to_add = 100 assert self.nbr_elements_to_add < self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.ones(self.nbr_elements_to_add, dtype=np.int32) self.id2 = self.v = np.arange(0, self.nbr_elements_to_add, dtype=np.int32) self.val = np.empty(self.nbr_elements_to_add, dtype=np.float64)
class LLMatScaleBenchmark(benchmark.Benchmark): label = "Simple scale with 100 elements and size = 1,000 (sigma = 10.47)" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.sigma = 10.47 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c[i,j] == self.A_p[i,j] def test_pysparse(self): self.A_p.scale(self.sigma) return def test_cysparse(self): self.A_c.scale(self.sigma) return def test_cysparse2(self): self.A_c *= self.sigma return
def hess(self, x, z=None, *args, **kwargs): """Evaluate Lagrangian Hessian at (x, z).""" model = self.model if isinstance(model, QuasiNewtonModel): return self.hop(x, z, *args, **kwargs) if z is None: z = np.zeros(self.m) on = model.n H = LLSparseMatrix(size=self.nvar, size_hint=self.model.nnzh, store_symmetric=True, itype=types.INT64_T, dtype=types.FLOAT64_T) H[:on, :on] = self.model.hess(x[:on], z, *args, **kwargs) return H
def setUp(self): self.nbr_elements = 100 self.size = 1000 self.nbr_elements_to_add = 100 assert self.nbr_elements_to_add < self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.ones(self.nbr_elements_to_add, dtype=np.int32) self.id2 = self.v = np.arange(0, self.nbr_elements_to_add, dtype=np.int32) self.val = np.empty(self.nbr_elements_to_add, dtype=np.float64)
def setUp(self): self.nbr_elements = 100 self.size = 1000 self.take_size = 1000 assert self.take_size <= self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.arange(0, self.take_size, dtype=np.int32) self.id2 = np.full(self.take_size, 37, dtype=np.int32) self.b_c = np.empty((self.take_size,),dtype=np.float64) self.b_p = np.empty((self.take_size,),dtype=np.float64)
class LLMatShiftBenchmark_2(LLMatShiftBenchmark): label = "Simple shift with 10,000 elements and size = 100,000 (sigma = 10.47)" each = 100 def setUp(self): self.nbr_elements = 10000 self.size = 100000 self.sigma = 10.47 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_c2 = self.A_c.copy() self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.A_p2 = self.A_p.copy()
class LLMatFindBenchmark(benchmark.Benchmark): label = "Simple find with 100 elements, size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) #def tearDown(self): # assert self.A_c.nnz == self.A_p.nnz # # reconstruct initial matrices # self.A_c_bis = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) # self.A_c_bis.put_triplet(self.A_c_rows, self.A_c_cols, self.A_c_vals) # self.A_p_bis = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) # self.A_p_bis.put(self.A_p_vals, self.A_p_rows, self.A_p_cols) # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c_bis[i, j] == self.A_p_bis[i, j] def test_pysparse(self): self.A_p_vals, self.A_p_rows, self.A_p_cols = self.A_p.find() return def test_cysparse(self): self.A_c_rows, self.A_c_cols, self.A_c_vals = self.A_c.find() return
class LLMatFindBenchmark(benchmark.Benchmark): label = "Simple find with 100 elements, size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) #def tearDown(self): # assert self.A_c.nnz == self.A_p.nnz # # reconstruct initial matrices # self.A_c_bis = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) # self.A_c_bis.put_triplet(self.A_c_rows, self.A_c_cols, self.A_c_vals) # self.A_p_bis = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) # self.A_p_bis.put(self.A_p_vals, self.A_p_rows, self.A_p_cols) # for i in xrange(self.size): # for j in xrange(self.size): # assert self.A_c_bis[i, j] == self.A_p_bis[i, j] def test_pysparse(self): self.A_p_vals, self.A_p_rows, self.A_p_cols = self.A_p.find() return def test_cysparse(self): self.A_c_rows, self.A_c_cols, self.A_c_vals = self.A_c.find() return
class LLMatMatVecTranspBenchmark(benchmark.Benchmark): label = "A^t * b with 100 elements and size = 1,000" each = 100 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.v = np.arange(0, self.size, dtype=np.float64) #def tearDown(self): # for i in xrange(self.size): # assert self.w_c[i] == self.w_p[i] # #assert self.w_c[i] == self.w_s[i] def test_pysparse(self): self.w_p = np.empty(self.size, dtype=np.float64) self.A_p.matvec_transp(self.v, self.w_p) return def test_cysparse(self): self.w_c = self.A_c.T * self.v return def test_cysparse2(self): self.w_c = self.A_c.matvec_transp(self.v) return
class LLMatMatDotBenchmark(benchmark.Benchmark): label = "Simple matdot (A^t * B) with 100 elements and size = 1,000" each = 10 def setUp(self): self.nbr_elements = 100 self.size = 1000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.C_c = None self.C_c_via_T = None self.C_p = None #def tearDown(self): # for i in xrange(self.size): # for j in xrange(self.size): # assert self.C_c[i,j] == self.C_p[i,j] def test_pysparse(self): self.C_p = spmatrix.dot(self.A_p, self.A_p) return def test_cysparse(self): self.C_c = self.A_c.matdot_transp(self.A_c) return def test_cysparse2(self): self.C_c = self.A_c.T * self.A_c return