PDA

View Full Version : Multiplying an arbitrary number of polynomials



akindofmagic
09-11-2010, 10:41 AM
Hello guys, I have a problem as a VBA beginner, I hope you can give me some hints.
I'm using Microsoft Excel 2003 with Microsoft Visual Basic 6.5.

I have to multiply an arbitrary number of polynomials, say P1*P2*P3*...*Pn.
P1 = an + a(n-1)*X^1 + a(n-2)*X^2 + ...+ a0*x^i
.
.
.
Pn = ...

I have managed to write the code for 3 polynomials, but I'm stuck for n polynomials.
I think the Sub needs to work with a matrix, where each row is the coefficients of each polynomial. The user will type the
maximum grades and the coefficients in the worksheet. The program will read the matrix and the calculus will begin.

Help me with this.

Thank you!

Here is what I've worked so far:

Sub Product3Pol()
Dim n As Integer, m As Integer, r As Integer, xo As Integer
Dim p1 As Integer, p2 As Integer
Dim aArray(20) As Double, bArray(20) As Double, cArray(20) As Double, qArray(60) As Double, rArray(60) As Double

' Colecting user data:
n = Application.InputBox("The maxim grade for the first polynomial is?")
m = Application.InputBox("The maxim grade for the second polynomial is?")
r = Application.InputBox("The maxim grade for the third polynomial is?")
xo = Application.InputBox("x0 value is?")

For i = 0 To n
aArray(i) = Application.InputBox("a" & i & "= ", "1st polynomial coefficients")
Next i

For i = 0 To m
bArray(i) = Application.InputBox("b" & i & "= ", "2nd polynomial coefficients")
Next i

For i = 0 To r
cArray(i) = Application.InputBox("c" & i & "= ", "3rd polynomial coefficients")
Next i

' Multiplying first two polynomials
p1 = n + m

For i = 0 To p1
For k = 0 To i
qArray(i) = qArray(i) + aArray(k) * bArray(i - k)
Next k
Next i

' Multiplying the result of the first two polynomials with the third one
p2 = p1 + r

For i = 0 To p2
For k = 0 To i
rArray(i) = rArray(i) + qArray(k) * cArray(i - k)
Next k
Next i


' Evaluating the resultant polynom for x0
i = 0
b = rArray(i)

Do
i = i + 1
b = b * xo + rArray(i)
Loop Until i = p2 + 1

' Output
Application.Worksheets("Sheet1").Cells(1, 1).Value = "Product polynomial maximum grade is:"
Application.Worksheets("Sheet1").Cells(1, 2).Value = p2

Application.Worksheets("Sheet1").Cells(2, 1).Value = "Product polynomial coefficients are:"

For i = 0 To p2
Application.Worksheets("Sheet1").Cells(2, 2 + i).Value = rArray(i)
Next i
Application.Worksheets("Sheet1").Cells(3, 1).Value = "Polynom value for x0 is:"
Application.Worksheets("Sheet1").Cells(3, 2).Value = b

End Sub

akindofmagic
09-12-2010, 01:35 AM
Anybody? :help

mikerickson
09-12-2010, 09:06 AM
How are you representing a polynomial?
As a string (e.g. "1 + 3*x^1 + 2*x^2" ? or some other method.

One more question, can we assume that all polynomials will be of the same variable?
Or will some aspect of your project require that a polynomial of x be multiplied by a polynomial in y, where the result is a function of two variables.

mikerickson
09-12-2010, 01:19 PM
Here are two methods.
Along with a raft of polynomial handling functions. The function EvaluatePolynomial will take a polynomial and an x value and evaluate, but is not used anywhere else, it is included to show how a polynomial is stored.

Test1 multiplies polynomials together, as the user enters them.
Test2 has the user enter all the polynomials and then multiplies them together. note that polyProduct is set to the polynomial 1 before the multiplications start.

Also, using an InputBox to input these polynomials give a clunky user interface. A userform would be better for the final use.
Sub test1()
Dim runningProduct As Variant
Dim nextPolynomial As Variant
Dim showString As String
runningProduct = UserInputPolynomial
Do
nextPolynomial = UserInputPolynomial
showString = PolyString(runningProduct) & vbCr & "*" & vbCr & PolyString(nextPolynomial)
runningProduct = polyMult(runningProduct, nextPolynomial)
showString = showString & vbCr & "=" & vbCr & PolyString(runningProduct)
Loop Until MsgBox(showString & vbCr & vbCrLf & "Another?", vbYesNo) = vbNo
End Sub

Sub test2()
Dim myPolys As Variant
Dim polyProduct As Variant
Dim polyCount As Long, pointer As Long, i As Long
Dim outputString As String

ReDim myPolys(0 To 0)
Do
ReDim Preserve myPolys(0 To pointer)
myPolys(pointer) = UserInputPolynomial
pointer = pointer + 1
Loop Until MsgBox("You entered: " & PolyString(myPolys(pointer - 1)) & vbCr & "Enter another?", vbYesNo) = vbNo

Rem set polyProduct to the polynomal 1
ReDim polyProduct(0 To 0): polyProduct(0) = 1
outputString = "The product of" & vbCr
For i = 0 To (pointer - 1)
outputString = outputString & PolyString(myPolys(i)) & vbCr
polyProduct = polyMult(polyProduct, myPolys(i))
Next i
outputString = outputString & "is" & vbCr & PolyString(polyProduct)
MsgBox outputString
End Sub

Function polyMult(aPoly As Variant, bPoly As Variant) As Variant
Dim Result As Variant
ReDim Result(0 To 0)
Result(0) = 0
Dim i As Long
For i = 0 To UBound(aPoly)
Result = PolySum(Result, PolyTimesAxN(bPoly, aPoly(i), i))
Next i
polyMult = Result
End Function

Function PolySum(aPoly As Variant, bPoly As Variant) As Variant
Dim Result() As Double
Dim Degree As Long, i As Long
Degree = Application.Max(UBound(aPoly), UBound(bPoly))
ReDim Preserve aPoly(0 To Degree)
ReDim Preserve bPoly(0 To Degree)
ReDim Result(0 To Degree)
For i = 0 To Degree
Result(i) = aPoly(i) + bPoly(i)
Next i
PolySum = reducePoly(Result)
End Function

Function PolyTimesAxN(aPoly As Variant, aCoefficient, nExponent As Long) As Variant
Dim Result() As Double, i As Long
ReDim Result(0 To UBound(aPoly) + nExponent)
For i = 0 To UBound(aPoly)
Result(i + nExponent) = aPoly(i) * aCoefficient
Next i
PolyTimesAxN = reducePoly(Result)
End Function

Function reducePoly(ByVal aPoly As Variant) As Variant
Dim i As Long
i = UBound(aPoly)
Do Until aPoly(i) <> 0 Or i = 0
i = i - 1
Loop
ReDim Preserve aPoly(0 To i)
reducePoly = aPoly
End Function

Function UserInputPolynomial(Optional Title As String) As Variant
Dim myPoly() As Double, strPrompt As String
Dim Degree As Long, uiCoef As Variant
Degree = 0
ReDim myPoly(0 To 0)
Do
strPrompt = strPrompt & vbCr & "Enter the coefficient of the " & Degree & " power term." & vbCr & "Cancel to end entry."
uiCoef = Application.InputBox(prompt:=strPrompt, Default:="0", Type:=1)
ReDim Preserve myPoly(0 To Degree)
myPoly(Degree) = uiCoef
Degree = Degree + 1
strPrompt = "Current: " & PolyString(myPoly) & vbCr
strPrompt = Application.Substitute(strPrompt, "^", vbNullString)
Loop Until TypeName(uiCoef) = "Boolean"
ReDim Preserve myPoly(0 To Degree - 1)
UserInputPolynomial = reducePoly(myPoly)
End Function

Function PolyString(aPoly As Variant, Optional strNumberformat As Variant, Optional strArgument As String = "x") As String
Dim i As Long
If Not IsArray(aPoly) Then PolyString = "bad polynomial": Exit Function
If IsMissing(strNumberformat) Then
PolyString = CStr(aPoly(0))
If 0 < UBound(aPoly) Then
For i = 1 To UBound(aPoly)
PolyString = PolyString & " + " & CStr(aPoly(i)) & "*" & strArgument & "^" & i
Next i
End If
Else
PolyString = Format(aPoly(0), strNumberformat)
If 0 < UBound(aPoly) Then
For i = 1 To UBound(aPoly)
PolyString = PolyString & " + " & Format(aPoly(i), strNumberformat) & "*" & strArgument & "^" & i
Next i
End If

End If
End Function

Function evaluatePoly(aPoly As Variant, xVal As Double) As Double
Dim i As Long
For i = 0 To UBound(aPoly)
evaluatePoly = evaluatePoly + aPoly(i) * xVal ^ i
Next i
End Function

akindofmagic
09-13-2010, 12:05 AM
As a beginner it will take time to understand your code. Thank you very much for your effort, I will come back later with the results.

Stefan.