LoginSignup
2
0

More than 3 years have passed since last update.

【Python】行列の積 〜pypy〜用【AtCoder】

Last updated at Posted at 2021-01-24

ABC189
E - Rotate and Flip
ででてきた行列の積

今後のためにも行列の積の定義をfor文で作った
※np.dotよりも処理速度速い気がする 〜pypy用〜

行列の積

test.py
def dot_nm(a,b):
    n,l,m = 3,5,2
    return [[sum([a[i][k]*b[k][j] for k in range(l)]) for j in range(m)] for i in range(n)]
def dot_n1(a,b):
    n,l = 3,2
    return [sum([a[i][j]*b[j] for j in range(l)]) for i in range(n)]
def dot_1m(a,b):
    l,m = 4,2
    return [sum([a[j]*b[j][i] for j in range(l)]) for i in range(m)]

print('---pypy---')
print(dot_nm([[2,1,9,5,1],[3,5,-2,-1,-9],[7,9,0,6,5]], [[4,6],[1,1],[-3,4],[3,1],[9,3]]))
print(dot_n1([[2,1],[3,5],[7,9]], [4,6]))
print(dot_1m([3,4,-7,1], [[2,1],[3,5],[9,8],[8,6]]))
print('---numpy---')
import numpy as np
print(np.dot([[2,1,9,5,1],[3,5,-2,-1,-9],[7,9,0,6,5]], [[4,6],[1,1],[-3,4],[3,1],[9,3]]))
print(np.dot([[2,1],[3,5],[7,9]], [4,6]))
print(np.dot([3,4,-7,1], [[2,1],[3,5],[9,8],[8,6]]))

スクリーンショット 2021-01-24 20.32.05.png

おわり!!!

2
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
2
0