User Tools

Site Tools


Writing /var/lib/dokuwiki/data/meta/teaching/ie0117/actividad_4.meta failed
Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.
teaching:ie0117:actividad_4

This is an old revision of the document!


Actividad 5. Python, objectos, matrices.

En esta actividad se practicarán conceptos asociados a programación en python.

Preguntas de la actividad

  • Qué es un paradigma de programación y cómo me afecta a la hora de programar?
  • Qué es un lenguaje de programación?
  • Qué es un lenguaje interpretado? Compilado?
  • Cómo programo de forma básica con python?
  • Qué es un objeto?
  • Cómo programo en Python usando objetos?
  • Qué es una estructura de datos?
  • Qué estructura de datos debo usar para mi aplicación?
  • Qué es sobrecarga de operadores? Y cómo lo uso en python?
  • Herencia?

Temas tentativos a cubrir

  • ipython
  • for, enumerate, zip
  • list comprenhesions
    • xrange
    • map
    • reduce
  • lambda
  • functions
  • lists
  • ints
  • strings
  • floats
  • dicts
  • while
    • break, continue
  • pass
  • tuples
  • sets
  • immutable
  • First program
    • Accepting arguments: sys.argv, optparse
  • input, output, open, pickle
  • exceptions
  • classes
  • Numpy
  • Objetos
  • Clases
  • Sobrecarga de operadores
  • Referencias
  • Documentación en el código
  • self: objetos en C

Instrucciones

  • Escriba el siguiente programa y nombre el archivo matrix.py:
#!/usr/bin/env python

class Matrix(object):
    def __init__(self, data, shape):
        print "Initializing object"
        self.data=data
        self.shape=shape

    def imprimir(self):
        for i in xrange(self.shape[0]):
            print self.data[self.shape[1]*i:self.shape[1]*(i+1)]
        print "good bye"

    def add(self, o_matrix):
        result=Matrix(self.data, self.shape)
        for i in xrange(self.shape[0]):
            for j in xrange(self.shape[1]):
                result.data[i*result.shape[1]+j]=self.data[i*self.shape[1]+j]+o_matrix.data[i*o_matrix.shape[1]+j]
        return(result)

    def __len__(self):
        return(self.shape[0]*self.shape[1])

def main():
    data=[1,2,3,4,5,6,7,8,9,10,11,12]
    shape=(4,3)
    matrix1=Matrix(data, shape)
    matrix1.imprimir()
    data2=[1]*shape[0]*shape[1]
    data2=range(12)
    data2.reverse()
    matrix2=Matrix(data2, shape)
    matrix2.imprimir()
    matrix3=matrix1.add(matrix2)
    matrix3.imprimir()
    print len(matrix3)

if __name__=="__main__":
    main()
  • Ejecútelo.
  • Ahora sobrecarge el método “add” para poder usar el símbolo “+”
  • Siga las instrucciones del profesor para agregar una excepción.
  • Agregue el método de multiplicación matricial (dot product).
  • Sobrecarge el método de multiplicación matricial para usar el símbolo “*”.

Evaluación

Se evaluará el funcionamiento correcto de la suma y multiplicación de dos matrices usando objetos en python sin utilizar numpy.

~~DISCUSSION~~

teaching/ie0117/actividad_4.1461867107.txt.gz · Last modified: 2022/09/20 00:08 (external edit)