-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonomial_form.py
85 lines (73 loc) · 3.04 KB
/
monomial_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
from __future__ import division
from sympy.functions.elementary.trigonometric import TrigonometricFunction
from sympy.functions.elementary.trigonometric import InverseTrigonometricFunction
import sympy
from sympy import Add,Mul,rcollect,Number,NumberSymbol,sin,cos,Pow,Integer,Symbol,gcd,div
from .singleton_form import *
from .form_utils import *
def is_monomial_form(expr):
'''Determines whether an expression is in proper monomial form.
Monomials are defined as either a singleton or a single product of
singletons, where singletons are optionally raised to a power.
Args:
expr: A standard Sympy expression
Returns:
A tuple containing:
[0]: bool containing the result
[1]: string describing the result
'''
if is_singleton_form(expr)[0]:
return True, MonomialOutput.strout("IS_SINGLETON")
if is_numerically_reducible_monomial(expr)[0]:
return False, MonomialOutput.strout("REDUCIBLE")
if isinstance(expr,Pow):
if is_singleton_form(expr.args[0])[0]:
if expr.args[1] == 0 or expr.args[1] == 1:
return False, MonomialOutput.strout("REDUCIBLE")
if is_singleton_form(expr.args[1])[0]:
return True, MonomialOutput.strout("PROPER")
return False, MonomialOutput.strout("IMPROPER")
return False, MonomialOutput.strout("EXPANDABLE")
elif isinstance(expr,Add):
return False, MonomialOutput.strout("MULTIPLE_TERMS")
elif isinstance(expr,Mul):
for j in expr.args:
if expr.args == 0:
return False, MonomialOutput.strout("REDUCIBLE")
result = is_monomial_form(j)
if not result[0]:
return False, result[1]
result = duplicate_bases(expr)
return not result[0], result[1]
def duplicate_bases(expr):
'''Returns if any bases in a monomial are duplicates
Preconditions: monomial is otherwise well-formed.
Args:
expr: A standard Sympy expression
Returns:
A tuple containing:
[0]: bool containing the result
[1]: string describing the result
'''
bases = _search_bases(expr)
#Set only collects unique bases
if len(bases) != len(set(bases)):
return True, MonomialOutput.strout("REDUCIBLE")
return False, MonomialOutput.strout("PROPER")
def _search_bases(expr):
'''Searches through the bases in an expression. This function is \
exclusively used by duplicate_bases.
Args:
expr: A standard Sympy expression
Returns:
A set with all of the bases in the expression
'''
exprbases = []
for i in range(0, len(expr.args)):
if isinstance(expr.args[i], Pow):
exprbases.append(expr.args[i].args[0])
elif isinstance(expr.args[i],Mul):
exprbases += _search_bases(expr.args[i])
else: #Trig/InverseTrig functions, Add for factored exprs
exprbases.append(expr.args[i])
return exprbases