El algoritmo de Ruffini (Horner) y su generalización para ordenadores
1
El algoritmo de Ruffini (Horner) y su generalización para ordenadores.
Aladar Peter Santha
Teorema 1: Si f X a n X n a n 1 X n 1 ? a 2 X 2 a1 X a 0 n 1 y g X X a
, son dos polinomios con coeficientes en un anillo conmutativo con elemento neutro,
entonces existe un polinomio q X
A X de grado n 1 y r A tales que
f X
Demostración: Suponiendo que
X a q X r
(1)
q X
bn 1 X n
1
bn
2 X
n 2
? b1 X
b0
X
a q X
r
bn 1 X
n
bn
2
abn
1
X
n 1
bn
3
abn
2
X n
2
?
?
b2
ab3 X
3
b1
ab2 X
2
b0
ab1 X
r
ab0
, de la igualdad (1) resulta que
bn
1
an
bn
bn
2
3
abn
abn
1
2
an 1
an 2
bn
bn
2
3
an
an
1
2
abn 1
abn 2
………………………………………………………………
b2 ab3 a3
b1 ab2 a3
b0 ab1 a3
r ab0 a0
b2 a3 ab3
b1 a2 ab2
b0 a1 ab1
r a0 ab0
(2)
Según las igualdades (2), al conocer el coeficiente bi del polinomio q X , el
coeficiente bi 1 se puede calcular según la fórmula siguiente:
bi 1 ai abi i n 1 , ? 0 .
Este procedimiento sencillo para el cálculo de los coeficientes del cociente, entre el
polinomio f X y el polinomio X a , se llama el algoritmo de Ruffini (ó Hörner). El
cálculo se puede organizar según el esquema siguiente:
a
an
an
abn
1
1
an
abn
2
2
…
…
a 2
ab2
a1
ab1
a0
ab0
bn
1
an
bn
2
bn
3
…
b1
b0
r
Para rellenar la tabla, se completan primero las casillas con los coeficientes del
polinomio y la casilla con el valor de a . Como segundo paso, en la casilla que se
encuentra en la columna de an y la tercera fila, se coloca el valor de an . En la tercera
etapa, se rellenan progresivamente las casillas de la segunda y la tercera fila. Para esto
se multiplica con a el contenido de la última casilla rellenada en la tercera fila y se
coloca el producto en la casilla de la segunda fila de la columna siguiente, luego se
suma el contenido de ésta última casilla con el contenido de la casilla que se encuentra
en la primera fila de la misma columna, colocando la suma en la tercera fila de ésta
columna. El proceso se continúa hasta que la tabla quedará rellenada.
Ejemplo 1: Si A R , f X X 4 2 X 2 2 X 5 y a 2 , entonces se obtiene la tabla
siguiente:
2
2
1
1
0
2
2
-2
4
2
3
4
7
-5
14
11
, de donde resulta que
q X
X 3
2 X 2
2 X
7 y r
11 .
Si los coeficientes del polinomio y el valor del parámetro a son enteros o decimales, la
codificación del cálculo del ejemplo 1 es la siguiente:
Public Function RuffiniAR(ByRef p() As Double, ByVal a As Double) As String
Dim q() As Double, g As Integer, i As Integer
Dim res As String, c() As Double, rc As String
g = UBound(p()): rc = Chr$(13) + Chr$(10)
ReDim q(g), c(g – 1)
q(0) = p(0)
For i = 0 To g – 1
q(i + 1) = q(i) * a + p(i + 1)
Next i
For i = 0 To g – 1: c(i) = q(i): Next i
'c() es el cociente
res = "Cosiente = " + FormatoPolR(c()) + rc
RuffiniAR = res + "Resto = " + Str$(q(g)) ' q(g) es el resto
End Function
————————————-
Public Function FormatoPolR(ByRef qx() As Double) As String
Dim i As Integer, pol As String
gx = UBound(qx())
If Abs(qx(0)) = 1 Then
If qx(0) = 1 Then
If gx > 1 Then
pol = pol + "X^" + Str$(gx)
Else
pol = pol + "X"
End If
End If
If qx(0) = -1 Then
If gx > 1 Then
pol = pol + "- X^" + Str$(gx)
Else
pol = pol + "- X"
End If
End If
Else
If Abs(qx(0)) > 1 Then
pol = pol + Str$(qx(0))
Else
If qx(0) > 0 Then
pol = pol + "0" + Mid$(Str$(Abs(qx(0))), 2)
Else
pol = pol + "- 0" + Mid$(Str$(Abs(qx(0))), 2)
End If
End If
If gx > 1 Then
pol = pol + " X^" + Str$(gx)
Else
pol = pol + " X"
End If
End If
For i = 1 To gx
If qx(i) 0 Then
If qx(i) < 0 Then
pol = pol + " – "
Else
pol = pol + " + "
End If
If Abs(qx(i)) 1 Then
If Abs(qx(i)) > 1 Then
pol = pol + Mid$(Str$(Abs(qx(i))), 2)
3
Else
pol = pol + "0" + Mid$(Str$(Abs(qx(i))), 2)
End If
Else
If i = gx Then
pol = pol + Mid$(Str$(1), 2)
End If
End If
If i < gx Then
pol = pol + " X"
End If
If i < gx – 1 Then
pol = pol + "^" + Str$(gx – i)
End If
End If
Next i
VerPolR = pol
End Function
Observación 1: Si se pone X a en la igualdad (1) resulta que f a r , es decir el
valor del polinomio f x en el punto a es el resto de la división del polinomio
con X a . De hecho, el esquema de Ruffini se utiliza más bien para calcular los valores
de las funciones de tipo polinomio (este es el caso cuando se aproximan los ceros de un
polinomio por el método de la bipartición). La función RuffiniAR se puede adaptar a
este caso, cuando no interesan los coeficientes del cociente:
Public Function ValPolR(ByRef p() As Double, ByVal a As Double) As Double
Dim i As Integer, gp As Integer, q As Double
gp = UBound(p()): q = p(0)
For i = 0 To gp 1: q = q* a + p(i + 1): Next i
ValPolR = q
End Function
Si el polinomio o el parámetro a tienen valores que no son ni enteros ni decimales,
entonces considerando valores aproximados para ellos, el código RuffiniA devolverá
un resultado que es también aproximado. Por ejemplo, si
f X
X 4
3 X 2 X 2 2 X 5 y a 7 / 3
Entonces considerando las aproximaciones 3 1.732050807588 y 7 / 3 2.333333333333 ,
la función RuffiniAdevolverá el resultado siguiente:
q X
X 3
q1 X 2
q 2 X
q 3 y r
50.4232133….
, donde q1 4.065384140901, q2 7.48589632876764 y q3 4670914337887 .
La precisión de estos últimos cálculos no es muy grande, teniendo en cuenta que
haciendo los cálculos a mano se obtiene r 50.42319 … .
Si en la resolución de las ecuaciones algebraicas por el método de la bipartición se
quieren calcular los ceros con más de 16 decimales después del punto decimal, los
valores de la polinomio hay que calcular con el método de Ruffini adaptado a las
operaciones con enteros y decimales largos:
Public Function ValPolRG(ByRef p() As String, ByVal a As String) As String
Dim i As Integer, gp As Integer, q As String, x(2) String, n as integer
gp = UBound(p()): q = p(0):n7= 7
For i = 0 To gp 1
x(1) = q: x(2) = a: x(1) = MultiplicarDec(x(1), x(2))
x(2) = p(i+1): q = SumarDec(x(1), x(2))
Next i
ValPolRG = q
End Function
Ejemplo 2: Si A C , f X
2 3i X 4
2 X 2
5 i X
7 4i y a
3 i
, entonces se obtiene la tabla siguiente:
q X
4
3+i
2-3i
2-3i
0
9-7i
9-7i
-2
34-12i
32-12i
5+i
108-4i
113-3i
7-4i
342+104i
349+100i
Así,
2 3i X 3 9 7i X 2 32 12i X 113 3i y r 349 100i
Los cálculos se pueden programar para ordenadores de la manera siguiente:
Public Function RuffiniAC(ByRef p1() As Double, ByRef p2() As Double, ByRef a() As Double) As Variant
Dim i As Integer, gx As Integer, coci As String, r As String, rc As String
Dim q() As Double, x() As Double, rt() As Double, ra As String
gx = UBound(p1()): rc = Chr$(13) + Chr$(10)
ReDim q(gx, 2), x(2)
q(0, 1) = p1(0): q(0, 2) = p2(0)
For i = 1 To gx
x(1) = q(i – 1, 1): x(2) = q(i – 1, 2)
rt() = ProdNC(x(), a())
q(i, 1) = rt(1) + p1(i): q(i, 2) = rt(2) + p2(i)
Next i
ReDim q1(gx – 1), q2(gx – 1)
For i = 0 To gx – 1
q1(i) = q(i, 1): q2(i) = q(i, 2)
Next i
coci = FormatoPolinomioComplejo(q1(), q2())
ra = "Cociente: " + coci + rc
r = FormatoNumeroComplejo(q(gx, 1), q(gx, 2))
ra = ra + rc + "Resto de la división = " + r
RuffiniAC = ra
End Function
——————————————
Public Function ProdNC(ByRef x() As Double, ByRef a() As Double) As Variant
Dim pr() As Double
ReDim pr(2)
pr(1) = x(1) * a(1) – x(2) * a(2)
pr(2) = x(1) * a(2) + a(1) * x(2)
ProdNC = pr()
End Function
——————————————————–
Public Function FormatoPolinomioComplejo(ByRef z1() As Double, ByRef z2() As Double) As String
Dim i As Integer, j As Integer, gx As Integer
Dim cd As String, cm As String
gx = UBound(z1())
For i = 0 To gx
If z1(i) 0 Or z2(i) 0 Then
If i = 0 Then
If z2(0) = 0 Then
If Abs(z1(0)) 1 Then
cm = f2(z1(0))
Else
If gx 0 Then
If z1(0) = -1 Then cm = "-"
Else
If z1(0) = -1 Then cm = Str$(-1) Else cm = Mid$(Str$(1), 2)
End If
End If
Else
If gx 0 Then
If z1(0) 0 Then
cm = cm + "(" + f2(z1(0))
End If
If Abs(z2(0)) 1 Then
If z1(0) 0 Then
If z2(0) > 0 Then
cm = cm + " + " + f1(Mid$(Str$(z2(0)), 2)) + " i )"
Else
cm = cm + " – " + f1(Mid$(Str$(z2(0)), 2)) + " i )"
5
End If
Else
cm = cm + f2(z2(0)) + " i"
End If
Else
If z2(0) = 1 Then cm = cm + " + i )" Else cm = cm + " – i )"
End If
Else
cm = cm + f2(z1(0))
If Abs(z2(0)) 1 Then
If z2(0) > 0 Then
cm = cm + " + " + f1(Mid$(Str$(z2(0)), 2)) + " i"
Else
cm = cm + " – " + f1(Mid$(Str$(z2(0)), 2)) + " i"
End If
Else
If z2(0) = 1 Then cm = cm + "+ i" Else cm = cm + "- i"
End If
End If
End If
If gx 0 Then
If gx = 1 Then
cm = cm + " X "
Else
cm = cm + " X^" + Mid$(Str$(gx), 2)
End If
End If
Else
If Abs(z2(i)) 0 Then
If i < gx Then
If z1(i) 0 Then
cd = cd + " + (" + f2((z1(i)))
If Abs(z2(i)) 1 Then
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " i )"
Else
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " i )"
End If
End If
Else
If z2(i) = 1 Then cd = cd + " + i )" Else cd = cd + " – i )"
End If
Else
If Abs(z2(i)) 1 Then
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " i"
Else
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " i"
End If
End If
Else
If z2(i) = 1 Then cd = cd + " + i" Else cd = cd + " – i"
End If
End If
Else
If z1(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z1(i)), 2))
Else
If z1(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z1(i)), 2))
End If
End If
If Abs(z2(i)) 1 Then
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " i"
Else
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " i"
End If
End If
Else
If z2(i) = 1 Then cd = cd + " + i" Else cd = cd + " – i"
End If
End If
6
Else
If Abs(z1(i)) 0 Then
If Abs(z1(i)) 1 Then
If z1(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z1(i)), 2))
Else
cd = cd + " + " + f1(Mid$(Str$(z1(i)), 2))
End If
Else
If z1(i) = 1 Then cd = cd + " + " Else cd = cd + " – "
End If
End If
End If
If gx > 1 Then
If i < gx – 1 Then
cd = cd + " X^" + Mid$(Str$(gx – i), 2)
Else
If i = gx – 1 Then
cd = cd + " X "
End If
End If
End If
cm = cm + cd: cd = ""
End If
End If
Next i
FormatoPolinomioComplejo = cm
End Function
——————————————————-
Public Function FormatoNumeroComplejo(ByVal pr As Double, ByVal pi As Double) As String
Dim r As String
If pr 0 Then
r = r + f2(pr)
End If
If pi 0 Then
If Abs(pi) = 1 Then
If pi = 1 Then r = r + " + " Else r = r + " – "
Else
If pi > 0 Then
If pr 0 Then r = r + " + "
r = r + f2(pi)
Else
r = r + " – " + f1(Mid$(Str$(pi), 2))
End If
End If
r = r + " i"
End If
If r = "" Then r = "0"
FormatoNumeroComplejo = r
End Function
——————————————
Public Function f1(ByVal x As String) As String
If Abs(Val(x)) >= 1 Then
f1 = x
Else
If Left$(x, 1) = "." Then f1 = "0" + x Else f1 = x
End If
End Function
——————————————
Public Function f2(ByVal x As Double) As String
Dim xx As String
xx = Str$(x)
If Abs(x) >= 1 Then
f2 = Str$(x)
Else
If Left$(xx, 1) = "-" Then
If Left$(xx, 2) = "-." Then f2 = "-0" + Right$(xx, Len(xx) – 1) Else f2 = xx
Else
If x = 0 Then
f2 = Str$(0)
Else
If Left$(xx, 2) = " ." Then f2 = "0" + Right$(xx, Len(xx) – 1) Else f2 = xx
End If
End If
End If
End Function
q X
7
Ejemplo 3: Sea A DU , donde DU es el anillo de los números duales, que tienen la
forma a b ( a, b R, 2 0 y
0 ). Si
f X
2 3 X
4
2 X
2
5
X 7 4 y a 3
, entonces
3
2- 3
2- 3
0
6-7
6-7
-2
18-15
16-15
5+
48-29
53-28
7- 4
159-31
166-35
2 3 X 3 6 7 X 2 16 15 X 53 28i y r 166 35
Los cálculos se pueden programar para ordenadores de la manera siguiente:
Public Function RuffiniAD(ByRef p1() As Double, ByRef p2() As Double, ByRef a() As Double) As Variant
Dim i As Integer, gx As Integer, coci As String, r As String, rc As String
Dim q() As Double, x() As Double, rt() As Double, ra As String
gx = UBound(p1()): rc = Chr$(13) + Chr$(10)
ReDim q(gx, 2), x(2)
q(0, 1) = p1(0): q(0, 2) = p2(0)
For i = 1 To gx
x(1) = q(i – 1, 1): x(2) = q(i – 1, 2)
rt() = ProdND(x(), a())
q(i, 1) = rt(1) + p1(i): q(i, 2) = rt(2) + p2(i)
Next i
ReDim q1(gx – 1), q2(gx – 1)
For i = 0 To gx – 1
q1(i) = q(i, 1): q2(i) = q(i, 2)
Next i
coci = FormatoPolinomioComplejo(q1(), q2())
ra = "Cociente: " + coci + rc
r = FormatoNumeroComplejo(q(gx, 1), q(gx, 2))
ra = ra + rc + "Resto de la división = " + r
RuffiniAD = ra
End Function
——————————————
Public Function ProdND(ByRef x() As Double, ByRef a() As Double) As Variant
Dim pr() As Double
ReDim pr(2)
pr(1) = x(1) * a(1)
pr(2) = x(1) * a(2) + a(1) * x(2)
ProdND = pr()
End Function
——————————————
Public Function FormatoPolinomioDual(ByRef z1() As Double, ByRef z2() As Double) As String
Dim i As Integer, j As Integer, gx As Integer
Dim cd As String, cm As String
gx = UBound(z1())
For i = 0 To gx
If z1(i) 0 Or z2(i) 0 Then
If i = 0 Then
If z2(0) = 0 Then
If Abs(z1(0)) 1 Then
cm = f2(z1(0))
Else
If gx 0 Then
If z1(0) = -1 Then cm = "-"
8
Else
If z1(0) = -1 Then cm = Str$(-1) Else cm = Mid$(Str$(1), 2)
End If
End If
Else
If gx 0 Then
If z1(0) 0 Then cm = cm + "(" + f2(z1(0))
If Abs(z2(0)) 1 Then
If z1(0) 0 Then
If z2(0) > 0 Then
cm = cm + " + " + f1(Mid$(Str$(z2(0)), 2)) + " e )"
Else
cm = cm + " – " + f1(Mid$(Str$(z2(0)), 2)) + " e )"
End If
Else
cm = cm + f2(z2(0)) + " e"
End If
Else
If z2(0) = 1 Then cm = cm + " + e )" Else cm = cm + " – e )"
End If
Else
cm = cm + f2(z1(0))
If Abs(z2(0)) 1 Then
If z2(0) > 0 Then
cm = cm + " + " + f1(Mid$(Str$(z2(0)), 2)) + " e"
Else
cm = cm + " – " + f1(Mid$(Str$(z2(0)), 2)) + " e"
End If
Else
If z2(0) = 1 Then cm = cm + "+ e" Else cm = cm + "- e"
End If
End If
End If
If gx 0 Then
If gx = 1 Then
cm = cm + " X "
Else
cm = cm + " X^" + Mid$(Str$(gx), 2)
End If
End If
Else
If Abs(z2(i)) 0 Then
If i < gx Then
If z1(i) 0 Then
cd = cd + " + (" + f2((z1(i)))
If Abs(z2(i)) 1 Then
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " e )"
Else
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " e )"
End If
End If
Else
If z2(i) = 1 Then cd = cd + " + e )" Else cd = cd + " – e )"
End If
Else
If Abs(z2(i)) 1 Then
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " e"
Else
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " e"
End If
End If
Else
If z2(i) = 1 Then cd = cd + " + e" Else cd = cd + " – e"
End If
End If
Else
If z1(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z1(i)), 2))
Else
If z1(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z1(i)), 2))
End If
9
End If
If Abs(z2(i)) 1 Then
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " e"
Else
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " e"
End If
End If
Else
If z2(i) = 1 Then cd = cd + " + e" Else cd = cd + " – e"
End If
End If
Else
If Abs(z1(i)) 0 Then
If Abs(z1(i)) 1 Then
If z1(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z1(i)), 2))
Else
cd = cd + " + " + f1(Mid$(Str$(z1(i)), 2))
End If
Else
If z1(i) = 1 Then cd = cd + " + " Else cd = cd + " – "
End If
End If
End If
If gx > 1 Then
If i < gx – 1 Then
cd = cd + " X^" + Mid$(Str$(gx – i), 2)
Else
If i = gx – 1 Then
cd = cd + " X "
End If
End If
End If
cm = cm + cd: cd = ""
End If
End If
Next i
FormatoPolinomioDual = cm
End Function
——————————————
Public Function f1(ByVal x As String) As String
If Abs(Val(x)) >= 1 Then
f1 = x
Else
If Left$(x, 1) = "." Then
f1 = "0" + x
Else
f1 = x
End If
End If
End Function
- – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Public Function f2(ByVal x As Double) As String
Dim xx As String
xx = Str$(x)
If Abs(x) >= 1 Then
f2 = Str$(x)
Else
If Left$(xx, 1) = "-" Then
If Left$(xx, 2) = "-." Then
f2 = "-0" + Right$(xx, Len(xx) – 1)
Else
f2 = xx
End If
Else
—————————————————–
Public Function FormatoNumeroDual(ByVal pr As Double, ByVal pi As Double) As String
Dim r As String
If pr 0 Then
r = r + f2(pr)
End If
If pi 0 Then
If Abs(pi) = 1 Then
If pi = 1 Then r = r + " + " Else r = r + " – "
f X
2 son:
X
X
X
10
Else
If pi > 0 Then
If pr 0 Then
r=r+"+ "
End If
r = r + f2(pi)
Else
r = r + " – " + f1(Mid$(Str$(pi), 2))
End If
End If
r = r + " e"
End If
If r = "" Then r = "0"
FormatoNumeroDual = r
End Function
Si en lugar de X a , se desea dividir el polinomio
bX a (b 0) , entonces de las igualdades:
con el polinomio
f X
X
a
b
q X
r
f X
bX
a q1 X
r1
X
a
b
q X
r
bX
a
q X
b
r
, resulta que
q1 X
q X
b
y r1
r
Ejemplo 4: Para dividir el polinomio del ejemplo 1 con 3X 2 se dividirá primero el
polinomio f X con X 2 / 3 . Los cálculos se reflejan en la tabla siguiente:
2
3
1
1
0
2
3
2
3
-2
4
9
14
9
3
28
27
53
27
-5
106
81
299
81
, de donde resulta que el cociente y el resto en la división con 3X
q X
1 3
3
2 2
9
14
27
X
53
81
y r
299
81
, respectivamente.
Ejemplo 5: Para dividir el polinomio del ejemplo 2 con (3 i) X (2 5i) se dividirá el
polinomio primero entre X
2 5i
3 i
1 17
10 10
i , según la tabla siguiente:
1 17i
10
2+3i
2+3i
0
49 37i
10
49 37i
10
-2
339 398i
50
239 398i
50
5+i
28
27
53
27
7-4i
106
81
299
81
X
X
1
1
1
i
11
q X
3 11i 3
10
92 31i 2
10
223 191i
100
X
159 53i
270
y r
299
81
Dados el polinomio P X de grado n y el número real a , el esquema de Ruffini es
también útil para hallar los coeficientes de un polinomio Q Y de grado n , tal que
Q X a P X . En efecto, si
P X
P X
P X
P2 X
X a r0
X a r1
………. ………. ………. ………. ……
Pn
1
X
Pn X
X
a
rn
, donde grado Pi X
Entonces
n i
i 1, ? , n
P X
P X
X
a
r0
P2 X
X
a
2
r1 X
a
r0
P3 X
X
a
3
r2 X
a
2
r1 X
a
r0
………. ………. ………. ………. ………. ………. ………. .
rn X
X
a
n
rn
1
X
a
n 1
? r1 X
a
r0
, donde r0 P a , r1 P1 a , r2 P2 a , ? , rn Pn a .
Los cálculos anteriores se pueden codificar de la manera siguiente:
Public Function RuffiniB(ByRef p() As Double, ByVal a As Double) As String
Dim i As Integer, j As Integer, g As Integer, r() As Double, q() As Double
g = UBound(p())
ReDim r(g, g), q(g)
For i = 0 To g
r(i, 0) = p(i)
Next i
For i = 1 To g
r(0, i) = p(0)
Next i
For j = 1 To g-j
For i = 1 To g – j + 1
r(i, j) = a * r(i – 1, j) + r(i, j – 1)
Next i
Next j
q(0) = p(0)
For i = 1 To g
q(i) = r(i, g – i + 1)
Next i
RuffiniB = FormatoPol(q())
End Function
Los coeficientes del polinomio buscado serán los números q i
0, ? , g , donde g
es el grado del polinomio p() .
Ejemplo 6: Si P X 2 X 3 7 X 2 5 X 9 y a 3 entonces según la función
anterior, Q X 2 X 3 11X 2 17 X 15 y así, P X 2 X 3 3 11 X 3 2 17 X 3 15 .
Los cálculos a mano se podrían organizar según la tabla siguiente:
2,
-7
5
9
3
3
3
2
2
2
-1
5
11 r2
17
2
r1
15 r0
4
3
2
12
2
r3
Ejemplo 7: Si P X
2 3i X 4
1 7i X 3
9 5i X 2
8 i X
4 3i
y a 3 i , entonces
Q X
2 3i X 4
37 21i X 3
201 11i X 2
426 167i X
309 282i
,y
P X
2 3i X
a
37 21 X
a
201 11i X
a
426 167i X
a
309 282i
El esquema de Ruffini para dividir un polinomio por el polinomio X a se puede
generalizar para el caso cuando se divide con un polinomio cuyo coeficiente director es
1. En efecto, si se quiere dividir el polinomio
, con el polinomio
A X
a0 X n a1 X n 1 ? a n 1 X a n
(3)
P X
X m
p1 X m
1
?
p m 1 X
p m
(m
n)
(4)
, sean
Q X
q0 X n
m
q1 X n
m 1
? qn
m 1
X
qn
m
(5)
R X
r0 X n
m 1
r1 X n
m 2
? rn
m 2 X
rn
m 1
(6)
, el cociente y el resto de la división, respectivamente. Entonces se cumple la igualdad
A X P X Q X R X
(7)
A continuación se efectuará el desarrollo de la expresión P X Q X R X en los dos
casos siguientes:
1) Si n 8 y m 3 , el cálculo de la expresión mencionada se puede organizar según la
tabla siguiente:
X 8
X 7
X 6
X 5
X 4
X 3
X 2
X 1
X 0
q0
q1
q 2
q3
q 4
q5
p1q0
p1q1
p1q2
p1q3
p1q4
p1q5
p2 q0
p2 q1
p2 q2
p2 q3
p2 q4
p2 q5
p3 q0
p3 q1
p3 q2
p3 q3
p3 q4
p3 q5
Así
r0
r1
r2
P X Q X
R X
S X
s 0 X 8
s1 X 7
s 2 X 6
s 3 X 5
s 4 X 4
s 5 X 3
s 6 X 2
s 7 X
s8
, donde
s0
s1
s2
q0
q1 p1q0
q2 p1q1 p2 q0
s3
q3 p1q2 ` p2 q1
p3 q0
s4
q4
p1q3
p2 q2
p3 q1
(8)
s6
R X
13
s 5
a q5
p1 q 4
p 2 q 3
p 3 q 2
p1q5 p2 q4
p3 q3 r0
s7
s8
p2 q5 p3 q4
p3 q5 r2
r1
Identificando los polinomios A X y S X resulta que
q0 a0
q1
a1
p1q0
a1
p j qk
(0
j
m, 0
k 1)
j k 1
q2
a2
p1q1
p2 q0
a2
p j qk
(0
j
m, 0 k
2 )
j k 2
q3
a3
p1q2
p2 q1
p3 q0
a3
p j qk
0
j
m, 0 k
3
j k 3
q4
a4
p1q3
p2 q2
p3 q1
a4
p j qk
0
j
m, k
4
(9)
j k 4
q5
a5
p1q4
p2 q3
p3 q2
qn
n
an
m
p j qk
0
j
m,
k
5
j k n m
r0
a6
p1q5
p2 q4
p3 q3
an
m 1
p j
qk
0
j
m, k
n m
j k n m 1
r1
a7
p2 q5
p3 q4
an
m 2
p j
qk
0
j
m, k
n m
j k n m 2
r2
a8
p3 q5
an
m 3
p j
qk
0
j
m, k
n m
jçk n m 3
2) Si n 8 y m 6 , el cálculo de la expresión P X Q X
la tabla siguiente:
se puede efectuar según
X 8
X 7
X 6
X 5
X 4
X 3
X 2
X 1
X 0
q0
q1
q 2
p1q0
p1q1
p1q2
p2 q0
p2 q1
p2 q2
p3 q0
p3 q1
p3 q2
p4 q0
p4 q1
p4 q2
p5 q0
p5 q1
p5 q2
p6 q0
p6 q1
p6 q2
r0
r1
r2
r3
r4
r5
Así,
P X Q X
R X
S X
s 0 X 8
s1 X 7
s 2 X 6
s 3 X 5
s 4 X 4
s 5 X 3
s 6 X 2
s 7 X
s8
s0
s1
s2
q0
q1 p1q0
q2 p1q1 p2 q0
s3
p1q2 ` p2 q1 p3 q0
r0
s4
s5
p2 q2
p3 q2
p2 q1
p4 q1
p3 q0 r1
p5 q0 r2
(8)
(10)
j
14
s6
s7
s8
p4 q2
p5 q2
p6 q2
p5 q1 p6 q0
p6 q1 r4
r5
r3
Luego, Identificando los polinomios A X y S X resulta que
q0
a0
q1
a1
p1q0
a1
p j qk
(0
j
m, 0 k 1 )
j k 1
q2
a2
p1q1
p2 q0
a2
p j qk
(0
j
3, 0 k
2 )
j k 2
r0
a3
p1q2
p2 q1
p3 q0
an
m 1
p j qk
0
j
m, 0 k
n m
j k n m 1
r1
a4
p2 q2
p2 q1
p3 q0
an
m 2
p j
qk
0
j
m, 0 k
n m
(9)
j k n m 2
r2
a5
p3 q2
p4 q1
p5 q0
an
m 3
p j
qk
0
j
m, 0 k
n m
j k n m 3
r3
a6
p4 q2
p5 q1
p6 q0
an
m 4
p j
qk
0
j
m, 0 k
n m
j k n m 4
r4
a7
p5 q2
p6 q1
an
m 5
p j
qk
0
j
m, 0 k
n m
j k n m 5
r5
a8
p6 q2
an
m 6
p j
qk
0
j
m, 0 k
n m
j k n m 6
Examinando los dos casos anteriores se puede enunciar el teorema siguiente:
Teorema 2: Dados los polinomios A X y P X , las fórmulas que determinan los
polinomios Q X y R X y que verifican en la igualdad (7) son las siguientes:
q0 a0 y qi ai
j k i
p j qk , 0 i n m , 0 j m , 0 k i
ri
an
m i 1
p j
qk , 0 i
m 1 , 0
m , 0 k
n m
(11)
j k n m i 1
Por supuesto, las fórmulas (10) y (11) no son tan manejables como las fórmulas en el
caso de la división entre X .a , pero son fácilmente programables y el ordenador las
ejecuta con tanta facilidad como las personas la regla normal de Ruffini.
Esta manera de efectuar la división tiene sus ventajas, sobre todo cuando los
coeficientes de los polinomios son enteros o decimales (trabajar con fracciones es más
penoso). Si el coeficiente director p0 del polinomio P X no fuera 1 entonces habrá
que dividir P X entre p0 y hacer la división con el polinomio así obtenido. El resto
será válido y para obtener el verdadero cociente hay que dividir el cociente obtenido
entre p0 .
Ejemplo 3: De acuerdo con lo dicho anteriormente, para hallar el cociente y el resto en
la división euclidea del polinomio A X entre P X , donde
A X
2 X 8
P X
3 X 7
4
X
5 X 6
3
2 X
X 5
5 X
2
3 X 4
3 X
4 X 3
4
7 X 2
3 X
4
, se obtiene el cociente y el resto siguientes:
Q X
2 X 4
X 3
3 X 2 18 X 13
R X
81X 3 100 X 2
30 X
56
2
15
, respectivamente.
Para dividir el polinomio A X entre el polinomio P X , cuyo coeficiente director b0 1 ,
se dividirá el polinomio A X entre el polinomio
P1 X
1
p 0
P X
, según el esquema general de Ruffini. Suponiendo que,
, de
A X
P X Q X
R X
A X
1
p 0
P X Q1 X
R1 X
, resulta que
Q X
1
p 0
Q1 X
y
R X
R1 X
En general, los coeficientes del cociente y del resto serán aproximaciones decimales de
los coeficientes reales.
Ejemplo 4: Para dividir el polinomio
A X
3 X 5
7 X 3
4 X 2
5 X 12
, con el polinomio
P X
2 X 3
5 X 2
7 X
8
, se divide primero el polinomio A X
entre el polinomio
P1 X
X 3
2.5 X 2
3.5 X
4
, y se obtiene el cociente y el resto
Q1 X
3 X 2
7.5 X
4
y
R1 X
7.125 X 2
20.625 X 17
, respectivamente. Entonces el cociente y el resto de la división inicial serán:
Q X
1
2
Q1 X
1.5 X 2
3.75 X
2
y
R X
R1 X
,respectivamente.
Ejemplo 5: Para dividir el polinomio
A X
3 X 5
7 X 3
4 X 2
5 X 12
, entre el polinomio
P X
7 X 3
5 X 2
7 X
8
, hay que dividir el polinomio P X entre 7 y así los coeficientes del polinomio P1 X ya
no serán números decimales sino números reales periódicos.
Por tanto, si no se quiere trabajar con fracciones, tendremos que hacer la división de
A X con un polinomio P2 X que, al trabajar con bastantes decimales después de la
coma, será una buena aproximación del polinomio P1 X . Así el resultado final será
también una aproximación del cociente verdadero. Tomando
P2 X
X 3
0.714286 X 2
X 1.142857
, se obtendrá el resultado aproximado siguiente;
Q X
0.428572 X 2
0.306123X 1.209912
R x
0.763851X
5.918367 X
2.320702
Para efectuar la división de esta manera, se pueden utilizar las funciones siguientes:
Public Function RuffiniG(ByRef a() As Double, ByRef p() As Double) As Variant
Dim i As Integer, j As Integer, k As Integer, r() As Double, q() As Double
Dim r1() As Double, p1() As Double, gr As Integer, sw As Integer
Dim j1 As Integer, ga As Integer, gp As Integer, gb As Integer
Dim cxq As String, cxr As String, res(2) As String
ga = UBound(a()): gp = UBound(p()): gb = ga – gp
16
ReDim b(gb), p1(gp)
If p(0) 1 Then
For i = 0 To gp: p1(i) = p(i) / p(0): Next i
Else
For i = 0 To gp: p1(i) = p(i): Next i
End If
'Cálculo de los coeficientes del cociente.
b(0) = a(0)
For i = 1 To gb
b(i) = a(i)
For j = 0 To gp
For k = 0 To i – 1
If j + k = i Then
b(i) = b(i) – p1(j) * b(k)
End If
Next k
Next j
Next i
ReDim q(gb)
If p(0) 1 Then
For i = 0 To gb: q(i) = b(i) / p(0): Next i
Else
For i = 0 To gb: q(i) = b(i): Next i
End If
cxq = VerPol(q())
'Calculo de los coeficientes del resto.
ReDim r(gp – 1)
For i = 0 To gp – 1
r(i) = a(gb + i + 1)
For j = 1 To gp
For k = 0 To gb
If j + k = gb + i + 1 Then
r(i) = r(i) – p1(j) * b(k)
End If
Next k
Next j
Next i
gr = UBound(r())
For i = 0 To gr
If Abs(r(i)) > 0.000000000001 Then
sw = 1
End If
Next i
If sw = 1 Then cxr = VerPol(r()) Else cxr = "0"
res(1) = cxq: res(2) = cxr
RuffiniG = res()
End Function
——————————————
Public Function VerPol(ByRef xx() As Double) As String
Dim i As Integer, gx As Integer, pol As String, x() As Double
x() = xx(): gx = UBound(x())
If gx 0 Then
If x(0) 0 Then
If Abs(x(0) – 1) < 10 ^ (-15) Then x(0) = 1
If Abs(x(0) + 1) < 10 ^ (-15) Then x(0) = -1
If Abs(x(0)) = 1 Then
If x(0) = 1 Then
If gx > 1 Then
pol = pol + "X^" + Str$(gx)
Else
pol = pol + "X"
End If
End If
If x(0) = -1 Then
If gx > 1 Then
pol = pol + "- X^" + Str$(gx)
Else
pol = pol + "- X"
End If
End If
Else
If Abs(x(0)) > 1 Then
pol = pol + Str$(x(0))
Else
If x(0) > 0 Then
pol = pol + "0" + Mid$(Str$(Abs(x(0))), 2)
3
2
2
5
Q X
R X
17
Else
pol = pol + "- 0" + Mid$(Str$(Abs(x(0))), 2)
End If
End If
If gx > 1 Then
pol = pol + " X^" + Str$(gx)
Else
pol = pol + " X"
End If
End If
End If
For i = 1 To gx
If x(i) 0 Then
If Abs(x(i) – 1) < 10 ^ (-15) Then x(1) = 1
If Abs(x(i) + 1) < 10 ^ (-15) Then x(i) = -1
If x(i) < 0 Then
pol = pol + " – "
Else
pol = pol + " + "
End If
If Abs(x(i)) 1 Then
If Abs(x(i)) > 1 Then
pol = pol + Mid$(Str$(Abs(x(i))), 2)
Else
pol = pol + "0" + Mid$(Str$(Abs(x(i))), 2)
End If
Else
If i = gx Then
pol = pol + Mid$(Str$(1), 2)
End If
End If
If i < gx Then
pol = pol + " X"
End If
If i < gx – 1 Then
pol = pol + "^" + Str$(gx – i)
End If
End If
Next i
Else
If x(0) > 0 Then
If x(0) < 1 Then
pol = pol + "0" + Mid$(Str$(x(0)), 2)
Else
pol = pol + Mid$(Str$(x(0)), 2)
End If
Else
If x(0) > -1 Then
pol = pol + "- 0" + Mid$(Str$(Abs(x(0))), 2)
Else
pol = pol + "-" + Mid$(Str$(Abs(x(0))), 2)
End If
End If
End If
VerPol = pol
End Function
Ejemplo 6: Según el código anterior, si
A X
4 X 6
7 X 5
8 X 4
3 X 3 12 X 2
5 X
4 y P X
X 5
2 X 4
3 X 3
5 X 2
X
4
, entonces Q X
4X 1 y R X
2 X 4
20 X 3 13 X 2 10 X .
Ejemplo 7: Si A X
4 X 6
7 X 5
8 X 4
3 X 3 12 X 2
5 X
4 y P X
5 X 3
4 X 2
3 X
7
, entonces Q X
0.8 X
2.04 X
3.712 X
4.7136 y R X
32.2704 X
35.1248 X
36.9952
Ejemplo 8: si A X 7 X 4 4 X 3 15 X 2 3 X 7 y P X
, entonces el ordenador devuelve el resultado siguiente:
3 X 2 11X
2.3333333333 333 X 2
7.2222222222 2222 X
27.5925925925 926 X 2
264.407407407407 X 130.962962963
En este caso sencillo, no es difícil ver que
X
18
Q X
7 2
3
65
9
X
745
27
y R X
7139
27
X
3536
27
Cuando se trata de polinomios con coeficientes complejos en vez de la función RuffiniGhay que
utilizar la función siguiente, donde a1 () y a2 () ( p01() y p02() ) son las matrices
unidimensionales que contienen las partes reales e imaginarias de los coeficientes del
dividiendo (divisor), respectivamente.
Public Function RuffiniGC(ByRef a1() As Double, ByRef a2() As Double, ByRef p01() As Double, p02() As Double) As Variant
Dim g1 As Integer, g2 As Integer, gq As Integer, r() As Double, r1() As Double
Dim i As Integer, j As Integer, j1 As Integer, res(2) As String, r2() As Double
Dim b() As Double, x(2) As Double, y(2) As Double, p1() As Double, p2() As Double.
Dim q1() As Double, q2() As Double, b1() As Double, b2() As Double, rr() As Double
g1 = UBound(a1()): g2 = UBound(p01()): gq = g1 – g2
ReDim b(gq, 2), b1(gq), b2(gq), r1(g2 – 1), r2(g – 1)
'División del divisor con su coeficiente director
If p01(0) = 1 And p02(0) = 0 Then
p1() = p01(): p2() = p02()
Else
ReDim p1(g1), p2(g1)
p1(0) = 1: p2(0) = 0
y(1) = p01(0): y(2) = p02(0)
For k = 1 To g2
x(1) = p01(k): x(2) = p02(k)
rr() = DivNC(x(), y())
p1(k) = rr(1): p2(k) = rr(2)
Next k
End If
'Calculo del cociente
b(0, 1) = a1(0): b(0, 2) = a2(0)
For i = 1 To gq
b(i, 1) = a1(i): b(i, 2) = a2(i)
For j = 0 To g2
For k = 0 To i – 1
If j + k = i Then
x(1) = p1(j): x(2) = p2(j): y(1) = b(k, 1): y(2) = b(k, 2)
rr() = ProdNC(x(), y())
b(i, 1) = b(i, 1) – rr(1): b(i, 2) = b(i, 2) – rr(2)
End If
Next k
Next j
Next i
For i = 0 To gq
b1(i) = b(i, 1): b2(i) = b(i, 2)
Next i
If p01(0) 1 Or p02(0) 0 Then
ReDim q1(gq), q2(gq)
For i = 0 To gq
x(1) = b1(i): x(2) = b2(i): y(1) = p01(0): y(2) = p02(0)
rr() = DivNC(x(), y())
q1(i) = rr(1): q2(i) = rr(2)
Next i
Else
q1() = b1(): q2() = b2()
End If
' Cálculo del resto
ReDim r(g2 – 1, 2): k = 0
For i = 0 To g2 – 1
r(k, 1) = a1(gq + i + 1): r(k, 2) = a2(gq + i + 1)
j = k: j1 = 0
Do
x(1) = p1(j + 1): x(2) = p2(j + 1): y(1) = b1(gq – j1): y(2) = b2(gq – j1)
rr() = ProdNC(x(), y())
r(k, 1) = r(k, 1) – rr(1): r(k, 2) = r(k, 2) – rr(2)
j=j+1
j1 = j1 + 1
Loop While j + 1 = 0
k=k+1
Next i
For i = 0 To g2 – 1
r1(i) = r(i, 1): r2(i) = r(i, 2)
Next i
' El ociente en la pantalla
19
res(1) = FormatoPolinomioComplejo(q1(), q2())
' El resto en la pantalla
res(2) = FormatoPolinomioComplejo(r1(), r2())
RuffiniGC = res()
End Function
- – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Public Function ProdNC(ByRef x() As Double, ByRef y() As Double) As Variant
Dim pr(2) As Double
pr(1) = x(1) * y(1) – x(2) * y(2)
pr(2) = x(1) * y(2) + x(2) * y(1)
ProdNC = pr()
End Function
- – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Public Function DivNC(ByRef u() As Double, ByRef v() As Double) As Variant
Dim cmv As Double, co() As Double, x(2) As Double, y(2) As Double, rr() As Double
ReDim co(2)
cmv = v(1) * v(1) + v(2) * v(2)
x(1) = u(1): x(2) = u(2): y(1) = v(1): y(2) = -v(2)
rr() = ProdNC(x(), y())
co(1) = rr(1) / cmv: co(2) = rr(2) / cmv
DivNC = co()
End Function
- – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Public Function FPolC(ByRef z1() As Double, ByRef z2() As Double) As String
Dim i As Integer, j As Integer, gx As Integer, pr as Double
Dim cd As String, cm As String
gx = UBound(z1()) : pr = 0.000000000000001
For i = 0 To gx
If Abs(z1(i) – 1) < pr Then z1(i) = 1
If Abs(z1(i) + 1) < pr Then z1(i) = -1
If Abs(z2(i) – 1) < pr Then z2(i) = 1
If Abs(z2(i) + 1) < pr Then z2(i) = -1
If Abs(z1(i)) < pr Then z1(i) = 0
If Abs(z2(i)) < pr Then z2(i) = 0
If z1(i) 0 Or z2(i) 0 Then
If i = 0 Then
If z2(0) = 0 Then
If Abs(z1(0)) 1 Then
cm = f2(z1(0))
Else
If gx 0 Then
If z1(0) = -1 Then
cm = "-"
End If
Else
If z1(0) = -1 Then
cm = Str$(-1)
Else
cm = Mid$(Str$(1), 2)
End If
End If
End If
Else
If gx 0 Then
If z1(0) 0 Then
cm = cm + "(" + f2(z1(0))
End If
If Abs(z2(0)) 1 Then
If z1(0) 0 Then
If z2(0) > 0 Then
cm = cm + " + " + f1(Mid$(Str$(z2(0)), 2)) + " i )"
Else
cm = cm + " – " + f1(Mid$(Str$(z2(0)), 2)) + " i )"
End If
Else
cm = cm + f2(z2(0)) + " i"
End If
Else
If z2(0) = 1 Then
cm = cm + " + i )"
Else
cm = cm + " – i )"
End If
End If
Else
cm = cm + f2(z1(0))
20
If Abs(z2(0)) 1 Then
If z2(0) > 0 Then
cm = cm + " + " + f1(Mid$(Str$(z2(0)), 2)) + " i"
Else
cm = cm + " – " + f1(Mid$(Str$(z2(0)), 2)) + " i"
End If
Else
If z2(0) = 1 Then
cm = cm + "+ i"
Else
cm = cm + "- i"
End If
End If
End If
End If
If gx 0 Then
If gx = 1 Then
cm = cm + " X "
Else
cm = cm + " X^" + Mid$(Str$(gx), 2)
End If
End If
Else
If Abs(z2(i)) 0 Then
If i < gx Then
If z1(i) 0 Then
cd = cd + " + (" + f2((z1(i)))
If Abs(z2(i)) 1 Then
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " i )"
Else
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " i )"
End If
End If
Else
If z2(i) = 1 Then
cd = cd + " + i )"
Else
cd = cd + " – i )"
End If
End If
Else
If Abs(z2(i)) 1 Then
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " i"
Else
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " i"
End If
End If
Else
If z2(i) = 1 Then
cd = cd + " + i"
Else
cd = cd + " – i"
End If
End If
End If
Else
If z1(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z1(i)), 2))
Else
If z1(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z1(i)), 2))
End If
End If
If Abs(z2(i)) 1 Then
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " i"
Else
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " i"
End If
End If
Else
21
If z2(i) = 1 Then
cd = cd + " + i"
Else
cd = cd + " – i"
End If
End If
End If
Else
If Abs(z1(i)) 0 Then
If Abs(z1(i)) 1 Then
If z1(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z1(i)), 2))
Else
cd = cd + " + " + f1(Mid$(Str$(z1(i)), 2))
End If
Else
If z1(i) = 1 Then
cd = cd + " + "
Else
cd = cd + " – "
End If
End If
End If
End If
If gx > 1 Then
If i < gx – 1 Then
cd = cd + " X^" + Mid$(Str$(gx – i), 2)
Else
If i = gx – 1 Then
cd = cd + " X "
End If
End If
End If
cm = cm + cd: cd = ""
End If
End If
Next i
FPolC = cm
End Function
- – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Public Function f1(ByVal x As String) As String
If Abs(Val(x)) >= 1 Then
f1 = x
Else
If Left$(x, 1) = "." Then
f1 = "0" + x
Else
f1 = x
End If
End If
End Function
- – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Public Function f2(ByVal x As Double) As String
Dim xx As String
xx = Str$(x)
If Abs(x) >= 1 Then
f2 = Str$(x)
Else
If Left$(xx, 1) = "-" Then
If Left$(xx, 2) = "-." Then
f2 = "-0" + Right$(xx, Len(xx) – 1)
Else
f2 = xx
End If
Else
If x = 0 Then
f2 = Str$(0)
Else
If Left$(xx, 2) = " ." Then
f2 = "0" + Right$(xx, Len(xx) – 1)
Else
f2 = xx
End If
End If
End If
End If
y
i
R X
22
End Function
Ejemplo 9: Si
A X
2 3i X 5
1 i X 4
7 11i X 3
12 5i X 2
6 13i X 15 4i
, y
P X
X 3
2 4i X 2
3 7i X 1 9i
, entonces según el código anterior,
Q X
2 3i X 2
17 3i X
56 68i
y R X
117 504 i X 2
346 759 X
683 432 i
Ejemplo 10: Si
A X
2 3i X 5
1 i X 4
7 11i X 3
12 5i X 2
6 13i X 15 4i ,
B X )
iX
3
7 3i X
2
6 X 13 4i
, entonces
Q X
3 2i X 2
24 16i X 17 227i
R X
725 1703i X 2
268 1487i X
672 3023i
Ejemplo 11: Si
A X
2 5i X 4
3 i X 3
2 7i X 2
4 2i X
3 8i ,
y P X
2iX 2
5
6i X
7
, entonces
Q X
2.5 i X 2
5.5 10.75i X 16.125 56.25i
310.125 445.75i X 53.625 401.875i
La regla general de Ruffini se puede aplicar también a los polinomios con coeficientes
duales excepto en el caso cuando el coeficiente director del divisor es un divisor de cero
(dual puro). En este caso el código es la siguiente:
Public Function RuffiniGD(ByRef a1() As Double, ByRef a2() As Double, ByRef p01() As Double, p02() As Double) As Variant
Dim g1 As Integer, g2 As Integer, gq As Integer, r() As Double, r1() As Double
Dim i As Integer, j As Integer, j1 As Integer, res(2) As String, r2() As Double
Dim b() As Double, x(2) As Double, y(2) As Double, p1() As Double, p2() As Double
Dim q1() As Double, q2() As Double, b1() As Double, b2() As Double, rr() As Double
g1 = UBound(a1()): g2 = UBound(p01()): gq = g1 – g2
ReDim b(gq, 2), b1(gq), b2(gq), r1(g2 – 1), r2(g – 1)
If p01(1) = 0 Then
MsgBox "¡La división es imposible!"
Exit Function
End If
'División del divisor con su coeficiente director
If p01(0) = 1 And p02(0) = 0 Then
p1() = p01(): p2() = p02()
Else
ReDim p1(g1), p2(g1)
p1(0) = 1: p2(0) = 0
y(1) = p01(0): y(2) = p02(0)
For k = 1 To g2
x(1) = p01(k): x(2) = p02(k)
rr() = DivND(x(), y()) 'Cuando es posible
p1(k) = rr(1): p2(k) = rr(2)
Next k
End If
'Calculo del cociente
b(0, 1) = a1(0): b(0, 2) = a2(0)
For i = 1 To gq
b(i, 1) = a1(i): b(i, 2) = a2(i)
For j = 0 To g2
For k = 0 To i – 1
If j + k = i Then
x(1) = p1(j): x(2) = p2(j): y(1) = b(k, 1): y(2) = b(k, 2)
rr() = ProdND(x(), y())
b(i, 1) = b(i, 1) – rr(1): b(i, 2) = b(i, 2) – rr(2)
End If
Next k
Next j
Next i
23
For i = 0 To gq
b1(i) = b(i, 1): b2(i) = b(i, 2)
Next i
If p01(0) 1 Or p02(0) 0 Then
ReDim q1(gq), q2(gq)
For i = 0 To gq
x(1) = b1(i): x(2) = b2(i): y(1) = p01(0): y(2) = p02(0)
rr() = DivND(x(), y()) ' Cuando es posible
q1(i) = rr(1): q2(i) = rr(2)
Next i
Else
q1() = b1(): q2() = b2()
End If
' Cálculo del resto
ReDim r(g2 – 1, 2): k = 0
For i = 0 To g2 – 1
r(k, 1) = a1(gq + i + 1): r(k, 2) = a2(gq + i + 1)
j = k: j1 = 0
Do
x(1) = p1(j + 1): x(2) = p2(j + 1): y(1) = b1(gq – j1): y(2) = b2(gq – j1)
rr() = ProdND(x(), y())
r(k, 1) = r(k, 1) – rr(1): r(k, 2) = r(k, 2) – rr(2)
j=j+1
j1 = j1 + 1
Loop While j + 1 = 0
k=k+1
Next i
For i = 0 To g2 – 1
r1(i) = r(i, 1): r2(i) = r(i, 2)
Next i
' El ociente en la pantalla
res(1) = FormatoPolinomioDual(q1(), q2())
' El resto en la pantalla
res(2) = FormatoPolinomioDual(r1(), r2())
RuffiniGD = res()
End Function
————————————————-
Public Function DivND(ByRef u() As Double, ByRef v() As Double) As Variant
Dim cmv As Double, co(2) As Double, x(2) As Double, y(2) As Double, rr() As Double
If v(1) = 0 Then
MsgBox "¡La división es imposible!"
End
End If
cmv = v(1) * v(1)
x(1) = u(1): x(2) = u(2): y(1) = v(1): y(2) = -v(2)
rr() = ProdND(x(), y())
co(1) = rr(1) / cmv: co(2) = rr(2) / cmv
DivND = co()
End Function
————————————————-
Public Function FPolD(ByRef z1() As Double, ByRef z2() As Double) As String
Dim i As Integer, j As Integer, gx As Integer, pr As Double
Dim cd As String, cm As String
gx = UBound(z1()): pr = 0.000000000000001
For i = 0 To gx
If Abs(z1(i) – 1) < pr Then z1(i) = 1
If Abs(z1(i) + 1) < pr Then z1(i) = -1
If Abs(z2(i) – 1) < pr Then z2(i) = 1
If Abs(z2(i) + 1) < pr Then z2(i) = -1
If Abs(z1(i)) < pr Then z1(i) = 0
If Abs(z2(i)) < pr Then z2(i) = 0
If z1(i) 0 Or z2(i) 0 Then
If i = 0 Then
If z2(0) = 0 Then
If Abs(z1(0)) 1 Then
cm = f2(z1(0))
Else
If gx 0 Then
If z1(0) = -1 Then
cm = "-"
End If
Else
If z1(0) = -1 Then
cm = Str$(-1)
Else
cm = Mid$(Str$(1), 2)
24
End If
End If
End If
Else
If gx 0 Then
If z1(0) 0 Then
cm = cm + "(" + f2(z1(0))
End If
If Abs(z2(0)) 1 Then
If z1(0) 0 Then
If z2(0) > 0 Then
cm = cm + " + " + f1(Mid$(Str$(z2(0)), 2)) + " e )"
Else
cm = cm + " – " + f1(Mid$(Str$(z2(0)), 2)) + " e )"
End If
Else
cm = cm + f2(z2(0)) + " e"
End If
Else
If z2(0) = 1 Then
cm = cm + " + e )"
Else
cm = cm + " – e )"
End If
End If
Else
cm = cm + f2(z1(0))
If Abs(z2(0)) 1 Then
If z2(0) > 0 Then
cm = cm + " + " + f1(Mid$(Str$(z2(0)), 2)) + " e"
Else
cm = cm + " – " + f1(Mid$(Str$(z2(0)), 2)) + " e"
End If
Else
If z2(0) = 1 Then
cm = cm + "+ e"
Else
cm = cm + "- e"
End If
End If
End If
End If
If gx 0 Then
If gx = 1 Then
cm = cm + " X "
Else
cm = cm + " X^" + Mid$(Str$(gx), 2)
End If
End If
Else
If Abs(z2(i)) 0 Then
If i < gx Then
If z1(i) 0 Then
cd = cd + " + (" + f2((z1(i)))
If Abs(z2(i)) 1 Then
If z2(i) > 0 Then
cd = cd + " + " + f1(Mid$(Str$(z2(i)), 2)) + " e )"
Else
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " e )"
End If
End If
Else
If z2(i) = 1 Then
cd = cd + " + e )"
Else
cd = cd + " – e )"
End If
End If
Else
If Abs(z2(i)) 1 Then
If z2(i) < 0 Then
cd = cd + " – " + f1(Mid$(Str$(z2(i)), 2)) + " e"
Else
If z2(i
Página siguiente |