1. AB矩阵相乘
2. 代码测试
- python 代码
python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :ABTest.py
# @Time :2024/11/17 8:37
# @Author :Jason Zhang
import numpy as np
from abc import ABCMeta, abstractmethod
np.set_printoptions(suppress=True, precision=3)
np.random.seed(1213)
class ABMultipy(metaclass=ABCMeta):
@abstractmethod
def execute(self, in_a, in_b):
pass
@abstractmethod
def result(self):
pass
class DoProduct(ABMultipy):
def __init__(self, in_a, in_b):
self.in_a = in_a
self.in_b = in_b
def execute(self, in_a, in_b):
my_result = in_a @ in_b
return my_result
def result(self):
get_result = self.execute(self.in_a, self.in_b)
# print(f"result=\n{get_result}")
return get_result
class ColumnRowProduct(ABMultipy):
def __init__(self, in_a, in_b):
self.in_a = in_a
self.in_b = in_b
def execute(self, in_a, in_b):
a_r, a_c = np.shape(self.in_a)
b_r, b_c = np.shape(self.in_b)
result1 = np.zeros((a_r, b_c))
if a_c == b_r:
for i in range(a_c):
result1 += np.outer(self.in_a[:, i], self.in_b[i, :])
return result1
def result(self):
get_result1 = self.execute(self.in_a, self.in_b)
# print(f"result=\n{get_result1}")
return get_result1
class CheckResult(object):
def __init__(self):
self.step = 5
self.shape_abc = np.random.choice(20, (3, self.step), replace=False)
def execute(self):
# print(self.shape_abc)
for i in range(self.step):
a_row, a_column, b_column = self.shape_abc[:, i]
b_row = a_column
matrix_a = np.random.randint(1, 20, (a_row, a_column))
matrix_b = np.random.randint(1, 20, (b_row, b_column))
# print(f"*" * 50)
# print(f"a_row={a_row}")
# print(f"a_column={a_column}")
# print(f"b_column={b_column}")
# print(f"*" * 50)
my_strategy_do_product = DoProduct(matrix_a, matrix_b)
result1 = my_strategy_do_product.result()
my_strategy_column = ColumnRowProduct(matrix_a, matrix_b)
result2 = my_strategy_column.result()
check_result = np.allclose(result1, result2)
print(f"*" * 50)
print(f"matrix_a=\n{matrix_a}")
print(f"matrix_b=\n{matrix_b}")
# print(f"result1=\n{result1}")
# print(f"result2=\n{result2}")
print(f"result1 is {check_result} same with result2")
if __name__ == "__main__":
run_code = 0
my_test = CheckResult()
my_test.execute()
- 运行结果:
python
**************************************************
matrix_a=
[[15 15 14 13 19 10 12 2 11 11 4 9]
[13 10 19 3 18 13 7 11 8 13 7 7]
[13 2 18 17 2 14 16 11 6 18 2 12]
[ 5 18 18 8 12 14 14 7 9 9 15 7]
[18 5 12 7 11 4 6 2 18 8 12 3]
[ 6 7 13 11 1 6 18 18 17 9 13 7]
[19 3 13 1 11 17 14 5 9 16 18 16]
[12 18 14 16 8 8 10 15 18 11 1 13]
[10 7 12 3 1 6 2 11 9 9 19 4]
[18 19 16 15 13 9 1 5 10 6 14 1]
[15 17 16 2 5 1 18 15 13 16 2 16]
[11 8 18 14 7 12 11 12 17 12 16 19]
[ 4 13 3 1 1 4 15 12 13 11 1 17]
[12 18 5 4 10 2 8 18 10 16 12 6]
[ 3 2 19 3 7 17 8 3 11 17 14 10]
[ 6 18 18 11 12 7 9 9 14 8 2 1]
[10 2 6 5 1 6 10 15 6 7 16 15]
[ 7 14 5 8 4 10 5 11 7 15 15 19]
[18 7 19 10 13 13 2 7 5 12 13 17]]
matrix_b=
[[ 7 14 14 13 2 5]
[16 2 18 19 15 10]
[13 13 5 8 14 19]
[19 1 7 3 4 9]
[16 2 3 5 13 6]
[ 7 7 1 2 19 10]
[ 7 9 6 8 17 5]
[17 4 5 7 10 9]
[ 1 13 16 11 8 6]
[19 18 16 1 3 18]
[16 16 19 11 17 11]
[10 4 16 17 13 7]]
result1 is True same with result2
**************************************************
matrix_a=
[[19 15 5 3 8 4 10 3 14 6 11 3 4]
[16 3 6 1 17 8 10 4 3 17 8 9 7]
[ 1 5 17 13 8 18 11 15 7 14 12 17 12]
[18 5 1 6 13 17 18 9 1 12 18 1 1]
[12 9 18 16 2 8 3 13 2 2 1 13 4]
[19 5 8 19 15 19 15 4 16 16 12 6 19]
[ 2 1 14 3 7 14 4 17 7 12 8 16 8]
[ 2 18 14 18 11 10 17 19 10 3 13 3 14]
[13 17 17 12 8 9 1 7 13 8 19 14 4]
[ 7 8 11 17 11 5 15 6 19 12 18 4 9]
[ 7 9 4 11 18 2 16 5 10 18 2 10 14]
[14 2 18 15 13 12 13 15 11 19 8 1 6]
[15 9 2 11 7 10 5 2 9 16 2 19 18]
[18 4 4 9 5 10 19 17 8 7 13 2 6]
[11 9 16 13 11 11 4 5 16 1 12 10 12]
[17 3 8 13 18 3 15 12 3 5 11 16 15]
[ 4 6 2 17 4 16 19 10 13 15 17 13 19]]
matrix_b=
[[ 8 8 13 9 8 4 4 9 15 17 10 2 19 14]
[ 1 2 2 5 13 8 16 10 7 2 19 18 15 16]
[15 13 2 18 2 12 5 19 17 2 11 10 8 16]
[ 3 4 11 5 16 19 2 2 18 12 11 12 12 11]
[18 16 15 15 3 11 1 12 10 1 16 6 8 19]
[10 2 12 2 6 16 4 5 7 6 7 16 12 19]
[10 10 15 4 15 17 11 18 8 9 14 7 1 11]
[ 3 8 8 18 9 10 17 2 8 17 11 5 3 1]
[15 17 19 4 5 6 4 17 6 6 14 7 11 8]
[ 3 19 2 17 5 7 15 7 14 12 6 19 7 6]
[19 16 3 17 3 2 13 16 11 5 19 17 17 12]
[ 9 19 8 6 6 9 9 4 1 13 8 15 19 5]
[12 9 9 8 7 16 10 1 16 8 19 18 9 18]]
result1 is True same with result2
**************************************************
matrix_a=
[[18 10 13 16 12 16 1 19 4 18 6 13 18 19 6 6 16 11]
[15 10 13 15 18 8 13 13 15 5 15 11 11 13 7 17 2 14]]
matrix_b=
[]
result1 is True same with result2
**************************************************
matrix_a=
[[14 16 4 16 6 14 7 5 5]
[18 5 13 4 12 16 10 7 9]
[ 6 16 2 3 14 12 8 11 18]
[ 7 5 13 5 12 7 15 8 2]
[10 17 3 4 16 17 9 5 17]
[10 6 15 16 2 11 11 8 3]
[ 8 15 8 17 11 9 4 7 2]]
matrix_b=
[[10 8 8 4 4 5 11 11]
[ 1 17 4 10 16 10 19 11]
[19 10 14 4 3 16 19 7]
[11 16 8 1 13 13 10 12]
[ 6 12 11 15 16 12 2 5]
[ 6 6 15 7 8 14 10 6]
[ 5 17 12 4 7 17 3 10]
[ 4 2 11 13 3 13 16 18]
[ 7 3 16 17 11 6 15 18]]
result1 is True same with result2
**************************************************
matrix_a=
[[16 9 16]]
matrix_b=
[[ 4 14 11 5]
[ 9 7 16 7]
[ 4 11 9 19]]
result1 is True same with result2