DS 4
DS 4
DS 4
Ahmad
Lecture 4
Abstract data type: Polynomial
• Polynomials A(X)=3X20+2X5+4, B(X)=X4+10X3+3X2+1
• Structure Polynomial is
• objects:
– a set of ordered pairs of <ei,ai> where ai in Coefficients and ei in Exponents, ei are
integers >= 0
• functions:
– for all poly, poly1, poly2
• Polynomial, coef Coefficients, expon Exponents
• Coefficient Coef(poly, expon) ::= if (expon poly) return its coefficient else
return Zero
• Return sum[ ]
Polynomial Multiplication
0 1 2 3 4 5 6
0 4 18 9 3 -4 6 0
1 12 45 74 15 0 98 0
2 84 87 75 67 81 85 79
• The declaration must specify the number of rows and the number of columns.
Declaration & Initialization
• Rows of Array3:
• 120
• 400
Sparse Matrix
• Objects
– a set of triples, <row, column, value>, where row and column are
integers and form a unique combination, and value comes from the
set item.
• Functions
– Create(max_row, max_col) ::= return a Sparse_matrix that
can hold up to
max_items = max _row max_col
and whose maximum row size is
max_row and whose maximum
column size is max_col.
– Transpose(a) ::= return the matrix produced by
interchanging the row and column
value of every triple.
Sparse Matrix
– Add(a, b) ::= if the dimensions of a and b are the
same return the matrix produced by
adding corresponding items, namely
those with identical row and column
values else return error
– Multiply(a, b) ::= if number of columns in a equals
number of rows in b return the matrix
d produced by multiplying a by b
according to the formula:
d [i] [j] = (a[i][k]•b[k][j]) where d (i, j)
is the (i,j)th element else return error.