Data structure Java Implementation--④ array--sparse matrix ternary sequential storage--matrix inversion

Source: Internet
Author: User


Author information


Text Description


Inversion: The line of matrix A is replaced by the corresponding column , and the resulting new matrix is called a transpose matrix, which is recorded as at or a.


The first column of the matrix is usually the first row of the transpose matrix, and the first row is the first column of the transpose matrix.


So, in the order storage structure of ternary group that has been stored, how to realize the inversion of the matrix???


The first reaction must be a direct traversal, and then the row and column can be swapped, but because the line number is required to take precedence (can also be the number of priority, but must be inverted before and after the inverted in a prioritized manner), so if you swap directly, then it will become There is no order , but one order is needed, which is not good.



1. Normal inversion


Now that you want to make a row, the line number after the swap is the column number before the swap , so you can scan the column number multiple times.

from the beginning of the small column number to scan the swap, then the swap must be able to meet the line number first


2, fast inverted


It is clear that for normal inversion, it is possible to scan the entire storage space multiple times, wasting a lot of time, so it can be optimized


We can first record the number of elements in each column (after the inverted line), and then set aside space


Then scan the element directly into the appropriate reserved position.




Code Implementation



1. Normal inversion


/** * @Stone6762 */public Sparsearray transpose () {Sparsearray TM = new Sparsearray (nums);//Create a post-transpose matrix object tm.cols = rows;// Row and column changes, non-0 number invariant tm.rows = Cols;tm.nums = Nums;int q = 0;//from small to large scan column number, then change for (int col = 0; col < cols; col++) {for (int p = 0; P < nums; p++) {if (data[p].getcolumn () = = col) {Tm.data[q].setcolumn (Data[p].getrow ()); Tm.data[q].setrow (Data[p].getcolumn () ); Tm.data[q].setvalue (Data[p].getvalue ()); q++;}}} return TM;}



2. Fast Inversion

/** * @Stone6762 */public sparsearray fasttranspose () {/* * First reserve the location and then "fill in the blanks". num [cols]; the size of each "empty".  Copt[cols]; The starting position of each "empty" */sparsearray TM = new Sparsearray (nums);//Create a Transpose object Tm.cols = rows;//column changes, Non-0 element number invariant tm.rows = Cols;tm.nums = Nums;int Tcol = 0, Indexofc = 0;if (Nums > 0) {int[] num = new int[cols];//original matrix in col column  The number of non-0 elements int[] Copt = new int[cols];//The initial value of n is the first non-0 element of the Col column in the TM location//Initialize num and copt array for (int i = 0; i < nums; i++) {Tcol = Data[i].getcolumn (); num[tcol]++;} Copt[0] = 0;for (int i = 1; i < cols; i++) {copt[i] = Copt[i-1] + num[i-1];} Find the position of each element in the transpose ternary group for (int i = 0; i < nums; i++) {tcol = Data[i].getcolumn ();//Get the column value of the first element in the sweep tn Tcolindexofc = Copt [tcol];//gets the position of the next element of the Tcol column should be stored Tm.data[indexofc].setrow (Data[i].getcolumn ()); Tm.data[indexofc].setcolumn (data[i ].getrow ()); Tm.data[indexofc].setvalue (Data[i].getvalue ()); copt[tcol]++;//Copt[col at this point) indicates where the next col column element will be stored}} return TM;}






Data structure Java Implementation--④ array--sparse matrix ternary sequential storage--matrix inversion

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.