happymath 0.0.1__tar.gz
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.
- happymath-0.0.1/Differential_Equation/ODE/__init__.py +1 -0
- happymath-0.0.1/Differential_Equation/ODE/utils.py +50 -0
- happymath-0.0.1/Differential_Equation/__init__.py +5 -0
- happymath-0.0.1/Differential_Equation/utils.py +156 -0
- happymath-0.0.1/LICENSE +21 -0
- happymath-0.0.1/PKG-INFO +16 -0
- happymath-0.0.1/README.md +1 -0
- happymath-0.0.1/happymath.egg-info/PKG-INFO +16 -0
- happymath-0.0.1/happymath.egg-info/SOURCES.txt +12 -0
- happymath-0.0.1/happymath.egg-info/dependency_links.txt +1 -0
- happymath-0.0.1/happymath.egg-info/requires.txt +4 -0
- happymath-0.0.1/happymath.egg-info/top_level.txt +1 -0
- happymath-0.0.1/setup.cfg +4 -0
- happymath-0.0.1/setup.py +20 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .utils import *
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from sympy import dsolve
|
|
2
|
+
from IPython.display import display
|
|
3
|
+
from ...latex_trans import tex2sympy
|
|
4
|
+
from ..utils import is_linear_de,de_var_order,de_func
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def is_ode(ode_sympy):
|
|
10
|
+
var_list, _ = de_var_order(ode_sympy)
|
|
11
|
+
|
|
12
|
+
if not de_func(ode_sympy):
|
|
13
|
+
return False
|
|
14
|
+
if len(var_list) == 1:
|
|
15
|
+
return True
|
|
16
|
+
elif len(var_list) > 1:
|
|
17
|
+
return False
|
|
18
|
+
else:
|
|
19
|
+
return False
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def ode_analyzer(ode_object):
|
|
23
|
+
if type(ode_object) == str:
|
|
24
|
+
ode_object = tex2sympy(ode_object)
|
|
25
|
+
|
|
26
|
+
bool_ode = is_ode(ode_object)
|
|
27
|
+
bool_linear = is_linear_de(ode_object)
|
|
28
|
+
_, older_ode = de_var_order(ode_object)
|
|
29
|
+
|
|
30
|
+
if not bool_ode:
|
|
31
|
+
raise ValueError("这似乎并不是一个常微分方程,请仔细检查输入表达式!")
|
|
32
|
+
|
|
33
|
+
if bool_linear:
|
|
34
|
+
linear = "线性"
|
|
35
|
+
else:
|
|
36
|
+
linear = "非线性"
|
|
37
|
+
|
|
38
|
+
try:
|
|
39
|
+
res_solve = dsolve(ode_object)
|
|
40
|
+
bool_solve = True
|
|
41
|
+
except Exception as e:
|
|
42
|
+
bool_solve = False
|
|
43
|
+
|
|
44
|
+
if bool_solve:
|
|
45
|
+
print(f"这是一个{older_ode}阶{linear}常微分方程,方程具有解析解,为:")
|
|
46
|
+
display(res_solve)
|
|
47
|
+
return res_solve
|
|
48
|
+
else:
|
|
49
|
+
print(f"这是一个{older_ode}阶{linear}常微分方程,方程不具有解析解,考虑数值方法求解!")
|
|
50
|
+
return None
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import sympy
|
|
3
|
+
from ..latex_trans import tex2sympy
|
|
4
|
+
|
|
5
|
+
def de_func(de_sympy):
|
|
6
|
+
# 判断微分方程中未知函数
|
|
7
|
+
str_de = str(de_sympy)
|
|
8
|
+
pattern_func = "Derivative\((\S*), (\S*)"
|
|
9
|
+
cnt_find = re.findall("Derivative", str_de)
|
|
10
|
+
|
|
11
|
+
tmp_str = ""
|
|
12
|
+
tmp_de = str_de
|
|
13
|
+
|
|
14
|
+
for i in range(0, len(cnt_find)):
|
|
15
|
+
find_num = tmp_de.find("Derivative")
|
|
16
|
+
match_str = tmp_de[find_num + 10:]
|
|
17
|
+
|
|
18
|
+
cnt = 0
|
|
19
|
+
bool_str = []
|
|
20
|
+
check_sign = False
|
|
21
|
+
for check in list(match_str):
|
|
22
|
+
cnt += 1
|
|
23
|
+
if check == '(':
|
|
24
|
+
bool_tmp = True
|
|
25
|
+
bool_str.append(bool_tmp)
|
|
26
|
+
elif check == ')':
|
|
27
|
+
bool_str.pop()
|
|
28
|
+
|
|
29
|
+
if len(bool_str) == 0:
|
|
30
|
+
break
|
|
31
|
+
|
|
32
|
+
content = "Derivative" + tmp_de[find_num + 10:find_num + 10 + cnt]
|
|
33
|
+
match = re.search(pattern_func, content)
|
|
34
|
+
|
|
35
|
+
if tmp_str != "" and tmp_str != match.group(1):
|
|
36
|
+
return None
|
|
37
|
+
|
|
38
|
+
else:
|
|
39
|
+
tmp_str = match.group(1)
|
|
40
|
+
tmp_de = tmp_de.replace(content, "")
|
|
41
|
+
|
|
42
|
+
return tmp_str
|
|
43
|
+
|
|
44
|
+
def de_var_order(ode_sympy):
|
|
45
|
+
tex_ode = str(ode_sympy)
|
|
46
|
+
|
|
47
|
+
# 返回Derivative字段的起始和结束位置
|
|
48
|
+
pattern = "Derivative"
|
|
49
|
+
matches = re.finditer(pattern, tex_ode)
|
|
50
|
+
pos_pattern_start = []
|
|
51
|
+
pos_pattern_end = []
|
|
52
|
+
for match in matches:
|
|
53
|
+
start = match.start()
|
|
54
|
+
end = match.end()
|
|
55
|
+
pos_pattern_start.append(start)
|
|
56
|
+
pos_pattern_end.append(end)
|
|
57
|
+
|
|
58
|
+
# 存储完整的Derivative函数内容
|
|
59
|
+
bool_str = []
|
|
60
|
+
end_pos = []
|
|
61
|
+
for i in pos_pattern_end:
|
|
62
|
+
for j in range(i, len(tex_ode)):
|
|
63
|
+
if tex_ode[j] == '(':
|
|
64
|
+
bool_tmp = True
|
|
65
|
+
bool_str.append(bool_tmp)
|
|
66
|
+
elif tex_ode[j] == ')':
|
|
67
|
+
bool_str.pop()
|
|
68
|
+
|
|
69
|
+
if len(bool_str) == 0:
|
|
70
|
+
end_pos.append(j + 1)
|
|
71
|
+
break
|
|
72
|
+
|
|
73
|
+
der_list = []
|
|
74
|
+
for idx, k in enumerate(pos_pattern_start):
|
|
75
|
+
der_list.append(tex_ode[k:end_pos[idx]])
|
|
76
|
+
|
|
77
|
+
# 根据函数内容分情况输出ode的变量与阶数
|
|
78
|
+
older_max = 0
|
|
79
|
+
var_list = []
|
|
80
|
+
for der in der_list:
|
|
81
|
+
var_older_list = []
|
|
82
|
+
split_res = der[11:-1].split(", ")
|
|
83
|
+
var_older_list = [i.replace("(", "").replace(")", "") for i in split_res]
|
|
84
|
+
|
|
85
|
+
if len(var_older_list) - 1 == 1: ## 一阶单变量微分方程
|
|
86
|
+
older_max = max(1, older_max)
|
|
87
|
+
var_list.append(var_older_list[1])
|
|
88
|
+
else:
|
|
89
|
+
for var_oder in range(1, len(var_older_list)):
|
|
90
|
+
if var_older_list[var_oder].isdigit():
|
|
91
|
+
older_max = max(int(var_older_list[var_oder]), older_max)
|
|
92
|
+
else:
|
|
93
|
+
var_list.append(var_older_list[var_oder])
|
|
94
|
+
|
|
95
|
+
return list(set(var_list)), older_max
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def is_linear_de(de_sympy):
|
|
99
|
+
str_de = str(de_sympy)
|
|
100
|
+
fun_sym = de_func(de_sympy)
|
|
101
|
+
|
|
102
|
+
if not fun_sym:
|
|
103
|
+
raise ValueError("未知函数不存在,请检查方程定义是否正确!")
|
|
104
|
+
|
|
105
|
+
pattern = fun_sym + "*Derivative"
|
|
106
|
+
|
|
107
|
+
if str_de.find(pattern) == -1:
|
|
108
|
+
return True
|
|
109
|
+
else:
|
|
110
|
+
return False
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def apply_ics(res_od, ics: set, non_params: list = []):
|
|
114
|
+
# 根据解析解和初始条件求得特殊解
|
|
115
|
+
|
|
116
|
+
ics_pattern = r"(\S*)((\d+))"
|
|
117
|
+
var = list(res_od.lhs.free_symbols)[0]
|
|
118
|
+
trans_non = non_params
|
|
119
|
+
|
|
120
|
+
var_ics = []
|
|
121
|
+
trans_dic = {}
|
|
122
|
+
keys_list = list(ics.keys())
|
|
123
|
+
values_list = list(ics.values())
|
|
124
|
+
|
|
125
|
+
for i in range(0, len(keys_list)):
|
|
126
|
+
match = re.search(ics_pattern, str(keys_list[i]))
|
|
127
|
+
if match:
|
|
128
|
+
ics_value = match.group(2)
|
|
129
|
+
|
|
130
|
+
if type(keys_list[i]) == str:
|
|
131
|
+
tex_key = tex2sympy(keys_list[i])
|
|
132
|
+
else:
|
|
133
|
+
tex_key = keys_list[i]
|
|
134
|
+
|
|
135
|
+
values = values_list[i]
|
|
136
|
+
if type(values) == str:
|
|
137
|
+
value_tex = tex2sympy(values)
|
|
138
|
+
var_ics.append(value_tex)
|
|
139
|
+
|
|
140
|
+
var_ics.append(values)
|
|
141
|
+
trans_dic[tex_key] = values
|
|
142
|
+
|
|
143
|
+
if non_params:
|
|
144
|
+
trans_non = []
|
|
145
|
+
for j in non_params:
|
|
146
|
+
if type(j) == str:
|
|
147
|
+
tmp_tex = tex2sympy(j)
|
|
148
|
+
else:
|
|
149
|
+
tmp_tex = j
|
|
150
|
+
trans_non.append(tmp_tex)
|
|
151
|
+
|
|
152
|
+
apply_symbols = res_od.free_symbols - set(trans_non)
|
|
153
|
+
eqs = [(res_od.lhs.diff(var, n) - res_od.rhs.diff(var, n)).subs(var, ics_value).subs(trans_dic) for n in
|
|
154
|
+
range(len(ics))]
|
|
155
|
+
sol_params = sympy.solve(eqs, apply_symbols)
|
|
156
|
+
return res_od.subs(sol_params)
|
happymath-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [year] [fullname]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
happymath-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: happymath
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Test vsrsion for happymath
|
|
5
|
+
Author: Zou
|
|
6
|
+
Author-email: 1514117376@qq.com
|
|
7
|
+
License: MIT
|
|
8
|
+
Classifier: Framework :: Pytest
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Topic :: Software Development :: Testing
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: sympy
|
|
14
|
+
Requires-Dist: IPython
|
|
15
|
+
Requires-Dist: latex2sympy2
|
|
16
|
+
Requires-Dist: importlib
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This just a test module for happymath, coming soon...
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: happymath
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Test vsrsion for happymath
|
|
5
|
+
Author: Zou
|
|
6
|
+
Author-email: 1514117376@qq.com
|
|
7
|
+
License: MIT
|
|
8
|
+
Classifier: Framework :: Pytest
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Topic :: Software Development :: Testing
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: sympy
|
|
14
|
+
Requires-Dist: IPython
|
|
15
|
+
Requires-Dist: latex2sympy2
|
|
16
|
+
Requires-Dist: importlib
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
Differential_Equation/__init__.py
|
|
5
|
+
Differential_Equation/utils.py
|
|
6
|
+
Differential_Equation/ODE/__init__.py
|
|
7
|
+
Differential_Equation/ODE/utils.py
|
|
8
|
+
happymath.egg-info/PKG-INFO
|
|
9
|
+
happymath.egg-info/SOURCES.txt
|
|
10
|
+
happymath.egg-info/dependency_links.txt
|
|
11
|
+
happymath.egg-info/requires.txt
|
|
12
|
+
happymath.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Differential_Equation
|
happymath-0.0.1/setup.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from setuptools import setup,find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name='happymath',
|
|
5
|
+
version='0.0.1',
|
|
6
|
+
author="Zou",
|
|
7
|
+
author_email='1514117376@qq.com',
|
|
8
|
+
description='Test vsrsion for happymath',
|
|
9
|
+
classifiers=[# 分类索引 ,pip 对所属包的分类
|
|
10
|
+
'Framework :: Pytest',
|
|
11
|
+
'Programming Language :: Python',
|
|
12
|
+
'Topic :: Software Development :: Testing',
|
|
13
|
+
'Programming Language :: Python :: 3.8',
|
|
14
|
+
],
|
|
15
|
+
license='MIT',
|
|
16
|
+
# 需要安装的依赖
|
|
17
|
+
install_requires=[
|
|
18
|
+
'sympy', 'IPython', 'latex2sympy2', 'importlib',
|
|
19
|
+
]
|
|
20
|
+
)
|