LoginSignup
1
0

Pythonの標準オブジェクトで@を使いたい

Last updated at Posted at 2024-03-07

Numpyなど使っていると見る、行列積を求める時の@←これ、ありますよね(呼び方は知らない)。
ただこれ、listなどの標準オブジェクトには実装されていない。

じゃあ、listに実装しよう。←いまここ

github/list-at-list←ソースはここ

↓コピペするならここ

# forbiddenfruitをインストールすること
# pip install forbiddenfruit

from forbiddenfruit import curse

# matmulを実装するためのMatrixクラス定義(__getitem__とかも適宜実装する)
class Matrix (list):
    def __init__ (self, values): self.__values = values
    def __matmul__ (self, other): return Matrix(Matrix.multiply(self.__values, other.__values))
    def __imatmul__ (self, other): return self.__matmul__(other)
    def __rmatmul__ (self, other): return Matrix(Matrix.multiply(self.__values, other.__values))
    def __repr__ (self): return f'<Matrix values="{self.__values}">'
    def __getitem__ (self, index): return self.__values[index]
    def __setitem__ (self, index, value): self.__values[index] = value
    @staticmethod
    def multiply (mat1, mat2): return [[sum(mat1 * mat2 for mat1, mat2 in zip(mat1_row, mat2_col)) for mat2_col in zip(*mat2)] for mat1_row in mat1]

# 標準オブジェクトに__matmul__を追加する
curse(list, '__matmul__', lambda self, other: Matrix(self) @ Matrix(other))

a = [[1, 2, 3], [4, 5, 6],]
b = [[7, 8], [9, 10], [11, 12]]

c = a @ b
print(c[0])
# > [58, 64]

説明とかは苦手 + 説明するまでの内容も無いので、コピペ用記事として置いておきます。
使うときは自己判断でオナシャス!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0