-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsparsemat.c
53 lines (47 loc) · 1.21 KB
/
sparsemat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stddef.h>
#include <stdint.h>
#include "polx.h"
#include "sparsemat.h"
void sparsemat_polx_mul(sparsemat *r, const polx *c, const sparsemat *a) {
size_t i;
r->len = a->len;
for(i=0;i<a->len;i++) {
r->rows[i] = a->rows[i];
r->cols[i] = a->cols[i];
polx_mul(&r->coeffs[i],c,&a->coeffs[i]);
}
}
void sparsemat_polx_mul_add(sparsemat *r, const polx *c, const sparsemat *a) {
size_t i,j;
for(i=0;i<a->len;i++) {
for(j=0;j<r->len;j++) {
if(r->rows[j] == a->rows[i] && r->cols[j] == a->cols[i]) {
polx_mul_add(&r->coeffs[j],c,&a->coeffs[i]);
break;
}
}
if(j == r->len) {
r->rows[j] = a->rows[i];
r->cols[j] = a->cols[i];
polx_mul(&r->coeffs[j],c,&a->coeffs[i]);
r->len += 1;
}
}
}
void sparsemat_scale_add(sparsemat *r, const sparsemat *a, int64_t s) {
size_t i,j;
for(i=0;i<a->len;i++) {
for(j=0;j<r->len;j++) {
if(r->rows[j] == a->rows[i] && r->cols[j] == a->cols[i]) {
polx_scale_add(&r->coeffs[j],&a->coeffs[i],s);
break;
}
}
if(j == r->len) {
r->rows[j] = a->rows[i];
r->cols[j] = a->cols[i];
polx_scale(&r->coeffs[j],&a->coeffs[i],s);
r->len += 1;
}
}
}