forked from swarupgt/Matrix-operations-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
quickmaths.c
243 lines (214 loc) · 4.87 KB
/
quickmaths.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
233
234
235
236
237
238
239
240
241
242
243
#include<stdlib.h>
#include<stdio.h>
#include <math.h>
#include "MatrixLibrary.h"
float fMax(Matrix A)
{
float max = A.MatPtr[0][0];
for(int i = 0; i< A.r; i++)
{
for(int j = 0; j<A.c; j++)
{
if(A.MatPtr[i][j] > max)
max = A.MatPtr[i][j];
}
}
return max;
}
float fMin(Matrix A)
{
float min = A.MatPtr[0][0];
for(int i = 0; i< A.r; i++)
{
for(int j = 0; j<A.c; j++)
{
if(A.MatPtr[i][j] < min)
min = A.MatPtr[i][j];
}
}
return min;
}
Matrix maxMat(Matrix mat, int axis)
{
Matrix max_mat;
if(axis == 0)
{
initialiseZero(&max_mat, 1, mat.c);
float cmax;
for(int i = 0; i<mat.c;i++)
{
cmax = mat.MatPtr[i][0];
for(int j=0;j<mat.r;j++)
{
if(mat.MatPtr[j][i] > cmax)
cmax = mat.MatPtr[j][i];
}
max_mat.MatPtr[0][i] = cmax;
}
}
else
{
initialiseZero(&max_mat, mat.r, 1);
float rmax;
for(int i = 0; i<mat.r;i++)
{
rmax = mat.MatPtr[0][i];
for(int j=0;j<mat.c;j++)
{
if(mat.MatPtr[i][j] > rmax)
rmax = mat.MatPtr[i][j];
}
max_mat.MatPtr[i][0] = rmax;
}
}
return max_mat;
}
Matrix minMat(Matrix mat, int axis)
{
Matrix min_mat;
if(axis == 0)
{
initialiseZero(&min_mat, 1, mat.c);
float cmin;
for(int i = 0; i<mat.c;i++)
{
cmin = mat.MatPtr[i][0];
for(int j=0;j<mat.r;j++)
{
if(mat.MatPtr[j][i] < cmin)
cmin = mat.MatPtr[j][i];
}
min_mat.MatPtr[0][i] = cmin;
}
}
else
{
initialiseZero(&min_mat, mat.r, 1);
float rmin;
for(int i = 0; i<mat.r;i++)
{
rmin = mat.MatPtr[0][i];
for(int j=0;j<mat.c;j++)
{
if(mat.MatPtr[i][j] < rmin)
rmin = mat.MatPtr[i][j];
}
min_mat.MatPtr[i][0] = rmin;
}
}
return min_mat;
}
Matrix sumMat(Matrix mat, int axis)
{
Matrix sum_mat;
if(axis == 0)
{
initialiseZero(&sum_mat, 1, mat.c);
float csum = 0.0;
for(int i = 0; i<mat.c;i++)
{
csum = 0.0;
for(int j=0;j<mat.r;j++)
{
csum += mat.MatPtr[j][i];
}
sum_mat.MatPtr[0][i] = csum;
}
}
else
{
initialiseZero(&sum_mat, mat.r, 1);
float rsum = 0.0;
for(int i = 0; i<mat.r;i++)
{
rsum = 0.0;
for(int j=0;j<mat.c;j++)
{
rsum += mat.MatPtr[i][j];
}
sum_mat.MatPtr[i][0] = rsum;
}
}
return sum_mat;
}
float fsum(Matrix mat)
{
float s;
for(int i = 0;i<mat.r;i++)
{
for(int j = 0;j<mat.c;j++)
{
s += mat.MatPtr[i][j];
}
}
return s;
}
Matrix meanMat(Matrix mat, int axis)
{
Matrix mean_mat;
if(axis == 0)
{
initialiseZero(&mean_mat, 1, mat.c);
float csum = 0.0;
for(int i = 0; i<mat.c;i++)
{
csum = 0.0;
for(int j=0;j<mat.r;j++)
{
csum += mat.MatPtr[j][i];
}
mean_mat.MatPtr[0][i] = csum/mat.r;
}
}
else
{
initialiseZero(&mean_mat, mat.r, 1);
float rsum = 0.0;
for(int i = 0; i<mat.r;i++)
{
rsum = 0.0;
for(int j=0;j<mat.c;j++)
{
rsum += mat.MatPtr[i][j];
}
mean_mat.MatPtr[i][0] = rsum/mat.c;
}
}
return mean_mat;
}
Matrix stdMat(Matrix mat, int axis)
{
Matrix std_mat,mean_mat;
if(axis == 0)
{
initialiseZero(&std_mat, 1, mat.c);
mean_mat = meanMat(mat, axis =0);
float csum = 0.0;
for(int i = 0; i<mat.c;i++)
{
csum = 0.0;
for(int j=0;j<mat.r;j++)
{
csum += pow((mean_mat.MatPtr[0][i] - mat.MatPtr[j][i]),2);
}
std_mat.MatPtr[0][i] = csum/mat.r;
}
}
else
{
initialiseZero(&std_mat, mat.r, 1);
mean_mat = meanMat(mat, axis =1);
float rsum = 0.0;
for(int i = 0; i<mat.r;i++)
{
rsum = 0.0;
for(int j=0;j<mat.c;j++)
{
rsum += pow((mean_mat.MatPtr[i][0]-mat.MatPtr[i][j]),2);
}
std_mat.MatPtr[i][0] = rsum/mat.c;
}
}
std_mat = powMat(std_mat,0.5);
return std_mat;
}