Coverage for src/pqlattice/linalg/_utils.py: 96%

1 import math

2

3 from ..typing import Matrix, Vector, validate_aliases

4

5

6@validate_aliases

7 def row_swap(m: Matrix, i: int, k: int) -> None:

8 """_summary_

9

10 Parameters

11 ----------

12 m : Matrix

13 _description_

14 i : int

15 _description_

16 k : int

17 _description_

18 """

19 m[[i, k]] = m[[k, i]]

20

21

22@validate_aliases

23 def row_scale(m: Matrix, i: int, s: float | int) -> None:

24 """_summary_

25

26 Parameters

27 ----------

28 m : Matrix

29 _description_

30 i : int

31 _description_

32 s : float | int

33 _description_

34 """

35 m[i] *= s

36

37

38@validate_aliases

39 def row_add(m: Matrix, i: int, k: int, s: float | int) -> None:

40 """_summary_

41

42 Parameters

43 ----------

44 m : Matrix

45 _description_

46 i : int

47 _description_

48 k : int

49 _description_

50 s : float | int

51 _description_

52 """

53 m[i] += s * m[k]

54

55

56@validate_aliases

57 def col_swap(m: Matrix, i: int, k: int) -> None:

58 """_summary_

59

60 Parameters

61 ----------

62 m : Matrix

63 _description_

64 i : int

65 _description_

66 k : int

67 _description_

68 """

69 m[:, [i, k]] = m[:, [k, i]]

70

71

72@validate_aliases

73 def col_scale(m: Matrix, i: int, s: float | int) -> None:

74 """_summary_

75

76 Parameters

77 ----------

78 m : Matrix

79 _description_

80 i : int

81 _description_

82 s : float | int

83 _description_

84 """

85 m[:, i] *= s

86

87

88@validate_aliases

89 def col_add(m: Matrix, i: int, k: int, s: float | int) -> None:

90 """_summary_

91

92 Parameters

93 ----------

94 m : Matrix

95 _description_

96 i : int

97 _description_

98 k : int

99 _description_

100 s : float | int

101 _description_

102 """

103 m[:, i] += s * m[:, k]

104

105

106 def norm2(v: Vector) -> int:

107 """_summary_

108

109 Parameters

110 ----------

111 v : Vector

112 _description_

113

114 Returns

115 -------

116 int

117 _description_

118 """

119 return int(v @ v.T)

120

121

122 def norm(v: Vector) -> float:

123 """_summary_

124

125 Parameters

126 ----------

127 v : Vector

128 _description_

129

130 Returns

131 -------

132 float

133 _description_

134 """

135 return math.sqrt(norm2(v))

136

137

138 def per_row_norm2(A: Matrix) -> list[int]:

139 """_summary_

140

141 Parameters

142 ----------

143 A : Matrix

144 _description_

145

146 Returns

147 -------

148 list[int]

149 _description_

150 """

151 return [norm2(row) for row in A]

152

153

154 def per_row_norm(A: Matrix) -> list[float]:

155 """_summary_

156

157 Parameters

158 ----------

159 A : Matrix

160 _description_

161

162 Returns

163 -------

164 list[float]

165 _description_

166 """

167 return [math.sqrt(n2) for n2 in per_row_norm2(A)]