fork download
  1. import numpy as np
  2.  
  3. # Vector: shape (3,)
  4. vec1 = np.array([1.0, 1.0, 1.0])
  5.  
  6. # 3D tensor with shape (2, 3, 4) in column-major order
  7. data = np.array([
  8. 1.0, 2.0, 3.0, 4.0,
  9. 5.0, 6.0, 7.0, 8.0,
  10. 9.0, 10.0, 11.0, 12.0,
  11. 13.0, 14.0, 15.0, 16.0,
  12. 17.0, 18.0, 19.0, 20.0,
  13. 21.0, 22.0, 23.0, 24.0
  14. ])
  15.  
  16. # Reshape using 'F' (Fortran/column-major) order
  17. mat3d = data.reshape((2, 3, 4), order='F')
  18.  
  19. # Dot product: contracts over dimension 1 of mat3d with vec1
  20. result1 = np.tensordot(vec1, mat3d, axes=([0], [1]))
  21.  
  22. # Or equivalently using einsum notation:
  23. # result1 = np.einsum('i,jik->jk', vec1, mat3d)
  24.  
  25. print("Result shape:", result1.shape) # (2, 4)
  26. print("Result:")
  27. print(result1)
  28.  
Success #stdin #stdout 0.09s 23632KB
stdin
Standard input is empty
stdout
('Result shape:', (2, 4))
Result:
[[ 9. 27. 45. 63.]
 [12. 30. 48. 66.]]