# What is wrong with this matrix multiplication in python

In the below code (Jupiter notebook), output is given wrong, how to correct the code? import numpy as np A = [1,22,231,1540] B = [[-1,1,0],[1,-1,1],[0,6,0],[0,6,0]] C = [,,] result = np.dot(np.dot(A, B),C) print(result) output [-1800609408] The actual answer is I want to find the error and correct it >Solution : You’re probably running this code… Read More What is wrong with this matrix multiplication in python

# Multiplying each row of an array by a list, element-wise

I have a 3 dimensionnal numpy array and a list which look like this : array = [ [[1,2,3,10], [4,5,6,11], [7,8,9,12]], [[1,2,3,10], [4,5,6,11], [7,8,9,12]] ] lst = [50, 60, 70] I would like to multiply each column of my array by the list, element-wise. Hence, the result would look like : result = [[[50, 100,… Read More Multiplying each row of an array by a list, element-wise

# Optimizing Matrix Multiplication in Python

I’m currently working on a project that involves multiplying large matrices in Python. The matrices have dimensions A (m x n), B (n x p), and C (m x p), where m, n, and p can be very large (e.g., millions). To perform the matrix multiplication, I have implemented a basic algorithm using nested loops.… Read More Optimizing Matrix Multiplication in Python

# Matrix multiplication from the data frame in R

I’m studying matrix multiplication in R. I want to do matrix multiplication from the data frame. Let’s say I have df and beta as follows: df <- data.frame(one = c(1,1,1,1,1), x1=c(21,34,24,35,42), x2=c(32,24,13,21,35)) beta<-c(1,2,2) df is a 5 by 3 matrix and beta is 3 by 1 matrix. I want to multiply beta to df to… Read More Matrix multiplication from the data frame in R

# Why am I getting an incorrect result from multiplying an inverted matrix by a vector?

I’m trying to learn Python for basic work in linear algebra. I’m running into the following problem with a simple system of linear equations: import scipy.linalg as la import numpy as np A = np.array([[186/450, 54/21, 30/60], [12/450, 6/21 , 3/60], [9/450, 6/21 , 15/60]]) l = np.array([18/450, 12/21, 30/36]) b = np.array([2, 0, 1/6])… Read More Why am I getting an incorrect result from multiplying an inverted matrix by a vector?

# Matrix generation based on list's objects in R

I have a list of objects in R like follows: set.seed(1234) data <- matrix(rnorm(3*4,mean=0,sd=1), 3, 4) results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i])) which will result in the following matrices [] [,1] [,2] [,3] [1,] 1.4570077 -0.33487534 -1.3089918 [2,] -0.3348753 0.07696698 0.3008557 [3,] -1.3089918 0.30085569 1.1760127 [] [,1] [,2] [,3] [1,] 5.502298 -1.0065968 -1.1870541… Read More Matrix generation based on list's objects in R

# Matrix calculation between list objects in R

I have created list of objects in R as follows: set.seed(1234) data <- matrix(rnorm(3*4,mean=0,sd=1), 3, 4) results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i])) all 4 list objects have dim=3×3. I also have the following matrix matr <- matrix(c(2,4,6,8),ncol=4), where each value corresponds to the above list objects. Then, I use this equation matr[,1]*matr[,2]*results[]*results[] between… Read More Matrix calculation between list objects in R

# multiple matrix generation based on vectors in R

I have an (5×4) matrix in R, namely data defined as follows: data <- matrix(rnorm(5*4,mean=0,sd=1), 5, 4) and I want to create 4 different matrices that follows this formula: Assume that data[,1] = [A1,A2,A3,A4,A5]. I want to create the following matrix: A1*A1 A1*A2 A1*A3 A1*A4 A1*A5 A2*A1 A2*A2 A2*A3 A2*A4 A2*A5 G1 = A3*A1 A3*A2… Read More multiple matrix generation based on vectors in R

# Python: Create a function that takes dimensions & scaling factor. Returns a two-dimensional array multiplication table scaled by the scaling factor

I am trying to create a function that does what the title is asking for. Without the use of any functions besides: range, len or append. The function would take the dimensional input of the 2D array, as well as the scaling factor, and then return a two-dimensional array multiplication table scaled by the scaling… Read More Python: Create a function that takes dimensions & scaling factor. Returns a two-dimensional array multiplication table scaled by the scaling factor

# Python filling all lists at once

I wrote this function in python to do matrix multiplication but for some reason it’s not working as intended. The code looks fine to me but python seems to be filling in all lists at position y in my matrix and I don’t understand why its doing this. def maal(N, M): a = [] b… Read More Python filling all lists at once