# Python and Matlab differences in linear algebra calculates

I need to write a program for linear algebra calculates. I switched from Matlab to python and worked with numpy and scipy. There are some small differences in precision between Python and Matlab which cause different result in the end. For example for the matrix A =[2 -25, -25 -622] in Matlab det(A) = 627.0… Read More Python and Matlab differences in linear algebra calculates

# How to solve 3 equations in Python without matrix?

Hi I have 3 simple equation that I wanted to solve in python. 5x+9y=23 2x+3z=11 7x+5y+6z=35 first I wanted to solve with np.array but first two equation has 2 different unknowns. I can’t find similar problems in internet and I don’t know what should I use to solve this. >Solution : Use np.linalg.solve and assign… Read More How to solve 3 equations in Python without matrix?

# Finding Permutation Matrix with NumPy

I am looking for the correct permutation matrix that would take matrix a and turn it into matrix b given a = np.array([[1,4,7,-2],[3,0,-2,-1],[-4,2,1,0],[-8,-3,-1,2]]) b = np.array([[-4,2,1,0],[3,0,-2,-1],[-8,-3,-1,2],[1,4,7,-2]]) I tried x = np.linalg.solve(a,b) However, I know this is incorrect and it should be np.array([[0,0,1,0],[0,1,0,0],[0,0,0,1],[1,0,0,0]]) What numpy code would deliver this matrix from the other two? >Solution :… Read More Finding Permutation Matrix with NumPy

# Take the matrix product of all the elements in an array in R?

How can one take the matrix product of all the elements in an array in R? I have searched stack exchange, and have not found any results. n <- 3 ARRAY <- array(NA, dim = c(4, 4, n)) for (i in 1:4) { for (j in 1:4) { ARRAY[i, j, ] <- rnorm(n) } }… Read More Take the matrix product of all the elements in an array in R?

# np.linalg.multi_dot for R

I’m trying to do a nested dot M <- matrix(1:9,ncol=3) x <- c(1,2,3) m <- M for (op in 1:1){ m <- m %*% M } z = x %*% m result is effectively x.dot(M.dot(M)): 228, 516, 804 In python this loop can be reduced by: x.dot(np.linalg.multi_dot([M]*2)) Is there something similar for R? >Solution :… Read More np.linalg.multi_dot for R

# Template superclass constructor called when converting to base

I’m trying to write a utility header for a linear algebra program, it’s mainly for learning so I’m more concerned about ease than efficiency. Anyways, the idea is that I have a Matrix type, and a Vector type which inherits from Matrix. Because I want to add some specialized function especially for the 3D case,… Read More Template superclass constructor called when converting to base

# Rapid Selection of points from large numpy array

Minimal example: import numpy as np list1 = [1,3,5,7] list2 = [3,6,9,4] list3 = [6,5,3,2] arr = np.random.rand(72,22,22) pos_list = np.vstack([list1, list2, list3]).T print(pos_list) arr[pos_list[0][0], pos_list[0][1], pos_list[0][2]] for i in pos_list: print(arr[i[0], i[1], i[2]]) My co-worker and I are attempting to increase the efficiency of the above point selection. There are many very large matrices… Read More Rapid Selection of points from large numpy array