-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmstd.c
61 lines (49 loc) · 1.32 KB
/
mmstd.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
54
55
56
57
58
59
60
61
/* C implementation of the miniMatlab standard library */
#include <stdio.h>
#include <stdlib.h>
int printStr(char *string)
{ return printf("%s",string); }
int printInt(int value)
{ return printf("%d",value); }
int printDouble(double value)
{ return printf("%lf",value); }
int readInt(int *addr)
{ return !scanf("%d",addr); }
int readDouble(double *addr)
{ return !scanf("%lf",addr); }
int rows(void *ptr)
{ return ((int*)ptr)[0]; }
int cols(void *ptr)
{ return ((int*)ptr)[1]; }
int printMat(void *ptr) {
int ret = 0 , r = rows(ptr) , c = cols(ptr) , i , j;
double *mat = (double*)ptr; ++mat;
for( i = 0 ; i < r ; ++i ) {
for( j = 0 ; j < c ; ++j , ++mat )
ret += printf("%10.4lf ",*mat);
ret += printf("\n");
}
return ret;
}
void matMult(void *ret,void *lx,void *rx) {
int u = rows(lx) , v = cols(lx) , w = cols(rx);
if( v != rows(rx) ) abort();
if( rows(ret) != u || cols(ret) != w ) abort();
double *z = (double*)ret; z++;
double *x = (double*)lx; x++;
double *y = (double*)rx; y++;
int i , j , k;
for( i = 0 ; i < u ; i++ ) {
for( j = 0 ; j < w ; j++ ) {
double *xp = x + i * v , *yp = y + j;
double val = 0.0;
for( k = 0 ; k < v ; k++ ) {
// fprintf(stderr,"%lf,%lf\n",*xp,*yp);
val += (*xp) * (*yp);
xp++; yp += w;
}
*z = val;
z++;
}
}
}