Hello to everyone! This is my first post here, I hope you can help me. I'm making a class work in python and I'm a little lost:
I have to use classes to represent monomials and polynomials, and the classes should contain methods to add, substract, multiply and divide them.
So far, I've been able to create the whole monomials class, and almost all the polynomial class, but I can't find a way to code polynomial division.
Surfing the web I've seen some methods based on binary conversion and things like that, but that doesn't seem to work for me because of how the program is made.
here's my code. The variable names are in Spanish, sorry for that :P
As you can see, each monomial has two attributes (coefficient and exponent), and the polynomial class takes as an argument a list of monomial objects.
# -*- coding: cp1252 -*-
import math
class Monomio:
def __init__(self, coeficiente, exponente):
if isinstance(coeficiente, int):
self.coeficiente = float(coeficiente)
else:
self.coeficiente = coeficiente
if isinstance(exponente, int):
self.exponente = float(exponente)
else:
self.exponente = exponente
def sumar (monomio1, monomio2):
# adding
if monomio1.exponente == monomio2.exponente:
suma = Monomio(monomio1.coeficiente + monomio2.coeficiente, monomio1.exponente )
return suma
else:
return "error"
def restar (monomio1, monomio2):
# substracting
if monomio1.exponente == monomio2.exponente:
resta = Monomio(monomio1.coeficiente - monomio2.coeficiente, monomio1.exponente)
return resta
else:
return "error"
def multiplicar (monomio1, monomio2):
# product
producto = Monomio(monomio1.coeficiente * monomio2.coeficiente, monomio1.exponente + monomio2.exponente)
return producto
def dividir ( monomio1, monomio2):
# division
cociente = Monomio(monomio1.coeficiente / monomio2.coeficiente, monomio1.exponente - monomio2.exponente)
return cociente
def mostrar (monomio):
# showing the monomials on screen
if monomio.coeficiente == math.floor(monomio.coeficiente) and monomio.exponente == math.floor(monomio.exponente):
print str(int(monomio.coeficiente)) + "x^" + str(int(monomio.exponente))
elif monomio.coeficiente == math.floor(monomio.coeficiente) and monomio.exponente != math.floor(monomio.exponente):
print str(int(monomio.coeficiente)) + "x^" + str(monomio.exponente)
elif monomio.coeficiente != math.floor(monomio.coeficiente) and monomio.exponente == math.floor(monomio.exponente):
print str(monomio.coeficiente) + "x^" + str(int(monomio.exponente))
else:
print str(monomio.coeficiente) + "x^" + str(monomio.exponente)
class Polinomio:
def __init__(self, polinomio):
self.polinomio = polinomio
self.size = len(polinomio)
def ordenar(polinomio):
# sorts the list
for i in range(1, polinomio.size):
for j in range(0, polinomio.size-i):
if polinomio.polinomio[j].exponente > polinomio.polinomio[j+1].exponente:
elemento = polinomio.polinomio[j]
polinomio.polinomio[j] = polinomio.polinomio[j+1]
polinomio.polinomio[j+1] = elemento
polinomio.polinomio.reverse()
def mostrar(polinomio):
#shows the monomials list on screen
for i in range(0, polinomio.size):
polinomio.polinomio[i].mostrar()
def simplificar(polinomio):
# simplifies the polynomial by adding all the elements with the same exponent.
for i in range(1, polinomio.size - 2):
if polinomio.polinomio[i].exponente == polinomio.polinomio[i+1].exponente:
polinomio.polinomio[i] = Monomio.sumar(polinomio.polinomio[i], polinomio.polinomio[i+1])
del polinomio.polinomio[i+1]
polinomio.size = len(polinomio.polinomio)
def sumar(poli1,poli2):
# adding
suma = []
for i in range(0,poli1.size):
suma.append(poli1.polinomio[i])
for j in range(0,poli2.size):
suma.append(poli2.polinomio[j])
polisuma = Polinomio(suma)
polisuma.ordenar()
polisuma.simplificar()
return polisuma
def restar(poli1,poli2):
# substracting
poliresto = Polinomio(poli2.polinomio)
for a in range (0,poliresto.size):
poliresto.polinomio[a].coeficiente = 0 - poliresto.polinomio[a].coeficiente
resta = []
for i in range(0,poli1.size):
resta.append(poli1.polinomio[i])
for j in range(0,poliresto.size):
resta.append(poliresto.polinomio[j])
poliresta = Polinomio(resta)
poliresta.ordenar()
poliresta.simplificar()
return poliresta
def multipilicar(poli1,poli2):
#product
multi = []
for i in range(0,poli1.size):
for j in range(0,poli2.size):
multi.append(Monomio.multiplicar(poli1.polinomio[i],poli2.polinomio[j]))
polimulti = Polinomio(multi)
polimulti.ordenar()
polimulti.simplificar()
return polimulti
My problem, as I said, is that I have no idea about how to even start coding the polynomial division. Any ideas?