Efficiently construct FEM/FVM matrix

This is a typical use case for FEM/FVM equation systems, so is perhaps of broader interest. From a triangular mesh à la I would like to create a scipy.sparse.csr_matrix. The matrix rows/columns represent values at the nodes of the mesh. The matri...
more »

2017-02-20 17:02 (2) Answers

Column-wise matrix traversal in assembly

Disclaimer: I have absolutely no experience with assembly programming, hence this question. I want to write a program to traverse a matrix two ways, row-wise and column-wise, to demonstrate cache stuff for a presentation etc. The problem is, if I w...
more »

2017-02-18 20:02 (0) Answers

Fill 1D array from matrix

I have a matrix, my mission is to fill 1D array from my matrix. Example: 1 2 3 1 2 3 1 2 3 I need to sum the columns and fill the sum of every column in a 1D array Here is my code (that doesn't work), (int[,] mat) is the matrix that the functio...
more »

2017-02-15 14:02 (4) Answers

Adding matrices with different dimensions

In numpy and tensorflow it's possible to add matrices (or tensors) of different dimensionality if the shape of smaller matrix is a suffix of bigger matrix. This is an example: x = np.ndarray(shape=(10, 7, 5), dtype = float) y = np.ndarray(shape=(7, ...
more »

2017-02-13 22:02 (2) Answers

A better 8x8 bytes matrix transpose with SSE?

I found this post that explains how to transpose an 8x8 bytes matrix with 24 operations, and a few scrolls later there's the code that implements the transpose. However, this method does not exploit the fact that we can block the 8x8 transpose into f...
more »

2017-02-10 15:02 (3) Answers

R equivalent of Python's np.dot for 3D array

I am translating some code from Python to R involving 3D matrices. Which is tricky as I know very little Python or matrix algebra. Anyhow in the Python code I have a matrix dot.product as follows: np.dot(A, B). Matrix A has dimension (10, 4) and B is...
more »

2017-02-10 12:02 (2) Answers

Can I use `sortrows`s compare function?

I have a matrix sorted by the built-in function sortrows... Now, for some algorithm, I would like to compare two rows of this matrix with the same compare function that sortrows uses. Is there any? Edit, example: sortrows has to figure out whether ...
more »

2017-02-08 11:02 (1) Answers

optimize.root with a matrix equation

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(f...
more »

2017-02-06 03:02 (1) Answers

Combine matrices - numpy

I have four numpy matrix, each which shape is (2544, 2544). I wish to combine them to create a matrix which is (2544, 2544) by adding the corresponding elements from each. How can I do this? For example if I had this matrix: x = [1,2 3,4] y = [...
more »

2017-02-05 18:02 (2) Answers

Creating a pairwise product matrix in R

I need to take the following data frame and create a 3x3 matrix with all pairwise products of the prop variable. Here is the data I am starting with... > example Parasite prop 1 Hel_1.1 0.06818182 2 Hel_11 0.18181818 3 Hel_13 0.02272...
more »

2017-02-03 23:02 (1) Answers

Inverse diagonal on matrix

So I have 2 dataframes. picture them like so: data1 <- data.frame(c(1.2, 1.4, 1.3), c(1.12,1.1, 1.9), c(1.8, 1.1, 1.32)) data2 <- data.frame(c(0.4, 0.2, 0.3), c(0.1, 0.1, 0.4), ...
more »

2017-02-03 16:02 (1) Answers

Building a numpy matrix

I'm trying to build a matrix in numpy. The matrix dimensions should be (5001x7). Here is my code: S=np.array([.0788,.0455,.0222,.0042,.0035,.0029,.0007]) #This is vector S, comprised of 7 scalars. lamb=list(range(0,5001)) #This is a list of possibl...
more »

2017-02-02 02:02 (2) Answers

Boolean matrix form Python's dict of lists

I have a dict of lists e.g., dictionary_test = {'A': ['hello', 'byebye', 'howdy'], 'B': ['bonjour', 'hello', 'ciao'], 'C': ['ciao', 'hello', 'byebye']} I want to convert it into a boolean affiliation matrix for further analysis. Preferably, dict ...
more »

2017-01-31 19:01 (2) Answers

sort each row of a matrix in Armadillo

I want to sort each row of a matrix in Armadillo and the results are index/value pairs. For example, I have a 4*2 matrix: 33.0000 26.0000 28.0000 29.0000 18.0000 20.0000 28.0000 29.0000 I want the results to be stored in v...
more »

2017-01-31 15:01 (0) Answers

Numpy solving 3d linear equation without loop

I want solve linear equation Ax= b, each A contains in 3d matrix. For-example, In Ax = B, Suppose A.shape is (2,3,3) i.e. = [[[1,2,3],[1,2,3],[1,2,3]] [[1,2,3],[1,2,3],[1,2,3]]] and B.shape is (3,1) i.e. [1,2,3]^T And I want to know each 3-vect...
more »

2017-01-31 01:01 (1) Answers

Why Matlab matrix inversion is faster than numpy?

This is related to several questions that discuss the speed of numpy vs Matlab. However most of them have several matrix operations than a single operation. E.g. Difference on performance between numpy and matlab For me, the time numpy take just to ...
more »

2017-01-22 17:01 (1) Answers