Professional Documents
Culture Documents
By Karim Kaddoura
For CS146-6
Polynomial ADT
What is it? (Recall it from mathematics)
An example of a single variable
polynomial:
4x6 + 10x4 - 5x + 3
Remark: the order of this polynomial is
6
(look for highest exponent)
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Now what?
By definition of a data types:
A set of values and a set of allowable
operations on those values.
We can now operate on this polynomial the
way we like
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Array Implementation:
p1(x) = 8x3 + 3x2 + 2x + 6
p2(x) = 23x4 + 18x - 3
p1(x)
6
0
p2(x)
-3
0
Index
represents
exponents
18
0
2
23
4
Polynomial ADT
(continued)
-3
WASTE OF SPACE!
16
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Start p2(x)
Coefficient
23
18
-41 163
6
-5
10
12
Exponent
End p1(x)
End p2(x)
Polynomial ADT
(continued)
Polynomial ADT
(continued)
23
18
41
18
P2
10
12
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Polynomial ADT
(continued)
Polynomial ADT
(continued)
4x3 + 10x2 + 5x + 3
- Place it in an array, call it a[i], and compute it
Polynomial ADT
(continued)
4x3 + 10x2 + 5x + 3
A general (and inefficient) algorithm:
int Poly = 0;
int Multiply;
for (int i=0; i < a.Size; i++)
{
Multiply =1;
for (int j=0; j<i; j++)
{
Multiply *= x;
}
Poly += m*a[i];
}
Poly
5x + 3
10x + 5x +3
10x2 + 5x + 3
4x + 10x2 + 5x + 3
4x2 + 10x2 + 5x + 3
4x3 + 10x2 + 5x + 3
Polynomial ADT
4x3 + 10x2 + 5x + 3
(continued)
Poly
4x + 10
4x2 + 10x + 5
4x3 +10x2 + 5x +3
Polynomial ADT
(continued)
Polynomial ADT
(continued)
So in general
anxn + a(n-1)x(n-1) + + a1x1 + a0
EQUALS:
(((a + a
n
(n-1)
)x + a(n-2) x + + a1 x + a0