PyPyNum 1.9.1__py3-none-any.whl → 1.9.2__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.
- {PyPyNum-1.9.1.dist-info → PyPyNum-1.9.2.dist-info}/METADATA +91 -19
- {PyPyNum-1.9.1.dist-info → PyPyNum-1.9.2.dist-info}/RECORD +15 -15
- {PyPyNum-1.9.1.dist-info → PyPyNum-1.9.2.dist-info}/WHEEL +1 -1
- pypynum/Array.py +60 -56
- pypynum/Matrix.py +5 -0
- pypynum/README.md +90 -18
- pypynum/Tensor.py +7 -4
- pypynum/Tree.py +43 -7
- pypynum/__init__.py +3 -2
- pypynum/equations.py +2 -2
- pypynum/maths.py +21 -9
- pypynum/numbers.py +58 -9
- pypynum/polynomial.py +108 -4
- pypynum/ufuncs.py +83 -7
- {PyPyNum-1.9.1.dist-info → PyPyNum-1.9.2.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: PyPyNum
|
|
3
|
-
Version: 1.9.
|
|
3
|
+
Version: 1.9.2
|
|
4
4
|
Summary: A multifunctional mathematical calculation package written in pure Python programming language [Python>=3.4]
|
|
5
5
|
Home-page: https://github.com/PythonSJL/PyPyNum
|
|
6
6
|
Author: Shen Jiayi
|
|
@@ -692,7 +692,7 @@ Description-Content-Type: text/markdown
|
|
|
692
692
|
[](https://pepy.tech/project/pypynum)
|
|
693
693
|
[](https://pepy.tech/project/pypynum)
|
|
694
694
|
|
|
695
|
-
## Version -> 1.9.
|
|
695
|
+
## Version -> 1.9.2 | PyPI -> https://pypi.org/project/PyPyNum/ | Gitee -> https://www.gitee.com/PythonSJL/PyPyNum | GitHub -> https://github.com/PythonSJL/PyPyNum
|
|
696
696
|
|
|
697
697
|

|
|
698
698
|
|
|
@@ -748,6 +748,7 @@ The logo cannot be displayed on PyPI, it can be viewed in Gitee or GitHub.
|
|
|
748
748
|
| `pypynum.tools` | 辅助函数 Auxiliary functions |
|
|
749
749
|
| `pypynum.Tree` | 树形数据结构 Tree data structure |
|
|
750
750
|
| `pypynum.types` | 特殊类型 Special types |
|
|
751
|
+
| `pypynum.ufuncs` | 通用函数 Universal functions |
|
|
751
752
|
| `pypynum.utils` | 实用工具 Utility |
|
|
752
753
|
| `pypynum.Vector` | 向量运算 Vector operation |
|
|
753
754
|
|
|
@@ -789,11 +790,74 @@ Python interpreter and run it!
|
|
|
789
790
|
```
|
|
790
791
|
!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
|
|
791
792
|
|
|
792
|
-
|
|
793
|
+
新增代码行数大约三百行
|
|
793
794
|
|
|
794
|
-
|
|
795
|
+
Approximately 300 new lines of
|
|
796
|
+
code added
|
|
795
797
|
|
|
796
798
|
!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
|
|
799
|
+
|
|
800
|
+
修改过的函数
|
|
801
|
+
|
|
802
|
+
Modified functions
|
|
803
|
+
|
|
804
|
+
├── Array
|
|
805
|
+
fill(shape: Any, sequence: Any, repeat: Any, pad: Any, rtype: Any) -> Any
|
|
806
|
+
zeros(shape: Any, rtype: Any) -> Any
|
|
807
|
+
zeros_like(a: Any, rtype: Any) -> Any
|
|
808
|
+
|
|
809
|
+
!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
|
|
810
|
+
|
|
811
|
+
新增加的函数
|
|
812
|
+
|
|
813
|
+
Newly added functions
|
|
814
|
+
|
|
815
|
+
class Polynomial(builtins.object)
|
|
816
|
+
<以下为新增加的方法>
|
|
817
|
+
<The following are the newly added methods>
|
|
818
|
+
__divmod__ = __truediv__(self, other)
|
|
819
|
+
__float__(self)
|
|
820
|
+
__int__(self)
|
|
821
|
+
__round__(self, n=None)
|
|
822
|
+
coeffs(self, reverse=False)
|
|
823
|
+
degs(self, reverse=False)
|
|
824
|
+
deriv(self)
|
|
825
|
+
evaluate(self, x)
|
|
826
|
+
gcd(self, other)
|
|
827
|
+
integ(self, constant=0)
|
|
828
|
+
is_zero(self)
|
|
829
|
+
latex(self)
|
|
830
|
+
lcm(self, other)
|
|
831
|
+
roots(self)
|
|
832
|
+
sqrt(self)
|
|
833
|
+
|
|
834
|
+
PyPyNum
|
|
835
|
+
├── Array
|
|
836
|
+
│ └── FUNCTION
|
|
837
|
+
│ ├── full(shape: Any, fill_value: Any, rtype: Any) -> Any
|
|
838
|
+
│ ├── full_like(a: Any, fill_value: Any, rtype: Any) -> Any
|
|
839
|
+
│ ├── ones(shape: Any, rtype: Any) -> Any
|
|
840
|
+
│ ├── ones_like(a: Any, rtype: Any) -> Any
|
|
841
|
+
├── maths
|
|
842
|
+
│ └── FUNCTION
|
|
843
|
+
│ ├── sumprod(arrays: typing.Union[list, tuple]) -> typing.Union[int, float, complex]
|
|
844
|
+
├── polynomial
|
|
845
|
+
│ └── FUNCTION
|
|
846
|
+
│ ├── from_coeffs(coeffs: Any) -> Any
|
|
847
|
+
│ ├── leggauss(polynomial: Any) -> Any
|
|
848
|
+
│ ├── legpoly(n: Any) -> Any
|
|
849
|
+
├── ufuncs
|
|
850
|
+
│ ├── CLASS
|
|
851
|
+
│ └── FUNCTION
|
|
852
|
+
│ ├── add(x: Any, y: Any) -> Any
|
|
853
|
+
│ ├── base_ufunc(arrays: Any, func: Any, args: Any, rtype: Any) -> Any
|
|
854
|
+
│ ├── divide(x: Any, y: Any) -> Any
|
|
855
|
+
│ ├── floor_divide(x: Any, y: Any) -> Any
|
|
856
|
+
│ ├── modulo(x: Any, y: Any) -> Any
|
|
857
|
+
│ ├── multiply(x: Any, y: Any) -> Any
|
|
858
|
+
│ ├── power(x: Any, y: Any, m: Any) -> Any
|
|
859
|
+
│ ├── subtract(x: Any, y: Any) -> Any
|
|
860
|
+
│ └── ufunc_helper(x: Any, y: Any, func: Any) -> Any
|
|
797
861
|
```
|
|
798
862
|
|
|
799
863
|
### 运行用时测试
|
|
@@ -823,12 +887,15 @@ PyPyNum
|
|
|
823
887
|
│ │ └── Array(object)/__init__(self: Any, data: Any, check: Any) -> Any
|
|
824
888
|
│ └── FUNCTION
|
|
825
889
|
│ ├── array(data: Any) -> Any
|
|
826
|
-
│ ├── fill(shape: Any, sequence: Any, repeat: Any, pad: Any) -> Any
|
|
827
|
-
│ ├──
|
|
890
|
+
│ ├── fill(shape: Any, sequence: Any, repeat: Any, pad: Any, rtype: Any) -> Any
|
|
891
|
+
│ ├── full(shape: Any, fill_value: Any, rtype: Any) -> Any
|
|
892
|
+
│ ├── full_like(a: Any, fill_value: Any, rtype: Any) -> Any
|
|
828
893
|
│ ├── get_shape(data: Any) -> Any
|
|
829
894
|
│ ├── is_valid_array(_array: Any, _shape: Any) -> Any
|
|
830
|
-
│ ├──
|
|
831
|
-
│
|
|
895
|
+
│ ├── ones(shape: Any, rtype: Any) -> Any
|
|
896
|
+
│ ├── ones_like(a: Any, rtype: Any) -> Any
|
|
897
|
+
│ ├── zeros(shape: Any, rtype: Any) -> Any
|
|
898
|
+
│ └── zeros_like(a: Any, rtype: Any) -> Any
|
|
832
899
|
├── FourierT
|
|
833
900
|
│ ├── CLASS
|
|
834
901
|
│ │ └── FT1D(object)/__init__(self: Any, data: Any) -> Any
|
|
@@ -1046,12 +1113,13 @@ PyPyNum
|
|
|
1046
1113
|
│ ├── sech(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
1047
1114
|
│ ├── sigma(i: int, n: int, f: Any) -> typing.Union[int, float, complex]
|
|
1048
1115
|
│ ├── sigmoid(x: typing.Union[int, float]) -> float
|
|
1049
|
-
│ ├── sign(x: typing.Union[int, float]) -> int
|
|
1116
|
+
│ ├── sign(x: typing.Union[int, float, complex]) -> typing.Union[int, float, complex]
|
|
1050
1117
|
│ ├── sin(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
1051
1118
|
│ ├── sinh(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
1052
1119
|
│ ├── skew(data: typing.Union[list, tuple]) -> float
|
|
1053
1120
|
│ ├── square_mean(numbers: typing.Union[list, tuple]) -> typing.Union[int, float, complex]
|
|
1054
1121
|
│ ├── std(numbers: typing.Union[list, tuple], dof: int) -> typing.Union[int, float, complex]
|
|
1122
|
+
│ ├── sumprod(arrays: typing.Union[list, tuple]) -> typing.Union[int, float, complex]
|
|
1055
1123
|
│ ├── tan(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
1056
1124
|
│ ├── tanh(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
1057
1125
|
│ ├── totient(n: int) -> int
|
|
@@ -1062,6 +1130,7 @@ PyPyNum
|
|
|
1062
1130
|
│ └── FUNCTION
|
|
1063
1131
|
│ ├── float2fraction(number: float, mixed: bool, error: float) -> tuple
|
|
1064
1132
|
│ ├── int2roman(integer: int, overline: bool) -> str
|
|
1133
|
+
│ ├── int2words(integer: int) -> str
|
|
1065
1134
|
│ ├── roman2int(roman_num: str) -> int
|
|
1066
1135
|
│ └── str2int(string: str) -> int
|
|
1067
1136
|
├── plotting
|
|
@@ -1077,6 +1146,9 @@ PyPyNum
|
|
|
1077
1146
|
│ ├── CLASS
|
|
1078
1147
|
│ │ └── Polynomial(object)/__init__(self: Any, terms: Any) -> Any
|
|
1079
1148
|
│ └── FUNCTION
|
|
1149
|
+
│ ├── from_coeffs(coeffs: Any) -> Any
|
|
1150
|
+
│ ├── leggauss(polynomial: Any) -> Any
|
|
1151
|
+
│ ├── legpoly(n: Any) -> Any
|
|
1080
1152
|
│ └── poly(terms: Any) -> Any
|
|
1081
1153
|
├── probability
|
|
1082
1154
|
│ ├── CLASS
|
|
@@ -1138,12 +1210,14 @@ PyPyNum
|
|
|
1138
1210
|
│ ├── CLASS
|
|
1139
1211
|
│ └── FUNCTION
|
|
1140
1212
|
│ ├── add(x: Any, y: Any) -> Any
|
|
1213
|
+
│ ├── base_ufunc(arrays: Any, func: Any, args: Any, rtype: Any) -> Any
|
|
1141
1214
|
│ ├── divide(x: Any, y: Any) -> Any
|
|
1142
1215
|
│ ├── floor_divide(x: Any, y: Any) -> Any
|
|
1143
1216
|
│ ├── modulo(x: Any, y: Any) -> Any
|
|
1144
1217
|
│ ├── multiply(x: Any, y: Any) -> Any
|
|
1145
1218
|
│ ├── power(x: Any, y: Any, m: Any) -> Any
|
|
1146
|
-
│
|
|
1219
|
+
│ ├── subtract(x: Any, y: Any) -> Any
|
|
1220
|
+
│ └── ufunc_helper(x: Any, y: Any, func: Any) -> Any
|
|
1147
1221
|
└── utils
|
|
1148
1222
|
├── CLASS
|
|
1149
1223
|
│ ├── InfIterator(object)/__init__(self: Any, start: typing.Union[int, float, complex], mode: str, common: typing.Union[int, float, complex]) -> Any
|
|
@@ -1364,9 +1438,7 @@ print(equations.polynomial_equation(p))
|
|
|
1364
1438
|
print(equations.linear_equation(*m))
|
|
1365
1439
|
|
|
1366
1440
|
"""
|
|
1367
|
-
[
|
|
1368
|
-
[ 0 (2.5615528128088294+4.456233626665941e-24j) 0]
|
|
1369
|
-
[ 0 0 (1.0000000000000007+3.241554513744382e-25j)]]
|
|
1441
|
+
[(-1.5615528128088307-6.5209667308287455e-24j) (1.0000000000000007+3.241554513744382e-25j) (2.5615528128088294+4.456233626665941e-24j)]
|
|
1370
1442
|
[ 1.6666666666666667 -0.6666666666666666 -0.4444444444444444]
|
|
1371
1443
|
"""
|
|
1372
1444
|
|
|
@@ -1467,10 +1539,10 @@ print(random.randint(0, 9, [2, 3, 4]))
|
|
|
1467
1539
|
print(random.uniform(0, 9, [2, 3, 4]))
|
|
1468
1540
|
|
|
1469
1541
|
"""
|
|
1470
|
-
[[[
|
|
1471
|
-
[[[0.
|
|
1472
|
-
[[[5,
|
|
1473
|
-
[[[
|
|
1542
|
+
[[[2.0337109813389342, 0.07828151434838644, 0.5770175491332994, -0.45862437785903776], [-0.5647580909376742, 0.04013315334953438, -1.4415329001085142, 0.21063309355876483], [-0.13079032580688052, -0.12269092226721959, -1.0486596849517071, 0.33707912556088127]], [[1.0924880424965842, -0.2904666571377903, -0.8147193339820543, -0.12425697574808597], [-0.9431495143889028, 1.6993259508582454, 2.459143670684122, 0.6706774051649211], [-0.2067467760214054, -1.335666880934244, -0.06604610266464511, 1.4659516633001894]]]
|
|
1543
|
+
[[[0.5056293647418786, 0.41138133426895374, 0.1116780669974381, 0.5421022799938007], [0.8529591223803364, 0.7967626494191837, 0.6778986058446654, 0.7966034784840031], [0.8226174224158366, 0.6882274477983558, 0.2043768498847348, 0.20649565416723548]], [[0.7858708880353261, 0.293208591223893, 0.9713037501099235, 0.1277647867709274], [0.5310738736523741, 0.3775394394569467, 0.6618665928287699, 0.7813821035754579], [0.41759114496811056, 0.8741519554894022, 0.8325056303107449, 0.8127323540413558]]]
|
|
1544
|
+
[[[5, 9, 7, 5], [9, 9, 9, 8], [6, 2, 0, 3]], [[4, 7, 5, 8], [5, 7, 2, 4], [9, 9, 2, 2]]]
|
|
1545
|
+
[[[7.666875196011509, 4.2629925536138815, 6.269304350870346, 6.1616482850575816], [8.736107228623906, 4.050564635824004, 8.719616126170123, 8.241460211008127], [2.2965292897567497, 2.3960057526618233, 2.406429664045121, 7.644380154396355]], [[3.8027437908649793, 5.075197041264121, 3.778237396690295, 0.9427794634466875], [1.9182768078467933, 6.926954119152528, 2.5353235396092666, 3.0648655668955422], [8.538065261473607, 7.652025295242501, 4.086320910353441, 7.457914057699455]]]
|
|
1474
1546
|
"""
|
|
1475
1547
|
|
|
1476
1548
|
print(regression.linear_regression(list(range(5)), [2, 4, 6, 7, 8]))
|
|
@@ -1480,7 +1552,7 @@ print(regression.polynomial_regression(list(range(5)), [2, 4, 6, 7, 8], 4))
|
|
|
1480
1552
|
"""
|
|
1481
1553
|
[1.5, 2.4000000000000004]
|
|
1482
1554
|
[-0.21428571428571563, 2.3571428571428625, 1.971428571428569]
|
|
1483
|
-
[0.
|
|
1555
|
+
[0.0833333333480164, -0.6666666668091551, 1.416666667838451, 1.1666666648311779, 2.0000000002900586]
|
|
1484
1556
|
"""
|
|
1485
1557
|
|
|
1486
1558
|
print(tools.classify([1, 2.3, 4 + 5j, "string", list, True, 3.14, False, tuple, tools]))
|
|
@@ -1489,7 +1561,7 @@ print(tools.frange(0, 3, 0.4))
|
|
|
1489
1561
|
print(tools.linspace(0, 2.8, 8))
|
|
1490
1562
|
|
|
1491
1563
|
"""
|
|
1492
|
-
{<class 'int'>: [1], <class 'float'>: [2.3, 3.14], <class 'complex'>: [(4+5j)], <class 'str'>: ['string'], <class 'type'>: [<class 'list'>, <class 'tuple'>], <class 'bool'>: [True, False], <class 'module'>: [<module 'pypynum.tools' from '
|
|
1564
|
+
{<class 'int'>: [1], <class 'float'>: [2.3, 3.14], <class 'complex'>: [(4+5j)], <class 'str'>: ['string'], <class 'type'>: [<class 'list'>, <class 'tuple'>], <class 'bool'>: [True, False], <class 'module'>: [<module 'pypynum.tools' from 'C:\\Users\\Administrator\\PycharmProjects\\pythonProject\\pypynum\\tools.py'>]}
|
|
1493
1565
|
['Python', 6, 'NumPy', <class 'int'>, 'PyPyNum', 9, 'pypynum', True]
|
|
1494
1566
|
[0.0, 0.4, 0.8, 1.2000000000000002, 1.6, 2.0, 2.4000000000000004, 2.8000000000000003]
|
|
1495
1567
|
[0.0, 0.39999999999999997, 0.7999999999999999, 1.2, 1.5999999999999999, 1.9999999999999998, 2.4, 2.8]
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
pypynum/Array.py,sha256=
|
|
1
|
+
pypynum/Array.py,sha256=izB5QfStvpRmYLdlqYl-N3GO0P6m4Ub6E23deKj2-HE,8604
|
|
2
2
|
pypynum/FourierT.py,sha256=AtG0tESykzEs4gDsXhcizW7qhQnmw0gjcWcXefBqzhs,1401
|
|
3
3
|
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=
|
|
7
|
+
pypynum/Matrix.py,sha256=b2op-7V-6OQJLeHqFFRbZ2766ZdueZ485t04UeGQrLc,18248
|
|
8
8
|
pypynum/NeuralN.py,sha256=iSOvC9JW1h4AFGokGGOTkKie5hAYN_YT9H4f3apI9b8,3275
|
|
9
9
|
pypynum/PyPyNum.png,sha256=t96tJPWfHxT8kcXm_qZI2z5W36TgOqjCU9qdgbmlFws,11623
|
|
10
10
|
pypynum/Quaternion.py,sha256=-BW_crP_i-veHN0_pD3Z1dipFNUX198oZDrUYTsoZw0,8017
|
|
11
|
-
pypynum/README.md,sha256=
|
|
11
|
+
pypynum/README.md,sha256=4ZmUnLvcZM3JzhKH_cV3_ry6LGavjY8Pbymo8GV0kDM,56625
|
|
12
12
|
pypynum/Symbolics.py,sha256=u-Dig3OLs6qoLzxMpTAYJGq5uSWDMvgU13TAHKLyjMY,2768
|
|
13
|
-
pypynum/Tensor.py,sha256=
|
|
14
|
-
pypynum/Tree.py,sha256=
|
|
13
|
+
pypynum/Tensor.py,sha256=gi7OjrGgP5BSJS9Oma1B2EdX9qSpMNIP2BnKLojdT78,3930
|
|
14
|
+
pypynum/Tree.py,sha256=BYnlb2kKNJ8kkuq8BAHIcLvPZw5KHxbvM7YuVGhQVMk,4330
|
|
15
15
|
pypynum/Vector.py,sha256=0GSLDmUE0ZWB4JELB4rL6xHhf767wJlSIIdLt2ZpO9M,3526
|
|
16
|
-
pypynum/__init__.py,sha256=
|
|
16
|
+
pypynum/__init__.py,sha256=dfbbZv0z8rF8t-zfg9aA6QaSSUFZa3N6eE49o-N2LkA,1964
|
|
17
17
|
pypynum/chars.py,sha256=VcK9w0i73FMCzc-9aIibjdHqyMsofJXdoBq0d8L7Vr0,1001
|
|
18
18
|
pypynum/cipher.py,sha256=DaitY3DCoTuzyrXtD8Ap3IYDLhlMc7-o4AJfLlicvB4,10011
|
|
19
19
|
pypynum/constants.py,sha256=xELv4DIKEqBdwF9tUqCmTQVbgVrlrj385ht5eawvshU,1406
|
|
20
|
-
pypynum/equations.py,sha256=
|
|
20
|
+
pypynum/equations.py,sha256=jwdW2eKBEkL5sMJQINtTycushz6ng3gKrL3aEDECSf4,770
|
|
21
21
|
pypynum/errors.py,sha256=I3nh5YD5F_sBoa7hjPJi81goRflbVAZcjtmW7O2BhJk,220
|
|
22
22
|
pypynum/file.py,sha256=rqrPdcX0NOEPM8s40-ymgPKsTn0HKABW-VEh1OtPnsk,3188
|
|
23
|
-
pypynum/maths.py,sha256=
|
|
24
|
-
pypynum/numbers.py,sha256=
|
|
23
|
+
pypynum/maths.py,sha256=3DA3TV8gON4A2Cp_Tt6wwbFPWQLJIniUxOKnt1OfROg,27602
|
|
24
|
+
pypynum/numbers.py,sha256=S49O4z_uO2FKLdOpAhDtKSVRbG8_xB8ihVDoPYiVlcE,7880
|
|
25
25
|
pypynum/plotting.py,sha256=mbIYK5TpY1qvuMJrqz4d8bxJEiZww3AI684vSKV-DgU,7781
|
|
26
|
-
pypynum/polynomial.py,sha256=
|
|
26
|
+
pypynum/polynomial.py,sha256=B4HN_H3V_Ffcg3RQOY_c3SBZvAYhSkCe_u5PCmPSMHk,9362
|
|
27
27
|
pypynum/probability.py,sha256=aUHaS7NMIJJWI8vZktgvn1zQKLtjdBiY5d-qwbJ6fGM,3778
|
|
28
28
|
pypynum/random.py,sha256=vfTtyH5yOSKJoSI_-NT_Tg8wC-ZXT0wt34f87oaSpJY,4610
|
|
29
29
|
pypynum/regression.py,sha256=HNGifq9rhS7JWFO12nvbP978IZaOOAmgibJURpqYV_0,2070
|
|
@@ -32,9 +32,9 @@ pypynum/test.py,sha256=ph9oBFjwT1DNUAqdEivJfhIaysDsgWxwLFi8Ekx733U,9119
|
|
|
32
32
|
pypynum/this.py,sha256=oRX1OpMa4NJmQSdEjJxfszEo5FRYlxRiF8OTDRIcdMA,2154
|
|
33
33
|
pypynum/tools.py,sha256=jv-R0fXJ0ptszAyaFwwakyCMFL835p8T20ihvzL4vbc,12665
|
|
34
34
|
pypynum/types.py,sha256=CVWPZo_ACr_QGH5gAOhoG3jK35peiqipu3PH8ScEYHE,181
|
|
35
|
-
pypynum/ufuncs.py,sha256=
|
|
35
|
+
pypynum/ufuncs.py,sha256=g2tewdsGa4VrIq2khR-0SWJoXBitVRN87DulOnTxxDA,2572
|
|
36
36
|
pypynum/utils.py,sha256=oeHpAMFItWXfKjb0UQITPwMVKZBd3H5JT3R-jBgn2_w,14466
|
|
37
|
-
PyPyNum-1.9.
|
|
38
|
-
PyPyNum-1.9.
|
|
39
|
-
PyPyNum-1.9.
|
|
40
|
-
PyPyNum-1.9.
|
|
37
|
+
PyPyNum-1.9.2.dist-info/METADATA,sha256=228Hjzw5LqRYU5RmkjteaDMn4MoJ-4RA-sSHeYP-syo,98069
|
|
38
|
+
PyPyNum-1.9.2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
39
|
+
PyPyNum-1.9.2.dist-info/top_level.txt,sha256=4wW_Xb4bRglmiMsdPAe9f75MkXhNpuN88H17g_Cr5u8,8
|
|
40
|
+
PyPyNum-1.9.2.dist-info/RECORD,,
|
pypynum/Array.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
from .errors import ShapeError
|
|
2
|
-
from .ufuncs import *
|
|
3
2
|
|
|
4
3
|
ArrayError = ShapeError("The shape of the array is invalid")
|
|
5
4
|
MatchError = ShapeError("The shapes of the two arrays do not match")
|
|
@@ -7,8 +6,7 @@ MatchError = ShapeError("The shapes of the two arrays do not match")
|
|
|
7
6
|
|
|
8
7
|
class Array:
|
|
9
8
|
"""
|
|
10
|
-
It is the base class of vectors, matrices, and
|
|
11
|
-
which does not support operations but has many statistical functions.
|
|
9
|
+
It is the base class of vectors, matrices, and tensors, supporting operations and many statistical functions.
|
|
12
10
|
:param data: An array in the form of a list
|
|
13
11
|
:param check: Check the rationality of the input array
|
|
14
12
|
"""
|
|
@@ -22,7 +20,7 @@ class Array:
|
|
|
22
20
|
self.data = data
|
|
23
21
|
|
|
24
22
|
def __repr__(self):
|
|
25
|
-
return
|
|
23
|
+
return "{}({})".format(self.__class__.__name__, self.data)
|
|
26
24
|
|
|
27
25
|
def __str__(self):
|
|
28
26
|
if not self.data:
|
|
@@ -59,8 +57,9 @@ class Array:
|
|
|
59
57
|
return self.data[item]
|
|
60
58
|
|
|
61
59
|
def __round__(self, n=None):
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
from .ufuncs import base_ufunc
|
|
61
|
+
return base_ufunc(self, func=lambda number, digits: round(number.real, digits) + round(
|
|
62
|
+
number.imag, digits) * 1j if isinstance(number, complex) else round(number, digits), args=[n])
|
|
64
63
|
|
|
65
64
|
def __hash__(self):
|
|
66
65
|
return hash(repr(self.data))
|
|
@@ -69,9 +68,10 @@ class Array:
|
|
|
69
68
|
if isinstance(other, Array):
|
|
70
69
|
if self.shape != other.shape:
|
|
71
70
|
raise MatchError
|
|
72
|
-
return
|
|
71
|
+
return type(self)(fill(self.shape, [t1 / t2 for t1, t2 in zip(self.flatten(), other.flatten())]))
|
|
73
72
|
elif isinstance(other, (int, float, complex)):
|
|
74
|
-
|
|
73
|
+
from .ufuncs import divide
|
|
74
|
+
return divide(self, other)
|
|
75
75
|
else:
|
|
76
76
|
raise ValueError("Another must be an array or number")
|
|
77
77
|
|
|
@@ -79,9 +79,10 @@ class Array:
|
|
|
79
79
|
if isinstance(other, Array):
|
|
80
80
|
if self.shape != other.shape:
|
|
81
81
|
raise MatchError
|
|
82
|
-
return
|
|
82
|
+
return type(self)(fill(self.shape, [t1 // t2 for t1, t2 in zip(self.flatten(), other.flatten())]))
|
|
83
83
|
elif isinstance(other, (int, float, complex)):
|
|
84
|
-
|
|
84
|
+
from .ufuncs import floor_divide
|
|
85
|
+
return floor_divide(self, other)
|
|
85
86
|
else:
|
|
86
87
|
raise ValueError("Another must be an array or number")
|
|
87
88
|
|
|
@@ -89,15 +90,18 @@ class Array:
|
|
|
89
90
|
if isinstance(other, Array):
|
|
90
91
|
if self.shape != other.shape:
|
|
91
92
|
raise MatchError
|
|
92
|
-
return
|
|
93
|
+
return type(self)(fill(self.shape, [t1 % t2 for t1, t2 in zip(self.flatten(), other.flatten())]))
|
|
93
94
|
elif isinstance(other, (int, float, complex)):
|
|
94
|
-
|
|
95
|
+
from .ufuncs import modulo
|
|
96
|
+
return modulo(self, other)
|
|
95
97
|
else:
|
|
96
98
|
raise ValueError("Another must be an array or number")
|
|
97
99
|
|
|
98
100
|
def __pow__(self, _exp, _mod=None):
|
|
99
|
-
if isinstance(_exp, (int, float, complex)) and isinstance(_mod,
|
|
100
|
-
|
|
101
|
+
if isinstance(_exp, (int, float, complex, Array)) and isinstance(_mod,
|
|
102
|
+
(int, float, complex, Array, type(None))):
|
|
103
|
+
from .ufuncs import power
|
|
104
|
+
return power(self, _exp, _mod)
|
|
101
105
|
else:
|
|
102
106
|
raise ValueError()
|
|
103
107
|
|
|
@@ -107,8 +111,8 @@ class Array:
|
|
|
107
111
|
data = sum(data, [])
|
|
108
112
|
return data
|
|
109
113
|
|
|
110
|
-
def reshape(self, shape):
|
|
111
|
-
return type(self)(fill(shape, self.flatten()))
|
|
114
|
+
def reshape(self, shape, repeat=True, pad=0):
|
|
115
|
+
return type(self)(fill(shape, self.flatten(), repeat, pad))
|
|
112
116
|
|
|
113
117
|
def copy(self):
|
|
114
118
|
from copy import deepcopy
|
|
@@ -178,7 +182,7 @@ def get_shape(data):
|
|
|
178
182
|
while isinstance(_sub, list):
|
|
179
183
|
_shape.append(len(_sub))
|
|
180
184
|
_sub = _sub[0]
|
|
181
|
-
return _shape
|
|
185
|
+
return tuple(_shape)
|
|
182
186
|
|
|
183
187
|
|
|
184
188
|
def is_valid_array(_array, _shape):
|
|
@@ -198,28 +202,45 @@ def array(data=None):
|
|
|
198
202
|
return Array(data)
|
|
199
203
|
|
|
200
204
|
|
|
201
|
-
def
|
|
202
|
-
|
|
203
|
-
return 0
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
def
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
205
|
+
def full(shape, fill_value, rtype=Array):
|
|
206
|
+
def inner(data):
|
|
207
|
+
return fill_value if len(data) == 0 else [(inner(data[1:])) for _ in range(data[0])]
|
|
208
|
+
|
|
209
|
+
if isinstance(fill_value, list):
|
|
210
|
+
raise TypeError("The filled value cannot be a list")
|
|
211
|
+
result = inner(shape)
|
|
212
|
+
return result if rtype is list else rtype(result)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def full_like(a, fill_value, rtype=Array):
|
|
216
|
+
def inner(data):
|
|
217
|
+
return [inner(item) for item in data] if isinstance(data, list) else fill_value
|
|
218
|
+
|
|
219
|
+
if isinstance(fill_value, list):
|
|
220
|
+
raise TypeError("The filled value cannot be a list")
|
|
221
|
+
if isinstance(a, Array):
|
|
222
|
+
a = a.data
|
|
223
|
+
result = inner(a)
|
|
224
|
+
return result if rtype is list else rtype(result)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def zeros(shape, rtype=Array):
|
|
228
|
+
return full(shape, 0, rtype)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def zeros_like(a, rtype=Array):
|
|
232
|
+
return full_like(a, 0, rtype)
|
|
220
233
|
|
|
221
234
|
|
|
222
|
-
def
|
|
235
|
+
def ones(shape, rtype=Array):
|
|
236
|
+
return full(shape, 1, rtype)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def ones_like(a, rtype=Array):
|
|
240
|
+
return full_like(a, 1, rtype)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def fill(shape, sequence=None, repeat=True, pad=0, rtype=Array):
|
|
223
244
|
pointer = -1
|
|
224
245
|
length = 1
|
|
225
246
|
for item in shape:
|
|
@@ -243,22 +264,5 @@ def fill(shape, sequence=None, repeat=True, pad=0):
|
|
|
243
264
|
_array.append(_row)
|
|
244
265
|
return _array
|
|
245
266
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
def function(_array, _function, args=None):
|
|
250
|
-
_type = str(type(_function))
|
|
251
|
-
if not isinstance(_array, Array) or not (_type.startswith("<function ") or _type.startswith("<class ")):
|
|
252
|
-
raise TypeError("The input parameter type is incorrect")
|
|
253
|
-
data = _array.data
|
|
254
|
-
|
|
255
|
-
def inner(_array):
|
|
256
|
-
if isinstance(_array, list):
|
|
257
|
-
_copy = []
|
|
258
|
-
for item in _array:
|
|
259
|
-
_copy.append(inner(item))
|
|
260
|
-
return _copy
|
|
261
|
-
else:
|
|
262
|
-
return _function(_array) if args is None else _function(_array, *args)
|
|
263
|
-
|
|
264
|
-
return type(_array)(inner(data), False)
|
|
267
|
+
result = inner(shape)
|
|
268
|
+
return result if rtype is list else rtype(result)
|
pypynum/Matrix.py
CHANGED
|
@@ -92,6 +92,11 @@ class Matrix(Array):
|
|
|
92
92
|
matrix[k][j] -= matrix[i][j] * matrix[k][i]
|
|
93
93
|
return determinant
|
|
94
94
|
|
|
95
|
+
def slogdet(self):
|
|
96
|
+
from math import log
|
|
97
|
+
det_value = self.det()
|
|
98
|
+
return (0.0, float("-inf")) if det_value == 0 else (det_value / abs(det_value), log(abs(det_value)))
|
|
99
|
+
|
|
95
100
|
def inv(self):
|
|
96
101
|
if self.rows != self.cols:
|
|
97
102
|
raise SquareError
|
pypynum/README.md
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
[](https://pepy.tech/project/pypynum)
|
|
19
19
|
[](https://pepy.tech/project/pypynum)
|
|
20
20
|
|
|
21
|
-
## Version -> 1.9.
|
|
21
|
+
## Version -> 1.9.2 | PyPI -> https://pypi.org/project/PyPyNum/ | Gitee -> https://www.gitee.com/PythonSJL/PyPyNum | GitHub -> https://github.com/PythonSJL/PyPyNum
|
|
22
22
|
|
|
23
23
|

|
|
24
24
|
|
|
@@ -74,6 +74,7 @@ The logo cannot be displayed on PyPI, it can be viewed in Gitee or GitHub.
|
|
|
74
74
|
| `pypynum.tools` | 辅助函数 Auxiliary functions |
|
|
75
75
|
| `pypynum.Tree` | 树形数据结构 Tree data structure |
|
|
76
76
|
| `pypynum.types` | 特殊类型 Special types |
|
|
77
|
+
| `pypynum.ufuncs` | 通用函数 Universal functions |
|
|
77
78
|
| `pypynum.utils` | 实用工具 Utility |
|
|
78
79
|
| `pypynum.Vector` | 向量运算 Vector operation |
|
|
79
80
|
|
|
@@ -115,11 +116,74 @@ Python interpreter and run it!
|
|
|
115
116
|
```
|
|
116
117
|
!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
|
|
117
118
|
|
|
118
|
-
|
|
119
|
+
新增代码行数大约三百行
|
|
119
120
|
|
|
120
|
-
|
|
121
|
+
Approximately 300 new lines of
|
|
122
|
+
code added
|
|
121
123
|
|
|
122
124
|
!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
|
|
125
|
+
|
|
126
|
+
修改过的函数
|
|
127
|
+
|
|
128
|
+
Modified functions
|
|
129
|
+
|
|
130
|
+
├── Array
|
|
131
|
+
fill(shape: Any, sequence: Any, repeat: Any, pad: Any, rtype: Any) -> Any
|
|
132
|
+
zeros(shape: Any, rtype: Any) -> Any
|
|
133
|
+
zeros_like(a: Any, rtype: Any) -> Any
|
|
134
|
+
|
|
135
|
+
!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
|
|
136
|
+
|
|
137
|
+
新增加的函数
|
|
138
|
+
|
|
139
|
+
Newly added functions
|
|
140
|
+
|
|
141
|
+
class Polynomial(builtins.object)
|
|
142
|
+
<以下为新增加的方法>
|
|
143
|
+
<The following are the newly added methods>
|
|
144
|
+
__divmod__ = __truediv__(self, other)
|
|
145
|
+
__float__(self)
|
|
146
|
+
__int__(self)
|
|
147
|
+
__round__(self, n=None)
|
|
148
|
+
coeffs(self, reverse=False)
|
|
149
|
+
degs(self, reverse=False)
|
|
150
|
+
deriv(self)
|
|
151
|
+
evaluate(self, x)
|
|
152
|
+
gcd(self, other)
|
|
153
|
+
integ(self, constant=0)
|
|
154
|
+
is_zero(self)
|
|
155
|
+
latex(self)
|
|
156
|
+
lcm(self, other)
|
|
157
|
+
roots(self)
|
|
158
|
+
sqrt(self)
|
|
159
|
+
|
|
160
|
+
PyPyNum
|
|
161
|
+
├── Array
|
|
162
|
+
│ └── FUNCTION
|
|
163
|
+
│ ├── full(shape: Any, fill_value: Any, rtype: Any) -> Any
|
|
164
|
+
│ ├── full_like(a: Any, fill_value: Any, rtype: Any) -> Any
|
|
165
|
+
│ ├── ones(shape: Any, rtype: Any) -> Any
|
|
166
|
+
│ ├── ones_like(a: Any, rtype: Any) -> Any
|
|
167
|
+
├── maths
|
|
168
|
+
│ └── FUNCTION
|
|
169
|
+
│ ├── sumprod(arrays: typing.Union[list, tuple]) -> typing.Union[int, float, complex]
|
|
170
|
+
├── polynomial
|
|
171
|
+
│ └── FUNCTION
|
|
172
|
+
│ ├── from_coeffs(coeffs: Any) -> Any
|
|
173
|
+
│ ├── leggauss(polynomial: Any) -> Any
|
|
174
|
+
│ ├── legpoly(n: Any) -> Any
|
|
175
|
+
├── ufuncs
|
|
176
|
+
│ ├── CLASS
|
|
177
|
+
│ └── FUNCTION
|
|
178
|
+
│ ├── add(x: Any, y: Any) -> Any
|
|
179
|
+
│ ├── base_ufunc(arrays: Any, func: Any, args: Any, rtype: Any) -> Any
|
|
180
|
+
│ ├── divide(x: Any, y: Any) -> Any
|
|
181
|
+
│ ├── floor_divide(x: Any, y: Any) -> Any
|
|
182
|
+
│ ├── modulo(x: Any, y: Any) -> Any
|
|
183
|
+
│ ├── multiply(x: Any, y: Any) -> Any
|
|
184
|
+
│ ├── power(x: Any, y: Any, m: Any) -> Any
|
|
185
|
+
│ ├── subtract(x: Any, y: Any) -> Any
|
|
186
|
+
│ └── ufunc_helper(x: Any, y: Any, func: Any) -> Any
|
|
123
187
|
```
|
|
124
188
|
|
|
125
189
|
### 运行用时测试
|
|
@@ -149,12 +213,15 @@ PyPyNum
|
|
|
149
213
|
│ │ └── Array(object)/__init__(self: Any, data: Any, check: Any) -> Any
|
|
150
214
|
│ └── FUNCTION
|
|
151
215
|
│ ├── array(data: Any) -> Any
|
|
152
|
-
│ ├── fill(shape: Any, sequence: Any, repeat: Any, pad: Any) -> Any
|
|
153
|
-
│ ├──
|
|
216
|
+
│ ├── fill(shape: Any, sequence: Any, repeat: Any, pad: Any, rtype: Any) -> Any
|
|
217
|
+
│ ├── full(shape: Any, fill_value: Any, rtype: Any) -> Any
|
|
218
|
+
│ ├── full_like(a: Any, fill_value: Any, rtype: Any) -> Any
|
|
154
219
|
│ ├── get_shape(data: Any) -> Any
|
|
155
220
|
│ ├── is_valid_array(_array: Any, _shape: Any) -> Any
|
|
156
|
-
│ ├──
|
|
157
|
-
│
|
|
221
|
+
│ ├── ones(shape: Any, rtype: Any) -> Any
|
|
222
|
+
│ ├── ones_like(a: Any, rtype: Any) -> Any
|
|
223
|
+
│ ├── zeros(shape: Any, rtype: Any) -> Any
|
|
224
|
+
│ └── zeros_like(a: Any, rtype: Any) -> Any
|
|
158
225
|
├── FourierT
|
|
159
226
|
│ ├── CLASS
|
|
160
227
|
│ │ └── FT1D(object)/__init__(self: Any, data: Any) -> Any
|
|
@@ -372,12 +439,13 @@ PyPyNum
|
|
|
372
439
|
│ ├── sech(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
373
440
|
│ ├── sigma(i: int, n: int, f: Any) -> typing.Union[int, float, complex]
|
|
374
441
|
│ ├── sigmoid(x: typing.Union[int, float]) -> float
|
|
375
|
-
│ ├── sign(x: typing.Union[int, float]) -> int
|
|
442
|
+
│ ├── sign(x: typing.Union[int, float, complex]) -> typing.Union[int, float, complex]
|
|
376
443
|
│ ├── sin(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
377
444
|
│ ├── sinh(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
378
445
|
│ ├── skew(data: typing.Union[list, tuple]) -> float
|
|
379
446
|
│ ├── square_mean(numbers: typing.Union[list, tuple]) -> typing.Union[int, float, complex]
|
|
380
447
|
│ ├── std(numbers: typing.Union[list, tuple], dof: int) -> typing.Union[int, float, complex]
|
|
448
|
+
│ ├── sumprod(arrays: typing.Union[list, tuple]) -> typing.Union[int, float, complex]
|
|
381
449
|
│ ├── tan(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
382
450
|
│ ├── tanh(x: typing.Union[int, float]) -> typing.Union[int, float]
|
|
383
451
|
│ ├── totient(n: int) -> int
|
|
@@ -388,6 +456,7 @@ PyPyNum
|
|
|
388
456
|
│ └── FUNCTION
|
|
389
457
|
│ ├── float2fraction(number: float, mixed: bool, error: float) -> tuple
|
|
390
458
|
│ ├── int2roman(integer: int, overline: bool) -> str
|
|
459
|
+
│ ├── int2words(integer: int) -> str
|
|
391
460
|
│ ├── roman2int(roman_num: str) -> int
|
|
392
461
|
│ └── str2int(string: str) -> int
|
|
393
462
|
├── plotting
|
|
@@ -403,6 +472,9 @@ PyPyNum
|
|
|
403
472
|
│ ├── CLASS
|
|
404
473
|
│ │ └── Polynomial(object)/__init__(self: Any, terms: Any) -> Any
|
|
405
474
|
│ └── FUNCTION
|
|
475
|
+
│ ├── from_coeffs(coeffs: Any) -> Any
|
|
476
|
+
│ ├── leggauss(polynomial: Any) -> Any
|
|
477
|
+
│ ├── legpoly(n: Any) -> Any
|
|
406
478
|
│ └── poly(terms: Any) -> Any
|
|
407
479
|
├── probability
|
|
408
480
|
│ ├── CLASS
|
|
@@ -464,12 +536,14 @@ PyPyNum
|
|
|
464
536
|
│ ├── CLASS
|
|
465
537
|
│ └── FUNCTION
|
|
466
538
|
│ ├── add(x: Any, y: Any) -> Any
|
|
539
|
+
│ ├── base_ufunc(arrays: Any, func: Any, args: Any, rtype: Any) -> Any
|
|
467
540
|
│ ├── divide(x: Any, y: Any) -> Any
|
|
468
541
|
│ ├── floor_divide(x: Any, y: Any) -> Any
|
|
469
542
|
│ ├── modulo(x: Any, y: Any) -> Any
|
|
470
543
|
│ ├── multiply(x: Any, y: Any) -> Any
|
|
471
544
|
│ ├── power(x: Any, y: Any, m: Any) -> Any
|
|
472
|
-
│
|
|
545
|
+
│ ├── subtract(x: Any, y: Any) -> Any
|
|
546
|
+
│ └── ufunc_helper(x: Any, y: Any, func: Any) -> Any
|
|
473
547
|
└── utils
|
|
474
548
|
├── CLASS
|
|
475
549
|
│ ├── InfIterator(object)/__init__(self: Any, start: typing.Union[int, float, complex], mode: str, common: typing.Union[int, float, complex]) -> Any
|
|
@@ -690,9 +764,7 @@ print(equations.polynomial_equation(p))
|
|
|
690
764
|
print(equations.linear_equation(*m))
|
|
691
765
|
|
|
692
766
|
"""
|
|
693
|
-
[
|
|
694
|
-
[ 0 (2.5615528128088294+4.456233626665941e-24j) 0]
|
|
695
|
-
[ 0 0 (1.0000000000000007+3.241554513744382e-25j)]]
|
|
767
|
+
[(-1.5615528128088307-6.5209667308287455e-24j) (1.0000000000000007+3.241554513744382e-25j) (2.5615528128088294+4.456233626665941e-24j)]
|
|
696
768
|
[ 1.6666666666666667 -0.6666666666666666 -0.4444444444444444]
|
|
697
769
|
"""
|
|
698
770
|
|
|
@@ -793,10 +865,10 @@ print(random.randint(0, 9, [2, 3, 4]))
|
|
|
793
865
|
print(random.uniform(0, 9, [2, 3, 4]))
|
|
794
866
|
|
|
795
867
|
"""
|
|
796
|
-
[[[
|
|
797
|
-
[[[0.
|
|
798
|
-
[[[5,
|
|
799
|
-
[[[
|
|
868
|
+
[[[2.0337109813389342, 0.07828151434838644, 0.5770175491332994, -0.45862437785903776], [-0.5647580909376742, 0.04013315334953438, -1.4415329001085142, 0.21063309355876483], [-0.13079032580688052, -0.12269092226721959, -1.0486596849517071, 0.33707912556088127]], [[1.0924880424965842, -0.2904666571377903, -0.8147193339820543, -0.12425697574808597], [-0.9431495143889028, 1.6993259508582454, 2.459143670684122, 0.6706774051649211], [-0.2067467760214054, -1.335666880934244, -0.06604610266464511, 1.4659516633001894]]]
|
|
869
|
+
[[[0.5056293647418786, 0.41138133426895374, 0.1116780669974381, 0.5421022799938007], [0.8529591223803364, 0.7967626494191837, 0.6778986058446654, 0.7966034784840031], [0.8226174224158366, 0.6882274477983558, 0.2043768498847348, 0.20649565416723548]], [[0.7858708880353261, 0.293208591223893, 0.9713037501099235, 0.1277647867709274], [0.5310738736523741, 0.3775394394569467, 0.6618665928287699, 0.7813821035754579], [0.41759114496811056, 0.8741519554894022, 0.8325056303107449, 0.8127323540413558]]]
|
|
870
|
+
[[[5, 9, 7, 5], [9, 9, 9, 8], [6, 2, 0, 3]], [[4, 7, 5, 8], [5, 7, 2, 4], [9, 9, 2, 2]]]
|
|
871
|
+
[[[7.666875196011509, 4.2629925536138815, 6.269304350870346, 6.1616482850575816], [8.736107228623906, 4.050564635824004, 8.719616126170123, 8.241460211008127], [2.2965292897567497, 2.3960057526618233, 2.406429664045121, 7.644380154396355]], [[3.8027437908649793, 5.075197041264121, 3.778237396690295, 0.9427794634466875], [1.9182768078467933, 6.926954119152528, 2.5353235396092666, 3.0648655668955422], [8.538065261473607, 7.652025295242501, 4.086320910353441, 7.457914057699455]]]
|
|
800
872
|
"""
|
|
801
873
|
|
|
802
874
|
print(regression.linear_regression(list(range(5)), [2, 4, 6, 7, 8]))
|
|
@@ -806,7 +878,7 @@ print(regression.polynomial_regression(list(range(5)), [2, 4, 6, 7, 8], 4))
|
|
|
806
878
|
"""
|
|
807
879
|
[1.5, 2.4000000000000004]
|
|
808
880
|
[-0.21428571428571563, 2.3571428571428625, 1.971428571428569]
|
|
809
|
-
[0.
|
|
881
|
+
[0.0833333333480164, -0.6666666668091551, 1.416666667838451, 1.1666666648311779, 2.0000000002900586]
|
|
810
882
|
"""
|
|
811
883
|
|
|
812
884
|
print(tools.classify([1, 2.3, 4 + 5j, "string", list, True, 3.14, False, tuple, tools]))
|
|
@@ -815,7 +887,7 @@ print(tools.frange(0, 3, 0.4))
|
|
|
815
887
|
print(tools.linspace(0, 2.8, 8))
|
|
816
888
|
|
|
817
889
|
"""
|
|
818
|
-
{<class 'int'>: [1], <class 'float'>: [2.3, 3.14], <class 'complex'>: [(4+5j)], <class 'str'>: ['string'], <class 'type'>: [<class 'list'>, <class 'tuple'>], <class 'bool'>: [True, False], <class 'module'>: [<module 'pypynum.tools' from '
|
|
890
|
+
{<class 'int'>: [1], <class 'float'>: [2.3, 3.14], <class 'complex'>: [(4+5j)], <class 'str'>: ['string'], <class 'type'>: [<class 'list'>, <class 'tuple'>], <class 'bool'>: [True, False], <class 'module'>: [<module 'pypynum.tools' from 'C:\\Users\\Administrator\\PycharmProjects\\pythonProject\\pypynum\\tools.py'>]}
|
|
819
891
|
['Python', 6, 'NumPy', <class 'int'>, 'PyPyNum', 9, 'pypynum', True]
|
|
820
892
|
[0.0, 0.4, 0.8, 1.2000000000000002, 1.6, 2.0, 2.4000000000000004, 2.8000000000000003]
|
|
821
893
|
[0.0, 0.39999999999999997, 0.7999999999999999, 1.2, 1.5999999999999999, 1.9999999999999998, 2.4, 2.8]
|
pypynum/Tensor.py
CHANGED
|
@@ -18,7 +18,8 @@ class Tensor(Array):
|
|
|
18
18
|
if isinstance(other, Tensor):
|
|
19
19
|
if self.shape != other.shape:
|
|
20
20
|
raise MatchError
|
|
21
|
-
return Tensor(fill(self.shape, [t1 + t2 for t1, t2 in zip(self.flatten(), other.flatten())]
|
|
21
|
+
return Tensor(fill(self.shape, [t1 + t2 for t1, t2 in zip(self.flatten(), other.flatten())], rtype=list),
|
|
22
|
+
False)
|
|
22
23
|
elif isinstance(other, (int, float, complex)):
|
|
23
24
|
return Tensor(tensor_and_number(self, "+", other), False)
|
|
24
25
|
else:
|
|
@@ -28,7 +29,8 @@ class Tensor(Array):
|
|
|
28
29
|
if isinstance(other, Tensor):
|
|
29
30
|
if self.shape != other.shape:
|
|
30
31
|
raise MatchError
|
|
31
|
-
return Tensor(fill(self.shape, [t1 - t2 for t1, t2 in zip(self.flatten(), other.flatten())]
|
|
32
|
+
return Tensor(fill(self.shape, [t1 - t2 for t1, t2 in zip(self.flatten(), other.flatten())], rtype=list),
|
|
33
|
+
False)
|
|
32
34
|
elif isinstance(other, (int, float, complex)):
|
|
33
35
|
return Tensor(tensor_and_number(self, "-", other), False)
|
|
34
36
|
else:
|
|
@@ -38,7 +40,8 @@ class Tensor(Array):
|
|
|
38
40
|
if isinstance(other, Tensor):
|
|
39
41
|
if self.shape != other.shape:
|
|
40
42
|
raise MatchError
|
|
41
|
-
return Tensor(fill(self.shape, [t1 * t2 for t1, t2 in zip(self.flatten(), other.flatten())]
|
|
43
|
+
return Tensor(fill(self.shape, [t1 * t2 for t1, t2 in zip(self.flatten(), other.flatten())], rtype=list),
|
|
44
|
+
False)
|
|
42
45
|
elif isinstance(other, (int, float, complex)):
|
|
43
46
|
return Tensor(tensor_and_number(self, "*", other), False)
|
|
44
47
|
else:
|
|
@@ -94,7 +97,7 @@ def tensorproduct(*tensors: Tensor) -> Tensor:
|
|
|
94
97
|
return tensors[0].copy()
|
|
95
98
|
|
|
96
99
|
def mul(a, b):
|
|
97
|
-
return Tensor(fill(a.shape + b.shape, [i * j for i in a.flatten() for j in b.flatten()]), False)
|
|
100
|
+
return Tensor(fill(a.shape + b.shape, [i * j for i in a.flatten() for j in b.flatten()], rtype=list), False)
|
|
98
101
|
|
|
99
102
|
first = tensors[0]
|
|
100
103
|
for second in tensors[1:]:
|
pypynum/Tree.py
CHANGED
|
@@ -26,26 +26,56 @@ class MultiTreeNode:
|
|
|
26
26
|
|
|
27
27
|
def traverse(self, traversal_type="preorder"):
|
|
28
28
|
if traversal_type == "preorder":
|
|
29
|
-
|
|
29
|
+
result = [self.data]
|
|
30
30
|
for child in self.children:
|
|
31
|
-
child.traverse(traversal_type)
|
|
31
|
+
result.extend(child.traverse(traversal_type))
|
|
32
|
+
return result
|
|
32
33
|
elif traversal_type == "postorder":
|
|
34
|
+
result = []
|
|
33
35
|
for child in self.children:
|
|
34
|
-
child.traverse(traversal_type)
|
|
35
|
-
|
|
36
|
+
result.extend(child.traverse(traversal_type))
|
|
37
|
+
result.append(self.data)
|
|
38
|
+
return result
|
|
39
|
+
elif traversal_type == "levelorder":
|
|
40
|
+
result = []
|
|
41
|
+
queue = [self]
|
|
42
|
+
while queue:
|
|
43
|
+
current_node = queue.pop(0)
|
|
44
|
+
result.append(current_node.data)
|
|
45
|
+
queue.extend(current_node.children)
|
|
46
|
+
return result
|
|
36
47
|
else:
|
|
37
48
|
raise ValueError("Invalid traversal type")
|
|
38
49
|
|
|
50
|
+
def to_list(self):
|
|
51
|
+
node_list = [self.data]
|
|
52
|
+
if self.children:
|
|
53
|
+
node_list.extend([child.to_list() for child in self.children])
|
|
54
|
+
return node_list
|
|
55
|
+
|
|
56
|
+
def print_tree(self, node=None, level=0, prefix="", use_repr=True):
|
|
57
|
+
if node is None:
|
|
58
|
+
node = self
|
|
59
|
+
if node is None:
|
|
60
|
+
print("The tree is empty")
|
|
61
|
+
return
|
|
62
|
+
prefix = "Root\n└── "
|
|
63
|
+
print(" " * level + prefix + (repr if use_repr else str)(node.data))
|
|
64
|
+
children_prefix = "│ " if node.children else "" # TODO 前缀暂未正确处理
|
|
65
|
+
for i, child in enumerate(node.children):
|
|
66
|
+
child_prefix = "├── " if i < len(node.children) - 1 else "└── "
|
|
67
|
+
child.print_tree(child, level + 1, child_prefix, use_repr)
|
|
68
|
+
|
|
39
69
|
def __str__(self):
|
|
40
70
|
return str(self.data)
|
|
41
71
|
|
|
42
72
|
|
|
43
73
|
class MultiTree:
|
|
44
74
|
def __init__(self, root=None):
|
|
45
|
-
self.root = root if root is None else MultiTreeNode(root)
|
|
75
|
+
self.root = root if root is None or isinstance(root, MultiTreeNode) else MultiTreeNode(root)
|
|
46
76
|
|
|
47
77
|
def add_node(self, data, parent=None):
|
|
48
|
-
new_node = MultiTreeNode(data)
|
|
78
|
+
new_node = data if isinstance(data, MultiTreeNode) else MultiTreeNode(data)
|
|
49
79
|
if parent is None:
|
|
50
80
|
if self.root is None:
|
|
51
81
|
self.root = new_node
|
|
@@ -85,6 +115,12 @@ class MultiTree:
|
|
|
85
115
|
|
|
86
116
|
def traverse(self, traversal_type="preorder"):
|
|
87
117
|
if self.root:
|
|
88
|
-
self.root.traverse(traversal_type)
|
|
118
|
+
return self.root.traverse(traversal_type)
|
|
89
119
|
else:
|
|
90
120
|
raise ValueError("Tree is empty")
|
|
121
|
+
|
|
122
|
+
def to_list(self):
|
|
123
|
+
return self.root.to_list()
|
|
124
|
+
|
|
125
|
+
def print_tree(self, use_repr=True):
|
|
126
|
+
self.root.print_tree(use_repr=use_repr)
|
pypynum/__init__.py
CHANGED
|
@@ -8,7 +8,7 @@ r"""
|
|
|
8
8
|
\|__| |\___/ / \|__| |\___/ / \|__| \|__| \|_______| \|__| \|__|
|
|
9
9
|
\|___|/ \|___|/
|
|
10
10
|
"""
|
|
11
|
-
from .Array import array, fill,
|
|
11
|
+
from .Array import array, fill, full, full_like, zeros, zeros_like, ones, ones_like
|
|
12
12
|
from . import chars
|
|
13
13
|
from .cipher import *
|
|
14
14
|
from . import constants
|
|
@@ -36,10 +36,11 @@ from .Tensor import ten, tensorproduct
|
|
|
36
36
|
from .tools import *
|
|
37
37
|
from .Tree import *
|
|
38
38
|
from . import types
|
|
39
|
+
from .ufuncs import *
|
|
39
40
|
from .utils import OrderedSet, InfIterator, LinkedList
|
|
40
41
|
from .Vector import vec
|
|
41
42
|
|
|
42
|
-
__version__ = "1.9.
|
|
43
|
+
__version__ = "1.9.2"
|
|
43
44
|
print("PyPyNum", "Version -> " + __version__, "PyPI -> https://pypi.org/project/PyPyNum/",
|
|
44
45
|
"Gitee -> https://www.gitee.com/PythonSJL/PyPyNum", "GitHub -> https://github.com/PythonSJL/PyPyNum", sep=" | ")
|
|
45
46
|
del math, arr, ite, num, real, geom, ContentError, RandomError, LogicError, InputError, FullError, Union
|
pypynum/equations.py
CHANGED
|
@@ -12,5 +12,5 @@ def polynomial_equation(coefficients: list) -> list:
|
|
|
12
12
|
from .Array import array
|
|
13
13
|
from .Matrix import eigen, mat
|
|
14
14
|
p = [_ / coefficients[0] for _ in coefficients[1:]]
|
|
15
|
-
return array(eigen(mat([[-p[i] if j == 0 else 1 if i + 1 == j else 0
|
|
16
|
-
|
|
15
|
+
return array(sorted(eigen(mat([[-p[i] if j == 0 else 1 if i + 1 == j else 0 for j in range(len(p))]
|
|
16
|
+
for i in range(len(p))]))[0].diag().t()[0], key=lambda c: (c.real, c.imag)))
|
pypynum/maths.py
CHANGED
|
@@ -9,6 +9,23 @@ __mod = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 145, 160, 169,
|
|
|
9
9
|
436, 441, 481, 484, 496, 505, 529, 544, 576, 580, 585, 601, 625, 640, 649, 676}
|
|
10
10
|
|
|
11
11
|
|
|
12
|
+
def sumprod(*arrays: arr) -> num:
|
|
13
|
+
"""
|
|
14
|
+
introduction
|
|
15
|
+
==========
|
|
16
|
+
The sum of the products of the corresponding elements in multiple arrays
|
|
17
|
+
|
|
18
|
+
example
|
|
19
|
+
==========
|
|
20
|
+
>>> sumprod([1, 2, 3], [4, 5, 6], [7, 8, 9])
|
|
21
|
+
270
|
|
22
|
+
>>>
|
|
23
|
+
:param arrays: list | tuple
|
|
24
|
+
:return:
|
|
25
|
+
"""
|
|
26
|
+
return math.fsum(map(product, zip(*arrays)))
|
|
27
|
+
|
|
28
|
+
|
|
12
29
|
def root(x: num, y: num) -> num:
|
|
13
30
|
"""
|
|
14
31
|
introduction
|
|
@@ -989,7 +1006,7 @@ def sigmoid(x: real) -> float:
|
|
|
989
1006
|
return 1 / (1 + math.exp(-x))
|
|
990
1007
|
|
|
991
1008
|
|
|
992
|
-
def sign(x:
|
|
1009
|
+
def sign(x: num) -> num:
|
|
993
1010
|
"""
|
|
994
1011
|
introduction
|
|
995
1012
|
==========
|
|
@@ -998,17 +1015,12 @@ def sign(x: real) -> int:
|
|
|
998
1015
|
example
|
|
999
1016
|
==========
|
|
1000
1017
|
>>> sign(10)
|
|
1001
|
-
1
|
|
1018
|
+
1.0
|
|
1002
1019
|
>>>
|
|
1003
|
-
:param x: integer | float
|
|
1020
|
+
:param x: integer | float | complex
|
|
1004
1021
|
:return:
|
|
1005
1022
|
"""
|
|
1006
|
-
|
|
1007
|
-
return 1
|
|
1008
|
-
elif x < 0:
|
|
1009
|
-
return -1
|
|
1010
|
-
else:
|
|
1011
|
-
return 0
|
|
1023
|
+
return x / abs(x)
|
|
1012
1024
|
|
|
1013
1025
|
|
|
1014
1026
|
def parity(x: int) -> int:
|
pypynum/numbers.py
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
ContentError = ValueError("The content of the string is invalid")
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
ROMAN_VALUES = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
|
|
3
|
+
ROMAN_SYMBOLS = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def int2words(integer: int) -> str:
|
|
7
|
+
"""
|
|
8
|
+
introduction
|
|
9
|
+
==========
|
|
10
|
+
Convert integers into natural language.
|
|
11
|
+
|
|
12
|
+
example
|
|
13
|
+
==========
|
|
14
|
+
>>> int2words(4294967296)
|
|
15
|
+
'four billion two hundred and ninety-four million nine hundred and sixty-seven thousand two hundred and ninety-six'
|
|
16
|
+
>>>
|
|
17
|
+
:param integer: integer
|
|
18
|
+
:return:
|
|
19
|
+
"""
|
|
20
|
+
if not isinstance(integer, int):
|
|
21
|
+
raise TypeError("The input must be an integer")
|
|
22
|
+
if integer == 0:
|
|
23
|
+
return "zero"
|
|
24
|
+
if integer < 0:
|
|
25
|
+
return "negative " + int2words(abs(integer))
|
|
26
|
+
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
|
|
27
|
+
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
|
|
28
|
+
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
|
|
29
|
+
|
|
30
|
+
def two_digit(num):
|
|
31
|
+
if num < 20:
|
|
32
|
+
return ones[num] if num > 0 else ""
|
|
33
|
+
ten, one = divmod(num, 10)
|
|
34
|
+
return tens[ten] + ("-" if one else "") + ones[one]
|
|
35
|
+
|
|
36
|
+
def three_digit(num):
|
|
37
|
+
if num == 0:
|
|
38
|
+
return ""
|
|
39
|
+
hundred, rem = divmod(num, 100)
|
|
40
|
+
return ones[hundred] + " hundred " + ("and " if rem else "") + two_digit(rem)
|
|
41
|
+
|
|
42
|
+
big_units = [(1000000000000000000, "quintillion"), (1000000000000000, "quadrillion"), (1000000000000, "trillion"),
|
|
43
|
+
(1000000000, "billion"), (1000000, "million"), (1000, "thousand")]
|
|
44
|
+
result = []
|
|
45
|
+
for unit, name in big_units:
|
|
46
|
+
if integer >= unit:
|
|
47
|
+
value, integer = divmod(integer, unit)
|
|
48
|
+
result.append(int2words(value) + " " + name)
|
|
49
|
+
if integer > 0:
|
|
50
|
+
words = two_digit(integer) if integer < 100 else three_digit(integer)
|
|
51
|
+
result.append(words)
|
|
52
|
+
return " ".join(result)
|
|
4
53
|
|
|
5
54
|
|
|
6
55
|
def str2int(string: str) -> int:
|
|
@@ -84,9 +133,9 @@ def int2roman(integer: int, overline: bool = True) -> str:
|
|
|
84
133
|
roman = ""
|
|
85
134
|
i = 0
|
|
86
135
|
while number > 0:
|
|
87
|
-
tmp = number //
|
|
88
|
-
roman +=
|
|
89
|
-
number -=
|
|
136
|
+
tmp = number // ROMAN_VALUES[i]
|
|
137
|
+
roman += ROMAN_SYMBOLS[i] * tmp
|
|
138
|
+
number -= ROMAN_VALUES[i] * tmp
|
|
90
139
|
i += 1
|
|
91
140
|
return roman
|
|
92
141
|
|
|
@@ -126,16 +175,16 @@ def roman2int(roman_num: str) -> int:
|
|
|
126
175
|
last = 10000
|
|
127
176
|
i = 0
|
|
128
177
|
while i < len(number):
|
|
129
|
-
if number[i:i + 2] in
|
|
130
|
-
value =
|
|
178
|
+
if number[i:i + 2] in ROMAN_SYMBOLS:
|
|
179
|
+
value = ROMAN_VALUES[ROMAN_SYMBOLS.index(number[i:i + 2])]
|
|
131
180
|
if value > last:
|
|
132
181
|
raise ContentError
|
|
133
182
|
else:
|
|
134
183
|
last = value
|
|
135
184
|
part += value
|
|
136
185
|
i += 2
|
|
137
|
-
elif number[i] in
|
|
138
|
-
value =
|
|
186
|
+
elif number[i] in ROMAN_SYMBOLS:
|
|
187
|
+
value = ROMAN_VALUES[ROMAN_SYMBOLS.index(number[i])]
|
|
139
188
|
if value > last:
|
|
140
189
|
raise ContentError
|
|
141
190
|
else:
|
pypynum/polynomial.py
CHANGED
|
@@ -33,6 +33,57 @@ class Polynomial:
|
|
|
33
33
|
else:
|
|
34
34
|
self.terms.insert(insert_index, (degree, coefficient))
|
|
35
35
|
|
|
36
|
+
def is_zero(self):
|
|
37
|
+
return not self.terms
|
|
38
|
+
|
|
39
|
+
def gcd(self, other):
|
|
40
|
+
temp = self
|
|
41
|
+
while not other.is_zero():
|
|
42
|
+
temp, other = other, temp % other
|
|
43
|
+
return temp
|
|
44
|
+
|
|
45
|
+
def lcm(self, other):
|
|
46
|
+
gcd_poly = self.gcd(other)
|
|
47
|
+
if gcd_poly.is_zero():
|
|
48
|
+
raise ValueError("Cannot compute LCM of polynomials with zero GCD")
|
|
49
|
+
return self * other // gcd_poly
|
|
50
|
+
|
|
51
|
+
def roots(self):
|
|
52
|
+
from .equations import polynomial_equation
|
|
53
|
+
return polynomial_equation(self.coeffs(True))
|
|
54
|
+
|
|
55
|
+
def evaluate(self, x):
|
|
56
|
+
return sum([coefficient * x ** degree for degree, coefficient in self.terms])
|
|
57
|
+
|
|
58
|
+
def degs(self, reverse=False):
|
|
59
|
+
terms = reversed(self.terms) if reverse else self.terms
|
|
60
|
+
return [deg for deg, _ in terms]
|
|
61
|
+
|
|
62
|
+
def coeffs(self, reverse=False):
|
|
63
|
+
max_deg = self.degree()
|
|
64
|
+
coeffs = [0] * (max_deg + 1)
|
|
65
|
+
for deg, coeff in self.terms:
|
|
66
|
+
coeffs[deg] = coeff
|
|
67
|
+
if reverse:
|
|
68
|
+
coeffs.reverse()
|
|
69
|
+
return coeffs
|
|
70
|
+
|
|
71
|
+
def sqrt(self):
|
|
72
|
+
deg, coeff = self.terms[-1]
|
|
73
|
+
new = deg // 2, coeff ** 0.5
|
|
74
|
+
root = Polynomial([new])
|
|
75
|
+
temp = Polynomial([new])
|
|
76
|
+
rem = self - root * root
|
|
77
|
+
for _ in range(deg):
|
|
78
|
+
temp.add_term(*temp.terms[0])
|
|
79
|
+
try:
|
|
80
|
+
root.add_term(*(rem // temp).terms[-1])
|
|
81
|
+
except IndexError:
|
|
82
|
+
break
|
|
83
|
+
temp.add_term(*root.terms[0])
|
|
84
|
+
rem = rem - temp * Polynomial([temp.terms[0]])
|
|
85
|
+
return root, rem
|
|
86
|
+
|
|
36
87
|
def remove0terms(self):
|
|
37
88
|
self.terms = [(deg, coeff) for deg, coeff in self.terms if coeff != 0]
|
|
38
89
|
|
|
@@ -46,7 +97,20 @@ class Polynomial:
|
|
|
46
97
|
return self.terms[-1][1]
|
|
47
98
|
return 0
|
|
48
99
|
|
|
49
|
-
def
|
|
100
|
+
def deriv(self):
|
|
101
|
+
derivative_terms = [(deg - 1, deg * coeff) for deg, coeff in self.terms if deg > 0]
|
|
102
|
+
return Polynomial(derivative_terms)
|
|
103
|
+
|
|
104
|
+
def integ(self, constant=0):
|
|
105
|
+
integrated_terms = [(deg + 1, coeff / (deg + 1)) for deg, coeff in self.terms]
|
|
106
|
+
if constant != 0:
|
|
107
|
+
integrated_terms.append((0, constant))
|
|
108
|
+
return Polynomial(integrated_terms)
|
|
109
|
+
|
|
110
|
+
def latex(self):
|
|
111
|
+
return self.__repr__(True)
|
|
112
|
+
|
|
113
|
+
def __repr__(self, use_latex=False):
|
|
50
114
|
if not self.terms:
|
|
51
115
|
return "0"
|
|
52
116
|
result = ""
|
|
@@ -61,7 +125,10 @@ class Polynomial:
|
|
|
61
125
|
result += str(abs(coefficient))
|
|
62
126
|
if degree > 0:
|
|
63
127
|
if degree > 1:
|
|
64
|
-
|
|
128
|
+
if use_latex:
|
|
129
|
+
result += "x^{" + str(degree) + "}"
|
|
130
|
+
else:
|
|
131
|
+
result += "x^" + str(degree)
|
|
65
132
|
else:
|
|
66
133
|
result += "x"
|
|
67
134
|
if result and result[0] == "+":
|
|
@@ -72,8 +139,7 @@ class Polynomial:
|
|
|
72
139
|
return Polynomial([(degree, coefficient) for degree, coefficient in self.terms])
|
|
73
140
|
|
|
74
141
|
def __neg__(self):
|
|
75
|
-
|
|
76
|
-
return Polynomial(negated_terms)
|
|
142
|
+
return Polynomial([(degree, -coefficient) for degree, coefficient in self.terms])
|
|
77
143
|
|
|
78
144
|
def __add__(self, other):
|
|
79
145
|
result = Polynomial()
|
|
@@ -143,6 +209,19 @@ class Polynomial:
|
|
|
143
209
|
def __mod__(self, other):
|
|
144
210
|
return (self / other)[1]
|
|
145
211
|
|
|
212
|
+
__divmod__ = __truediv__
|
|
213
|
+
|
|
214
|
+
def __int__(self):
|
|
215
|
+
self.terms = [(degree, int(coefficient)) for degree, coefficient in self.terms]
|
|
216
|
+
return 0
|
|
217
|
+
|
|
218
|
+
def __float__(self):
|
|
219
|
+
self.terms = [(degree, float(coefficient)) for degree, coefficient in self.terms]
|
|
220
|
+
return 0.0
|
|
221
|
+
|
|
222
|
+
def __round__(self, n=None):
|
|
223
|
+
return Polynomial([(degree, round(coefficient, n)) for degree, coefficient in self.terms])
|
|
224
|
+
|
|
146
225
|
def __pow__(self, power, modulo=None):
|
|
147
226
|
if power == 0:
|
|
148
227
|
return Polynomial([(0, 1)])
|
|
@@ -156,3 +235,28 @@ class Polynomial:
|
|
|
156
235
|
|
|
157
236
|
def poly(terms=None):
|
|
158
237
|
return Polynomial(terms)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def from_coeffs(coeffs):
|
|
241
|
+
return Polynomial([(degree, coefficient) for degree, coefficient in enumerate(coeffs) if coefficient != 0])
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def legpoly(n):
|
|
245
|
+
x = poly([(1, 1)])
|
|
246
|
+
p = [poly([(0, 1)]), x]
|
|
247
|
+
for i in range(1, n):
|
|
248
|
+
m = poly([(0, (2 * i + 1) / (i + 1))]) * x * p[i] - poly([(0, i / (i + 1))]) * p[i - 1]
|
|
249
|
+
p.append(m)
|
|
250
|
+
return p[n]
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def leggauss(polynomial):
|
|
254
|
+
roots = polynomial.roots()
|
|
255
|
+
weights = []
|
|
256
|
+
for node in roots:
|
|
257
|
+
factor1 = 1 - node ** 2
|
|
258
|
+
derivative_poly = polynomial.deriv()
|
|
259
|
+
derivative_value = derivative_poly.evaluate(node)
|
|
260
|
+
weight = 2 / (factor1 * derivative_value ** 2)
|
|
261
|
+
weights.append(weight)
|
|
262
|
+
return roots, weights
|
pypynum/ufuncs.py
CHANGED
|
@@ -1,26 +1,102 @@
|
|
|
1
|
+
from .Array import Array as __Array
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def base_ufunc(*arrays, func, args=(), rtype=None):
|
|
5
|
+
_type = str(type(func))
|
|
6
|
+
if not all([isinstance(item, __Array) for item in arrays]) or not (
|
|
7
|
+
_type.startswith("<function ") or _type.startswith("<class ")):
|
|
8
|
+
raise TypeError("The input parameter type is incorrect")
|
|
9
|
+
if len(set([item.shape for item in arrays])) != 1:
|
|
10
|
+
raise ValueError("All arrays must have the same shape")
|
|
11
|
+
data = [item.data for item in arrays]
|
|
12
|
+
|
|
13
|
+
def inner(*arrs):
|
|
14
|
+
if isinstance(arrs[0], list):
|
|
15
|
+
_copy = []
|
|
16
|
+
for item in zip(*arrs):
|
|
17
|
+
_copy.append(inner(*item))
|
|
18
|
+
return _copy
|
|
19
|
+
else:
|
|
20
|
+
return func(*arrs, *args) if args else func(*arrs)
|
|
21
|
+
|
|
22
|
+
if rtype is None:
|
|
23
|
+
rtype = type(arrays[0])
|
|
24
|
+
return rtype(inner(*data))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def ufunc_helper(x, y, func):
|
|
28
|
+
if isinstance(x, __Array):
|
|
29
|
+
if isinstance(y, __Array):
|
|
30
|
+
return base_ufunc(x, y, func=func)
|
|
31
|
+
else:
|
|
32
|
+
return base_ufunc(x, func=func, args=[y])
|
|
33
|
+
else:
|
|
34
|
+
if isinstance(y, __Array):
|
|
35
|
+
return base_ufunc(y, func=lambda b, a: func(a, b), args=[x])
|
|
36
|
+
else:
|
|
37
|
+
return func(x, y)
|
|
38
|
+
|
|
39
|
+
|
|
1
40
|
def add(x, y):
|
|
2
|
-
|
|
41
|
+
def inner(_x, _y):
|
|
42
|
+
return _x + _y
|
|
43
|
+
|
|
44
|
+
return ufunc_helper(x, y, inner)
|
|
3
45
|
|
|
4
46
|
|
|
5
47
|
def subtract(x, y):
|
|
6
|
-
|
|
48
|
+
def inner(_x, _y):
|
|
49
|
+
return _x - _y
|
|
50
|
+
|
|
51
|
+
return ufunc_helper(x, y, inner)
|
|
7
52
|
|
|
8
53
|
|
|
9
54
|
def multiply(x, y):
|
|
10
|
-
|
|
55
|
+
def inner(_x, _y):
|
|
56
|
+
return _x * _y
|
|
57
|
+
|
|
58
|
+
return ufunc_helper(x, y, inner)
|
|
11
59
|
|
|
12
60
|
|
|
13
61
|
def divide(x, y):
|
|
14
|
-
|
|
62
|
+
def inner(_x, _y):
|
|
63
|
+
return _x / _y
|
|
64
|
+
|
|
65
|
+
return ufunc_helper(x, y, inner)
|
|
15
66
|
|
|
16
67
|
|
|
17
68
|
def floor_divide(x, y):
|
|
18
|
-
|
|
69
|
+
def inner(_x, _y):
|
|
70
|
+
return _x // _y
|
|
71
|
+
|
|
72
|
+
return ufunc_helper(x, y, inner)
|
|
19
73
|
|
|
20
74
|
|
|
21
75
|
def modulo(x, y):
|
|
22
|
-
|
|
76
|
+
def inner(_x, _y):
|
|
77
|
+
return _x % _y
|
|
78
|
+
|
|
79
|
+
return ufunc_helper(x, y, inner)
|
|
23
80
|
|
|
24
81
|
|
|
25
82
|
def power(x, y, m=None):
|
|
26
|
-
|
|
83
|
+
from .Array import full
|
|
84
|
+
|
|
85
|
+
def inner(_x, _y, _m=None):
|
|
86
|
+
try:
|
|
87
|
+
return pow(_x, _y, _m)
|
|
88
|
+
except ValueError:
|
|
89
|
+
return pow(_x, _y)
|
|
90
|
+
|
|
91
|
+
is_array = [isinstance(x, __Array), isinstance(y, __Array), isinstance(m, __Array)]
|
|
92
|
+
if not any(is_array):
|
|
93
|
+
return pow(x, y, m)
|
|
94
|
+
first = (x, y, m)[is_array.index(True)]
|
|
95
|
+
shape = first.shape
|
|
96
|
+
if not is_array[0]:
|
|
97
|
+
x = full(shape, x)
|
|
98
|
+
if not is_array[1]:
|
|
99
|
+
y = full(shape, y)
|
|
100
|
+
if not is_array[2]:
|
|
101
|
+
m = full(shape, m)
|
|
102
|
+
return base_ufunc(x, y, m, func=inner, rtype=type(first))
|
|
File without changes
|