matrix-multiplication

How to optimize and speed up this matrices multiplication in Python

How to optimize and speed up this matrices multiplication in Python Question: According to the gradient equation, matrices multiplication is given by where both @ and * are needed. Here is the code if readers are interested: # parameters beta = 0.98 alpha = 0.03 delta = 0.1 T = 1000 loop = 1 dif …

Total answers: 1

Multiply tensors containing matrices, following matrix multiplication rule

Multiply tensors containing matrices, following matrix multiplication rule Question: Say I have a tensor, where A, B, C, and D are all 2×2 matrices: M = [[A, B], [C, D]] How do I get to the power of n, for example with n=2, with Python or MATLAB M^2 = [[A@A + B@C, A@B + B@D], …

Total answers: 3

Multiply a (N,N) matrix by a (N,M,O) matrix along the O dimension with Numba

Multiply a (N,N) matrix by a (N,M,O) matrix along the O dimension with Numba Question: I’m trying to multiply a matrix A of size $(N,N)$ by a matrix B of size $(N,M,O)$ matrix along the O dimension (that is, left-multiply all the "pages" of B along the O dimension by A), using a jitted numba …

Total answers: 2

Matrix multiplication using numpy.matmul()

Matrix multiplication using numpy.matmul() Question: I am trying to multiply to numpy arrays which both have the shape (2000,2,2). I want the outcome array to also have the shape (2000,2,2) where each of the 2000 (2,2) matrices of the first array have been matrix multiplied by the 2000 (2,2) matrices of the second array. I …

Total answers: 1

Matrix multiplication in numpy vs normal for loop in python

Matrix multiplication in numpy vs normal for loop in python Question: I thought of checking the time difference for a matrix multiplication using numpy matrix multiplication vs normal for loop method. I understand numpy will be faster because of vectorization, but I couldn’t prove it using a simple code like below. I am getting python …

Total answers: 1

Understanding matrix multiplication of ragged nested sequences in NumPy

Understanding matrix multiplication of ragged nested sequences in NumPy Question: I am new to NumPy and I am trying to migrate some code I developed in MATLAB for calculating 2×2 transfer functions. Here is the code snippet I wrote. v = np.arange(-0.5, 0.5, 0.001) z = np.exp(-1j * 2 * np.pi * v); Hcoup0 = …

Total answers: 1

In numpy, multipy two structured matrices concisely

In numpy, multipy two structured matrices concisely Question: I have two matrices. The first has the following structure: [[1, 0, a], [0, 1, b], [1, 0, c], [0, 1, d]] where 1, 0, a, b, c, and d are scalars. The matrix is 4 by 3 The second is just a 2 by 3 matrix: …

Total answers: 3

How do I multiply two matrices in pyhton without numpy?

How do I multiply two matrices in pyhton without numpy? Question: Implement a function mat_mult_by_transpose(mat) which gets a valid matrix called mat and returns a new matrix which is the matrix multiplication of and ( ) , i.e. ( ) ⋅ ( ) . Return a new matrix, without modifying mat2. You may assume that …

Total answers: 1

Avoid overflow in random matrix multiplication in python

Avoid overflow in random matrix multiplication in python Question: I have to multiply many (about 700) matrices with a random element (in the following, I’m using a box distribution) in python: #define parameters μ=2. σ=2. L=700 #define random matrix T=[None]*L product=np.array([[1,0],[0,1]]) for i in range(L): m=np.random.uniform(μ-σ*3**(1/2), μ+σ*3**(1/2)) #box distribution T[i]=np.array([[-1,-m/2],[1,0]]) product=product.dot(T[i]) #multiplying matrices Det=abs(np.linalg.det(product)) print(Det) …

Total answers: 2