PyPyNum 1.8.0__py3-none-any.whl → 1.8.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PyPyNum
3
- Version: 1.8.0
3
+ Version: 1.8.1
4
4
  Summary: A multifunctional mathematical calculation package written in pure Python programming language [Python>=3.5]
5
5
  Home-page: https://www.gitee.com/PythonSJL/PyPyNum
6
6
  Author: Shen Jiayi
@@ -692,7 +692,7 @@ Description-Content-Type: text/markdown
692
692
  [![Downloads](https://static.pepy.tech/badge/pypynum/month)](https://pepy.tech/project/pypynum)
693
693
  [![Downloads](https://static.pepy.tech/badge/pypynum/week)](https://pepy.tech/project/pypynum)
694
694
 
695
- ## Version -> 1.8.0 | PyPI -> https://pypi.org/project/PyPyNum/ | Gitee -> https://www.gitee.com/PythonSJL/PyPyNum
695
+ ## Version -> 1.8.1 | PyPI -> https://pypi.org/project/PyPyNum/ | Gitee -> https://www.gitee.com/PythonSJL/PyPyNum
696
696
 
697
697
  ![LOGO](PyPyNum.png)
698
698
 
@@ -741,65 +741,11 @@ Python interpreter and run it!)
741
741
  ```
742
742
  !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
743
743
 
744
- 新增代码行数为603行
744
+ eig函数修复了计算错误并改名为eigen
745
745
 
746
- Add 603 new lines of code
747
-
748
- !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
749
-
750
- 新增函数“magic_square”的功能简介:可
751
- 以生成任意大于等于三的整数阶幻方。
752
-
753
- Introduction to the newly added
754
- function "magic square": It can
755
- generate any integer order magic
756
- square greater than or equal to
757
- three.
758
-
759
- !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
760
-
761
- 新增模块“Graph”的功能简介:具有有向图
762
- 、无向图、带权有向图、带权无向图这四个对
763
- 象,支持添加或删除顶点和边,以及深度优先
764
- 搜索与广度优先搜索这两种图的遍历方式,还
765
- 有计算最短路径等功能。
766
-
767
- Introduction to the functions of
768
- the newly added module "Graph":
769
- it has four objects: directed
770
- graph, undirected graph,
771
- weighted directed graph, and
772
- weighted undirected graph. It
773
- supports adding or removing
774
- vertices and edges, as well as
775
- depth first search and breadth
776
- first search for graph
777
- traversal. It also has functions
778
- such as calculating the shortest
779
- path.
780
-
781
- !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
782
-
783
- 新增模块“polynomial”的功能简介:有一
784
- 个多项式对象,可以通过传入包含“(次数,
785
- 系数)”的二元组组成的序列创建多项式对象
786
- ,可以随时设置某一项的数值,并且支持多
787
- 项式形式的四则运算,还有取商式、取余式
788
- 、求幂、求模幂的功能。
789
-
790
- Introduction to the newly added
791
- module "polynomial": There is a
792
- polynomial object that can be
793
- created by passing in a sequence
794
- of binary tuples containing
795
- "degree, coefficient". The value
796
- of a certain term can be set at
797
- any time, and it supports
798
- polynomial form arithmetic
799
- operations. It also has
800
- functions such as quotient,
801
- remainder, exponentiation, and
802
- modular exponentiation.
746
+ The eig function fixed
747
+ calculation errors and changed
748
+ its name to eigen
803
749
 
804
750
  !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
805
751
 
@@ -808,23 +754,13 @@ modular exponentiation.
808
754
  <<<The ellipsis in the following structure represents the original part>>>
809
755
 
810
756
  PyPyNum
811
- Graph [Graph theory]
812
- CLASSES
813
- BaseGraph
814
- BaseWeGraph
815
- WeDiGraph
816
- WeUnGraph
817
- DiGraph
818
- UnGraph
819
- ★ polynomial [Polynomial object]
820
- CLASSES
821
- Polynomial
822
- FUNCTIONS
823
- poly(terms=None)
824
- ★ tools [Other useful tools]
757
+ Matrix [Matrix calculation]
825
758
  FUNCTIONS
826
759
  ...
827
- magic_square(n)
760
+ rotate90(matrix: pypynum.Matrix.Matrix, times: int) -> pypynum.Matrix.Matrix
761
+ hessenberg(matrix: pypynum.Matrix.Matrix) -> tuple
762
+ eigen(matrix: pypynum.Matrix.Matrix) -> tuple
763
+ ...
828
764
  ```
829
765
 
830
766
  ### 运行用时测试
@@ -939,14 +875,16 @@ PyPyNum
939
875
  CLASSES
940
876
  Matrix
941
877
  FUNCTIONS
942
- eig(matrix)
943
- identity(n)
944
- lu(matrix)
878
+ eigen(matrix: pypynum.Matrix.Matrix) -> tuple
879
+ hessenberg(matrix: pypynum.Matrix.Matrix) -> tuple
880
+ identity(n: int) -> pypynum.Matrix.Matrix
881
+ lu(matrix: pypynum.Matrix.Matrix) -> tuple
945
882
  mat(data)
946
- qr(matrix)
883
+ qr(matrix: pypynum.Matrix.Matrix) -> tuple
884
+ rotate90(matrix: pypynum.Matrix.Matrix, times: int) -> pypynum.Matrix.Matrix
947
885
  same(rows, cols, value=0)
948
- svd(matrix)
949
- tril_indices(n, k=0, m=None)
886
+ svd(matrix: pypynum.Matrix.Matrix) -> tuple
887
+ tril_indices(n: int, k: int = 0, m: int = None) -> tuple
950
888
  zeros(_dimensions)
951
889
  zeros_like(_nested_list)
952
890
  ★ NeuralN [A simple neural network model]
@@ -1130,6 +1068,9 @@ PyPyNum
1130
1068
  average(data, weights, expected=False)
1131
1069
  exgcd(a: int, b: int) -> tuple
1132
1070
  crt(n: arr, a: arr) -> int
1071
+ isqrt(x: int) -> int
1072
+ is_possibly_square(n: int) -> bool
1073
+ is_square(n: int) -> bool
1133
1074
  ★ numbers [Conversion of various numbers]
1134
1075
  FUNCTIONS
1135
1076
  float2fraction(number: float, mixed: bool = False, error: float = 1e-15) -> tuple
@@ -1359,7 +1300,7 @@ print(v1.angles())
1359
1300
  6.62607015e-34
1360
1301
  1.618033988749895
1361
1302
  3.141592653589793
1362
- 1000000000000.0
1303
+ 1000000000000
1363
1304
 
1364
1305
  >>> p = [1, -2, -3, 4]
1365
1306
  >>> m = [
@@ -1373,7 +1314,9 @@ print(v1.angles())
1373
1314
  >>> print(equations.polynomial_equation(p))
1374
1315
  >>> print(equations.linear_equation(*m))
1375
1316
 
1376
- [(-1.5615528128088307-6.5209667308287455e-24j) (2.5615528128088294+4.456233626665941e-24j) (1.0000000000000007+3.241554513744382e-25j)]
1317
+ [[(-1.5615528128088307-6.5209667308287455e-24j) 0 0]
1318
+ [ 0 (2.5615528128088294+4.456233626665941e-24j) 0]
1319
+ [ 0 0 (1.0000000000000007+3.241554513744382e-25j)]]
1377
1320
  [ 1.6666666666666667 -0.6666666666666666 -0.4444444444444444]
1378
1321
 
1379
1322
  >>> print(maths.cot(constants.pi / 3))
@@ -1468,10 +1411,10 @@ print(v1.angles())
1468
1411
  >>> print(random.randint(0, 9, [2, 3, 4]))
1469
1412
  >>> print(random.uniform(0, 9, [2, 3, 4]))
1470
1413
 
1471
- [[[0.4334341920363395, 0.055711784711422116, -1.0235500373980284, 0.30031229336738374], [-0.2650367914670356, 0.5513398538865067, -0.9735921328831166, 0.41345578602104827], [-0.11598957920080871, -0.9044539791933183, 1.6448227575237069, -0.26304156924843813]], [[0.27363898507271256, -0.5897181011789576, 1.5120937498473583, 2.1302709742844694], [1.9743293887616236, 0.4117207260898469, 0.5809554193110543, -1.8456249006764007], [1.274481044612177, -0.30645083457981553, -1.3285606156236818, 0.33473439037886943]]]
1472
- [[[0.5269441534226782, 0.36498666932667356, 0.7363066388832684, 0.5878544826035406], [0.5684721009896431, 0.9009577979323332, 0.036288112799501615, 0.18351641818419884], [0.24258369409385339, 0.09354340906140202, 0.4856203412285762, 0.783031677244552]], [[0.8777465681935882, 0.6406910705155251, 0.10275292827025073, 0.01295823682977526], [0.3898500974345528, 0.6216248983423127, 0.3179425906177036, 0.012870877167621808], [0.2660481991211192, 0.09872041627158801, 0.3681944568198672, 0.494087114885137]]]
1473
- [[[5, 9, 5, 6], [6, 7, 6, 1], [1, 3, 2, 4]], [[5, 8, 8, 3], [3, 2, 3, 9], [3, 0, 7, 1]]]
1474
- [[[8.610851610963957, 1.3747433091161905, 1.3831050577679438, 4.715182178697273], [0.8765517657148284, 4.809554825684029, 2.7557819856736137, 5.938765584746821], [6.088739464744903, 4.627722536295625, 0.6116370455995369, 5.875683438664389]], [[7.7228845997304845, 5.428461366109726, 8.02712172516869, 5.9319006090345425], [5.726626482636939, 7.978329508380601, 1.114307478513796, 6.236721706167868], [1.4123245528031072, 5.327811122183013, 7.324213082306745, 1.5016363011868927]]]
1414
+ [[[1.524086835643172, -0.20868457467847845, 0.5240261503975477, -0.6439838767682032], [-1.091904210196648, -0.20567633973733265, 1.374424576574523, 0.6563097903476932], [0.2171635934136032, 1.0821030876490199, -0.8410496800310051, -0.8321549344577578]], [[0.5306996954571072, -0.4441704154154241, 1.0481960055260355, 0.39805451821848287], [-0.4006858882593715, -0.06238294764009237, -1.1536673264483728, -0.8063185246185602], [0.3029117113345387, -0.32570360518676644, 0.6608320231980702, 1.7415150171137153]]]
1415
+ [[[0.3736243541521843, 0.8599079983285199, 0.4260061864869946, 0.8441437619796597], [0.8955986631978392, 0.7570336992646656, 0.6706841989644684, 0.328634366074538], [0.4371430562585502, 0.9576395263025738, 0.2380278778546957, 0.806813631306664]], [[0.18549375381453237, 0.5749941389233029, 0.7009767023241946, 0.30017399397762223], [0.6661914823434657, 0.7802291606608635, 0.6847755352217044, 0.2661053533652564], [0.07937643994416943, 0.5452043474222034, 0.8026792060861194, 0.07776400257578953]]]
1416
+ [[[9, 0, 9, 0], [2, 6, 3, 4], [5, 8, 4, 7]], [[7, 7, 6, 3], [5, 5, 5, 8], [3, 4, 6, 6]]]
1417
+ [[[5.049093842782947, 1.3880585421884204, 8.533634113864629, 3.550264239771317], [3.3311351975225176, 5.131771033264564, 0.9570872044431911, 5.165536082759862], [1.2035779060925538, 8.292998518472567, 8.014641974770818, 6.251632912237915]], [[6.411677800595937, 5.365937405245105, 8.70943859614565, 4.348757668525482], [7.827612569569748, 1.3718742546020972, 0.5252489627763138, 2.065015517785291], [4.620664668451086, 2.604569735623819, 5.548107842615733, 7.60342292447815]]]
1475
1418
 
1476
1419
  >>> print(regression.linear_regression(list(range(5)), [2, 4, 6, 7, 8]))
1477
1420
  >>> print(regression.parabolic_regression(list(range(5)), [2, 4, 6, 7, 8]))
@@ -4,26 +4,25 @@ pypynum/Geometry.py,sha256=bJCuif-wHO-t7oHFEE7ntzIdQwzUEbT3mcKgmBV6Wps,13982
4
4
  pypynum/Graph.py,sha256=m9iTCNYRCZmAexDzwJ8Y6J7v1aweT-6TZkqh4fOtZDE,10131
5
5
  pypynum/Group.py,sha256=m39OZNRoG9b3p_7sS1u50A0rJl0aYqYPVvnttkYQ_gw,2944
6
6
  pypynum/Logic.py,sha256=IJAv59ECHU0HmG9lYCAQ_puqeL6Zor3-IDIVH48KBWE,11000
7
- pypynum/Matrix.py,sha256=Z2k0bl_VpnaWB3QZjokR9CR7-kDaYIKUjKITWTYGa98,11024
7
+ pypynum/Matrix.py,sha256=vjkz-m90aWDIv2vzXBriyb4vwaZv8tsPYZzPUeUh-Vk,18050
8
8
  pypynum/NeuralN.py,sha256=iSOvC9JW1h4AFGokGGOTkKie5hAYN_YT9H4f3apI9b8,3275
9
9
  pypynum/PyPyNum.png,sha256=t96tJPWfHxT8kcXm_qZI2z5W36TgOqjCU9qdgbmlFws,11623
10
10
  pypynum/Quaternion.py,sha256=E7ajc4sDJyOuQQIwKPhUEHgiaeeR5BEzFZHhJMxvbok,8007
11
- pypynum/README.md,sha256=fiCTdsbI9rza9ZogDQ2jwxHhTFXD7dR6vfAbBTkYUP4,43971
11
+ pypynum/README.md,sha256=eouG6yg8No8iooz_5cLZqS-pR0eMzgtbLBassoqspVE,42891
12
12
  pypynum/Symbolics.py,sha256=Nfo0DEch7LII53ye1wZAdIfd3IAOKbECnZQMSk_cmCs,2898
13
13
  pypynum/Tensor.py,sha256=SJzGRNwHZzOgEabt47scccb-plA25-6s01SFyHd6lJo,3801
14
14
  pypynum/Tree.py,sha256=MUT9LErZ3ER_cbiga-nOaZC7Hln8jEV8tRca2dRPWmY,2827
15
15
  pypynum/Vector.py,sha256=eiFCY6H35pMvvDX7NpLux_bSfLI8rK8JHkaRxv7pOW8,3552
16
- pypynum/__init__.py,sha256=akxx-NuZTFE9fnNW9u6KUo3KyA-vU8iU_m70FQmfzb0,1831
17
- pypynum/__temporary.py,sha256=OAs7it7xjDovgAmKLcj-9Pqp0228SSon3OZx0ih_h4I,5860
16
+ pypynum/__init__.py,sha256=wjl8Vtz9hWI7UP-Hh2JggiLf1FR0VMusXUyG2CGfzAg,1843
18
17
  pypynum/chars.py,sha256=VcK9w0i73FMCzc-9aIibjdHqyMsofJXdoBq0d8L7Vr0,1001
19
18
  pypynum/cipher.py,sha256=J5PZl8Xt71LuX29eSBu4cybNK6Z3rhA9og_YzD0YJfU,1002
20
19
  pypynum/constants.py,sha256=s2P1K8HH2nqoA6_YOVf6TKRl1Ozjf79GmCmLN9YbWxI,843
21
- pypynum/equations.py,sha256=_4xIXr7A56qBXKzJXyb91L4h09H7TDVNrcufy-XxkaI,744
20
+ pypynum/equations.py,sha256=Bn1_VjwMnnvzT0mHYI0eAARRqIHuc3IpOfYvzojTPx0,713
22
21
  pypynum/errors.py,sha256=11-Au4aV8H5ExaDuMtu5iuGvpRaJP8gW7XbGNRmefSI,173
23
22
  pypynum/file.py,sha256=rqrPdcX0NOEPM8s40-ymgPKsTn0HKABW-VEh1OtPnsk,3188
24
- pypynum/maths.py,sha256=53TDBkZkHh1FZIQFfhi-_YwXvTSf630G-ooDS1Z7PFc,26518
23
+ pypynum/maths.py,sha256=Nf_1k1VpKDFWUJQlwB40ljML6xHcW9INlAtstJi3Uc0,26637
25
24
  pypynum/numbers.py,sha256=WOxwwAN3yHakN4eHcVNn35qsfDcgw3UPSblv4woWyDE,6028
26
- pypynum/plotting.py,sha256=YQg9sO_nuLSaAKQuSpebWVvbu-ns2A7JOrHch6r0FQ0,8326
25
+ pypynum/plotting.py,sha256=xoaG6KVoKTu2Zax2FXk3_bHkiu5LguGi_siNi1FLE98,8463
27
26
  pypynum/polynomial.py,sha256=psFukA31Ed4U7hZHcCGWCpYMi8tanTsZl-pUdklikfg,5998
28
27
  pypynum/probability.py,sha256=kZLCfZb2q_y1CVbozSZj9xQ8Dw3il5WcAojX0jsNyXw,2130
29
28
  pypynum/random.py,sha256=vfTtyH5yOSKJoSI_-NT_Tg8wC-ZXT0wt34f87oaSpJY,4610
@@ -34,7 +33,7 @@ pypynum/this.py,sha256=8Swp75xE5g6436NCVDfYUVwUzUcQFPLcCNFWQWY5eGQ,2129
34
33
  pypynum/tools.py,sha256=jqmzsRi2INRlnI4hb4IbpMKHlvEww3ZKO9y6DxrJUbA,12697
35
34
  pypynum/types.py,sha256=CVWPZo_ACr_QGH5gAOhoG3jK35peiqipu3PH8ScEYHE,181
36
35
  pypynum/utils.py,sha256=V1k-JFHLr_VGUVqwOeAVH9Ju7AHQVEoVgYA7M-dPsLw,9881
37
- PyPyNum-1.8.0.dist-info/METADATA,sha256=sKDkUYxVw125A9ZsbFD_AigRGifQOSUMR0jrIgKkIh8,85418
38
- PyPyNum-1.8.0.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
39
- PyPyNum-1.8.0.dist-info/top_level.txt,sha256=4wW_Xb4bRglmiMsdPAe9f75MkXhNpuN88H17g_Cr5u8,8
40
- PyPyNum-1.8.0.dist-info/RECORD,,
36
+ PyPyNum-1.8.1.dist-info/METADATA,sha256=y9fXQuOCwcMcXjemYXpTwz5vPiQKTACt3l_9w-nhjxA,84338
37
+ PyPyNum-1.8.1.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
38
+ PyPyNum-1.8.1.dist-info/top_level.txt,sha256=4wW_Xb4bRglmiMsdPAe9f75MkXhNpuN88H17g_Cr5u8,8
39
+ PyPyNum-1.8.1.dist-info/RECORD,,
pypynum/Matrix.py CHANGED
@@ -217,13 +217,25 @@ def same(rows, cols, value=0):
217
217
  return Matrix([[value] * cols for _ in range(rows)])
218
218
 
219
219
 
220
- def lu(matrix):
220
+ def rotate90(matrix: Matrix, times: int) -> Matrix:
221
+ matrix = matrix.copy()
222
+ if times % 4 == 0:
223
+ return matrix
224
+ elif times % 4 == 1:
225
+ return Matrix([list(col)[::-1] for col in zip(*matrix)])
226
+ elif times % 4 == 2:
227
+ return Matrix([row[::-1] for row in matrix[::-1]])
228
+ else:
229
+ return Matrix([list(col)[::-1] for col in zip(*matrix[::-1])][::-1])
230
+
231
+
232
+ def lu(matrix: Matrix) -> tuple:
221
233
  rows = matrix.rows
222
234
  cols = matrix.cols
223
235
  if rows != cols:
224
236
  raise SquareError
225
237
  L = [[1.0 if a == b else 0.0 for a in range(cols)] for b in range(rows)]
226
- U = [[float(matrix.data[b][a]) for a in range(cols)] for b in range(rows)]
238
+ U = [[float(matrix[b][a]) for a in range(cols)] for b in range(rows)]
227
239
  for i in range(rows - 1):
228
240
  for j in range(i + 1, rows):
229
241
  L[j][i] = U[j][i] / U[i][i]
@@ -235,7 +247,7 @@ def lu(matrix):
235
247
  return Matrix(L), Matrix(U)
236
248
 
237
249
 
238
- def tril_indices(n, k=0, m=None):
250
+ def tril_indices(n: int, k: int = 0, m: int = None) -> tuple:
239
251
  if m is None:
240
252
  m = n
241
253
  if n < 1 or k <= -n or m < 1:
@@ -244,11 +256,11 @@ def tril_indices(n, k=0, m=None):
244
256
  return [y[1] for y in _indices], [x[0] for x in _indices]
245
257
 
246
258
 
247
- def identity(n):
259
+ def identity(n: int) -> Matrix:
248
260
  return Matrix([[1.0 if a == b else 0.0 for a in range(n)] for b in range(n)])
249
261
 
250
262
 
251
- def qr(matrix):
263
+ def qr(matrix: Matrix) -> tuple:
252
264
  from math import hypot
253
265
  r = matrix.rows
254
266
  c = matrix.cols
@@ -273,33 +285,225 @@ def qr(matrix):
273
285
  return Matrix([[item for item in line] for line in Q]), Matrix([[item for item in line] for line in R])
274
286
 
275
287
 
276
- def eig(matrix):
277
- try:
278
- from numpy.linalg import eig as eigen
279
- e, Q = eigen(matrix.data)
280
- e, Q = Matrix([e.tolist()]), Matrix(Q.tolist())
281
- return e, Q
282
- except ImportError:
283
- eigen = "\n\033[91m提示:Matrix模块的eig函数可能存在计算错误\n\nTip: The eig function of the Matrix module may have calculation errors\033[m\n"
284
- print(eigen)
285
- _qr = []
286
- n = matrix.rows
287
- Q = identity(n)
288
- for i in range(100):
289
- _qr = qr(matrix)
290
- Q = Q @ _qr[0]
291
- matrix = _qr[1] @ _qr[0]
292
- AK = (_qr[0] @ _qr[1]).data
293
- e = Matrix([[AK[i][i] for i in range(n)]])
294
- return e, Q
295
-
296
-
297
- def svd(matrix):
298
- e0, U = eig(matrix @ matrix.t())
299
- e1, V = eig(matrix.t() @ matrix)
300
- sigma = Matrix([[((e0.data[0][a] + e1.data[0][a]) / 2) ** 0.5 if a == b else 0 for a in range(matrix.cols)]
301
- for b in range(matrix.rows)])
302
- return U, sigma, V.t()
288
+ def hessenberg(matrix: Matrix) -> tuple:
289
+ from math import sqrt
290
+ a = matrix.copy()
291
+ n = a.rows
292
+ t = [0.0] * n
293
+ a[-1, -1] = float(a[-1, -1])
294
+ if n > 2:
295
+ inf = float("inf")
296
+ for i in range(n - 1, 1, -1):
297
+ scale = 0.0
298
+ for k in range(0, i):
299
+ scale += abs(a[i, k].real) + abs(a[i, k].imag)
300
+ scale_inv = 0.0
301
+ if scale != 0:
302
+ scale_inv = 1 / scale
303
+ if scale == 0 or scale_inv == inf:
304
+ t[i] = 0.0
305
+ a[i, i - 1] = 0.0
306
+ continue
307
+ h = 0.0
308
+ for k in range(0, i):
309
+ a[i, k] = a[i, k] * scale_inv
310
+ rr, ii = a[i, k].real, a[i, k].imag
311
+ h += rr * rr + ii * ii
312
+ f = a[i, i - 1]
313
+ f0 = abs(f)
314
+ g = sqrt(h)
315
+ a[i, i - 1] = - g * scale
316
+ if f0 == 0:
317
+ t[i] = g
318
+ else:
319
+ ff = f / f0
320
+ t[i] = f + g * ff
321
+ a[i, i - 1] = a[i, i - 1] * ff
322
+ h += g * f0
323
+ h = 1 / sqrt(h)
324
+ t[i] *= h
325
+ for k in range(0, i - 1):
326
+ a[i, k] = a[i, k] * h
327
+ for j in range(0, i):
328
+ g = t[i].conjugate() * a[j, i - 1]
329
+ for k in range(0, i - 1):
330
+ g += a[i, k].conjugate() * a[j, k]
331
+ a[j, i - 1] = a[j, i - 1] - g * t[i]
332
+ for k in range(0, i - 1):
333
+ a[j, k] = a[j, k] - g * a[i, k]
334
+ for j in range(0, n):
335
+ g = t[i] * a[i - 1, j]
336
+ for k in range(0, i - 1):
337
+ g += a[i, k] * a[k, j]
338
+ a[i - 1, j] = a[i - 1, j] - g * t[i].conjugate()
339
+ for k in range(0, i - 1):
340
+ a[k, j] = a[k, j] - g * a[i, k].conjugate()
341
+ q = a.copy()
342
+ if n == 1:
343
+ q[0, 0] = 1.0
344
+ return q, a
345
+ q[0, 0] = q[1, 1] = 1.0
346
+ q[0, 1] = q[1, 0] = 0.0
347
+ for i in range(2, n):
348
+ if t[i] != 0:
349
+ for j in range(0, i):
350
+ g = t[i] * q[i - 1, j]
351
+ for k in range(0, i - 1):
352
+ g += q[i, k] * q[k, j]
353
+ q[i - 1, j] -= g * t[i].conjugate()
354
+ for k in range(0, i - 1):
355
+ q[k, j] -= g * a[i, k].conjugate()
356
+ q[i, i] = 1.0
357
+ for j in range(0, i):
358
+ q[j, i] = q[i, j] = 0.0
359
+ for x in range(n):
360
+ for y in range(x + 2, n):
361
+ a[y, x] = 0.0
362
+ return q, a
363
+
364
+
365
+ def eigen(matrix: Matrix) -> tuple:
366
+ from math import hypot
367
+ from cmath import sqrt
368
+ a = matrix.copy()
369
+ n = a.rows
370
+ if n == 1:
371
+ return Matrix([[float(a[0, 0])]]), Matrix([[1.0]])
372
+ q, a = hessenberg(a)
373
+ norm = 0
374
+ for x in range(n):
375
+ for y in range(min(x + 2, n)):
376
+ norm += abs(a[y, x])
377
+ norm = norm ** 0.5 / n
378
+ if norm == 0:
379
+ return None, None
380
+ n0 = 0
381
+ n1 = n
382
+ its = 0
383
+ while True:
384
+ k = n0
385
+ while k + 1 < n1:
386
+ if abs(a[k + 1, k]) <= 1e-100:
387
+ break
388
+ k += 1
389
+ if k + 1 < n1:
390
+ a[k + 1, k] = 0.0
391
+ n0 = k + 1
392
+ its = 0
393
+ if n0 + 1 >= n1:
394
+ n0 = 0
395
+ n1 = k + 1
396
+ if n1 < 2:
397
+ break
398
+ else:
399
+ if its % 30 == 10:
400
+ shift = a[n1 - 1, n1 - 2]
401
+ elif its % 30 == 20:
402
+ shift = abs(a[n1 - 1, n1 - 2])
403
+ elif its % 30 == 29:
404
+ shift = norm
405
+ else:
406
+ t = a[n1 - 2, n1 - 2] + a[n1 - 1, n1 - 1]
407
+ s = (a[n1 - 1, n1 - 1] - a[n1 - 2, n1 - 2]) ** 2 + 4 * a[n1 - 1, n1 - 2] * a[n1 - 2, n1 - 1]
408
+ if s.real > 0:
409
+ s = sqrt(s)
410
+ else:
411
+ s = sqrt(-s) * 1j
412
+ if s.imag == 0:
413
+ s = s.real
414
+ a0 = (t + s) / 2
415
+ b0 = (t - s) / 2
416
+ if abs(a[n1 - 1, n1 - 1] - a0) > abs(a[n1 - 1, n1 - 1] - b0):
417
+ shift = b0
418
+ else:
419
+ shift = a0
420
+ its += 1
421
+ c = a[n0, n0] - shift
422
+ s = a[n0 + 1, n0]
423
+ v = hypot(abs(c), abs(s))
424
+ if v == 0:
425
+ c = 1
426
+ s = 0
427
+ else:
428
+ c /= v
429
+ s /= v
430
+ cc = c.conjugate()
431
+ cs = s.conjugate()
432
+ for k in range(n0, n):
433
+ x = a[n0, k]
434
+ y = a[n0 + 1, k]
435
+ a[n0, k] = cc * x + cs * y
436
+ a[n0 + 1, k] = c * y - s * x
437
+ for k in range(min(n1, n0 + 3)):
438
+ x = a[k, n0]
439
+ y = a[k, n0 + 1]
440
+ a[k, n0] = c * x + s * y
441
+ a[k, n0 + 1] = cc * y - cs * x
442
+ if not isinstance(q, bool):
443
+ for k in range(n):
444
+ x = q[k, n0]
445
+ y = q[k, n0 + 1]
446
+ q[k, n0] = c * x + s * y
447
+ q[k, n0 + 1] = cc * y - cs * x
448
+ for j in range(n0, n1 - 2):
449
+ c = a[j + 1, j]
450
+ s = a[j + 2, j]
451
+ v = hypot(abs(c), abs(s))
452
+ if v == 0:
453
+ a[j + 1, j] = 0.0
454
+ c = 1
455
+ s = 0
456
+ else:
457
+ a[j + 1, j] = v
458
+ c /= v
459
+ s /= v
460
+ a[j + 2, j] = 0.0
461
+ cc = c.conjugate()
462
+ cs = s.conjugate()
463
+ for k in range(j + 1, n):
464
+ x = a[j + 1, k]
465
+ y = a[j + 2, k]
466
+ a[j + 1, k] = cc * x + cs * y
467
+ a[j + 2, k] = c * y - s * x
468
+ for k in range(0, min(n1, j + 4)):
469
+ x = a[k, j + 1]
470
+ y = a[k, j + 2]
471
+ a[k, j + 1] = c * x + s * y
472
+ a[k, j + 2] = cc * y - cs * x
473
+ if not isinstance(q, bool):
474
+ for k in range(0, n):
475
+ x = q[k, j + 1]
476
+ y = q[k, j + 2]
477
+ q[k, j + 1] = c * x + s * y
478
+ q[k, j + 2] = cc * y - cs * x
479
+ e = Matrix([[a[i, i] if j == i else 0 for j in range(n)] for i in range(n)])
480
+ er = identity(n)
481
+ m = 1
482
+ for i in range(1, n):
483
+ s = a[i, i]
484
+ for j in range(i - 1, -1, -1):
485
+ r = 0
486
+ for k in range(j + 1, i + 1):
487
+ r += a[j, k] * er[k, i]
488
+ t = a[j, j] - s
489
+ r = -r / t
490
+ er[j, i] = r
491
+ m = max(m, abs(r))
492
+ if m != 1:
493
+ for k in range(0, i + 1):
494
+ er[k, i] /= m
495
+ q = q @ er
496
+ return e, q
497
+
498
+
499
+ def svd(matrix: Matrix) -> tuple:
500
+ print("\n警告:SVD函数可能存在计算错误\n\nWarning: The SVD function may have calculation errors\n")
501
+ e0, u = eigen(matrix @ matrix.t())
502
+ u = Matrix([_[::-1] for _ in u])
503
+ e1, v = eigen(matrix.t() @ matrix)
504
+ sigma = Matrix([[((e0[i, i] + e1[i, i]) / 2) ** 0.5 if j == i else 0 for j in range(matrix.cols - 1, -1, -1)]
505
+ for i in range(matrix.rows - 1, -1, -1)])
506
+ return u, sigma, v.t()
303
507
 
304
508
 
305
509
  del Array
pypynum/README.md CHANGED
@@ -18,7 +18,7 @@
18
18
  [![Downloads](https://static.pepy.tech/badge/pypynum/month)](https://pepy.tech/project/pypynum)
19
19
  [![Downloads](https://static.pepy.tech/badge/pypynum/week)](https://pepy.tech/project/pypynum)
20
20
 
21
- ## Version -> 1.8.0 | PyPI -> https://pypi.org/project/PyPyNum/ | Gitee -> https://www.gitee.com/PythonSJL/PyPyNum
21
+ ## Version -> 1.8.1 | PyPI -> https://pypi.org/project/PyPyNum/ | Gitee -> https://www.gitee.com/PythonSJL/PyPyNum
22
22
 
23
23
  ![LOGO](PyPyNum.png)
24
24
 
@@ -67,65 +67,11 @@ Python interpreter and run it!)
67
67
  ```
68
68
  !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
69
69
 
70
- 新增代码行数为603行
70
+ eig函数修复了计算错误并改名为eigen
71
71
 
72
- Add 603 new lines of code
73
-
74
- !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
75
-
76
- 新增函数“magic_square”的功能简介:可
77
- 以生成任意大于等于三的整数阶幻方。
78
-
79
- Introduction to the newly added
80
- function "magic square": It can
81
- generate any integer order magic
82
- square greater than or equal to
83
- three.
84
-
85
- !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
86
-
87
- 新增模块“Graph”的功能简介:具有有向图
88
- 、无向图、带权有向图、带权无向图这四个对
89
- 象,支持添加或删除顶点和边,以及深度优先
90
- 搜索与广度优先搜索这两种图的遍历方式,还
91
- 有计算最短路径等功能。
92
-
93
- Introduction to the functions of
94
- the newly added module "Graph":
95
- it has four objects: directed
96
- graph, undirected graph,
97
- weighted directed graph, and
98
- weighted undirected graph. It
99
- supports adding or removing
100
- vertices and edges, as well as
101
- depth first search and breadth
102
- first search for graph
103
- traversal. It also has functions
104
- such as calculating the shortest
105
- path.
106
-
107
- !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
108
-
109
- 新增模块“polynomial”的功能简介:有一
110
- 个多项式对象,可以通过传入包含“(次数,
111
- 系数)”的二元组组成的序列创建多项式对象
112
- ,可以随时设置某一项的数值,并且支持多
113
- 项式形式的四则运算,还有取商式、取余式
114
- 、求幂、求模幂的功能。
115
-
116
- Introduction to the newly added
117
- module "polynomial": There is a
118
- polynomial object that can be
119
- created by passing in a sequence
120
- of binary tuples containing
121
- "degree, coefficient". The value
122
- of a certain term can be set at
123
- any time, and it supports
124
- polynomial form arithmetic
125
- operations. It also has
126
- functions such as quotient,
127
- remainder, exponentiation, and
128
- modular exponentiation.
72
+ The eig function fixed
73
+ calculation errors and changed
74
+ its name to eigen
129
75
 
130
76
  !=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
131
77
 
@@ -134,23 +80,13 @@ modular exponentiation.
134
80
  <<<The ellipsis in the following structure represents the original part>>>
135
81
 
136
82
  PyPyNum
137
- Graph [Graph theory]
138
- CLASSES
139
- BaseGraph
140
- BaseWeGraph
141
- WeDiGraph
142
- WeUnGraph
143
- DiGraph
144
- UnGraph
145
- ★ polynomial [Polynomial object]
146
- CLASSES
147
- Polynomial
148
- FUNCTIONS
149
- poly(terms=None)
150
- ★ tools [Other useful tools]
83
+ Matrix [Matrix calculation]
151
84
  FUNCTIONS
152
85
  ...
153
- magic_square(n)
86
+ rotate90(matrix: pypynum.Matrix.Matrix, times: int) -> pypynum.Matrix.Matrix
87
+ hessenberg(matrix: pypynum.Matrix.Matrix) -> tuple
88
+ eigen(matrix: pypynum.Matrix.Matrix) -> tuple
89
+ ...
154
90
  ```
155
91
 
156
92
  ### 运行用时测试
@@ -265,14 +201,16 @@ PyPyNum
265
201
  CLASSES
266
202
  Matrix
267
203
  FUNCTIONS
268
- eig(matrix)
269
- identity(n)
270
- lu(matrix)
204
+ eigen(matrix: pypynum.Matrix.Matrix) -> tuple
205
+ hessenberg(matrix: pypynum.Matrix.Matrix) -> tuple
206
+ identity(n: int) -> pypynum.Matrix.Matrix
207
+ lu(matrix: pypynum.Matrix.Matrix) -> tuple
271
208
  mat(data)
272
- qr(matrix)
209
+ qr(matrix: pypynum.Matrix.Matrix) -> tuple
210
+ rotate90(matrix: pypynum.Matrix.Matrix, times: int) -> pypynum.Matrix.Matrix
273
211
  same(rows, cols, value=0)
274
- svd(matrix)
275
- tril_indices(n, k=0, m=None)
212
+ svd(matrix: pypynum.Matrix.Matrix) -> tuple
213
+ tril_indices(n: int, k: int = 0, m: int = None) -> tuple
276
214
  zeros(_dimensions)
277
215
  zeros_like(_nested_list)
278
216
  ★ NeuralN [A simple neural network model]
@@ -456,6 +394,9 @@ PyPyNum
456
394
  average(data, weights, expected=False)
457
395
  exgcd(a: int, b: int) -> tuple
458
396
  crt(n: arr, a: arr) -> int
397
+ isqrt(x: int) -> int
398
+ is_possibly_square(n: int) -> bool
399
+ is_square(n: int) -> bool
459
400
  ★ numbers [Conversion of various numbers]
460
401
  FUNCTIONS
461
402
  float2fraction(number: float, mixed: bool = False, error: float = 1e-15) -> tuple
@@ -685,7 +626,7 @@ print(v1.angles())
685
626
  6.62607015e-34
686
627
  1.618033988749895
687
628
  3.141592653589793
688
- 1000000000000.0
629
+ 1000000000000
689
630
 
690
631
  >>> p = [1, -2, -3, 4]
691
632
  >>> m = [
@@ -699,7 +640,9 @@ print(v1.angles())
699
640
  >>> print(equations.polynomial_equation(p))
700
641
  >>> print(equations.linear_equation(*m))
701
642
 
702
- [(-1.5615528128088307-6.5209667308287455e-24j) (2.5615528128088294+4.456233626665941e-24j) (1.0000000000000007+3.241554513744382e-25j)]
643
+ [[(-1.5615528128088307-6.5209667308287455e-24j) 0 0]
644
+ [ 0 (2.5615528128088294+4.456233626665941e-24j) 0]
645
+ [ 0 0 (1.0000000000000007+3.241554513744382e-25j)]]
703
646
  [ 1.6666666666666667 -0.6666666666666666 -0.4444444444444444]
704
647
 
705
648
  >>> print(maths.cot(constants.pi / 3))
@@ -794,10 +737,10 @@ print(v1.angles())
794
737
  >>> print(random.randint(0, 9, [2, 3, 4]))
795
738
  >>> print(random.uniform(0, 9, [2, 3, 4]))
796
739
 
797
- [[[0.4334341920363395, 0.055711784711422116, -1.0235500373980284, 0.30031229336738374], [-0.2650367914670356, 0.5513398538865067, -0.9735921328831166, 0.41345578602104827], [-0.11598957920080871, -0.9044539791933183, 1.6448227575237069, -0.26304156924843813]], [[0.27363898507271256, -0.5897181011789576, 1.5120937498473583, 2.1302709742844694], [1.9743293887616236, 0.4117207260898469, 0.5809554193110543, -1.8456249006764007], [1.274481044612177, -0.30645083457981553, -1.3285606156236818, 0.33473439037886943]]]
798
- [[[0.5269441534226782, 0.36498666932667356, 0.7363066388832684, 0.5878544826035406], [0.5684721009896431, 0.9009577979323332, 0.036288112799501615, 0.18351641818419884], [0.24258369409385339, 0.09354340906140202, 0.4856203412285762, 0.783031677244552]], [[0.8777465681935882, 0.6406910705155251, 0.10275292827025073, 0.01295823682977526], [0.3898500974345528, 0.6216248983423127, 0.3179425906177036, 0.012870877167621808], [0.2660481991211192, 0.09872041627158801, 0.3681944568198672, 0.494087114885137]]]
799
- [[[5, 9, 5, 6], [6, 7, 6, 1], [1, 3, 2, 4]], [[5, 8, 8, 3], [3, 2, 3, 9], [3, 0, 7, 1]]]
800
- [[[8.610851610963957, 1.3747433091161905, 1.3831050577679438, 4.715182178697273], [0.8765517657148284, 4.809554825684029, 2.7557819856736137, 5.938765584746821], [6.088739464744903, 4.627722536295625, 0.6116370455995369, 5.875683438664389]], [[7.7228845997304845, 5.428461366109726, 8.02712172516869, 5.9319006090345425], [5.726626482636939, 7.978329508380601, 1.114307478513796, 6.236721706167868], [1.4123245528031072, 5.327811122183013, 7.324213082306745, 1.5016363011868927]]]
740
+ [[[1.524086835643172, -0.20868457467847845, 0.5240261503975477, -0.6439838767682032], [-1.091904210196648, -0.20567633973733265, 1.374424576574523, 0.6563097903476932], [0.2171635934136032, 1.0821030876490199, -0.8410496800310051, -0.8321549344577578]], [[0.5306996954571072, -0.4441704154154241, 1.0481960055260355, 0.39805451821848287], [-0.4006858882593715, -0.06238294764009237, -1.1536673264483728, -0.8063185246185602], [0.3029117113345387, -0.32570360518676644, 0.6608320231980702, 1.7415150171137153]]]
741
+ [[[0.3736243541521843, 0.8599079983285199, 0.4260061864869946, 0.8441437619796597], [0.8955986631978392, 0.7570336992646656, 0.6706841989644684, 0.328634366074538], [0.4371430562585502, 0.9576395263025738, 0.2380278778546957, 0.806813631306664]], [[0.18549375381453237, 0.5749941389233029, 0.7009767023241946, 0.30017399397762223], [0.6661914823434657, 0.7802291606608635, 0.6847755352217044, 0.2661053533652564], [0.07937643994416943, 0.5452043474222034, 0.8026792060861194, 0.07776400257578953]]]
742
+ [[[9, 0, 9, 0], [2, 6, 3, 4], [5, 8, 4, 7]], [[7, 7, 6, 3], [5, 5, 5, 8], [3, 4, 6, 6]]]
743
+ [[[5.049093842782947, 1.3880585421884204, 8.533634113864629, 3.550264239771317], [3.3311351975225176, 5.131771033264564, 0.9570872044431911, 5.165536082759862], [1.2035779060925538, 8.292998518472567, 8.014641974770818, 6.251632912237915]], [[6.411677800595937, 5.365937405245105, 8.70943859614565, 4.348757668525482], [7.827612569569748, 1.3718742546020972, 0.5252489627763138, 2.065015517785291], [4.620664668451086, 2.604569735623819, 5.548107842615733, 7.60342292447815]]]
801
744
 
802
745
  >>> print(regression.linear_regression(list(range(5)), [2, 4, 6, 7, 8]))
803
746
  >>> print(regression.parabolic_regression(list(range(5)), [2, 4, 6, 7, 8]))
pypynum/__init__.py CHANGED
@@ -21,7 +21,7 @@ from .Graph import *
21
21
  from .Group import group
22
22
  from .Logic import *
23
23
  from .maths import *
24
- from .Matrix import mat, identity, lu, qr, eig, svd
24
+ from .Matrix import mat, identity, rotate90, lu, qr, eigen, svd
25
25
  from .NeuralN import *
26
26
  from .numbers import *
27
27
  from .plotting import unary, binary, c_unary, color
@@ -39,7 +39,7 @@ from . import types
39
39
  from .utils import OrderedSet, InfIterator
40
40
  from .Vector import vec
41
41
 
42
- __version__ = "1.8.0"
42
+ __version__ = "1.8.1"
43
43
  print("PyPyNum", "Version -> " + __version__, "PyPI -> https://pypi.org/project/PyPyNum/",
44
44
  "Gitee -> https://www.gitee.com/PythonSJL/PyPyNum", sep=" | ")
45
45
  del math, arr, ite, num, real, geom, ContentError, RandomError, LogicError, InputError, FullError, Union
pypynum/equations.py CHANGED
@@ -10,8 +10,7 @@ def linear_equation(left: list, right: list) -> list:
10
10
 
11
11
  def polynomial_equation(coefficients: list) -> list:
12
12
  from .Array import array
13
- from .Matrix import mat
14
- from .__temporary import eigenvalue as eig
13
+ from .Matrix import eigen, mat
15
14
  p = [_ / coefficients[0] for _ in coefficients[1:]]
16
- return array(eig(mat([[-p[i] if j == 0 else 1 if i + 1 == j else 0
17
- for j in range(len(p))] for i in range(len(p))])))
15
+ return array(eigen(mat([[-p[i] if j == 0 else 1 if i + 1 == j else 0
16
+ for j in range(len(p))] for i in range(len(p))]))[0].data)
pypynum/maths.py CHANGED
@@ -106,9 +106,14 @@ def lcm(*args: int) -> int:
106
106
  """
107
107
  if not args:
108
108
  return 0
109
+ try:
110
+ f = math.lcm
111
+ except AttributeError:
112
+ def f(a, b):
113
+ return a * b // math.gcd(a, b)
109
114
  lcm_value = args[0]
110
115
  for n in args[1:]:
111
- lcm_value = math.lcm(lcm_value, n)
116
+ lcm_value = f(lcm_value, n)
112
117
  return lcm_value
113
118
 
114
119
 
pypynum/plotting.py CHANGED
@@ -165,6 +165,7 @@ def c_unary(function, start: real, end: real, interval: real = 5, projection: st
165
165
  :param coloration: bool.
166
166
  :return:
167
167
  """
168
+ print("\n注意:自变量目前只支持实数范围\n\nNote: The independent variable currently only supports the real range\n")
168
169
  if abs(interval) != interval:
169
170
  raise ValueError("The interval cannot be less than zero")
170
171
  if not isinstance(character, str) or (len(character) != 1 and not coloration):
pypynum/__temporary.py DELETED
@@ -1,169 +0,0 @@
1
- def eigenvalue(matrix, eps=1e-100):
2
- from math import hypot
3
- from cmath import sqrt
4
- from .Matrix import mat
5
- A = matrix.copy()
6
- n = A.rows
7
- if n == 1:
8
- return [A[0]], mat([[1]])
9
- T = [0j] * n
10
- if n > 2:
11
- for i in range(n - 1, 1, -1):
12
- scale = 0
13
- for k in range(0, i):
14
- scale += abs(A[i, k].real) + abs(A[i, k].imag)
15
- scale_inv = 0
16
- if scale != 0:
17
- scale_inv = 1 / scale
18
- if scale == 0 or scale_inv == float("inf"):
19
- T[i] = 0
20
- A[i, i - 1] = 0
21
- continue
22
- H = 0
23
- for k in range(0, i):
24
- A[i, k] = A[i, k] * scale_inv
25
- rr, ii = A[i, k].real, A[i, k].imag
26
- H += rr * rr + ii * ii
27
- F = A[i, i - 1]
28
- f = abs(F)
29
- G = sqrt(H)
30
- A[i, i - 1] = - G * scale
31
- if f == 0:
32
- T[i] = G
33
- else:
34
- ff = F / f
35
- T[i] = F + G * ff
36
- A[i, i - 1] = A[i, i - 1] * ff
37
- H += G * f
38
- H = 1 / sqrt(H)
39
- T[i] *= H
40
- for k in range(0, i - 1):
41
- A[i, k] = A[i, k] * H
42
- for j in range(0, i):
43
- G = T[i].conjugate() * A[j, i - 1]
44
- for k in range(0, i - 1):
45
- G += A[i, k].conjugate() * A[j, k]
46
- A[j, i - 1] = A[j, i - 1] - G * T[i]
47
- for k in range(0, i - 1):
48
- A[j, k] = A[j, k] - G * A[i, k]
49
- for j in range(0, n):
50
- G = T[i] * A[i - 1, j]
51
- for k in range(0, i - 1):
52
- G += A[i, k] * A[k, j]
53
- A[i - 1, j] = A[i - 1, j] - G * T[i].conjugate()
54
- for k in range(0, i - 1):
55
- A[k, j] = A[k, j] - G * A[i, k].conjugate()
56
- Q = A.copy()
57
- for x in range(n):
58
- for y in range(x + 2, n):
59
- Q[y, x] = 0
60
- norm = 0
61
- for x in range(n):
62
- for y in range(min(x + 2, n)):
63
- norm += abs(Q[y, x])
64
- norm = norm ** 0.5 / n
65
- if norm == 0:
66
- return
67
- n0 = 0
68
- n1 = n
69
- eps0 = eps / (100 * n)
70
- its = 0
71
- while True:
72
- k = n0
73
- while k + 1 < n1:
74
- s = abs(Q[k, k].real) + abs(Q[k, k].imag) + abs(Q[k + 1, k + 1].real) + abs(Q[k + 1, k + 1].imag)
75
- if s < eps0 * norm:
76
- s = norm
77
- if abs(Q[k + 1, k]) < eps0 * s:
78
- break
79
- k += 1
80
- if k + 1 < n1:
81
- Q[k + 1, k] = 0
82
- n0 = k + 1
83
- its = 0
84
- if n0 + 1 >= n1:
85
- n0 = 0
86
- n1 = k + 1
87
- if n1 < 2:
88
- break
89
- else:
90
- if its % 30 == 10:
91
- shift = Q[n1 - 1, n1 - 2]
92
- elif its % 30 == 20:
93
- shift = abs(Q[n1 - 1, n1 - 2])
94
- elif its % 30 == 29:
95
- shift = norm
96
- else:
97
- t = Q[n1 - 2, n1 - 2] + Q[n1 - 1, n1 - 1]
98
- s = (Q[n1 - 1, n1 - 1] - Q[n1 - 2, n1 - 2]) ** 2 + 4 * Q[n1 - 1, n1 - 2] * Q[n1 - 2, n1 - 1]
99
- if s.real > 0:
100
- s = sqrt(s)
101
- else:
102
- s = sqrt(-s) * 1j
103
- a = (t + s) / 2
104
- b = (t - s) / 2
105
- if abs(Q[n1 - 1, n1 - 1] - a) > abs(Q[n1 - 1, n1 - 1] - b):
106
- shift = b
107
- else:
108
- shift = a
109
- its += 1
110
- c = Q[n0, n0] - shift
111
- s = Q[n0 + 1, n0]
112
- v = hypot(abs(c), abs(s))
113
- if v == 0:
114
- c = 1
115
- s = 0
116
- else:
117
- c /= v
118
- s /= v
119
- cc = c.conjugate()
120
- cs = s.conjugate()
121
- for k in range(n0, n):
122
- x = Q[n0, k]
123
- y = Q[n0 + 1, k]
124
- Q[n0, k] = cc * x + cs * y
125
- Q[n0 + 1, k] = c * y - s * x
126
- for k in range(min(n1, n0 + 3)):
127
- x = Q[k, n0]
128
- y = Q[k, n0 + 1]
129
- Q[k, n0] = c * x + s * y
130
- Q[k, n0 + 1] = cc * y - cs * x
131
- if not isinstance(A, bool):
132
- for k in range(n):
133
- x = A[k, n0]
134
- y = A[k, n0 + 1]
135
- A[k, n0] = c * x + s * y
136
- A[k, n0 + 1] = cc * y - cs * x
137
- for j in range(n0, n1 - 2):
138
- c = Q[j + 1, j]
139
- s = Q[j + 2, j]
140
- v = hypot(abs(c), abs(s))
141
- if v == 0:
142
- Q[j + 1, j] = 0
143
- c = 1
144
- s = 0
145
- else:
146
- Q[j + 1, j] = v
147
- c /= v
148
- s /= v
149
- Q[j + 2, j] = 0
150
- cc = c.conjugate()
151
- cs = s.conjugate()
152
- for k in range(j + 1, n):
153
- x = Q[j + 1, k]
154
- y = Q[j + 2, k]
155
- Q[j + 1, k] = cc * x + cs * y
156
- Q[j + 2, k] = c * y - s * x
157
- for k in range(0, min(n1, j + 4)):
158
- x = Q[k, j + 1]
159
- y = Q[k, j + 2]
160
- Q[k, j + 1] = c * x + s * y
161
- Q[k, j + 2] = cc * y - cs * x
162
- if not isinstance(A, bool):
163
- for k in range(0, n):
164
- x = A[k, j + 1]
165
- y = A[k, j + 2]
166
- A[k, j + 1] = c * x + s * y
167
- A[k, j + 2] = cc * y - cs * x
168
- E = [Q[_, _] for _ in range(n)]
169
- return E