当前位置:网站首页>Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
2022-07-03 19:50:00 【Rhinoceros Superman】
One 、 Ideas .
1. establish .
You can assign a string directly , But for 0 The elements of should also be assigned in turn , More trouble , But easy to understand can also be achieved .
Secondly, we can also conceive the assignment of triples , Only assign non-zero elements and its rows , Number of columns , When printing if Judge , Output without assignment 0, It's easy .
When creating a structure , A matrix needs to have its total number of rows and columns , And for triples , You also need the row and column of each element , And the sum of non-zero elements of this triple .
2. Traverse .
For three tuples , It includes non-zero element set and zero element set , For lines with non-zero elements , Number of columns , Carry on the double for loop , If the row of non-zero elements , Column number and for The variables in the loop are equal , Just output the value of this number , Otherwise output 0.
3. Transposition .
Transpose is to exchange row numbers and column numbers , If you order by line , Time complexity is too high , Therefore, column precedence is generally used .
- First of all to Total rows , Total number of columns , And the assignment of the total number of non-zero elements .
- Secondly, traverse the non-zero elements by column loop
- If the column number of the non-zero element == The column number of the outer cycle , Then proceed to line , Exchange of column numbers .
The detailed illustration of the rapid transposition method is a little complicated , So I didn't do , But the code has , And it's working .
4. Add , Subtraction , Multiplication .
Addition is very simple , stay A,B In two matrices , If the positions of non-zero elements are the same , Then the new matrix C The value of this position is =A,B The value of the combined , If not, the matrix C The value of is equal to A The value of or B Value .
Subtraction is adding a matrix *-1
If multiplication is equal, multiply , Don't care if it's not equal , Because even if you manage it, you will end up with 0;
Two 、 Specific code .
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#define false 0
#define true 1
#define max 100
typedef struct {
int value; //value Is the specific value
int row,col; //row For the line ,col Column
}Array;
typedef struct {
Array data[max+1];
int rows,cols,nums; //nums Is the number of non-zero elements
}Arrays;
// Create sparse matrix
int InitArray(Arrays *L,int rows,int cols,int nums){
int i,j;
L->nums=nums;
L->cols=cols;
L->rows=rows;
assert(L->nums<max);
printf("-------------\n");
printf(" Please enter the number of rows of non-zero elements in sequence , Number of columns , value :\n");
for (int i = 1; i <= L->nums; i++) {
scanf("%d %d %d", &L->data[i].row, &L->data[i].col, &L->data[i].value);
}
printf(" Create success !\n");
return true;
}
// Traverse the output sparse matrix Triple form
void bianli1(Arrays *L) {
printf("------------- Triple form traversal output :\n");
for (int i = 1; i <= L->rows; i++) {
printf("%d That's ok %d Column %d \n",L->data[i].row,L->data[i].col,L->data[i].value);
}
}
// Traverse the output sparse matrix Matrix form
void bianli2(Arrays *L) {
int flag = 1; //flag representative
for (int i = 1; i <= L->rows; i++) {
for (int j = 1; j <=L->cols; j++) {
if (L->data[flag].value!=0&&L->data[flag].row==i&&L->data[flag].col==j) {
printf(" %d", L->data[flag].value);
flag++;
}
else {
printf(" 0");
}
}
printf("\n");
}
}
// Matrix general transpose ( By column )
int Transform(Arrays a,Arrays *b){ // //a Is the original matrix ,b Is the transposed matrix
b->cols=a.cols;
b->nums=a.nums;
b->rows=a.rows;
if(b->nums>0){
int j=1;
for(int k=1;k<=a.cols;k++){
for(int i=1;i<=a.nums;i++){ // Enter the non-zero element cycle
if(a.data[i].col==k){ // If there are non-zero elements in each column , Then perform transpose operation
b->data[j].row=a.data[i].col; //j Representative table B Non-zero element number of , It is from 1 Start .
b->data[j].col=a.data[i].row;
b->data[j].value=a.data[i].value;
j++;
if(j>a.nums) return false;
}
}
}
}
}
// Matrix fast transpose
void FastTransform(Arrays *a,Arrays *b){ //a Is the original matrix ,b Is the transposed matrix
int num[max],position[max];
int col,i,k,m;
b->cols=a->cols;
b->nums=a->nums;
b->rows=a->rows;
if(b->nums>0){
for(col=1;col<=a->cols;col++){ // First of all, will num The values of the array are assigned 0
num[col]=0;
}
for(i=1;i<=a->nums;i++){ // Then traverse non-zero elements , Put the column of non-zero elements in num Array ++ operation
num[a->data[i].col]++;
}
position[1]=1; // The first non-zero element position Value is defined as 1
for(col=2;col<=a->cols;col++){
position[col]=position[col-1]+num[col-1]; // The starting position of non-zero elements in the previous column plus the number of non-zero elements is equal to the starting position of the next column
}
for(k=1;k<=a->nums;k++){ // Traverse the non-zero elements and transpose them in turn
col=a->data[k].col;
m=position[col];
b->data[m].row=a->data[k].col;
b->data[m].col=a->data[k].row;
b->data[m].value=a->data[k].value;
position[col]++;
}
}
}
// Matrix addition
int ADD(Arrays a,Arrays b,Arrays *c){
int k=1,i,j; //i by a The number of elements ,j by b The number of elements ,k by c The number of elements
// Only those in the same row and column can be added
if(a.cols!=b.cols||a.rows!=b.rows){
return false;
}
c->cols=a.cols; // Total number of assigned rows , The total number of columns
c->rows=a.rows;
// Perform traversal assignment
for(i=1,j=1;i<=a.nums&&j<=b.nums;){
//B The line number of is greater than A Direct will A Add elements in C matrix
if(a.data[i].row<b.data[j].row){
c->data[k].col=a.data[i].col;
c->data[k].row=a.data[i].row;
c->data[k].value=a.data[i].value;
k++; //C The element is incremented by one digit
i++; //a Assignment rule a The element is incremented by one digit , If the time is b be b The element is incremented by one digit
}
//B The line number of is less than A Direct will B Add elements in C matrix
else if(a.data[i].row>b.data[j].row){
c->data[k].col=b.data[j].col;
c->data[k].row=b.data[j].row;
c->data[k].value=b.data[j].value;
k++;
j++;
}else{ // Same line number
//B The column number of is less than A Direct will B Add elements in C matrix
if(a.data[i].col>b.data[j].col) {
c->data[k].col=b.data[j].col;
c->data[k].row=b.data[j].row;
c->data[k].value=b.data[j].value;
k++;
j++;
}
//B The column number of is greater than A Direct will A Add elements in C Moment
else if(a.data[i].col<b.data[j].col){
c->data[k].col=a.data[i].col;
c->data[k].row=a.data[i].row;
c->data[k].value=a.data[i].value;
k++;
i++;
}
// equal
else {
c->data[k].col=a.data[i].col;
c->data[k].row=a.data[i].row;
c->data[k].value=a.data[i].value+b.data[j].value;
k++;
i++;
j++;
}
}
}
while(i<=a.nums){ //B Take it out A Not finished
// take A The remaining elements in are added to C in
c->data[k].col=a.data[i].col;
c->data[k].row=a.data[i].row;
c->data[k].value=a.data[i].value;
k++;
i++;
}
while(j<=b.nums){ //A Take it out B Not finished
// take A The remaining elements in are added to C in
c->data[k].col=b.data[j].col;
c->data[k].row=b.data[j].row;
c->data[k].value=b.data[j].value;
k++;
j++;
}
}
// Matrix subtraction
int reduce(Arrays a,Arrays b,Arrays *c){ // Call the addition operation , stay b Multiply on the basis of matrix -1 In Canada a matrix
for(int i=1;i<=b.nums;i++){
b.data[i].value=b.data[i].value*-1;
}
ADD(a,b,c);
}
// Matrix multiplication
int multipe(Arrays a,Arrays b,Arrays *c){
int m=1,n=1,k=1; //m by a The number of elements ,n by b The number of elements ,k by c The number of elements
if(a.cols!=b.cols||a.rows!=b.rows){
return false;
}
c->cols=a.cols;
c->rows=a.rows;
while(m<=a.nums&&n<=b.nums){
if(a.data[m].col==b.data[n].col&&a.data[m].row==b.data[n].row){
c->data[k].col=a.data[m].col;
c->data[k].row=a.data[m].row;
c->data[k].value=a.data[m].value*b.data[n].value;
}
m++;n++;k++;
}
}
int main(){
Arrays s;
Arrays s1;
Arrays s2;
Arrays s3;
int row, col, num;
// establish
printf(" Please input the sparse matrix in sequence A The number of rows , Number of columns , The number of nonzero elements ( Space off ):\n");
scanf("%d %d %d", &row, &col, &num);
InitArray(&s,row,col,num);
bianli2(&s);
printf(" Please input the sparse matrix in sequence B The number of rows , Number of columns , The number of nonzero elements ( Space off ):\n");
scanf("%d %d %d", &row, &col, &num);
InitArray(&s1,row,col,num);
bianli2(&s1);
printf("--------- matrix B The transpose of is :\n");
Transform(s1,&s3);
bianli2(&s3);
printf("--------- Matrix multiplication is :\n");
multipe(s,s1,&s2);
bianli2(&s2);
printf("--------- The matrix is added to :\n");
ADD(s,s1,&s2);
bianli2(&s2);
printf("--------- The matrix is subtracted to :\n");
reduce(s,s1,&s2);
bianli2(&s2);
}
3、 ... and 、 Achieve results .
边栏推荐
- 2022-06-25 advanced network engineering (XI) IS-IS synchronization process of three tables (neighbor table, routing table, link state database table), LSP, cSNP, psnp, LSP
- Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
- NFT without IPFs and completely on the chain?
- 10 smart contract developer tools that miss and lose
- 3. Data binding
- 2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
- Pecan - route
- Detailed explanation of shuttle unity interworking principle
- Use of aggregate functions
- 2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
猜你喜欢
第二章:求长方体数组,指定区间内的完全数,改进指定区间内的完全数
Xctf attack and defense world crypto master advanced area olddriver
Chapter 1: recursively find the factorial n of n!
05 -- QT OpenGL draw cube uniform
2022-06-25 advanced network engineering (XI) IS-IS synchronization process of three tables (neighbor table, routing table, link state database table), LSP, cSNP, psnp, LSP
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
Kubernetes cluster builds efk log collection platform
Detailed explanation of shuttle unity interworking principle
Make a simple text logo with DW
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
随机推荐
Compared with 4G, what are the advantages of 5g to meet the technical requirements of industry 4.0
Day10 ---- 强制登录, token刷新与jwt禁用
第二章:求长方体数组,指定区间内的完全数,改进指定区间内的完全数
Floating source code comment (38) parallel job processor
Zhang Fei hardware 90 day learning notes - personal record of day 3, please see my personal profile / homepage for the complete
5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
Chapter 20: y= sin (x) /x, rambling coordinate system calculation, y= sin (x) /x with profile graphics, Olympic rings, ball rolling and bouncing, water display, rectangular optimization cutting, R que
Phpstudy set LAN access
Promethus
Class loading process
Day10 -- forced login, token refresh and JWT disable
Difference between surface go1 and surface GO2 (non professional comparison)
3. Data binding
QT -- qfile file read / write operation
Web Security (VIII) what is CSRF attack? Why can token prevent csdf attacks?
P1891 crazy LCM (Euler function)
NFT without IPFs and completely on the chain?