I have fixed up this problem with myself. I substitute scipy.poly1d for sympy
polynomials.
------------------------------
Coefficients of sympy's polynomial are restrained for Rational,int, float and
complex number. Below code indicate this restriction.
//@@
import sympy as ts
x = ts.Symbol('x')
print x + x
//@@@
2*x
So I decide to use scipy.poly1d. It can manipulate Bool Filed polynomials as in
below code.
//@@
import scipy as sc
# Bool Field
class BF(object):
"""' Bool Field: meber is 1 or 0
1 * 0 = 0 # and
1 * 1 = 1
0 * 0 = 0
1 + 0 = 1 # xor
1 + 1 = 0
0 + 0 = 0
'"""
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):
if '__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, inAg):
#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
def __pow__(self, inAg):
"""'
I had introduced power operator. But there may be no need. I couldn't
imagine any situation where bool power operator is usefull
'"""
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"
"""' __abs__() and __lt__() is used in poly1d.__str__(..)
'"""
def __abs__(self):
return self.m_val
def __lt__(self, inAg):
return False
B1 = BF(1)
B0 = BF(0)
plAt = sc.poly1d([B1,B1])
plAt2 = plAt**2
print plAt2
print sc.poly1d([B1, B0, B1])**2
print sc.poly1d([B1, B0, B1])*sc.poly1d([B1, B0, B0, B1])
#print sc.poly1d([B1, B0, B0, B1])/sc.poly1d([B1,B0, B1])
//@@@
python temp.py
2
1 x + 1
4
1 x + 1
5 3 2
1 x + 1 x + 1 x + 1
-------------------------------------
Thanks.
--
Kenji
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---