Hi all,
I want to use symbolic polynomials with Field Coefficients that are not int,
real or complex numbers.
They are usefull in some genres. For example polynomials with bool field
coefficients are usefull in degital circuit designs
I tested a code in the last appendix section with ver 0.6.3 sympy. And sympy
polynomial couldn't manipulate Bool Field coefficients.
Is there a smart mesure to manipulate Bool coefficient polynomials in sympy?
Thanks in advance for your help!
--
Kenji
--------- appendix beginning --------------
//@@
import sympy as ts
import sf
class BF(ts.Rational):
#class BF(object):
def __init__(self, inAg):
if isinstance(inAg,int):
self.m_val = inAg%2
elif isinstance(inAg,BF):
self.m_val = inAg.m_val
else:
assert False
def __int__(self):
return self.m_val
# array needs __long__
# e.g. ;;mt = kzrs(3,3,int); mt[0,1] = oc.BF(1);mt
def __long__(self):
return self.m_val
def __add__(self, inAg):
assert isinstance(inAg,(int, BF) )
return BF(self.m_val + (int(inAg) % 2))
def __radd__(self, inAg):
return self.__add__(inAg)
def __sub__(self, inAg):
return self.__add__(inAg)
def __mul__(self, inAg):
#assert isinstance(inAg,(int, BF) )
if isinstance(inAg, sf.ClFldTns):
return inAg.__rmul__(self)
elif isinstance(inAg, ts.Symbol) or isinstance(inAg, ts.core.power.Pow):
if self.m_val % 2 == 0:
return 0
else:
return inAg
elif '__int__' in dir(inAg):
return BF(self.m_val * int(inAg))
else:
raise TypeError(" argment:" + str(inAg) +" at octn.BF.__mul__(..)")
def __div__(self, inAg):
assert isinstance(inAg,(int, BF) )
if ( int(inAg) % 2 == 0):
raise ZeroDivisionError("0 divition at __div__(.)")
else:
return BF(self.m_val)
def __truediv__(self, inAg):
#return __div__(self, inAg)
return self.__div__(inAg)
def __rdiv__(self, ag):
#return __div__(self, inAg)
return self.__div__(inAg)
def inv(self):
assert self.m_val != 0
return BF(self)
def __eq__(self, ag):
# assert isinstance(ag,(int, BF) )
# at "BF(1) in (bool, int, float) " ag == bool ... and upper error
if '__int__' in dir(ag) and not isinstance(ag, type):
return self.m_val == int(ag)%2
else:
return False
"""'
I had introduced power operator. But there may be no need. I couldn't
imagine any situation where bool power operator is usefull
'"""
def __pow__(self, inAg):
assert isinstance(inAg,(int, BF) )
if int(inAg) != 0:
return BF(self.m_val)
else:
return BF(1)
def __str__(self):
if self.m_val == 0:
return "0"
else:
return "1"
x = ts.Symbol('x')
B1 = BF(1)
print ((B1 * x**2 + B1)**2).expand()
"""'
print ((B1 * x**2 + B1)**2)
print ((B1 * x**2 + B1)*(B1*x**3+B1))
print ((B1 * x**2 + B1)*(B1*x**3+1))
print ((B1 * x**2 + B1)*(B1*x**3+1)).expand()
print ((B1*x**3+1)/(B1 * x**2 + B1)).expand()
print ((x**2 + x + B1)*(x**3+1)).expand()
print ((x**2 + x + 1)*(x**3+1)).expand()
print ((x**2 + 1)**2).expand()
print ((x**2 + B1)**2).expand()
print ((B1*x**2 + B1)**2).expand()
'"""
//@@@
1 + 2*x**2 + x**4
<== I want the result:1 + x**4
--------- appendix end --------------
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---