Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

# Copyright 2011-2013 Kwant authors. 

# 

# This file is part of Kwant. It is subject to the license terms in the file 

# LICENSE.rst found in the top-level directory of this distribution and at 

# http://kwant-project.org/license. A list of Kwant authors can be found in 

# the file AUTHORS.rst at the top-level directory of this distribution and at 

# http://kwant-project.org/authors. 

 

import numpy as np 

 

 

def prepare_for_fortran(overwrite, *args): 

"""Convert arrays to Fortran format. 

 

This function takes a number of array objects in `args` and converts them 

to a format that can be directly passed to a Fortran function (Fortran 

contiguous NumPy array). If the arrays have different data type, they 

converted arrays are cast to a common compatible data type (one of NumPy's 

`float32`, `float64`, `complex64`, `complex128` data types). 

 

If `overwrite` is ``False``, an NumPy array that would already be in the 

correct format (Fortran contiguous, right data type) is neverthelessed 

copied. (Hence, overwrite = True does not imply that acting on the 

converted array in the return values will overwrite the original array in 

all cases -- it does only so if the original array was already in the 

correct format. The conversions require copying. In fact, that's the same 

behavior as in SciPy, it's just not explicitly stated there) 

 

If an argument is ``None``, it is just passed through and not used to 

determine the proper data type. 

 

`prepare_for_lapack` returns a character indicating the proper 

data type in LAPACK style ('s', 'd', 'c', 'z') and a list of 

properly converted arrays. 

""" 

 

# Make sure we have NumPy arrays 

mats = [None]*len(args) 

for i in range(len(args)): 

40 ↛ 48line 40 didn't jump to line 48, because the condition on line 40 was never false if args[i] is not None: 

arr = np.asanyarray(args[i]) 

42 ↛ 43line 42 didn't jump to line 43, because the condition on line 42 was never true if not np.issubdtype(arr.dtype, np.number): 

raise ValueError("Argument cannot be interpreted " 

"as a numeric array") 

 

mats[i] = (arr, arr is not args[i] or overwrite) 

else: 

mats[i] = (None, True) 

 

# First figure out common dtype 

# Note: The return type of common_type is guaranteed to be a floating point 

# kind. 

dtype = np.common_type(*[arr for arr, ovwrt in mats if arr is not None]) 

 

55 ↛ 56line 55 didn't jump to line 56, because the condition on line 55 was never true if dtype == np.float32: 

lapacktype = 's' 

57 ↛ 58line 57 didn't jump to line 58, because the condition on line 57 was never true elif dtype == np.float64: 

lapacktype = 'd' 

59 ↛ 60line 59 didn't jump to line 60, because the condition on line 59 was never true elif dtype == np.complex64: 

lapacktype = 'c' 

61 ↛ 64line 61 didn't jump to line 64, because the condition on line 61 was never false elif dtype == np.complex128: 

lapacktype = 'z' 

else: 

raise AssertionError("Unexpected data type from common_type") 

 

ret = [ lapacktype ] 

for npmat, ovwrt in mats: 

# Now make sure that the array is contiguous, and copy if necessary. 

69 ↛ 89line 69 didn't jump to line 89, because the condition on line 69 was never false if npmat is not None: 

if npmat.ndim == 2: 

if not npmat.flags["F_CONTIGUOUS"]: 

npmat = np.asfortranarray(npmat, dtype = dtype) 

elif npmat.dtype != dtype: 

npmat = npmat.astype(dtype) 

75 ↛ 89line 75 didn't jump to line 89, because the condition on line 75 was never false elif not ovwrt: 

# ugly here: copy makes always C-array, no way to tell it 

# to make a Fortran array. 

npmat = np.asfortranarray(npmat.copy()) 

79 ↛ 87line 79 didn't jump to line 87, because the condition on line 79 was never false elif npmat.ndim == 1: 

80 ↛ 81line 80 didn't jump to line 81, because the condition on line 80 was never true if not npmat.flags["C_CONTIGUOUS"]: 

npmat = np.ascontiguousarray(npmat, dtype = dtype) 

elif npmat.dtype != dtype: 

npmat = npmat.astype(dtype) 

elif not ovwrt: 

npmat = np.asfortranarray(npmat.copy()) 

else: 

raise ValueError("Dimensionality of array is not 1 or 2") 

 

ret.append(npmat) 

 

return tuple(ret) 

 

 

def assert_fortran_mat(*mats): 

"""Check if the input ndarrays are all proper Fortran matrices.""" 

 

# This is a workaround for a bug in NumPy version < 2.0, 

# where 1x1 matrices do not have the F_Contiguous flag set correctly. 

for mat in mats: 

if (mat is not None and (mat.shape[0] > 1 or mat.shape[1] > 1) and 

not mat.flags["F_CONTIGUOUS"]): 

raise ValueError("Input matrix must be Fortran contiguous") 

 

 

def assert_fortran_matvec(*arrays): 

"""Check if the input ndarrays are all proper Fortran matrices 

or vectors.""" 

 

# This is a workaround for a bug in NumPy version < 2.0, 

# where 1x1 matrices do not have the F_Contiguous flag set correctly. 

for arr in arrays: 

112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never true if not arr.ndim in (1, 2): 

raise ValueError("Input must be either a vector " 

"or a matrix.") 

 

116 ↛ 118line 116 didn't jump to line 118, because the condition on line 116 was never true if (not arr.flags["F_CONTIGUOUS"] or 

(arr.ndim == 2 and arr.shape[0] == 1 and arr.shape[1] == 1) ): 

raise ValueError("Input must be a Fortran ordered " 

"NumPy array")