Channel Avatar

Smarothi @UCqUGaOVyDH5WEPrZSuWwGnw@youtube.com

7 subscribers - no pronouns :c

Hi there!


Welcoem to posts!!

in the future - u will be able to do some more stuff here,,,!! like pat catgirl- i mean um yeah... for now u can only see others's posts :c

Smarothi
Posted 11 months ago

//Fast transpose code in java
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter matrix's total no. of rows");
int r = sc.nextInt();
System.out.println("Enter matrix's total no. of cols");
int c = sc.nextInt();
System.out.println();
int[][] arr = new int[r][c];
int count = 0;

//taking matrix input
for(int i = 0; i<r; i++) {
System.out.println("Enter row " + i + " elements");

for(int j = 0; j<c; j++) {
arr[i][j] = sc.nextInt();
if(arr[i][j] != 0) {
count++;
}
}
System.out.println();
}

//creating sparse matrix
int[][] sparse = new int[3][count];
int ptr = 0;

for(int i = 0; i<r; i++) {
for (int j = 0; j<c; j++) {
if(arr[i][j] != 0) {
sparse[0][ptr] = i;
sparse[1][ptr] = j;
sparse[2][ptr] = arr[i][j];
ptr++;
}
}
}

System.out.println("Sparse matrix:");

for(int i = 0; i<3; i++) {
for(int j = 0; j<count; j++) {
System.out.print(sparse[i][j] + " ");
}
System.out.println();
}

//creating sparse matrix transpose
int[][] trans = new int[count][3];
System.out.println("Transpose of your matrix : ");

for(int i = 0; i<count; i++) {
for(int j = 0; j<3; j++) {
trans[i][j] = sparse[j][i];
System.out.print(sparse[j][i] + " ");
}
System.out.println();
}

//fast transpose
//first row - total row, total c, count
//other rows - r c v of nz item
//size -> [count+1][3]

int[][] fast_main = new int[count+1][3];
fast_main[0][0] = r;
fast_main[0][1] = c;
fast_main[0][2] = count;
System.out.println("\nfast transpose step 1 : ");
System.out.println(fast_main[0][0] + " " + fast_main[0][1] + " " + fast_main[0][2] + " ");

for(int i = 1; i<fast_main.length; i++) {
for(int j = 0; j<3; j++) {
fast_main[i][j] = trans[i-1][j];
System.out.print(fast_main[i][j] + " ");
}
System.out.println();
}

int[] total_matrx = new int[c];
System.out.println("\nTotal matrix : ");
for(int j = 0; j<c; j++) {
int ctr = 0;
for(int i = 0; i<r; i++) {
if(arr[i][j] != 0) {
ctr++;
}
}
total_matrx[j] = ctr;
System.out.println(ctr);
}

int[] index_matrix = new int[c+1];
index_matrix[0] = 1;
System.out.println("\nIndex matrix : \n1");

for(int i = 1; i<index_matrix.length; i++) {
index_matrix[i] = index_matrix[i-1] + total_matrx[i-1];
System.out.println(index_matrix[i]);
}

int[][] final_transpose = new int[count+1][3];
final_transpose[0][0] = fast_main[0][1];
final_transpose[0][1] = fast_main[0][0];
final_transpose[0][2] = fast_main[0][2];

for(int i = 1; i<final_transpose.length; i++) {
int index = index_matrix[fast_main[i][1]];
index_matrix[fast_main[i][1]]++;
final_transpose[index][0] = fast_main[i][1];
final_transpose[index][1] = fast_main[i][0];
final_transpose[index][2] = fast_main[i][2];
}
System.out.println("\nFinal Fast Transpose matrix : ");
for(int i = 0; i< final_transpose.length; i++) {
System.out.print(final_transpose[i][0] + " " + final_transpose[i][1] + " " + final_transpose[i][2]);
System.out.println();
}

}
}

0 - 0