fork download
  1. import numpy as np
  2.  
  3. a0 = np.array([[1, 2, 3]])
  4. b0 = np.array([[1, 2, 3]])
  5. c0 = np.array([[1, 2, 3]])
  6. d0 = ((a0.T * b0).flatten() * c0.T)
  7.  
  8. a1 = np.array([[2, 3, 4]])
  9. b1 = np.array([[2, 3, 4]])
  10. c1 = np.array([[2, 3, 4]])
  11. d1 = ((a1.T * b1).flatten() * c1.T)
  12.  
  13. a2 = np.array([[3, 4, 5]])
  14. b2 = np.array([[3, 4, 5]])
  15. c2 = np.array([[3, 4, 5]])
  16. d2 = ((a2.T * b2).flatten() * c2.T)
  17.  
  18. a = np.stack((a0, a1, a2))
  19. b = np.stack((b0, b1, b2))
  20. c = np.stack((c0, c1, c2))
  21.  
  22. d = (a.reshape(3, -1, 1) * b).reshape(3, 1, -1) * c.reshape(3, -1, 1)
  23.  
  24. print('Combined computation:')
  25. print(d)
  26.  
  27. print('Stacked individual computation:')
  28. print(np.stack((d0, d1, d2)))
Success #stdin #stdout 0.12s 24972KB
stdin
Standard input is empty
stdout
Combined computation:
[[[  1   2   3   2   4   6   3   6   9]
  [  2   4   6   4   8  12   6  12  18]
  [  3   6   9   6  12  18   9  18  27]]

 [[  8  12  16  12  18  24  16  24  32]
  [ 12  18  24  18  27  36  24  36  48]
  [ 16  24  32  24  36  48  32  48  64]]

 [[ 27  36  45  36  48  60  45  60  75]
  [ 36  48  60  48  64  80  60  80 100]
  [ 45  60  75  60  80 100  75 100 125]]]
Stacked individual computation:
[[[  1   2   3   2   4   6   3   6   9]
  [  2   4   6   4   8  12   6  12  18]
  [  3   6   9   6  12  18   9  18  27]]

 [[  8  12  16  12  18  24  16  24  32]
  [ 12  18  24  18  27  36  24  36  48]
  [ 16  24  32  24  36  48  32  48  64]]

 [[ 27  36  45  36  48  60  45  60  75]
  [ 36  48  60  48  64  80  60  80 100]
  [ 45  60  75  60  80 100  75 100 125]]]