Ask Your Question
0

no way to stop SageMath Animation ??

asked 2020-03-15 13:20:55 +0200

ortollj gravatar image

HI the only way I found to stop my animation is to reboot my computer.

a little weird ! ;-)

%matplotlib notebook 
import numpy as np
from scipy.spatial import distance
from sage.plot.point import Point
import numpy as np
import matplotlib.pyplot as plt
#from matplotlib.animation import FuncAnimation
#import animatplot as amp
print("https://www.youtube.com/watch?v=L5GEnUuhilw&t=204s")


#show(LatexExpr(r"\mathrm{rotation_matrix} = \begin{bmatrix}\cos{2\pi \over n} & -\sin{2\pi \over n} \\[8pt] \sin{2\pi \over n} & \cos{2\pi \over n}\end{bmatrix} \qquad \mathrm{s}_0 = \begin{bmatrix}1 & 0 \\ 0 & -1\end{bmatrix}"))
show(LatexExpr(r"\mathrm{rotation_{matrix}} = \begin{bmatrix}\cos{2\pi \over n} & -\sin{2\pi \over n} \\[8pt] \sin{2\pi \over n} & \cos{2\pi \over n}\end{bmatrix} "))
show(LatexExpr(r"\mathrm{reflexion_{matrix}} = \left(\begin{array}{rr} \cos 2 \theta & \sin 2 \theta  \\ \
                                         \sin 2 \theta  & -\cos 2 \theta \end{array}\right)"))

var('n')
n=6

maL=[]
shiftAngle=-2*pi/(2*n)
for i in range(1,n+1) :
    maL.append([cos(i*2*pi/n+shiftAngle),sin(i*2*pi/n+shiftAngle)])
show(maL)
#mL=[[1,1],[-1,1],[-1,-1],[1,-1]]
m=matrix(maL).transpose()

rotationNumber=3
def chooseColor(colorsNumber):
    if 'colorL' not in locals():
        colorL=[]
        for i in range(0,colorsNumber) :
            colorL.append([random(),random(),random()])
        #show(colorL)


    D=distance.cdist(colorL, colorL, 'cityblock')

    Dm=matrix(D)
    DvL=[]
    for v in (Dm.columns()):
        DvL.append((sum(v)))
    #show(DvL)

    keys = range(len(DvL))
    DvDict=dict(zip(DvL, keys))

    sorted_x = sorted(DvDict.items(), key=lambda kv: kv[1])
    #show(sorted_x)



    keys = range(len(DvL))
    DvDict=dict(zip(DvL, keys))
    #show(DvDict)
    DvDictSorted={k: v for k, v in sorted(DvDict.items(), key=lambda item: item[0],reverse=True)}
    #show(DvDictSorted)

    colorSortedL=[]
    for key,val in DvDictSorted.items():
        colorSortedL.append(colorL[val])
        #print (key, "=>", val)
    return colorSortedL
colorSortedL=None
if colorSortedL==None :
    colorSortedL=chooseColor(2*rotationNumber)
#show(colorSortedL)
rot=matrix(SR,[[cos(2*pi/n),-sin(2*pi/n)],[sin(2*pi/n),cos(2*pi/n)]])
ref=matrix(SR,[[cos(2*2*pi/n),sin(2*2*pi/n)],[sin(2*2*pi/n),-cos(2*2*pi/n)]])
show("rotation Matrix :\t",rot)
show("reflexion Matrix :\t",ref)    

def getRotAndReflexionMatrix(rotAndRefSerie,m) :


    #show(rot,ref)
    mrt=m
    for c in rotAndRefSerie :
        if c=='r' :
            mrt=rot*mrt
        elif c=='t' :
            mrt=ref*mrt
        else :
            print("only r or t please")
    return mrt



def buildMatriceFigPlots(matricesL):
    figPlotsL=[]
    figPlot=plot(0, ymin=-1.25, ymax=1.25)

    for matrice in matricesL :
        matriceL=list(matrice.transpose())
        figPlot=(list_plot(matriceL,color='black',size=40,figsize=(8,8)))
        for i in range(0,len(matriceL)) :
            #show(matriceL[i])
            figPlot+=text(str(i), matriceL[i], fontsize=50, rgbcolor=colorSortedL[i] )
        figPlot += circle((0,0), 1)
        figPlot += polygon(matriceL,fill=False, thickness=4, color='orange')

        figPlotsL.append(figPlot)
    #for c in range(0,t.dimensions()[1]) :
            #for r in range(0,t.dimensions()[0]) :
            #    show(r,":\t",r,":\t",t[r,c])
    return figPlotsL

TestAnimation=True            

if TestAnimation :

    #Test Animation
    max = 1.5

    matricesL=[]
    for k in range(0,4) :
        matrice=((rot^k)*m)
        if k==3 :
            matrice=((ref^1)*matrice) # commute trigo to clockWise  
            #t=((ref^2)*m) # no change
        matricesL.append(matrice)
        #show(list(t.transpose()))
    #show("matriceL :\n", matricesL)
        #PtsL.append(showFig(tL))


    matriceFigPlotsL=buildMatriceFigPlots(matricesL)

    #for p in MatriceFigPlotsL :
    #    show(p)
    animate(matriceFigPlotsL,ymin=-max,ymax=max,axes_labels=['x','y'],fontsize=12,figsize=(8,8)).show(delay=100)

else:

    # choose rotations  and reflexion sequence by keyboard
    # example enter the sequence:rrrtrtr

    rotAndRefSerieString = input("rotation =r and reflexion =t chain :\t")
    rotAndRefSerie=list(rotAndRefSerieString)
    print("rotAndRefSerie :\t",rotAndRefSerie)
    rotAndReflexionMatrix=getRotAndReflexionMatrix(rotAndRefSerie,m)
    show("rotAndReflexionMatrix : \t",rotAndReflexionMatrix)

    # plot list of matrices, so embed the matrix  in a list
    matriceFigPlotsL=buildMatriceFigPlots([rotAndReflexionMatrix]) 

    #show("len(matriceFigPlotsL) :\t",len(matriceFigPlotsL))
    for p in matriceFigPlotsL :    
        show(p)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-03-15 15:14:33 +0200

Sébastien gravatar image

The documentation of a.show:

  sage: t = var('t')
  sage: a = animate((cos(c*pi*t) for c in sxrange(1,2,.2)))
  sage: a.show?

says:

...
* "iterations" -- integer (default: 0); number of iterations of
  animation. If 0, loop forever.
...

So you may do:

sage: a.show(iterations=3)

to have the animation stop after 3 iterations.

edit flag offensive delete link more

Comments

Thank you Sebastien.it is ok for your example (cos) but it is not working for my example . because me I got the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-4e61b4bbadb1> in <module>()
    129     #    show(p)
    130     a=animate(matriceFigPlotsL,ymin=-max,ymax=max,axes_labels=['x','y'],fontsize=Integer(12),figsize=(Integer(8),Integer(8))).show(delay=Integer(100))
--> 131     a.show(iterations=Integer(13))
    132 else:
    133 

AttributeError: 'NoneType' object has no attribute 'show'
ortollj gravatar imageortollj ( 2020-03-15 17:32:25 +0200 )edit
1

You are trying .show() of a .show()

FrédéricC gravatar imageFrédéricC ( 2020-03-15 18:11:23 +0200 )edit
1

@ortollj: Delete .show(delay=100) on the previous line.

John Palmieri gravatar imageJohn Palmieri ( 2020-03-15 18:58:03 +0200 )edit

it is ok now

a=animate(matriceFigPlotsL,ymin=-max,ymax=max,axes_labels=['x','y'],fontsize=12,figsize=(8,8)) a.show(delay=200,iterations=7)

it is ok now, thanks to @Sebastien, @FrédéricC, @John Palmieri

sorry to have been so heavy.

ortollj gravatar imageortollj ( 2020-03-15 20:04:44 +0200 )edit

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

Stats

Asked: 2020-03-15 13:20:55 +0200

Seen: 238 times

Last updated: Mar 15 '20