-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathakmatrix.c
235 lines (196 loc) · 5.2 KB
/
akmatrix.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* A matrix handling package based on the example in
* Korpela & Larmela: C-ohjelmointikieli, OtaData, 1986,(pages 80-81).
* It was the C programming language book of Helsinki
* University of Technology, pretty much the same as K&R C book.)
*
* Modernized and expanded by Antti Ketola
*/
#include <stdio.h>
#include <stdlib.h>
#include "akmatrix.h"
void allocMat(matrix a, int m, int n) {
for(int i=0;i<m;i++){
a[i] = (double *) malloc(n * sizeof(double));
}
}
void setMat(matrix a, int m, int n, double value) {
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j] = value;
}
}
}
void applyFxMat(matrix a, int m, int n, double (*fx)(double x)) {
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j] = fx(a[i][j]);
}
}
}
void addMat(matrix a, matrix b, int m, int n) {
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j] = a[i][j] + b[i][j];
}
}
}
void subMat(matrix a, matrix b, int m, int n) {
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j] = a[i][j] - b[i][j];
}
}
}
/**
* Copies values from matrix a to matrox b
* a and b must have same dimensions
*/
void copyMat(matrix a, matrix b, int rows, int cols){
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
b[i][j] = a[i][j];
}
}
}
/**
* Reads floating point numbers to the matrix a from the console
*/
int readMat(matrix a, int m, int n) {
int count = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(scanf("%lf",&a[i][j]) != 1) {
return count;
} else {
count++;
}
}
}
return count;
}
/**
* Prints the matrix a of dimensions m x n.
* w is the field width of each element in the printout
*
*/
void printMat(matrix a, char *matname, int m, int n, int w) {
char printformat[10];
//printf("\nPRINTMAT:\n");
sprintf(printformat," %%f%%c",w); /* adding the desired char field width to the end */
//printf("\n FORMAT=%s\n",printformat);
printf("\n%s:",matname);
printf("\n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
printf(printformat,a[i][j], (j==n-1) ? '\n' : ' ');
}
}
}
/****
*
* Matrix multiplication
*
* matrix a of doubles, dimensions dim_a_row by dim_a_col
* matrix b of doubles, dimensions dim_b_row by dim_b_col
*
* matrix b must have the same y- dimension as a has the x-dimension,
*
* returns result in c which must be previously allocated
*
*
*/
void matMul(matrix a, matrix b, matrix c, int dim_a_row, int dim_a_col, int dim_b_row) {
double sum;
//printf("\nMatMul a_row=%d, a_col=%d, b_row=%d\n", dim_a_row, dim_a_col, dim_b_row);
for(int i=0;i<dim_a_row;i++) {
for(int j=0;j<dim_b_row;j++) {
sum=0;
for(int k=0;k<dim_a_col;k++){
sum +=a[i][k]*b[k][j];
}
c[i][j] = sum;
}
}
}
/**
* Calculates matrix sum of a and b to c
* All three matrices must be equals in dimensions
*/
void matSum(matrix a, matrix b, matrix c, int dim_rows, int dim_cols) {
printf("\nMatSum rows=%d, cols=%d\n", dim_rows, dim_cols);
for(int i=0;i<dim_rows;i++) {
for(int j=0;j<dim_cols;j++) {
c[i][j] =a[i][j] + b[i][j];
}
}
}
/**
* Multiplies a matrix a with a scalar "multiplier".
* Result in matrix c, which must have the same dimensions as matrix a.
*/
void matScal(matrix a, matrix c, int dim_rows, int dim_cols, double multiplier) {
printf("\nMatScal rows=%d, cols=%d, multiplier=%f\n", dim_rows, dim_cols, multiplier);
for(int i=0;i<dim_rows;i++) {
for(int j=0;j<dim_cols;j++) {
c[i][j] = a[i][j] * multiplier;
}
}
}
/**
* Transposes the matrix a storing result in c.
*
* If the matrix a has dim_a_row rows and dim_a_col columns,
* the matrix c MUST have dim_a_col rows and dim_a_row columns.
*/
void matTranspose(matrix a, matrix c, int dim_rows, int dim_cols) {
for(int i=0;i<dim_rows;i++) {
for(int j=0;j<dim_rows;j++) {
c[i][j] = a[j][i];
}
}
}
/**
* Fills matrix a as unit matrix, i.e.
* the values at the diagonal are set to 1
* otherwise to 0
*/
void unitMat(matrix a, int m, int n) {
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i==j)
a[i][j] = 1.0;
else
a[i][j] = 0.0;
}
}
}
/**
* Copy array of N elements of doubles to
* 1 x N matrix of doubles element by element.
*
* The matrix m must be defined DEF_MAT(m,1,N)
* and allocated allocMat(m,1,N)
*/
void array2matX1(double a[],matrix m, int N) {
for(int i=0;i<N;i++) {
m[0][i] = a[i];
}
}
/**
* Compares matrices a and b element by element for equality
* While floating point numbers (doubles) are not always exact
* a comparison tolerance is introduced.
*
*/
int compareMat(matrix a, matrix b, int dim_row, int dim_col, double tolerance){
for(int i=0;i<dim_row;i++) {
for(int j=0;j<dim_col;j++) {
if(abs(a[i][j]-b[i][j])>tolerance) {
//printf("\nOut of tolerance %d at i=%d j=%d ",tolerance,i,j);
return 0;
}
}
}
return 1;
}