Sparse Matrix Multiplication (ternary Array Storage)

Source: Internet
Author: User

// Sparse matrix multiplication (ternary Array Storage) 1240
# Include <stdio. h>
# Include <stdlib. h>

Typedef struct
{
Int R;
Int C;
Int V;
} Spnode;

Typedef struct
{
Int Mu, nu, Tu;
Spnode data [7001];
} Spmatrix;

Int num [10001], rpot [10001];
Spmatrix * a, * B, * C;

Void num_rpot ()
{
Int I, J;
For (I = 0; I <(B-> mu); I ++)
Num [I] = 0;
For (I = 1; I <= (B-> Tu); I ++)
{
J = B-> data [I]. R;
Num [J] ++;
}
Rpot [0] = 1;
For (I = 1; I <(B-> mu); I ++)
Rpot [I] = rpot [I-1] + num [I-1];
}

Int main ()
{// Freopen ("1.txt"," r ", stdin );
A = (spmatrix *) malloc (sizeof (spmatrix ));
B = (spmatrix *) malloc (sizeof (spmatrix ));
C = (spmatrix *) malloc (sizeof (spmatrix ));
Int I, K;

Scanf ("% d", & A-> Mu, & A-> Nu, & A-> Tu );

For (I = 1; I <= (a-> Tu); I ++)
Scanf ("% d", & A-> data [I]. r, & A-> data [I]. c, & A-> data [I]. V );

Scanf ("% d", & B-> Mu, & B-> Nu, & B-> Tu );
For (I = 1; I <= (B-> Tu); I ++)
Scanf ("% d", & B-> data [I]. r, & B-> data [I]. c, & B-> data [I]. V );

C-> mu = A-> mu;
C-> Nu = B-> Nu;
If (a-> Nu! = B-> mu)
Return 0;
Else if (a-> tu * B-> Tu = 0)
Printf ("% d 0 \ n", C-> Mu, C-> Nu );
Else
{
Num_rpot ();
Int temp [10001];
Int J, R, Q, P, T;
P = 1; // indicates the position of the current non-zero element in A. Data
R = 0; // count the number of non-zero elements in C

For (I = 0; I <(a-> mu); I ++)
{
For (j = 0; j <(B-> Nu); j ++)
Temp [J] = 0; // The initialization of CIJ's Accumulators

While (a-> data [p]. r = I) // find
{
K = A-> data [p]. C; // column number of the current non-zero element in
If (k <(B-> mu)-1) // determine the position of the last Non-zero element in row K of B. Data
T = rpot [k + 1]-1;
Else
T = (B-> Tu );
For (q = rpot [k]; q <= T; q ++) // each non-zero element of row K in B
{
J = B-> data [Q]. C;
Temp [J] = temp [J] + (a-> data [p]. v) * (B-> data [Q]. V );
}
P ++;
} // While

For (k = 0; k <(B-> Nu); k ++)
If (temp [k])
{
R ++;
C-> data [R]. r = I;
C-> data [R]. c = K;
C-> data [R]. V = temp [k];
}
} //
C-> Tu = R;

Printf ("% d \ n", C-> Mu, C-> Nu, C-> Tu );
If (c-> tu> = 1)
For (I = 1; I <= r; I ++)
Printf ("% d \ n", C-> data [I]. r, C-> data [I]. c, C-> data [I]. V );
}
Return 0;
}

 

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.