#!/usr/bin/python # -*- coding: utf-8 -*- """ Bosquejo de un programa para manipular polinomios Autor: Antonio Carrillo Ledesma """ class Poly: C = [] # Coeficientes del polinomio Dim = 10 # Guarda la dimension del arreglo de coeficientes def __init__(self, coef): """Cosntructor de la clase""" self.C = [0 for i in range(self.Dim)] for i in range(len(coef)): self.C[i] = coef[i] def __str__(self): """Visualiza el polinomio""" r = "" for i in range(self.Dim): if self.C[i] != 0: r += " + " + str(self.C[i]) if i == 0: continue r += "X^" + str(i) return r def visualiza(self): """Visualiza el polinomio""" print(self) def grado(self): """Grado del polinomio""" for i in range(self.Dim - 1, 0, -1): if self.C[i] != 0.0: return i return 0 def suma(self, a, b): """suma polinomios""" for i in range(self.Dim): self.C[i] = a.C[i] + b.C[i] def multiplica(self, a, b): """multiplica polinomios""" for i in range(self.Dim): self.C[i] = 0.0 for i in range(self.Dim // 2): for j in range(self.Dim // 2): self.C[i + j] += a.C[i] * b.C[j] """ Prueba de las clases """ if __name__ == "__main__": a = Poly([1.0, 2.0, 3.0]) a.visualiza() b = Poly([4.0, 3.0]) b.visualiza() c = Poly([0.0]) c.multiplica(a, b) c.visualiza() print(c.grado())