Cleaned that code up a bit and made the var names a litle bit more
meaningful, seems to work
MyRing = QQ
Metric = diagonal_matrix(MyRing,[1,-2,-3,-5])
nn = Metric.nrows()
PRGA = FreeAlgebra(MyRing,nn,'g')
F = PRGA.monoid()
gen_str = str(PRGA.gens())
gen_str = gen_str[1:len(gen_str)-1]
exec gen_str + ' = F.gens()'
def SignChange(bit_str,mm):
mysign = 0
for ii in range(nn-mm-1):
mysign = mysign+Integer(bit_str[ii])
return (-1)^mysign
def MonProduct(mon_list):
mon_prod = mon_list[0]
for jj in range(1,len(mon_list)):
mon_prod = mon_prod*mon_list[jj]
return mon_prod
def MonList(bit_str):
mons_bit_rep = []
for jj in range(nn):
if bit_str[jj]=='0':
mons_bit_rep.append(F(1))
else:
mons_bit_rep.append(F.gen(nn-jj-1))
return mons_bit_rep[::-1]
def Coeff(mylist,kk):
if ('0'==mylist[kk][nn-kk-1]):
return Metric[kk,kk]
else:
return 1
def MonsMats():
mons_str = []
mons_bit_rep = []
mons_xgen = []
mons_xgen_coeff = []
mons=[]
for ii in range(2**nn):
bit_str = Integer(ii+nn^2).binary()[1:nn+1] #reversed ex. '1011'
corresponds to g0*g1*g3 etc
mons_bit_rep.append(bit_str)
mon_list = MonList(bit_str)
mon_prod = MonProduct(mon_list)
mons.append(mon_prod)
mons_str.append(str(mon_prod))
mons_bit_xgen = [Integer((ii+nn^2)^^(2^kk)).binary()[1:nn+1] for kk
in range(nn)]
mons_xgen.append([MonProduct(MonList(mons_bit_xgen[kk])) for kk in
range(nn)])
mons_xgen_coeff.append([SignChange(bit_str,kk)*Coeff(mons_bit_xgen,kk) for
kk in range(nn)])
mons_str_sorted = sorted(sorted(mons_str),key=len)
mons_bit_rep = [mons_bit_rep[[mons_str.index(mons_str_sorted[kk]) for
kk in range(len(mons_str))][ll]] for ll in range(len(mons_str))]
mons_xgen = [mons_xgen[[mons_str.index(mons_str_sorted[kk]) for kk in
range(len(mons_str))][ll]] for ll in range(len(mons_str))]
mons_xgen_coeff = [mons_xgen_coeff[[mons_str.index(mons_str_sorted[kk])
for kk in range(len(mons_str))][ll]] for ll in range(len(mons_str))]
mons = [mons[[mons_str.index(mons_str_sorted[kk]) for kk in
range(len(mons_str))][ll]] for ll in range(len(mons_str))]
mats = []
for ii in range(nn):
Struct_Matrix = matrix(MyRing,nn^2,nn^2,0)
for jj in range(len(mons_str)):
for kk in range(len(mons_str)):
if mons_xgen[jj][ii]==mons[kk]:
Struct_Matrix[jj,kk] = mons_xgen_coeff[jj][ii]
mats.append(Struct_Matrix)
return [mons, mats]
mons_mats = MonsMats()
exec preparse('ST.<' + gen_str +
'>=FreeAlgebraQuotient(PRGA,mons_mats[0],mons_mats[1])')
On Mon, Jul 28, 2014 at 8:07 AM, Stephen Kauffman <[email protected]>
wrote:
> If anyone is curious here's my code so far unvetted to create clifford
> algebra with free algebra quotient: I need to add some comments...
>
> nn=4
> MyRing=QQ
> Metric = diagonal_matrix(MyRing,[1,-2,-3,-5])
> PRGA=FreeAlgebra(MyRing,nn,'g')
>
> F = PRGA.monoid()
> MyStr=str(PRGA.gens())
> MyStr=MyStr[1:len(MyStr)-1]
> exec MyStr+' = F.gens()'
> #MG=matrix(F.gens())
> #MGG=MG.transpose()*MG
> #MGGM=MGG+MGG.transpose()-2*Metric
> def cmmm(ii,jj):
> if ii!=jj:
> return (Metric-MGG.transpose())[ii,jj]
> else:
> return Metric[ii,jj]
> def MyDeg2(MyIntStr):
> mydeg=0
> for ii in range(len(MyIntStr)):
> mydeg=mydeg+Integer(MyIntStr[ii])
> return mydeg
> def MySignDelta(MyIntStr,mm):
> mysign=0
> for ii in range(nn-mm-1):
> mysign=mysign+Integer(MyIntStr[ii])
> return (-1)^mysign
> def MyProduct(MyList):
> myprod=MyList[0]
> for jj in range(1,len(MyList)):
> myprod=myprod*MyList[jj]
> return myprod
> def MyMonList(MyIntStr):
> MyList2=[]
> for jj in range(nn):
> if MyIntStr[jj]=='0':
> MyList2.append(F(1))
> else:
> MyList2.append(F.gen(nn-jj-1))
> return MyList2[::-1]
> def MyCoeff(mylist3,kk):
> if ('0'==mylist3[kk][nn-kk-1]):
> return Metric[kk,kk]
> else:
> return 1
> MyList1=[]
> MyList2=[]
> MyList4=[]
> MyList5=[]
> MyList7=[]
> def runthis():
> for ii in range(2**nn):
> MyIntStr=Integer(ii+nn^2).binary()[1:nn+1]
> MyList2.append(MyIntStr)
> MyList=MyMonList(MyIntStr)
> myprod=MyProduct(MyList)
> MyList7.append(myprod)
> MyList1.append(str(myprod))
> MyList3=[Integer((ii+nn^2)^^(2^kk)).binary()[1:nn+1] for kk in
> range(nn)]
> MyList4.append([MyProduct(MyMonList(MyList3[kk])) for kk in
> range(nn)])
> MyList5.append([MySignDelta(MyIntStr,kk)*MyCoeff(MyList3,kk) for
> kk in range(nn)])
> runthis()
> MyList6=sorted(sorted(MyList1),key=len)
> MyList2=[MyList2[[MyList1.index(MyList6[kk]) for kk in
> range(len(MyList1))][ll]] for ll in range(len(MyList1))]
> MyList4=[MyList4[[MyList1.index(MyList6[kk]) for kk in
> range(len(MyList1))][ll]] for ll in range(len(MyList1))]
> MyList5=[MyList5[[MyList1.index(MyList6[kk]) for kk in
> range(len(MyList1))][ll]] for ll in range(len(MyList1))]
> MyList7=[MyList7[[MyList1.index(MyList6[kk]) for kk in
> range(len(MyList1))][ll]] for ll in range(len(MyList1))]
> mats=[]
> for ii in range(nn):
> MyMatrix=matrix(MyRing,nn^2,nn^2,0)
> for jj in range(len(MyList1)):
> for kk in range(len(MyList1)):
> if MyList4[jj][ii]==MyList7[kk]:
> MyMatrix[jj,kk]=MyList5[jj][ii]
> mats.append(MyMatrix)
> #ST=FreeAlgebraQuotient(PRGA,MyList6,mats, MyStr)
> #exec 'ST.<'+MyStr+'> = FreeAlgebraQuotient(PRGA,MyList6,mats)'
> #exec MyStr+' = ST.gens()'
>
>
> On Mon, Jul 28, 2014 at 7:59 AM, Stephen Kauffman <[email protected]>
> wrote:
>
>> The mons list MyList6 was in the original free algebra generators, but I
>> managed to fix it so it's in the monoid F generators and that fixed the
>> first error AttributeError: 'FreeAlgebra_generic_with_
>> category.element_class' object has no attribute '_element_list' My
>> matrices were transposed but I fixed that also. Checking the behavior of
>> the results now.
>>
>> The other good answer for Clifford Algebras was from David Joyner to use
>> sympy packages
>>
>> plenty to read in http://docs.sympy.org/latest/modules/galgebra/GA.html
>>
>> import sympy
>> from sympy import *
>> from sympy.galgebra import *
>> from sympy.galgebra.ga import *
>>
>> Fully featured but every statement has to be predicated in sage with a
>> print statement to display the result and the sympy package doesn't
>> dovetail with sage polynomial and quotient rings if you what to define
>> something like g^2(1-b^2)=1 or a^2+b^2+c^2=1 with quotients. You get
>> incompatible operand errors when you b*(sympyGAvector). Hopefully the free
>> algebra solution can be created over these quotient and polynomial rings
>> but I would have to reinvent all the myriad Clifford algebra operations...
>>
>>
>>
>> On Mon, Jul 28, 2014 at 1:21 AM, Nils Bruin <[email protected]> wrote:
>>
>>> On Sunday, July 27, 2014 8:40:50 PM UTC-7, Stephen Kauffman wrote:
>>>>
>>>> Thanks for your help but I think I need more. I've written some code
>>>> for a somewhat general case of n orthogonal generators and an arbitrary
>>>> diagonal metric and I think I've generated the correct 16x16 matrices for
>>>> my n=4 case.
>>>>
>>>
>>> I think you're mainly running into python syntax issues here. For the
>>> most part, whenever you use "exec" in a computer algebra package, you're
>>> doing something wrong. There is probably a better way of passing around
>>> information (mind you, sometimes there's not. You're still doing something
>>> wrong, but the package might not give you the tools to do it right).
>>>
>>>
>>>> When I finish running the .sage file and return to sage I execute
>>>>
>>>> F = PRGA.monoid()
>>>> MyStr=str(PRGA.gens())
>>>> MyStr=MyStr[1:len(MyStr)-1]
>>>> exec MyStr+' = F.gens()'
>>>> ST.<g0,g1,g2,g3>=FreeAlgebraQuotient(PRGA,MyList6,mats)
>>>> ST
>>>> Free algebra quotient on 4 generators ('g0', 'g1', 'g2', 'g3') and
>>>> dimension 16 over Rational Field
>>>>
>>>> but when I do
>>>>
>>>> g3*g3 # or ST.gen(3)*ST.gen(3)
>>>> AttributeError: 'FreeAlgebra_generic_with_category.element_class'
>>>> object has no attribute '_element_list'
>>>>
>>>
>>> I can't reproduce this error because you're not telling what MyList6 and
>>> mats are. If I set
>>>
>>> sage: g0,g1,g2,g3 = F.gens()
>>> sage: MyList6 = [ F(1), g0, g1, g2, g3, g0*g1, g0*g2, g0*g3, g1*g2,
>>> g2*g3, g3*g1, g0*g1*g2*g3*g0, g0*g1*g2*g3*g1, g0*g1*g2*g3*g2,
>>> g0*g1*g2*g3*g3, g0*g1*g2*g3]
>>> sage: mats=[matrix(QQ,16,16,1) for j in range(4)]
>>> sage: ST.<g0,g1,g2,g3>=FreeAlgebraQuotient(PRGA,MyList6,mats)
>>> sage: ST.gen(3)*ST.gen(3)
>>> g3
>>>
>>> I get no error (I do get a rather nonsensical result because I
>>> initialized the matrices to nonsense).
>>>
>>>
>>>> Further when I try to automate with the generated string MyStr='g0, g1,
>>>> g2, g3' and within the .sage file
>>>>
>>>> exec 'ST.<'+MyStr+'> = FreeAlgebraQuotient(PRGA,MyList6,mats)'
>>>>
>>>> Traceback (most recent call last):
>>>> File "<string>", line 1
>>>> ST.<g0, g1, g2, g3> = FreeAlgebraQuotient(PRGA,MyList6,mats)
>>>> ^
>>>> SyntaxError: invalid syntax
>>>>
>>>
>>> That's because "exec" is just python's "exec" and you're giving syntax
>>> that needs sage's preparser. The ST.<g0> syntax is not valid python. To see
>>> how it converts:
>>>
>>> sage: preparse("ST.<g0,g1,g2,g3> = FreeAlgebraQuotient(PRGA, mons,
>>> mats)")
>>> "ST = FreeAlgebraQuotient(PRGA, mons, mats, names=('g0', 'g1', 'g2',
>>> 'g3',)); (g0, g1, g2, g3,) = ST._first_ngens(4)"
>>>
>>> which also helps you for your next error:
>>>
>>>
>>>> If I try:
>>>>
>>>> ST=FreeAlgebraQuotient(PRGA,MyList6,mats, names='g0, g1, g2, g3') # or
>>>> anything else I try in names
>>>>
>>>> Traceback (most recent call last):
>>>> ValueError: first letter of variable name must be a letter
>>>>
>>>
>>> As the preparse command shows, it should work if you set names to a
>>> tuple (really, an iterable) of strings. A nasty design choice is that
>>> strings themselves are iterables, which produce their individual
>>> characters. Compare
>>>
>>> sage: [x for x in ('g0', 'g1', 'g2', 'g3')]
>>> ['g0', 'g1', 'g2', 'g3']
>>> sage: [x for x in "g0, g1, g2, g3"]
>>> ['g', '0', ',', ' ', 'g', '1', ',', ' ', 'g', '2', ',', ' ', 'g', '3']
>>>
>>> Hence the error message: '0' is not a valid generator name.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "sage-support" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/sage-support.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>
--
You received this message because you are subscribed to the Google Groups
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.