sparse-matrix

sqrt for element-wise sparse matrix

我是研究僧i 提交于 2019-12-08 04:24:26
问题 I have a sparse matrix: from scipy import sparse a = sparse.diags([1,4,9],[-1,0,1],shape =(10,10),format ="csr") I want to take the square root of each of the elements in the sparse matrix I look up on the internet and it says I can use numpy.sqrt() to implement this. But error occurs: b = numpy.sqrt(a) AttributeError: sqrt How can I do it? 回答1: Caveat, this will create a resulting numpy ndarray instead of a sparse csr array. from scipy import sparse a = sparse.diags([1,4,9],[-1,0,1],shape =

How would you manage a std::vector of structs with std::vectors with large range of sizes in both dimentions?

不羁的心 提交于 2019-12-08 03:56:28
I have a set of structures similar to: typedef struct { int a; int b; } ITEM; typedef struct { int orderID; std::vector<ITEM> items; } ORDER; typedef struct { int orderSetID; std::vector<ORDER> Orders; } ORDER_SET; The problem is that the number of orders ranges between 100,000 to 10,000,000 and the number of ITEMS in an ORDER ranges from 1 to 500. The issue is, as I build the ORDER_SET , I don't know how many ORDERs there will be. I do know when I add an ORDER how many ITEMS there will be. Here are some problems: 1) Ideally once I allocate the memory for all of the ORDERs using Orders.resize(

Is there a python library for sparse matrix operations for non-standard algebra-like objects?

时光毁灭记忆、已成空白 提交于 2019-12-08 03:28:12
问题 Summary: I am looking for a way to do computations with sparse matrices whose non-zero entries are not the usual integers/floats/etc., but elements of an algebra, ie instances of a non-standard python class with addition, multiplication and a zero element. It works fine for dense matrices. I have implemented this algebra by defining a python class algebra and overloading addition and multiplication: class algebra(object): ... __mul__(self,other): ... __add__(self,other): ... numpy allows me

How would you manage a std::vector of structs with std::vectors with large range of sizes in both dimentions?

感情迁移 提交于 2019-12-08 03:13:26
问题 I have a set of structures similar to: typedef struct { int a; int b; } ITEM; typedef struct { int orderID; std::vector<ITEM> items; } ORDER; typedef struct { int orderSetID; std::vector<ORDER> Orders; } ORDER_SET; The problem is that the number of orders ranges between 100,000 to 10,000,000 and the number of ITEMS in an ORDER ranges from 1 to 500. The issue is, as I build the ORDER_SET , I don't know how many ORDERs there will be. I do know when I add an ORDER how many ITEMS there will be.

sparse matrix LP problems in Gurobi / python

你。 提交于 2019-12-08 03:02:47
问题 I am trying to solve an LP problem represented using sparse matrices in Gurobi / python. max c′ x, subject to A x = b, L &leq; x &leq; U where A is a SciPy linked list sparse matrix of size ~1000 2 . Using the code model = gurobipy.Model() rows, cols = len(b), len(c) for j in range(cols): model.addVar(lb=L[j], ub=U[j], obj=c[j]) model.update() vars = model.getVars() S = scipy.sparse.coo_matrix(A) expr, used = [], [] for i in range(rows): expr.append(gurobipy.LinExpr()) used.append(False) for

python (scipy): Resizing a sparse matrix

醉酒当歌 提交于 2019-12-08 02:54:12
问题 I'm having trouble resizing a matrix - the set_shape function seems to have no effect: >>> M <14x3562 sparse matrix of type '<type 'numpy.float32'>' with 6136 stored elements in LInked List format> >>> new_shape = (15,3562) >>> M.set_shape(new_shape) >>> M <14x3562 sparse matrix of type '<type 'numpy.float32'>' with 6136 stored elements in LInked List format> Anyone else come across this? I also tried doing this by hand, i.e. >>> M._shape = new_shape >>> M.data = np.concatenate(M.data, np

Scipy sparse dia_matrix solver

我只是一个虾纸丫 提交于 2019-12-08 01:32:35
问题 In a scipy program I'm creating a dia_matrix (sparse matrix type) with 5 diagonals. The centre diagonal the +1 & -1 diagonals and the +4 & -4 diagonals (usually >> 4, but the principle is the same), i.e. I have a typical PDE system matrix of the form: [ a0 b0 0 0 0 d0 0 0 0 ... 0.0 ] [ c1 a1 b1 0 0 0 d1 0 0 ... 0.0 ] [ 0 c2 a2 b2 0 0 0 d2 0 ... 0.0 ] [ 0 0 c3 a3 b3 0 0 0 d3 ... 0.0 ] [ 0 0 0 c4 a4 b4 0 0 0 ... 0.0 ] [ e5 0 0 0 c5 a5 b5 0 0 ... 0.0 ] [ : : : : : : : : : : : ] [ 0 0 0 0 0 0 0 0

Find all-zero columns in pandas sparse matrix

有些话、适合烂在心里 提交于 2019-12-08 01:16:13
问题 For example I have a coo_matrix A : import scipy.sparse as sp A = sp.coo_matrix([3,0,3,0], [0,0,2,0], [2,5,1,0], [0,0,0,0]) How can I get result [0,0,0,1], which indicates that first 3 columns contain non-zero values, only the 4th column is all zeros. PS : cannot convert A to other type. PS2 : I tried using np.nonzeros but it seems that my implementation is not very elegant. 回答1: Approach #1 We could do something like this - # Get the columns indices of the input sparse matrix C = sp.find(A)

Remote linux server to remote linux server large sparse files copy - How To?

心已入冬 提交于 2019-12-07 18:18:43
问题 I have two twins CentOS 5.4 servers with VMware Server installed on each. What is the most reliable and fast method for copying virtual machines files from one server to the other, assuming that I always use sparse file for my vmware virtual machines? The vm's files are a pain to copy since they are very large (50 GB) but since they are sparse files I think something can be done to improve the speed of the copy. 回答1: If you want to copy large data quickly, rsync over SSH is not for you. As

What is the fastest way to multiply with extremely sparse matrix?

南笙酒味 提交于 2019-12-07 17:57:05
问题 I have an extremely sparse structured matrix. My matrix has exactly one non zero entry per column. But its huge(10k*1M) and given in following form(uisng random values for example) rows = np.random.randint(0, 10000, 1000000) values = np.random.randint(0,10,1000000) where rows gives us the row number for nonzero entry in each column. I want fast matrix multiplication with S and I am doing following right now - I convert this form to a sparse matrix(S) and do S.dot(X) for multiplication with