Unit-II (SparseMatrix Polynomial) Updated
Unit-II (SparseMatrix Polynomial) Updated
Where xi refers to the ith element of x variables can be expressed as – x[0], x[1],
…. X[4]
[0] 10 1024
[1] 20 1028
[2] 30 1032
[3] 40 1036
[4] 50 1040
[5] 60 1044
[6] 70 1048
▪a[S1][S2]…[SN]
▪Address of an element aij
▪Address of a[I1][I2]. . . .[lN] = B + W*[((E1S2 + E2 )S3 +E3 )S4 ….. + EN-
1)SN + EN]
▪Where B = base address
▪W represents the size of an element in bytes
▪Ei is given by Ei = li – ti, where li and ti are the calculated indexes (indices of array
element which needs to be determined) and lower bounds respectively.
▪S1, S2, S3, ………, SN are dimensions
▪l1, l2, l3, …………..lN are indices of the element whose address needs to be
calculated.
▪x4+59x+10
▪(3, (4, 1), (1, 59), (0, 10)) or 3 4 1 1 59 0 10
▪3x3y2 + 10xy -1
(3, (3, 2, 3), (1, 1, 10), (0, 0 , -1))
OR
3 3 2 3 1 1 10 0 0 -1
▪X2+5xy+y2-y-x
(5, (2,0,1), (1,1,5), (0,2,1), (0,1,-1), (1,0,-1))
OR
5 2 0 1 1 1 5 0 2 1 0 1 -1 0 0 -1
Vertical Method
};
Row0 15 0 0 22 0 -15
Row0 6 6 8
Row1 0 11 3 0 0 0 Row1 0 0 15
Row2 0 0 0 -6 0 0 Row2 0 3 22
Row3 0 0 0 0 0 0 Row3 0 5 -15
Row4 91 0 0 0 0 0 Row4 1 1 11
Row5 0 0 28 0 0 0 Row5 1 2 3
Row6 2 3 -6
Matrix1[6][6] Row7 4 0 91
Row8 5 2 28
Matrix2[9][3]
2 0 0 2 0 0
0 0 -3 Transpose 0 0 4
0 4 0 0 -3 0
3 3 3 3 3 3
0 0 2 0 0 2
Transpose
1 2 -3 2 1 -3
2 1 4 1 2 4
Is this a correct
matrix?
No, in a matrix values needs to stored
sequentially(row & column wise)
(a) (b)
(a) shows a sparse matrix and (b) shows its transpose stored as triples.
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 49
Simple Transpose of Sparse
Matrix
➢Transpose of a matrix is obtained by interchanging rows and
columns.
➢In another way, we can say that element in the i, j position gets put in
the j, i position.
➢But this is not the case with sparse matrix triple form.
➢To find transpose of sparse matrix triple form, find all elements in col0
and store then in row0 of the transpose matrix, find all elements in col1
and store then in row1 of the transpose matrix, and so on.
➢Since the original matrix ordered the rows, the columns within each
row of the transpose matrix will be arranged in ascending order as
well.
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 50
[0] [1] [2] [3] [4] [5]
A[2]
0
0
11
0
3
0
0
-6
0
0
0
0
Algorithm Transpose(A,B) A[3] 0 0 0 0 0 0
// A is a matrix represented in sparse form. B is set to be its
A[4] 91 0 0 0 0 0
transpose.
1 (m,n,t) :=(A(0,0), A(0,1), A(0,2)) A[5] 0 0 28 0 0 0
2 (B(0,0), B(0,1), B(0,2)) := (n,m,t) Col0 Col1 Col2
3 3 3 3 3 3
0 0 2 0 0 2
Transpose
1 2 -3 1 2 4
2 1 4 2 1 -3
Is this a correct
matrix?
Yes, because row and column indices are in
sequence.
Row0 6 6 8 Row0 6 6 8
Row1 0 0 15 Row1 0 0 15
Row2 0 3 22 Row2 0 4 91
Row3 0 5 -15 Row3 1 1 11
Row4 1 1 11 Row4 2 1 3
Row5 1 2 3 Row5 2 5 28
Row6 2 3 -6 Row6 3 0 22
Row7 4 0 91 Row7 3 2 -6
Row8 5 2 28 Row8 5 0 -15
(a) (b)
(a) shows a sparse matrix and (b) shows its transpose stored as triples.
Row2
0
0
0
3
15
22
procedure FAST_TRANSPOSE(A,B) Row3 0 5 -15
// A is a matrix represented in sparse form. B is set to be its transpose. t is non-zero terms Row4 1 1 11
declare S(n), T(n); //local array
1 (m,n,t) := (A(0,0), A(0,1), A(0,2))
Row5 1 2 3
2 (B(0,0), B(0,1), B(0,2)) := (n,m,t) Row6 2 3 -6
3 if t<=0 then return //check for zero matrix Row7 4 0 91
4 for i:=0 to n do S(i) :=0 end
5 for i:= 1 to t do //S(k) is the number of elements in row k of B Row8 5 2 28
6 S(A(i, 1)) := S(A(i, 1)) + 1 //elements in row k of B Col0 Col1 Col2
7 end
8 T(0):=1 //T(i) is the starting position of row i in B Row0 6 6 8
9 for i:=1 to n do Row1 0 0 15
10 T(i) := T(i -1 ) + S(i - 1)
11 end
Row2 0 4 91
12 for i:=1 to t do //move all t elements of A to B// Row3 1 1 11
13 j := A(i,1) //j is the row in B// Row4 2 1 3
14 (B(T(j),0), B(T(j),1), B(T(j),2)) := (A(i,1), A(i,0), A(i,2)) //store in triple//
15 T(j) :=T(j) + 1 //increase row j to next spot//
Row5 2 5 28
16 end Row6 3 0 22
17 end FAST_TRANSPOSE Row7 3 2 -6
11/27/2018 LINEAR DATA STRUCTURE (DS-I) Row8 5 0 57 -15
Additions of two Sparse Matrices
Col0 Col1 Col2
5. Find out the addition of two sparse matrices in triplet form and also find Simple and Fast
transpose? 4 5 6
4 5 6
0 3 7 0 3 5
0 4 6 1 3 8
1 4 4 1 4
+
45 +
2 1 8 2 3 4
3 2 45 3 2 45
6. Represent the following polynomial using array: 4 1 2
4 4 21
1. 3x3y2+10xy4+10x+1
2. 21+x4-5x7+18x68
3. 5x2+10xy+y2+20
4. 5x3y2z+3x2y3z2+6xyz3+10
5. 10x3y2z-8x3y3z3+5xyz2+10
(3x2 + 7x – 5) Plot A.
+ (5x2 – 4x + 11) Plot B.
8x2 + 3x + 6 Combine like terms.