SmtC: Show me the Code
Ole Peter Smith
Instituto de Matemática e Estatística
Universidade Federal de Goiás
http://www.olesmith.com.br

Matemática Computacional
O conhecimento adquirimos com os livros e os mestres.
A sabedoria aprendemos com o povo e os humildes.
Cora Coralina
< Atividades #1.1 | Vectores no Plano | Add >

Vectores em Python

  • Matemática: [; \underline{\textbf{v}}=(v_1,v_2,...), \underline{\textbf{w}}=(w_1,w_2,...) \in \mathbb{R}^n ;]
  • Python: v=[1,2,3,4]: vx=v[0]
  • Classe de Python: list
  • Somar Vetores: [; \underline{\textbf{u}}=\underline{\textbf{v}}+\underline{\textbf{w}} ;]
    • Matemática: [; u_i=v_i+w_i ;]
    • Python: for i in range( len(v) ): u.append( v[i]+w[i] )
  • Classe Vector herda list
Python Listing: ../Code/Vector.py.
    def __init__(v,w=[]):
        if (w.__class__.__name__=="int"):
            v.__Calloc__(w)
            
        else:
            v.__Calloc__( len(w) )
            for i in range( len(w) ):
                v[i]=1.0*w[i]
        
    def __Calloc__(v,size):
        for i in range(size):
            v.append(0.0)
        
    def __str__(v):
        #Diferentiate print vectors.
        text=[]
        for vi in v:
            text.append( str(vi) )

        return "{"+",".join(text)+"}"

< Atividades #1.1 | Vectores no Plano | Add >
Messages:
0 secs.