The compression algorithm---of the symmetric matrix of Java data structure

Source: Internet
Author: User

Tag: return denotes int matrix add ATI Mat i++ add support

Special matricesA special matrix is a class of matrices in which there are many elements with the same value or a number of 0 elements, and the distribution of elements with the same value or 0 elements has a certain regularity. A two-dimensional array is generally used to store matrix elements. However, for a special matrix, it is possible to store a copy of the same element by locating the mathematical mapping formula for all values of the same element in the matrix, thus achieving the purpose of compressing the amount of data stored. Compression storage of special matrices only one copy of the same matrix element is stored. This method of compressing storage is to find out the distribution law of special matrix data elements, and store only one copy of the same matrix elements. the compression storage correspondence relation of n-order symmetric matrices

Aij=aji 1<=i<=n,1<=j<=n

Number of elements m = n (n+1)/2

Print the line I of the symmetric matrix, the element of column J, and the subscript relationship to the one-dimensional array:

i* (i-1)/2+j-1 when I>=j

k=

j* (j-1)/2+i-1 when I<j

The two-dimensional arrays with unequal lengths are supported in two-dimension arrays, and for n-order symmetric matrices, it is possible to compress storage by simply applying for storing the two-dimensional arrays required for the lower triangular (or upper-triangular) matrix elements. Two-dimensional array structure with unequal length
1 //a compression algorithm for symmetric matrices2  Public classSymematric {3 4     Double[] A;//Matrix Element5     intN//the order of the Matrix6     intM//The number of elements of a one-dimensional array-length7 8      PublicSymematric (intN) {9         //The length of a one-dimensional array that is required to save to a one-dimensional array without repeating elements in the symmetric matrixTen         //2-order symmetric matrices correspond to (1+2=3)-dimensional arrays, 3-order symmetric matrices correspond to 1+2+3=6-dimensional arrays, One         //The 4-order symmetric matrices correspond to 1+2+3+4-dimensional arrays, and the N-order symmetric matrices correspond to the first N and A         //so the value of the length m of a one-dimensional array is the first n of 1,2,3...N and -m = n * (n + 1)/2;  -A =New Double[m]; the          This. N =N; -     } -  -     //initialized with a two-dimensional array +      Public voidEvalute (Double[] b) { -         intK = 0; +          for(inti = 0; I < n; i++) { A              for(intj = 0; J < N; J + +) { at                 //I >= J indicates that only the lower triangular elements are saved -                 if(I >=j) { -a[k++] =B[i][j]; -                 } -             } -         } in     } -  to     //initialized by a one-dimensional array, this one-dimensional array is a copy of the symmetric matrix element +      Public voidEvalute (Double[] b) { -          for(intk = 0; K < M; k++) { theA[K] =B[k]; *         } $     }Panax Notoginseng  -     //Symmetric matrix addition the      Publicsymematric Add (symematric b) { +Symematric T =Newsymematric (n); A         intK; the          for(inti = 1; I <= N; i++) { +              for(intj = 1; J <= N; J + +) { -                 if(I >=j) { $K = i * (i-1)/2 + j-1; $}Else { -K = J * (j-1)/2 + i-1; -                 } the                 //sum -T.A[K] = A[k] +B.a[k];Wuyi             } the         } -         returnT; Wu     } -  About     //Print the symmetric matrix, this is the key!!  $      Public voidprint () { -         intK; -          for(inti = 1; I <= N; i++) { -              for(intj = 1; J <= N; J + +) { A                 if(I >=j) { +K = i * (i-1)/2 + j-1; the}Else { -K = J * (j-1)/2 + i-1; $                 } theSystem.out.print ("" +a[k]); the             } the System.out.println (); the         } -     } in  the}
1  Public classTest {2      Public Static voidMain (string[] args) {3         4Symematric M1 =NewSymematric (3);5Symematric m2 =NewSymematric (3);6 symematric m3;7 8         Double[] A = {{1, 0, 0}, {2, 3, 0}, {4, 5, 6 } };9         Double[] b= {1,2,3,4,5,6};Ten          One M1.evalute (a); A M2.evalute (b); -          - m1.print (); the System.out.println (); - System.out.println (); - m2.print (); -     } +}

The compression algorithm---of the symmetric matrix of Java data structure

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.