Coverage for hiphive/core/tensors.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-11-28 11:20 +0000

1""" 

2Module containing tensor related functions 

3""" 

4 

5import numpy as np 

6 

7 

8_paths = dict() 

9 

10 

11def rotation_tensor_as_matrix(R, order): 

12 einsum_input = [] 

13 for i in range(order): 

14 einsum_input.append(R) 

15 einsum_input.append([i, order + i]) 

16 R_tensor = np.einsum(*einsum_input) 

17 R_matrix = R_tensor.reshape((3**order, 3**order)) 

18 return R_matrix 

19 

20 

21def rotate_tensor_precalc(T, R): 

22 order = len(T.shape) 

23 return np.dot(T.ravel(), R).reshape((3,) * order) 

24 

25 

26def rotate_tensor(T, R, path=None): 

27 """Equivalent to T_abc... = T_ijk... R_ia R_jb R_kc ... """ 

28 

29 order = len(T.shape) 

30 if order not in _paths: 

31 path = ['einsum_path', (0, 1)] 

32 for i in range(order - 1, 0, -1): 

33 path.append((0, i)) 

34 _paths[order] = path 

35 

36 einsum_input = [T, list(range(order))] 

37 for i in range(order): 

38 einsum_input.append(R) 

39 einsum_input.append([i, order + i]) 

40 return np.einsum(*einsum_input, optimize=_paths[order]) 

41 

42 

43def rotation_to_cart_coord(R, cell): 

44 """Return the rotation matrix in cart coord given a cell metric """ 

45 return np.dot(np.dot(cell.T, R), np.linalg.inv(cell.T))