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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/*
* Task 2 - Kronecker Product
*
* GUEST LANGUAGE: THIS IS THE C VERSION OF ch-2.pl.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <ctype.h>
bool debug=false;
#define MAXROWS 20
#define MAXCOLS 20
typedef int matrix[MAXROWS][MAXCOLS];
// process_flag_noarg( argc, argv );
// Process the -d flag, and check that there are no
// remaining arguments.
void process_flag_noarg( int argc, char **argv )
{
int arg=1;
if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
{
debug = true;
arg++;
}
int left = argc-arg;
if( left != 0 )
{
fprintf( stderr,
"Usage: kronecker-product [-d]\n" );
exit(1);
}
}
//
// mat_show( m, rows, cols );
// Show matrix m
//
void mat_show( matrix m, int rows, int cols )
{
// first, calculate the max cell width
int width = 1;
for( int i=0; i<rows; i++ )
{
for( int j=0; j<cols; j++ )
{
char buf[128];
sprintf( buf, "%d", m[i][j] );
int len = strlen(buf);
if( len > width )
{
width = len;
}
}
}
#if 0
fprintf( stderr, "width=%d\n", width );
exit(1);
#endif
for( int i=0; i<rows; i++ )
{
fputs( "[ ", stdout );
for( int j=0; j<cols; j++ )
{
printf( "%*d ", width, m[i][j] );
}
putchar( ']' );
putchar( '\n' );
}
}
//
// kronecker_product( a, ar, ac, b, br, bc, c, &rows, &cols );
// Form the Kronecker product of two matrices A (ar rows x ac cols)
// and B (br rows x bc cols) in C (and setting rows and cols
// to the dimensions of C).
// using the formula (A x B)i,j = Ai/p,j/q * Bi%p,j%q
// [where B is of size p x q]
//
void kronecker_product( matrix a, int m, int n, matrix b, int p, int q, matrix c, int *rp, int *cp )
{
int rows = m * p;
int cols = n * q;
//fprintf( stderr, "rows=%d, cols=%d\n", rows, cols );
for( int i=0; i<rows; i++ )
{
int arow = i / p;
int brow = i % p;
for( int j=0; j<cols; j++ )
{
int acol = j / q;
int bcol = j % q;
c[i][j] = a[arow][acol] * b[brow][bcol];
}
}
*rp = rows;
*cp = cols;
}
void small( void )
{
matrix a = { {1,2}, {3,4} };
//mat_show( a, 2, 2 );
matrix b = { {5, 6}, {7, 8} };
//mat_show( b, 2, 2 );
puts( "[ 1, 2 ] [5, 6]" );
puts( "[ 3, 4 ] k* [7, 8]" );
puts( "is:" );
matrix c;
int rows, cols;
kronecker_product( a, 2, 2, b, 2, 2, c, &rows, &cols );
mat_show( c, rows, cols );
}
void big( void )
{
matrix a = { {1,-4,7}, {-2,3,3} };
//mat_show( a, 2, 3 );
matrix b = {
{8, -9, -6, 5},
{1, -3, -4, 7},
{2, 8, -8, -3},
{1, 2, -5, -1},
};
//mat_show( b, 4, 4 );
puts( "[ 1, -4, 7 ] [8, -9, -6, 5]" );
puts( "[ -2, 3, 3 ] [1, -3, -4, 7]" );
puts( " k* [2, 8, -8, -3]" );
puts( " [1, 2, -5, -1]" );
puts( "is:" );
matrix c;
int cr, cc;
kronecker_product( a, 2, 3, b, 4, 4, c, &cr, &cc );
mat_show( c, cr, cc );
}
int main( int argc, char **argv )
{
process_flag_noarg( argc, argv );
small();
putchar( '\n' );
big();
return 0;
}
|