Matrix class: Two-dimensional array implementation!!
Each row is considered a one-dimensional array, which can be put into several sets;
Random numbers can be produced, and the maximum value in parentheses is used.
Matrix class: public class mymatrix { int[][] matrix ;//Matrix array Random random =new random () ;//random Number object//default construction method, generate 3*3 matrix Public mymatrix () { //default is the 3*3 matrix matrix = new int[3][3];//initial matrix for (int i=0;i<matrix.length;i+ +) {for (int j=0;j<matrix[i].length;j++) {matrix[i][j]=random.nextint (100);}}} Construction method for generating square matrices Public mymatrix (int n) { matrix = new int[n][n]; //initial matrix for (int i=0;i<matrix.length;i++) {for (int j=0;j<matrix[i].length; J + +) {matrix[i][j]=random.nextint (100);}}} Generates a m*n matrix. Public mymatrix (int m,int n) {matrix = new int[m][n];//initial matrix for (int i=0;i< matrix.length;i++) {for (int j=0;j<matrix[i].length;j++) {matrix[i][j]=random.nextint (100);}}} Generates a matrix Public mymatrix (Int[][] matrix) {This.matrix = matrix, according to a known two-dimensional array;} Returns a matrix array public Int[][] getmatrix () {Return this.matrix;} Print Matrix Public void print () {for (int i=0;i<matrix.length;i++) {for (Int j=0;j<matrix[i]. length;j++) {System.out.print (matrix[i][j]+ " ");} System.out.println ();} }//transpose Matrix Public mymatrix transport () { //row variable column int m = matrix[ 0].length;//Column Change Line int n = matrix.length; Mymatrix transmatrix = new mymatrix (m,n);//Initialize//The new matrix is traversed here, and the original matrix symmetry points are placed in for (int i=0;i< transmatrix.matrix.length;i++) {for (int j=0;j<transmatrix.matrix[i].length;j++) {transMatrix.matrix[i][j ] = matrix[j][i];}} return transmatrix; }//The judgment matrix is the upper triangular matrix Public boolean isuptriangle () { for (int i=1;i<matrix.length;i++) { for (int j=0;j<i;j++) { if (matrix[i][j]!=0) { return false; } } } return true;} Judging if it is the lower triangular matrix public boolean isdowntriangle () { for (int i=0;i<matrix.length;i++) { for (int j=matrix[i].length-1;j>i;j--) { if (matrix[i][j]!=0) { return false; } } } return true; } //Judging whether it is a symmetric matrix Public boolean issynmetry () { for ( int i=1;i<matrix.length;i++) { //here can be j<i, because only half view can, this will repeat &NBSP;&NBSP; for (int j=0;j<matrix[i].length;j++) { if ( Matrix[i][j]!=matrix[j][i]) { return false; } } } return true; } //Matrix summation public void add (mymatrix b) { int m = b.matrix.length; int n = B.matrix[0].length; if (m!=matrix.length| | N!=matrix[0].length) { System.out.println ("The matrix type is not the same, cannot add!") "); } else { for (int i=0;i<matrix.length;i++) { for (int j=0;j<matrix[i].length;j++) { matrix[i][j]+=b.matrix[i][j]; } } } }}
A compression algorithm for symmetric matrices:
The lower triangle or the upper triangle exists in a one-dimensional array;
The two-dimensional array of data in a triangular line of the existence of a one-dimensional array, when taken out, corresponding to the n*n loop, find the location of a one-dimensional array of data in a two-dimensional array, that is, a two-dimensional array.
Symmetric matrix class Compression public class synmematrix { A is used to store a copy of the Matrix double [] a;//the order of the matrix elements int n; //matrices; int m; //the number of one-dimensional arrays public Synmematrix (int n) {//The number of elements that need to be persisted is m=n* (n+1)/2 ;//here to note the size of M m = n* (n+1)/2 ;a = New double[m];this.n = n;} Initializes a two-dimensional array of public void evaluate (double[][] b) { int k=0; for (int i=0;i<n;i++) { for (int j=0;j<n;j++) { if (I>=J) { // System.out.println ("a[" +k+ "]=" +b[i][j]); //Save the data in B to a copy of the //because it's a symmetric array, A triangular line of a row of the inside, so as to save space for the role of a[k++]=b[i][j]; //only the lower triangular elements } } }}//is initialized by a one-dimensional array, this one-dimensional array is a copy of the symmetric matrix element / /b is a copy, passed in, so with a equal public void evaluate (double[] b) {for (int k=0;k<m;k++) {a[k]= b[k];}} Symmetric matrix addition Public synmematrix add (SYNMEMATRIX&NBSP;B) { synmematrix t = New synmematrix (n); int k; for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { //k refers to the position in the copy, according to I and J to find the value of the position should be the same, two synmematrix the same, so with the same rules if (I>=J) { k= i* (i-1)/2+j-1; } else { k= j* (j-1)/2+i-1; } t.a[k] = a[k]+b.a[k]; } } return t;} Print symmetric matrix Public void print () { int k; //is a copy, a one-dimensional array, Therefore, we can find the data of the corresponding position in one array from the position coordinates of the two-dimensional data, that is, the data for (int i=1;i<=n;i++) in the two-dimensional array { for (int j=1;j<=n;j++) { if (i>=j) { k= i* (i-1)/2+j-1; } else { k= j* (j-1)/2+i-1; } system.out.print (" " +a[k]); } system.out.println (); }}}
Sparse matrix compression algorithm:
Only the location and elements of the data are stored in ternary group;
The storage structure of ternary group can be used in one-dimensional array or linked list structure;
This uses the previous Myvector collection to store non-0 data information;
The following is a matrix of coefficients established by the structure of arrays;
Ternary class, non-0 element information, rows, columns, data//Each node is a data point public class three {public int row;public int col;public double value;public three (int r,int c,double v) {this.row = r;this.col = c;this.value = v;} Public three () {this (0,0,0.0);}} Sparse matrix class//required rows and columns, and non-0 element number and position can be public class spamatrix { int rows; // Number of rows int cols; //number of columns int dnum;//non 0 elements//v is a collection of non-0 elements, the order in V is not important, because they are independent of each other Myvector v; spamatrix (Int max) { rows = cols = dNum=0; v = New myvector (max); } //array of triples based on user To initialize the matrix public void evaluate (int r,int c,int d,three[] Item) throws exception   { this.rows = r; this.cols = c; this.dnum = d; for (int i=0;i<d;i++) { v.add (I, item[i]); } } //Sparse Matrix Transpose public SpaMatrix Transport () throws exception { // The sparse matrix requires no more than 0 elements of location and data, so the size of the incoming V is constructed; spamatrix a = new spamatrix (v.size ()); a.rows = this.cols; a.cols = this.rows; a.dnum = this.dnum; for (int i=0;i<dnum;i++) { Three t = (three) V.get (i); &Nbsp; a.v.add (I, new three (T.col,t.row,t.value)); } return a; } //method for printing sparse matrices public void print () throws Exception &NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (number of rows in the matrix: +this.rows); System.out.println ("Number of columns of the matrix:" +this.cols), system.out.println ("Number of elements not 0:" +this.dnum); system.out.println ("Matrix ternary is:"); for (int i =0;i<dnum;i++) { system.out.println ("a<" + ((three) v.get (i)). row+ "," + ((three) v.get (i)). col+ ">=" + ((three) v.get (i)). value); } }}
It can also be implemented using the linked list structure:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/2C/wKiom1XeaazwVPlAAABvwkubjDk226.jpg "title=" Picture 2.png "alt=" wkiom1xeaazwvplaaabvwkubjdk226.jpg "/> picture unclear, text description: Each node has location information, data information and the next node location, this method is not very good.
Arrays are combined with chained structures:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/2C/wKiom1XeaU-g4V1mAACGiuFZm6Q989.jpg "title=" Picture 1.png "alt=" Wkiom1xeau-g4v1maacgiufzm6q989.jpg "/>
The picture is not clear, the text description: This method is better than the place is very clear, the array record row, the following list records the column information in the row, connected in turn.
Learning Log---matrix representation and special matrix compression