Definition and usage of sparse matrix in Java data structure, java Data Structure

Source: Internet
Author: User

Definition and usage of sparse matrix in Java data structure, java Data Structure

This document describes the definition and usage of sparse matrix in Java data structures. We will share this with you for your reference. The details are as follows:

Triplet classes of non-zero elements in the sparse matrix:

Package com. clarck. datastructure. matrix; /*** sparse matrix compression storage ** Triple class of non-zero elements in the sparse matrix ** @ author clarck **/public class Triple implements Comparable <Triple> {/row number, column number, element value, default access permission int row, colum, value; public Triple (int row, int colum, int value) {if (row <0 | colum <0) {throw new IllegalArgumentException ("the row/column number of the sparse matrix element triplet is not a positive number");} this. row = row; this. colum = colum; this. value = value;}/*** copy construction method, copy A Triple ** @ param elem */public Triple (Triple elem) {this (elem. row, elem. colum, elem. value) ;}@ Override public String toString () {return "(" + row + "," + colum + "," + value + ")";} /*** determine whether the two triplet values are equal. Compare the position and element value */public boolean equals (Object obj) {if (! (Obj instanceof Triple) return false; Triple elem = (Triple) obj; return this. row = elem. row & this. colum = elem. colum & this. value = elem. value;}/*** compare the size of the two tuples based on the location of the three tuples, regardless of the element value, and specify the sequence of the three tuples */@ Override public int compareTo (Triple elem) {// small if (this. row <elem. row | this. row = elem. row & this. colum <elem. colum) return-1; // equal to the equals method. if (this. row = elem. row & this. colum = elem. colum) return 0; // large return 1 for the current Triple object;}/*** addition, + = Operator * @ param term */public void add (Triple term) {if (this. compareTo (term) = 0) this. value + = term. value; else throw new IllegalArgumentException ("the two indexes are different and cannot be added");}/*** specify to delete the element ** @ return */public boolean removable () {// The return this element is not stored as 0. value = 0;}/*** returns the triplet of the symmetric position matrix element * @ return */public Triple Tory ry () {return new Triple (this. colum, this. row, this. value);}/*** addition operation, overload operator + * @ return */public Triple plus (Triple term) {Triple tmp = new Triple (this); tmp. add (term); return tmp ;}}

Sparse Matrix class for triple sequential storage:

Package com. clarck. datastructure. matrix; import com. clarck. datastructure. linear. seqList; /*** sparse matrix compression storage ** sparse matrix triplet sequence table ** sparse matrix class for triple sequence storage ** @ author clarck **/public class SeqSparseMatrix {// Matrix number of rows, number of columns private int rows, columns; // sparse matrix Triple sequence table private SeqList <Triple> list;/*** construct rows, colums column zero matrix ** @ param rows * @ param columns */public SeqSparseMatrix (int rows, int columns) {if (rows <= 0 | col Umns <= 0) throw new IllegalArgumentException ("the number of matrix rows or columns is not a positive number"); this. rows = rows; this. columns = columns; // construct an empty sequence table and execute the SeqList () constructor this. list = new SeqList <Triple> ();} public SeqSparseMatrix (int rows, int columns, Triple [] elems) {this (rows, columns ); // Insert the triple for (int I = 0; I <elems. length; I ++) this. set (elems [I]);}/*** return the elements in column j of row I of the matrix, the sequential search algorithm of the sort order table, O (n) ** @ param I * @ param j * @ retur N */public int get (int I, int j) {if (I <0 | I >= rows | j <0 | j> = columns) throw new IndexOutOfBoundsException ("row or column number of matrix elements out of bounds"); Triple item = new Triple (I, j, 0); int k = 0; Triple elem = this. list. get (k); // search for the item object while (k <this. list. length () & item. compareTo (elem)> = 0) {// only compare the positions of the triple elements, that is, elem. row = I & elem. column = j if (item. compareTo (elem) = 0) return elem. value; // Find (I, j) and return the matrix element k ++; elem = this. list. get (k);} return 0;}/*** set the matrix element with a Triple ** @ param elem */public void set (Triple elem) {this. set (elem. row, elem. colum, elem. value);}/*** set the element value of column in the row column of the Matrix to value, and change or insert the productkey, devicename, and devicesecret of an element in the list of the sort order table in the Primary Order of the row, O (n) ** @ param row * @ param column * @ param value */public void set (int row, int column, int value) {// if (value = 0) return; if (row> = this. Rows | column> = this. columns) throw new IllegalArgumentException ("the row or column number of the Triple exceeded"); Triple elem = new Triple (row, column, value); int I = 0; // search for elem objects in the sorted triple sequence table, or change or insert while (I <this. list. length () {Triple item = this. list. get (I); // if elem exists, change the location matrix element if (elem. compareTo (item) = 0) {// set the I-th element of the sequence table to elem this. list. set (I, elem); return;} // if (elem. compareTo (item)> = 0) I ++; else B Reak;} this. list. insert (I, elem) ;}@ Override public String toString () {String str = "Triple sequence table:" + this. list. toString () + "\ n"; str + = "sparse matrix" + this. getClass (). getSimpleName () + "(" + rows + "*" + columns + "): \ n"; int k = 0; // return the k element, if the serial number specified by k is invalid, null Triple elem = this is returned. list. get (k ++); for (int I = 0; I <this. rows; I ++) {for (int j = 0; j <this. columns; j ++) if (elem! = Null & I = elem. row & j = elem. colum) {str + = String. format ("% 4d", elem. value); elem = this. list. get (k ++);} else {str + = String. format ("% 4d", 0) ;}str + = "\ n" ;}return str ;}/ *** returns the matrix of the current matrix and smat, smatc = this + smat, does not change the current matrix. The algorithm adds ** @ param smat * @ return */public SeqSparseMatrix plus (SeqSparseMatrix smat) {if (this. rows! = Smat. rows | this. columns! = Smat. columns) throw new IllegalArgumentException ("two matrices have different order and cannot be added"); // construct rows * columns zero matrix SeqSparseMatrix smatc = new SeqSparseMatrix (this. rows, this. columns); int I = 0, j = 0; // traverse the order tables of the two matrices respectively while (I <this. list. length () & j <smat. list. length () {Triple elema = this. list. get (I); Triple elemb = smat. list. get (j); // if two tuples represent a matrix element at the same position, the corresponding element values are added if (elema. compareTo (elemb) = 0) {// if the sum result is not zero, the new element is created. If (elema. value + elemb. value! = 0) smatc. list. append (new Triple (elema. row, elema. colum, elema. value + elemb. value); I ++; j ++;} else if (elema. compareTo (elemb) <0) {// Add a small Triple copy to the smatc sequence table. // copy the elema element and execute the Triple copy construction method smatc. list. append (new Triple (elema); I ++;} else {smatc. list. append (new Triple (elemb); j ++ ;}// copy the remaining three tuples of the current matrix sequence table and add them to the end of the smatc sequence table while (I <this. list. length () smatc. list. append (new Triple (this. list. get (I ++ )) ); // Add the remaining three tuples in the smat to the end of the smatc sequence table while (j <smatc. list. length () {Triple elem = smat. list. get (j ++); if (elem! = Null) {smatc. list. append (new Triple (elem) ;}} return smatc;}/*** Add the current matrix to the smat matrix. this + = smat changes the current matrix, the algorithm is used together with two polynomials ** @ param smat */public void add (SeqSparseMatrix smat) {if (this. rows! = Smat. rows | this. columns! = Smat. columns) throw new IllegalArgumentException ("two matrices have different order numbers and cannot be added"); int I = 0, j = 0; // insert (or add) The productkey, devicename, and devicesecret of the mat in sequence) to the current matrix triple sequence table while (I <this. list. length () & j <smat. list. length () {Triple elema = this. list. get (I); Triple elemb = smat. list. get (j); // if two tuples represent a matrix element at the same position, the corresponding element values are added if (elema. compareTo (elemb) = 0) {// if the sum result is not 0, the new element if (elema. value + elemb. value! = 0) this. list. set (I ++, new Triple (elema. row, elema. colum, elema. value + elemb. value); else this. list. remove (I); j ++;} else if (elema. compareTo (elemb) <0) {// continue to search for the insert element I ++ of elemb;} else {// copy the elemb element and insert it as this. the I-th element of list this. list. insert (I ++, new Triple (elemb); j ++ ;}// copy and insert the remaining three elements in mat into the current matrix three-element sequence table while (j <smat. list. length () {this. list. append (new Triple (smat. list. get (j ++) ;}// deep Copy public SeqSparseMatrix (SeqSparseMatrix smat) {this (smat. rows, smat. columns); // create an empty sequence table. The default capacity is this. list = new SeqList <Triple> (); // copy all the triplet objects in the smat for (int I = 0; I <smat. list. length (); I ++) this. list. append (new Triple (smat. list. get (I);}/*** compare two matrices for equality */public boolean equals (Object obj) {if (this = obj) return true; if (! (Obj instanceof SeqSparseMatrix) return false; SeqSparseMatrix smat = (SeqSparseMatrix) obj; return this. rows = smat. rows & this. columns = smat. columns & this. list. equals (smat. list);}/*** returns the transpose matrix * @ return */public SeqSparseMatrix transpose () {// constructs the zero matrix, specifying the number of rows and columns SeqSparseMatrix trans = new SeqSparseMatrix (columns, rows); for (int I = 0; I <this. list. length (); I ++) {// Insert the triple trans of the symmetric position element of the matrix. set (this. list. get (I ). toSymmetry ();} return trans ;}}

Test class:

Package com. clarck. datastructure. matrix; /*** sparse matrix compression storage ** sparse matrix triple sequence table ** sparse matrix represented by triple sequence table and addition operation ** @ author clarck **/public class SeqSparseMatrix_test {public static void main (String args []) {Triple [] elemsa = {new Triple (0, 2, 11), new Triple (0, 4, 17), new Triple (1, 1, 20 ), new Triple (3, 0, 19), new Triple (3, 5, 28), new Triple (4, 4, 50)}; SeqSparseMatrix smata = new SeqSparseMatrix (5, 6, el Emsa); System. out. print ("A" + smata. toString (); Triple [] elemsb = {new Triple (0, 2,-11), new Triple (0, 4,-17), new Triple (2, 3, 51), new Triple (3, 0, 10), new Triple (4, 5, 99), new Triple (1, 1, 0 )}; seqSparseMatrix smatb = new SeqSparseMatrix (5, 6, elemsb); System. out. print ("B" + smatb. toString (); SeqSparseMatrix smatc = smata. plus (smatb); System. out. print ("C = A + B" + smatc. toString (); System. o Ut. println (); smata. add (smatb); System. out. print ("A + = B" + smata. toString (); System. out. println ("C. equals ()? "+ Smatc. equals (smata); SeqSparseMatrix smatd = new SeqSparseMatrix (smatb); smatb. set (0, 2, 1); System. out. print ("B" + smatb. toString (); System. out. print ("D" + smatd. toString (); System. out. println ("A transpose" + smata. transpose (). toString ());}}

Running result:

A triple sequence table: (0, 2, 11), (0, 4, 17), (1, 1, 20), (3, 0, 19), (3, 5, 28), (4, 4, 50) sparse matrix SeqSparseMatrix (5*6 ): 0 0 11 0 17 0 0 20 0 0 0 0 0 0 0 0 0 19 0 0 0 0 28 0 0 0 0 0 0 0 0 B triple sequence table :( (0, 2, -11), (0, 4,-17), (2, 3, 51), (3, 0, 10), (4, 5, 99 )) sparse Matrix SeqSparseMatrix (5*6 ): 0 0-11 0-17 0 0 0 0 0 0 0 0 0 51 0 0 0 0 0 0 0 0 0 0 0 99C = A + B triple sequence table :( (1, 1, 20), (2, 3, 51), (3, 0, 29), (3, 5, 28 ),( 4, 4, 50), (4, 5, 99) sparse matrix SeqSparseMatrix (5*6 ): 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51 0 0 0 0 0 0 0 28 0 0 0 0 50 99A + = B triple sequence table :( (1, 1, 20), (2, 3, 51), (3, 0, 29), (3, 5, 28), (4, 4, 50), (4, (5, 99) sparse matrix SeqSparseMatrix (5*6 ): 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51 0 0 29 0 0 0 0 28 0 0 0 0 50 99C. equals ()? TrueB triple sequence table: (0, 2, 1), (0, 4,-17), (2, 3, 51), (3, 0, 10 ), (4, 5, 99) sparse matrix SeqSparseMatrix (5*6 ): 0 0 1 0-17 0 0 0 0 0 0 0 0 0 51 0 0 0 0 0 0 0 0 0 0 0 99D triple sequence table :( (0, 2, -11), (0, 4,-17), (2, 3, 51), (3, 0, 10), (4, 5, 99 )) sparse Matrix SeqSparseMatrix (5*6 ): 0 0-11 0-17 0 0 0 0 0 0 0 0 0 51 0 0 0 0 0 0 0 0 0 0 0 99A transpose triple sequence table :( (0, 3, 29), (1, 1, 20), (3, 2, 51), (4, 4, 50), (5, 3, 28), (5, (4, 99) sparse matrix SeqSparseMatrix (6*5 ): 0 0 0 29 0 0 0 0 0 0 0 0 0 0 0 0 51 0 0 0 0 0 0 0 0 0 0 28 99

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.