forked from b-97/sympy-form-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial_form.py
251 lines (214 loc) · 9.14 KB
/
polynomial_form.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from __future__ import division
from sympy.functions.elementary.trigonometric import TrigonometricFunction
from sympy.functions.elementary.trigonometric import InverseTrigonometricFunction
import sympy
from sympy import Poly
from sympy import ZZ,CC,QQ,RR
from sympy import Add,Mul,rcollect,Number,NumberSymbol,sin,cos,Pow,Integer,Symbol,gcd,div,degree, Derivative, discriminant, primitive, real_roots, sieve
from .monomial_form import *
from .form_utils import *
from .form_output import *
def is_fully_expanded_polynomial(expr, eval_trig=False):
'''Determines if a proper polynomial is fully expanded.
A polynomial that is fully expanded is defined as a sum of monomials
that cannot be expanded further.
Args:
expr: A standard sympy expression
Returns:
a tuple containing:
[0] - boolean result of the function
[1] - string describing the result
'''
result = is_monomial_form(expr)
if is_monomial_form(expr)[0]:
return True, result[1]
elif not isinstance(expr, Add):
return False, result[1]
result = const_divisible(expr)
if result[0]:
return False, result[1]
if all(is_monomial_form(i)[0] for i in expr.args):
return True, PolynomialOutput.strout("EXPANDED")
return False, PolynomialOutput.strout("NOT_EXPANDED")
def is_fully_factored_polynomial(expr, eval_trig=False, domain='RR'):
'''Determines if a proper polynomial is fully expanded.
A polynomial that is fully factored is defined as a sum or product of
polynomials that cannot be reduced further.
Args:
expr: A standard sympy expression
domain: (optional) determination of the field that the polynomial \
is to be either reducible or irreducible over. Domain \
specification is determined by two capital leterts to match \
sympy's style.
Options:
'RR' - Real numbers
'CC' - Complex numbers
TODO:
'QQ' - Rationals
'ZZ' - Integers
Returns:
a tuple containing:
[0] - boolean result of the function
[1] - string describing the result
'''
#If the expression is already a monomial or a singleton in the desired form
if is_monomial_form(expr)[0]:
return True, PolynomialOutput.strout("IS_MONOMIAL")
#Next, we check to see if individual terms in the polynomial are numerically
#reducible (i.e, 3/3, x/x x^2/x, etc.)
for i in mr_polynomial_terms(expr):
result = is_numerically_reducible_monomial(i)
if result[0]:
return False, result[1]
#Currently, no definition of polynomials allows for monomials that
#are combinable by integers or by bases, so we can filter those out
result = const_divisible(expr)
if result[0]:
return False, result[1]
#Finally, we analyze the reducibility of the polynomial according to the
#domain the user specified.
if domain == 'RR' or domain == RR:
result = real_field_reducible(expr)
return not result[0], result[1]
elif domain == 'CC' or domain == CC:
result = complex_field_reducible(expr)
return not result[0], result[1]
elif domain == 'ZZ' or domain == ZZ:
result = integer_field_reducible(expr)
return not result[0], result[1]
elif domain == 'QQ' or domain == QQ:
result = rational_field_reducible(expr)
return not result[0], result[1]
else:
return False, ErrorOutput.strout("ERROR")
def is_integer_content_free_polynomial(expr):
'''Determines if a polynomial is content-free. A polynomial that has
content is defined to have an integer gcd between all monomials that
is not equal to 1. Will always return false if there is only one term
in the expression,
Args:
expr: A standard sympy expression
Returns:
A tuple containing:
[0] - boolean result of the function
[1] - string describing the result
[2] - integer content of the polynomial
'''
if not isinstance(expr, Add):
return True, PolynomialOutput.strout("CONTENTFREE_MONOMIAL"), 1
result = primitive(expr)
if primitive(expr)[0] != 1:
return False, PolynomialOutput.strout("NOT_CONTENTFREE"), primitive(expr)[0]
return True, PolynomialOutput.strout("CONTENTFREE"), 1
def complex_field_reducible(expr):
'''Determines if the polynomial is reducible over the complex field.
According to the fundamental theorem of algebra, a polynomial is reducible
if and only if the degree is one. However, for this library, we won't count
monomials such as x^4, as being reducible.
Args:
expr: a standard Sympy expression
Returns:
a tuple containing:
[0] - boolean result of the function
[1] - string describing the result
'''
result = is_monomial_form(expr)
if result[0]:
return False, PolynomialOutput.strout("IS_MONOMIAL")
if isinstance(expr, Mul):
for i in expr.args:
result = complex_field_reducible(i)
if result[0]:
return result
return False, PolynomialOutput.strout("COMPLEX_FACTORED")
if isinstance(expr, Pow):
return complex_field_reducible(expr.args[0])
if degree(expr) > 1:
return True, PolynomialOutput.strout("COMPLEX_HIGH_DEGREE")
return False, PolynomialOutput.strout("COMPLEX_FACTORED")
def real_field_reducible(expr):
'''Determines if the polynomial is reducible over the real field.
According to the fundamental theorem of algebra, a polynomial is reducible
if and only if the following criterion are met:
1: Degree of polynomial is less than 3.
2: If degree of polynomial is 2, at least one of the roots are in
the complex field.
However, for this library, we won't count monomials, such as x^4,
as being reducible.
Args:
expr: a standard Sympy expression
Returns:
a tuple containing:
[0] - boolean result of the function
[1] - string describing the result
'''
result = is_monomial_form(expr)
if result[0]:
return False, PolynomialOutput.strout("IS_MONOMIAL")
if isinstance(expr, Mul):
for i in expr.args:
result = real_field_reducible(i)
if result[0]:
return result
return False, PolynomialOutput.strout("REAL_FACTORED")
if isinstance(expr, Pow):
return real_field_reducible(expr.args[0])
if degree(expr) > 2:
return True, PolynomialOutput.strout("REAL_HIGH_DEGREE")
if degree(expr) == 2 and discriminant(expr) >= 0:
return True, PolynomialOutput.strout("REAL_FACTORABLE_QUAD")
return False, PolynomialOutput.strout("REAL_FACTORED")
def integer_field_reducible(expr):
'''Determines if the polynomial is reducible over the field of integers.
A polynomial reducible ver the integers is one that has more than two \
integer roots or has integer content that can be factored.
However, for this library, we wholly exclude monomials, such as x^4,
as being reducible.
Args:
expr: a standard Sympy expression
Returns:
a tuple containing:
[0] - boolean result of the function
[1] - string describing the result
'''
result = is_monomial_form(expr)
if result[0]:
return False, PolynomialOutput.strout("IS_MONOMIAL")
if isinstance(expr, Add):
result = is_integer_content_free_polynomial(expr)
if not result[0]:
return True, result[1]
if isinstance(expr, Mul):
for i in expr.args:
result = integer_field_reducible(i)
if result[0]:
return result
if Poly(expr, domain=ZZ).is_irreducible:
return False, PolynomialOutput.strout("INTEGER_FACTORED")
return True, PolynomialOutput.strout("INTEGER_REDUCIBLE")
def rational_field_reducible(expr):
'''Determines if the polynomial is reducible over the field of integers.
A polynomial reducible over the rationals is one that has more than \
two rational roots or has rational content that can be factored.
However, for this library, we will wholly exclude monomials, such as x^4,
as being reducible.
Args:
expr: a standard Sympy expression
Returns:
a tuple containing:
[0] - boolean result of the function
[1] - string describing the result
'''
result = is_monomial_form(expr)
if result[0]:
return False, PolynomialOutput.strout("IS_MONOMIAL")
if isinstance(expr, Mul):
for i in expr.args:
result = rational_field_reducible(i)
if result[0]:
return result
if isinstance(expr, Pow):
return rational_field_reducible(expr.args[0])
if Poly(expr, domain=QQ).is_irreducible:
return False, PolynomialOutput.strout("RATIONAL_FACTORED")
return True, PolynomialOutput.strout("RATIONAL_REDUCIBLE")