sparse-matrix

Remove/set the non-zero diagonal elements of a sparse matrix in scipy

心已入冬 提交于 2021-02-07 03:12:45
问题 Say I would like to remove the diagonal from a scipy.sparse.csr_matrix . Is there an efficient way of doing so? I saw that in the sparsetools module there are C functions to return the diagonal. Based on other SO answers here and here my current approach is the following: def csr_setdiag_val(csr, value=0): """Set all diagonal nonzero elements (elements currently in the sparsity pattern) to the given value. Useful to set to 0 mostly. """ if csr.format != "csr": raise ValueError('Matrix given

Remove/set the non-zero diagonal elements of a sparse matrix in scipy

て烟熏妆下的殇ゞ 提交于 2021-02-07 03:08:34
问题 Say I would like to remove the diagonal from a scipy.sparse.csr_matrix . Is there an efficient way of doing so? I saw that in the sparsetools module there are C functions to return the diagonal. Based on other SO answers here and here my current approach is the following: def csr_setdiag_val(csr, value=0): """Set all diagonal nonzero elements (elements currently in the sparsity pattern) to the given value. Useful to set to 0 mostly. """ if csr.format != "csr": raise ValueError('Matrix given

scipy slow sparse matrix solver

不问归期 提交于 2021-01-29 05:52:31
问题 I can't seem to get a benefit out of scipy's CG and sparse matrix algorithms. When I try to solve this banded matrix equation import time import scipy.sparse.linalg as ssla import scipy.sparse as ss import numpy as np size = 3000 x = np.ones(size) A = ss.diags([1, -2, 1], [-1, 0, 1], shape=[size, size]).toarray() print 'cond. nr.:{}'.format(np.linalg.cond(A)) b = np.dot(A, x) start_time = time.clock() sol = np.linalg.solve(A, b) elapsed_time = time.clock() - start_time error = np.sum(np.abs

scipy slow sparse matrix solver

99封情书 提交于 2021-01-29 05:50:39
问题 I can't seem to get a benefit out of scipy's CG and sparse matrix algorithms. When I try to solve this banded matrix equation import time import scipy.sparse.linalg as ssla import scipy.sparse as ss import numpy as np size = 3000 x = np.ones(size) A = ss.diags([1, -2, 1], [-1, 0, 1], shape=[size, size]).toarray() print 'cond. nr.:{}'.format(np.linalg.cond(A)) b = np.dot(A, x) start_time = time.clock() sol = np.linalg.solve(A, b) elapsed_time = time.clock() - start_time error = np.sum(np.abs

How to select some rows from sparse matrix then use them form a new sparse matrix

左心房为你撑大大i 提交于 2021-01-29 05:34:32
问题 I have a very large sparse matrix(100000 column and 100000 rows). I want to select some of the rows of this sparse matrix and then use them to form a new sparse matrix. I tried to do it by first converting them to dense matrix and then convert them to sparse matrix again. But when I do this python raise a 'Memory error'. Then I tried another method, which is I select the rows of sparse matrix and then put them into a array, but when I try to convert this array to sparse matrix, it says:

Avoiding dynamic memory allocation on factorizing sparse matrix with Eigen

南楼画角 提交于 2021-01-29 05:11:17
问题 In my application I need to avoid dynamic memory allocation (malloc like) except in the class constructors. I have a sparse semidefinite matrix M whose elements change during the program execution but it mantains a fixed sparsity pattern. In order to solve many linear systems M * x = b as fast as possible, the idea is to use inplace decomposition in my class constructor as described in Inplace matrix decompositions, then call factorize method whenever M changes: struct MyClass { private:

Avoiding dynamic memory allocation on factorizing sparse matrix with Eigen

一笑奈何 提交于 2021-01-29 05:06:59
问题 In my application I need to avoid dynamic memory allocation (malloc like) except in the class constructors. I have a sparse semidefinite matrix M whose elements change during the program execution but it mantains a fixed sparsity pattern. In order to solve many linear systems M * x = b as fast as possible, the idea is to use inplace decomposition in my class constructor as described in Inplace matrix decompositions, then call factorize method whenever M changes: struct MyClass { private:

Converting SuiteSparse.SPQR.QRSparseQ to SparseMatrixCSC?

依然范特西╮ 提交于 2021-01-28 11:17:46
问题 I have this problem that converting the native sparse format for the QR decomposition of a sparse Matrix takes forever. However, I need it in the CSC format to use it for further computations. using LinearAlgebra, SparseArrays N = 1000 A = sprand(N,N,1e-4) @time F = qr(A) @time F.Q @time Q_sparse = sparse(F.Q) 0.000420 seconds (1.15 k allocations: 241.017 KiB) 0.000008 seconds (6 allocations: 208 bytes) 6.067351 seconds (2.00 M allocations: 15.140 GiB, 36.25% gc time) Any suggestions? 回答1:

replace a nested for loop with mapply

断了今生、忘了曾经 提交于 2021-01-28 08:24:45
问题 I am a beginner in R. I am working on linear programming using R studio's Cplex to solve a model. One of the constraint in my model is Xl(i,j,t) <= D(i,j,t). I am able to do this with nested for loop with a small dimension (16X16X6). But I want to run my model a lot bigger model, more like 2500X2500X60. I need to save memory and run it faster than the nested for loop. I thought about using apply but I don't know how to make it work. Any help would be greatly appreciated! location <-16 horizon

Spark HBase/BigTable - Wide/sparse dataframe persistence

不羁的心 提交于 2021-01-28 08:03:36
问题 I want to persist to BigTable a very wide Spark Dataframe (>100'000 columns) that is sparsely populated (>99% of values are null) while keeping only non-null values (to avoid storage cost). Is there a way to specify in Spark to ignore nulls when writing? Thanks ! 来源: https://stackoverflow.com/questions/65647574/spark-hbase-bigtable-wide-sparse-dataframe-persistence