matrix × matrix multiplication

Asked 1 years ago, Updated 1 years ago, 342 views

I want to write a code that extends the product function below and multiplies the matrix x vector and matrix x matrix, but I can't do it well....
The matrix x vector is well executed...
Also, you need to change the calculation depending on whether the multiplier is a vector or a matrix.
Please let me know if you understand.

from operator import mul

class Vector:
    def__init__(self, *args):
        self.num=args

    default (self):
        print(self.num)

class Matrix:
  def__init__(self, *args):
    self.matrix=args

  def__mul__(self, sensor):
    if isinstance(tensor, Vector):
      print(*[sum([col*tensor.num[i]for i, colin enumerate(row)])for row in self.matrix])))
      return Vector (*[sum([col*tensor.num[i]for i, colin enumerate(row)])for row in self.matrix])
    elifisinstance(tensor, Matrix):
      print([sum(map(mul, row, col)) for col in zip(*self.matrix) for row intensor.matrix])
      return [[sum(map(mul, row, col)) for col in zip(*self.matrix)] for row intensor.matrix]
    else:
      raise TypeError('Not a Vector or Matrix instance')



x = Vector (1,8,4)

A = Matrix ([1, 2, 3],
           [3,-2,1])
y = A * x

y.out()

# unit matrix
I = Matrix ([1,0,0],
           [0,1,0],
           [0,0,1])

y = I*x
y.out()

A = Matrix ([0,1],
           [2,3],
           [4,5])

B = Matrix ([0, 1, 2, 3],
           [4,5,6,7])

y = A * B
print(y.matrix)

Run result 1 (no print in __mul__)
Enter a description of the image here

Run result 2 (printed in __mul__ to make sure the calculation is correct)
Enter a description of the image here

python procession

2022-09-30 21:53

1 Answers

Below is a sample implementation.Please refer to it.

 class Vector:
  def__init__(self, *args):
    self.vector=args

  default (self):
    print(self.vector)

class Matrix:
  def__init__(self, *args):
    self.matrix=args

  def__mul__(self, sensor):
    if isinstance(tensor, Vector):
      US>return Vector(
        * [sum(col*tensor.vector[i]for i, colin enumerate(row))]
          for row in self.matrix])
    elifisinstance(tensor, Matrix):
      US>return Matrix(
        *[sum(a*b for a, bin zip(x,y)) for y in zip(*tensor.matrix)]
          for x in self.matrix])
    else:
      raise TypeError('Not a Vector or Matrix instance')

  default (self):
    print(self.matrix)

if__name__=='__main__':
  x = Vector (1,8,4)
  A = Matrix(
    [1,  2, 3],
    [3, -2, 1],
  )

  # matrix x vector
  y = A * x
  y.out()

  # unit matrix
  I = Matrix(
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1],
  )

  # matrix x vector
  y = I*x
  y.out()

  # matrix x matrix
  A = Matrix(
    [0, 1],
    [2, 3],
    [4, 5],
  )

  B = Matrix(
    [0, 1, 2, 3],
    [4, 5, 6, 7],
  )

  y = A * B
  y.out()

Run Results

(29,-9)
(1, 8, 4)
([4, 5, 6, 7], [12, 17, 22, 27], [20, 29, 38, 47])

Why don't you use isinstance() to determine whether it is of type Vector or Matrix and switch between actions?

class Matrix:
  def__init__(self, *args):
    self.matrix=args

  def__mul__(self, sensor):
    if isinstance(tensor, Vector):
      return Vector (*[sum([col*tensor.num[i]for i, colin enumerate(row)])for row in self.matrix])
    elifisinstance(tensor, Matrix):
      return...
    else:
      raise TypeError('Not a Vector or Matrix instance')


2022-09-30 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.