source: doc/theses/mike_brooks_MMath/programs/ar-bchk/treatment.c

Last change on this file was 80e83b6c, checked in by Peter A. Buhr <pabuhr@…>, 3 weeks ago

last proofread array chapter

  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include <stdio.h>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21void mul( size_t m,
22 size_t n, size_t p,
23 float res[m][n],
24 float lhs[m][p],
25 float rhs[p][n] ) {
26 for ( size_t i = 0;
27 i < m; i++ )
28 for ( size_t j = 0;
29 j < n; j++ ) {
30 res[i][j] = 0.0;
31 for ( size_t k = 0;
32 k < p; k++ )
33 @res[i][j] +=@
34 @lhs[i][k] *@
35 @rhs[k][j];@
36 }
37}
38
39
40
41
42
43
44
45
46
47#ifdef RUNIT
48
49
50static void zero( size_t r, size_t c, float mat[r][c] ) {
51 for ( size_t i = 0; i < r; i++ )
52 for ( size_t j = 0; j < c; j++ )
53 mat[i][j] = 0.0;
54}
55
56
57static void fill( size_t r, size_t c, float mat[r][c] ) {
58 for ( size_t i = 0; i < r; i++ )
59 for ( size_t j = 0; j < c; j++ )
60 mat[i][j] = 1.0 * (i + 1) + 0.1 * (j+1);
61}
62
63
64static void print( size_t r, size_t c, float mat[r][c] ) {
65 for ( size_t i = 0; i < r; i++ ) {
66 for ( size_t j = 0; j < c; j++ )
67 printf("%2g ", mat[i][j]);
68 printf("\n");
69 }
70}
71
72#define EXPSZ_M 2
73#define EXPSZ_N 3
74#define EXPSZ_P 4
75
76
77int main() {
78 float res[ EXPSZ_M ][ EXPSZ_N ]; zero( EXPSZ_M, EXPSZ_N, res );
79 float lhs[ EXPSZ_M ][ EXPSZ_P ]; fill( EXPSZ_M, EXPSZ_P, lhs );
80 float rhs[ EXPSZ_P ][ EXPSZ_N ]; fill( EXPSZ_P, EXPSZ_N, rhs );
81 mul( EXPSZ_M, EXPSZ_N, EXPSZ_P, res, lhs, rhs );
82 print(EXPSZ_M, EXPSZ_P, lhs);
83 printf("*\n");
84 print(EXPSZ_P, EXPSZ_N, rhs);
85 printf("=\n");
86 print(EXPSZ_M, EXPSZ_N, res);
87}
88
89
90
91#endif
Note: See TracBrowser for help on using the repository browser.