Home > Net >  Dealing with Matrix Manipulation in Python
Dealing with Matrix Manipulation in Python

Time:05-12

Is it possible to do the following matrix manipulations without a for loop or hunk of code? I feel it may be able to be done in one or two lines but I am not seeing this through. Below is the code:

 # Return a new matrix whose entries are the negatives of self's entries.
# Do not modify first
def __neg__(first):
    

Could I use numpy negative for that^?

Say I wanted to return a new matrix whose values are the element-wise sum of self and other's values: example: result[i,j] == first[i,j] other[i,j]

What could I use to accomplish this^

def __add__(first, other):

CodePudding user response:

If you want to perform the negation and addition operations in one line each, without using for loops, and without modifying the original matrices, then, assuming your matrices are represented as (or interconvertible with) lists:

You can use list comprehensions!

These work exactly like for loops, but condensed into a single line instead of a block.

Here is a simple example of both of the matrix manipulations you described done with list comprehensions:

matrix1 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

matrix2 = [
    [8, 5, 3],
    [3, 4, 5],
    [7, 6, 5]
]

# Returns a new matrix (without modifying matrix1) which has
# entries corresponding to the negatives of the entries in matrix1
matrix1Negative = [[-matrix1[j][i] for i in range(len(matrix1[j]))] for j in range(len(matrix1))]

# Returns a new matrix (without modifying matrix1 or matrix2) which
# has entries which are the sum of those in matrix1 and matrix2
matricesSum = [[(matrix1[j][i]   matrix2[j][i]) for i in range(len(matrix1[j]))] for j in range(len(matrix1))]

print(matrix1)
print("---------------------------------------------------")
print(matrix2)
print("---------------------------------------------------")
print(matrix1Negative)
print("---------------------------------------------------")
print(matricesSum)

CodePudding user response:

I think something like this will work:

A = [[...]]
B = [[...]]
product = [[sum(a*b for a, b in zip(row, col)) for row in A] for col in zip(*B)]

CodePudding user response:

numpy is very convenient for element-wise operations:

import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[1, 1], [1, 1]])

print(A)
print(-A)
print(A   B)
print(A * A)
print(A * 5)
print(np.matmul(A, B))
  • Multiplying a numpy array by a scalar is element-wise, just like scalar multiplication with matrices. Preceding a numpy array by the minus sign is the same as multiplying the array by scalar -1.
  • Adding two numpy arrays is element-wise, just like matrix addition.
  • Multiplying two numpy arrays is element-wise, unlike matrix multiplication.
  • To multiply two matrices A and B compatible for multiplication (i.e. for standard matrix multiplication), you can use np.matmul(A, B) (which performs AB).

Output

# A
[[1 2]
 [3 4]]
# -A
[[-1 -2]
 [-3 -4]]
# A   B 
[[2 3]
 [4 5]]
# A * A 
[[ 1  4]
 [ 9 16]]
# A * 5
[[ 5 10]
 [15 20]]
# np.matmul(A, B)
[[3 3]
 [7 7]]
  • Related