Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import time
- Nx = 3
- Ny = 4
- Nk = 5
- Nl = 6
- Nu = 7
- Nv = 8
- Fx = np.random.rand(Nx, Nk)
- Fy = np.random.rand(Ny, Nl)
- Fu = np.random.rand(Nu, Nk)
- Fv = np.random.rand(Nv, Nl)
- P = np.random.rand(Nx, Ny)
- B = np.random.rand(Nk, Nl)
- I1 = np.zeros([Nu, Nv])
- I2 = np.zeros([Nu, Nv])
- t = time.time()
- for iu in range(Nu):
- for iv in range(Nv):
- for ix in range(Nx):
- for iy in range(Ny):
- S = 0.
- for ik in range(Nk):
- for il in range(Nl):
- S += Fu[iu,ik]*Fv[iv,il]*Fx[ix,ik]*Fy[iy,il]*P[ix,iy]*B[ik,il]
- I1[iu, iv] += S
- I2[iu, iv] += S**2.
- print "time for loop:", time.time() - t;
- t = time.time()
- # 0.0787379741669
- I1_ = np.einsum('uk, vl, xk, yl, xy, kl->uv', Fu, Fv, Fx, Fy, P, B)
- print "time for I1_: ", time.time() - t
- # 0.00049090385437
- print np.allclose(I1_, I1)
- # True
- # Solution by expanding the square (not ideal)
- t = time.time()
- I2_ = np.einsum('uk,vl,xk,yl,um,vn,xm,yn,kl,mn,xy->uv', Fu,Fv,Fx,Fy,Fu,Fv,Fx,Fy,B,B,P**2)
- print "time for I2_: ", time.time() - t
- # 0.0226809978485 <- faster than for loop but still much slower than I1_ einsum
- print np.allclose(I2_, I2)
- t0 = time.time()
- E = np.einsum('uk, vl, xk, yl, xy, kl->uvxy', Fu, Fv, Fx, Fy, P, B)
- E1 = np.einsum('uvxy->uv', E)
- E2 = np.einsum('uvxy->uv', np.square(E))
- t1 = time.time() - t0
- print "time for E1,E2:", t1
- print "E1 ~ I1_:", np.allclose(E1, I1_)
- print "E2 ~ I2_:", np.allclose(E2, I2_)
- # True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement