# 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

# 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])

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]}

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 :

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+=pi.plot(color='black',opacity=0.5)
plt+=po.plot(color='red',opacity=0.5)
plt.show()

edit retag close merge delete

same pb in JupyterLab

( 2024-03-17 09:57:24 +0200 )edit

Sort by ยป oldest newest most voted

The first time you do

angleRot=pi/2


and you use that value, and at the end you do

pi=Polyhedron(list(tetraHedronDic.values()) , base_ring=QQ)


So the second time you run the same code from top to bottom, pi is going to refer to a polyhedron instead of a number, causing the error message you quoted.

So use a different name for your polyhedron.

In general, when code doesn't work if you run it twice, chances are you accidentally overwrote some global name that was used the first time around.

more

Oops sorry !! Thank you @rburing.

( 2024-03-17 10:23:38 +0200 )edit