running code twice generate error
Hi
W11 ,WSL ,Ubuntu 22.04.4 LTS,SageMath 10.2 (same PB with 10.0) In Jupyter notebook
When I run this code twice (first time is ok) , I get the error about the matrix : TypeError: no canonical coercion from Polyhedra in QQ^3 to Symbolic Ring (I need to reset SageMath kernel to run again without error)
But on sageCell I can run this code many times without getting error. So 3 questions :
Q1 : is there a SageMath kernel reset every time before SageCell code is executed ?
Q2 : Do you see something wrong I do in this code ?
Q3 : If no Q2 , is this the same problem for you ?
# functions
def polyhedronCentroid(vertices):
# vertices: List of tuples (x, y, z,...) representing the polyhedron vertices dim could be >3
num_vertices = len(vertices)
centroid = [sum(coord[i] for coord in vertices) / num_vertices for i in range(len(vertices[0]))]
return centroid
def translateStructure(structDic,pt):
newDic={}
for k in structDic.keys() :
newDic[k]=list( vector(structDic.get(k))+vector(pt) )
return newDic
def rotateStructure(structDic,angleRot,axisRot,sameCenter) :
global rotX,rotY,rotZ
if axisRot=='x' : # rotation around x axis
rotM=matrix( QQ,rotX.subs(t_x=angleRot ) )
elif axisRot=='y' : # rotation around y axis
rotM=matrix( QQ,rotY.subs(t_y=angleRot ) )
elif axisRot=='z' : # rotation around z axis
rotM=matrix( QQ, rotZ.subs(t_z=angleRot ) )
else :
print('something wrong for axis rotation')
return cubeDic
show('rotM : ',rotM)
newTDic={}
for k in structDic.keys() :
newTDic[k]=list(rotM*vector(list(structDic.get(k))))
if sameCenter :
centerV=vector(polyhedronCentroid(list(structDic.values())))
translatePt=list(vector(centerV-(rotM*centerV)))# reset center at old structure location
newTDic=translateStructure(newTDic,translatePt)
else :
return(newTDic)
#show('newDic : ' ,newDic)
#show('newTDic : ',newTDic)
return newTDic
def addAxis(plt,textToPlt,xBound,yBound,zBound,wA,rA) :
# add axis for real part
plt += arrow3d((0, 0, 0), (xBound, 0, 0), color='violet',width=wA,radius=rA)
plt += text3d("ex", (xBound, 0, 0), fontsize='200%', color='violet')
plt += arrow3d((0, 0, 0), (0, yBound, 0), color='gray',width=wA,radius=rA)
plt += text3d("ey", (0, yBound, 0), fontsize='200%', color='gray')
plt += arrow3d((0, 0, 0), (0, 0, zBound), color='pink',width=wA,radius=rA)
plt += text3d("ez", (0,0, zBound), fontsize='200%', color='pink')
plt+=textToPlt
return plt
###################################################################################
##################### end functions area ##########################################
var('t_x',latex_name=r"\\teta_{x}")
var('t_y',latex_name=r"\\teta_{y}")
var('t_z',latex_name=r"\\teta_{z}")
rotX=matrix(SR,[[1,0,0],[0,cos(t_x),-sin(t_x)],[0,sin(t_x),cos(t_x)]])
rotY=matrix(SR,[[cos(t_y),0,sin(t_y)],[0,1,0],[-sin(t_y),0,cos(t_y)]])
rotZ=matrix(SR,[[cos(t_z),-sin(t_z),0],[sin(t_z),cos(t_z),0],[0,0,1]])
xBound=1.5 ;yBound=1.5 ;zBound=1.5
shiftV=vector([1/10,1/10,1/10])
width=0.2 ;radius=0.02
cubeAdic={0: [0, 0, 0], 1: [1, 0, 0], 2: [1, 1, 0], 3: [0, 1, 0] , \
4: [0, 0, 1], 5: [1, 0, 1], 6: [1, 1, 1], 7: [0, 1, 1]}
cubeArevDic=dict(zip([str(cubeAdic.get(k)) for k in cubeAdic.keys()],[k for k in cubeAdic.keys()]))
cubeAsimplex3=[[0, 4, 5, 7], [0, 1, 2, 6], [0, 1, 5, 6], [0, 2, 3, 7], [0, 5, 6, 7], [0, 2, 6, 7]]
tetraHedron=cubeAsimplex3[0]
tetraHedronDic={}
for vertice in tetraHedron :
tetraHedronDic[vertice]=cubeAdic.get(vertice)
axisRot='z'
locationXYZ=[0,0,1]
angleRot=pi/2
structureDic=rotateStructure(tetraHedronDic,angleRot,axisRot,False)
textToPlt = text3d("AB",vector( (xBound, yBound, 1.5*zBound))+shiftV, fontsize='300%', fontweight=800)
print( 'tetraHedronDic.values : ',list(tetraHedronDic.values()) )
pi=Polyhedron(list(tetraHedronDic.values()) , base_ring=QQ)
print( 'structureDic.values : ',list(structureDic.values()) )
po=Polyhedron(list(structureDic.values()), base_ring=QQ)
plt=addAxis(Graphics(),textToPlt,xBound,yBound,zBound+1,width,radius)
plt+=pi.plot(color='black',opacity=0.5)
plt+=po.plot(color='red',opacity=0.5)
plt.show()
same pb in JupyterLab