Ask Your Question

interact PB on W10 notebook SageMath 8.7

asked 2019-07-20 08:40:29 +0200

ortollj gravatar image

updated 2019-07-23 14:30:10 +0200


on W10 Sagemath 8.7 notebook.

I get an error every time I click on the run button, but the error disappears if I change one of the three values in the little boxes. It looks like the vector is not initialized a first run, what do I need to change ?

def _(v=('vector', input_grid(1, 3, default=[[1,2,3]], to_value=lambda x: vector(matrix(flatten(x)))))):

TypeError: unable to convert ['vector', Grid(value=['vector', Grid(value=[...], children=(Label(value=u'vector'), VBox(children=(EvalText(value=u'1', layout=Layout(max_width=u'5em')),)), VBox(children=(EvalText(value=u'2', layout=Layout(max_width=u'5em')),)), VBox(children=(EvalText(value=u'3', layout=Layout(max_width=u'5em')),))))], children=(Label(value=u'vector'), VBox(children=(EvalText(value=u'1', layout=Layout(max_width=u'5em')),)), VBox(children=(EvalText(value=u'2', layout=Layout(max_width=u'5em')),)), VBox(children=(EvalText(value=u'3', layout=Layout(max_width=u'5em')),))))] to a matrix

but this code above produce no error every time I click on evaluate on

my complete code with the 3 sliders instead of the 3 little boxes:

from sage.plot.plot3d.plot3d import axes
aL=[2,1,3]   # coordinates list of the vector a on which b will be projeted
bL=[3,7,5] # coordinates list vector b (vector example to be projeted)
lL=5/2 # factor expansion to display red line in vector a direction

# here I adopt the convention a vector is a column matrix
A=matrix(QQ,aL).transpose() # matrix(2x1)

print( "P is the B projected vector on vector A ")
print("below all Capital letters are matrices(vectors are matrices),tiny letters are scalars ")
print("E=B-P with P=k*A " ,"E=B-k*A as E perpendicular to A => dot product(A,E)) = 0 means A.transpose()*E=0 " )
print("then A^T*(B-k*A)=0 so k*A^T*A)=A^T*B and k*(A^T*A)^-1 *A^T*A=(A^T*A)^-1 * A^T*B " )
print("so k=(A^T*A)^-1 * A^T*B")
def _(xb=slider([1..10],default=3),yb=slider([1..10],default=4),zb=slider([1..10],default=7)):
    B=matrix(QQ,bL).transpose() # matrix(3x1)
    # Projection Matrix , called Pr 
    #The general formula for the orthogonal projection onto the column space of a matrix A is
    # P = A (A^T A)^(−1) A^T
    Pr_num=A * (A.transpose()) # matrix 3x3
    Pr_den=(( (A.transpose()) * A ).det()) # scalar
    #Pr_den=1 # scalar
    #Pr=Pr_num/Pr_den # matrix(3x3) 
    # other compact form 
    Pr=A*((A.transpose()*A).inverse() * A.transpose())
    g1=line([[-lL*e for e in A.list()],[lL*e for e in A.list()]], legend_label=' a projection line', legend_color='red',color='red')
    g2=plot(vector(B),color="green",width=20,legend_label=' vector b', legend_color='green')
    g3=plot(vector(A),color="black",width=3,legend_label=' vector A', legend_color='black')
    aT = text3d("a",vector(A)*1.1,color='black',fontsize=20)
    bT = text3d("b",vector(B),color='green',fontsize=20)
    # projected vector P
    P=Pr*B # matrix 3x1

    k0=((A.transpose()*A).inverse() * A.transpose()*B).det()

    g4=plot(vector(P),color='blue',width=20,legend_label=' projected vector P', legend_color='blue',fontsize=20)
    pT = text3d("p",vector(P)*1.1,color='blue',fontsize=40)
    E=B-P # matrix 3x1
    g5=plot(arrow(P.coefficients(),B.coefficients(),color='pink',width=5,legend_label=' vector e', legend_color='pink'))
    eT = text3d("e",vector(E)/2+vector(P)*1.05,color='pink',fontsize=20)
    # unit vectors
    gx=plot(vector([1,0,0]),color='black',width=5,legend_label='x', legend_color='black')
    xT = text3d("x",vector([1,0.1,0.1])*1.1,color='black',fontsize=20)
    gy=plot(vector([0,1,0]),color='black',width=5,legend_label='x', legend_color='black')
    yT = text3d("y",vector([0.1,1,0.1])*1.1,color='black',fontsize=20)
    gz=plot(vector([0,0,1]),color='black',width=5,legend_label='x', legend_color='black')
    zT = text3d("z",vector([0.1,0.1,1])*1.1,color='black',fontsize=20)

    show(g1+g2+g3+g4+g5+aT+bT+pT+eT+gx+xT+gy+yT+gz+zT+axes(7, color='black') \
    ,figsize=(6, 6),xmin=-vector(B)[0],xmax=vector(B)[0]+3,ymax=vector(A)[1]+1,ymin=-1,zmax=vector(A)[2]+1,zmin=-1)

    show(" A : ",A, " B : ",B, " Pr_num : ",Pr_num, " Pr_den : ",Pr_den, " Pr : ",Pr)
    show(" A : ",A, " B : ",B, " projected vector   P : ",P  )
    print "verify that k=(A^T*A)^(-1) * A^T*B = ||P|| / ||A||"
    print " so P=A*k= A*(A^T*A)^-1 * A^T*B "
    show ("P also equals to : A*(A^T*A)^(-1) * A^T*B : ",A*((A.transpose()*A).inverse() * A.transpose())*B )
    show( "P= Projection Matrix * B= Pr*B so PR=A*((A.transpose()*A).inverse() * A.transpose() :",A*((A.transpose()*A).inverse() * A.transpose()))
    show ( " sqrt(((P.transpose()*P).det()/(A.transpose()*A).det())) = ||P|| / ||A|| : ",k1)
edit retag flag offensive close merge delete


Sliders effect is not fluid like in Geogebra, what I did should not be the right way to do an interactive drawing with sliders with Sagemath !.

I got the three little boxes code here: Quickstarts

ortollj gravatar imageortollj ( 2019-07-20 08:47:56 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2019-07-22 22:55:44 +0200

ortollj gravatar image

Well finally, it was not worth to Make a mountain out of a molehill ! I simply change the interact code in:

def _(xb=slider([1..10],default=3),yb=slider([1..10],default=4),zb=slider([1..10],default=7)):

and it is ok ( sorry )

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-07-20 08:40:29 +0200

Seen: 157 times

Last updated: Jul 23 '19