matrix

Matrix-Vector Multiplication - Sparse vs. Dense matrices

白昼怎懂夜的黑 提交于 2021-01-28 07:20:48
问题 I want to implement a matrix-vector multiplication in C. My matrix is 1000 * 1000^2 and highly sparse (less than 0.01% non-zero elements). The non-zero elements are dispersed among the rows (between 0 to 126 non-zero elements per row). I have heard that generally, using parallel processing for sparse matrix-vector multiplication is challenging and not as efficient as dense matrices because the ratio of computation to memory access is low (Here). But I cannot really understand what is the main

Scipy sparse memory explosion with simple matrix multiplication

家住魔仙堡 提交于 2021-01-28 06:24:18
问题 I noted that Scipy must be storing some intermediate arrays when doing matrix multiplication. I assume this can be helpful in some cases, but it is a pain sometimes. Consider the following example: from scipy.sparse import coo_matrix n = 100000000000 row = np.array([0, 0]) col = np.array([0, n-1]) data = np.array([1, 1]) A = coo_matrix((data, (row, col)), shape=(2, n)) Yes, this is a very large matrix. However it has only two nonzero values. The result of B = A.dot(A.T) can be evaluated by

Cholesky decomposition failure for my correlation matrix

末鹿安然 提交于 2021-01-28 06:10:05
问题 I am trying to use chol() to find the Cholesky decomposition of the correlation matrix below. Is there a maximum size I can use that function on? I am asking because I get the following: d <-chol(corrMat) Error in chol.default(corrMat) : the leading minor of order 61 is not positive definite but, I can decompose it for less than 60 elements without a problem (even when it contains the 61st element of the original): > d <-chol(corrMat[10:69, 10:69]) > d <-chol(corrMat[10:70, 10:70]) Error in

Python: writing large array of arrays to text file

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-28 05:45:17
问题 I'm new to Python and I have a solution for this but it seems slow and silly, so I wondered if there is a better way? Say I have a matrix defined like this: mat = [['hello']*4 for x in xrange(3)] I am using this function to write it to file: def writeMat(mat, outfile): with open(outfile, "w") as f: for item in mat: f.writelines(str(item).replace('[','').replace(',','').replace('\'','').replace(']','\n')) writeMat(mat, "temp.txt") which gives a text file that looks like: hello hello hello

How to interpolate matrix to get specific values

岁酱吖の 提交于 2021-01-28 05:34:54
问题 I have this matrix in MATLAB: x = [NaN -2 -1 0 1 2; 1 0.21 0.15 0.34 0.11 0.32; 2 0.14 0.10 0.16 0.31 0.11]; The first row represents the location of the values following X coordinates. I shift the first row by -0.63, so x becomes: New_x = [NaN -2.63 -1.63 -0.63 0.37 1.37; 1 0.21 0.15 0.34 0.11 0.32; 2 0.14 0.10 0.16 0.31 0.11]; How can I use interpolation to get the values at specific coordinates of the New_x matrix that we have in the x matrix? ( [-2 -1 0 1 2] points) New_xInterp = [NaN -2

How to aggregate matrices which have different dimensions? [R]

不羁的心 提交于 2021-01-28 05:10:19
问题 I have a few hundred thousands of matrices in a list which should be aggregated by group identifier. For instance, there are two matrices with different dimentions. a <- matrix(c(1:12),nrow=3,ncol=4,dimnames=list(c(0:2),c(0:3))) b <- matrix(c(1:6),nrow=2,ncol=3,dimnames=list(c(0:1),c(0:2))) > a 0 1 2 3 0 1 4 7 10 1 2 5 8 11 2 3 6 9 12 > b 0 1 2 0 1 3 5 1 2 4 6 Do you know how to aggregate those matrices to obtain following matrix in a simple way? c <- a + b > c 0 1 2 3 0 2 7 12 10 1 4 9 14 11

How to create a matrix of POSIXct

半腔热情 提交于 2021-01-28 03:23:58
问题 When I create a matrix given POSIXct vector in R 3.1.2 , the entries of the matrix are numeric instead of POSIXct: x <- as.POSIXct("2012-02-25 19:00:00") x attributes(x) m <- matrix(x, nrow=2, ncol=3) m attributes(m) What is the best way to create a matrix of POSIXct values? 回答1: I don't think I've ever seen someone create a matrix of POSIXct values before, although it's not difficult to imagine use-cases for such an object. R doesn't seem to support this type of object very well. The S3

scipy block_diag of a list of matrices

雨燕双飞 提交于 2021-01-28 00:23:27
问题 How can I get a matrix which has as diagonal some matrices that I have in a list? I can get this if the matrices are not in a list for example: x = np.random.normal(0, 1, (3,2)) y = np.random.randint(-2, 2, (5,4)) sp.linalg.block_diag(x, y) # correct result while if: matrices = [x, y] sp.linalg.block_diag(matrices) # wrong result. How can I solve this? 回答1: import numpy as np from scipy.linalg import block_diag A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) C = [A,B] block_diag

C++ Find the sum of elements between minimum and maximum element of matrix

核能气质少年 提交于 2021-01-27 20:30:44
问题 So, My program works as it should, but only if minimum and maximum element are in opposite corners. So my question is how to iterate two-dimensional array from one certain element to another(and perhaps get rid of some nested loops). Shall I convert this array to one-dimensional? Here's the correct work of the code: And here's when something went wrong. The elements {0}{1} and {1}{1} got lost. And here's the shortened varian of the code: #include <iostream> using namespace std; void matrix

optimize.root with a matrix equation

跟風遠走 提交于 2021-01-27 19:30:22
问题 I am trying to solve the following linear system using optimize.root AX = b With the following code. A = [[0,1,0],[2,1,0],[1,4,1]] def foo(X): b = np.matrix([2,1,1]) out = np.dot(A,X) - b return out.tolist() sol = scipy.optimize.root(foo,[0,0,0]) I know that I can simply use the numpy.linalg.solve to do this easily. But I am actually trying to solve a non linear system that is in matrix form. See my question here. So I need to find a way to make this method work. To do that I am trying to