-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrix.HC
More file actions
338 lines (273 loc) · 5.4 KB
/
Matrix.HC
File metadata and controls
338 lines (273 loc) · 5.4 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//Matrix Implementation
//Last Change June 16th, 2023
//Author Austin Sierra for Church of AI Christ
//Subject to Creative Commons BY-NC 4.0
#include "Vector.HC";
//Matrix Object Declaration
public class Matrix
{
I64 x,y;
Vector *data;
Matrix *next,*last;
};
//Initialize Matrix of size size
Matrix *CreateMatrix(I64 row,I64 col)
{
I64 i,j, size;
Matrix *m = MAlloc(sizeof(Matrix));
Matrix *first = m;
size = sizeof(Vector);
for(i=0;i<row;i++)
{
m->data = CreateVector(col);
for(j=0;j<col;j++)
{
m->x = row; m->y = col;
SetVector(m->data,0,j);
}
if(i!=row-1)
{
Matrix *temp = m;
m->next=MAlloc(sizeof(Matrix));
m= m->next;
m->last =temp;
}
else
{
m->next=first;
first->last = m;
}
}
return first;
}
//Set Matrix m at data position row to Vector d
U0 SetMatrixVector(Matrix *m,Vector *d, I64 row)
{
try{
I64 i=0;
while(i<row)
{
m = m->next;
i++;
}
Vector *vMS = m->data;
I64 sz = d->size;
I64 sz2 = vMS->size;
if(sz!=sz2)
{
"Cannot Set Matrix Value to Matrix of Different Size";
PutExcept;
}
for(i=0;i<d->size;i++)
{
SetVector(m->data,GetVector(d,i),i);
}
}
catch{
"Matrix Vector Location Out of Range";
PutExcept;
};
}
//Set Matrix m at data position row col to value d
U0 SetMatrix(Matrix *m,F64 d, I64 row,I64 col)
{
try{
I64 i=0;
while(i<row)
{
m = m->next;
i++;
}
SetVector(m->data,d,col);
}
catch{
"Matrix Location Out of Range";
PutExcept;
};
}
//Prints the data values of Matrix m
U0 PrintMatrix(Matrix *m)
{
I64 i,j;
"[";
for(i=0;i<m->x;i++)
{
for(j=0;j<m->y;j++)
{
F64 value = GetVector(m->data,j);
Print("%.2f",value);
if(j<m->y-1){
", ";
}
}
if(i<m->x-1){
",\n ";
}
m = m->next;
}
" ]\n";
}
//returns the data value at position x,y in Matrix m
F64 GetMatrix(Matrix *m, I64 row, I64 col)
{
I64 i=0;
while(i<row)
{
m = m->next;
i++;
}
return GetVector(m->data,col);
}
//returns the Vector at row x.
Vector *GetMatrixVector(Matrix *m, I64 row)
{
I64 i=0;
while(i<row)
{
m = m->next;
i++;
}
return m->data;
}
//Add two matrix of the same size
Matrix *AddMatrix(Matrix *m1, Matrix *m2)
{
I64 mX, mY;
if(m1->x != m2->x || m1->y != m2->y){
"Error: attempt to add matrixes of different size.";
return m1;
}
else
{
Matrix *sum = CreateMatrix(m1->x,m1->y);
for(mX=0;mX<m1->x;mX++)
{
for(mY=0;mY<m1->y;mY++)
{
SetMatrix(sum,GetMatrix(m1,mX,mY)+GetMatrix(m2,mX,mY),mX,mY);
}
}
return sum;
}
}
//Subtract two matrix of the same size
Matrix *SubMatrix(Matrix *m1, Matrix *m2)
{
I64 mX, mY;
if(m1->x != m2->x || m1->y != m2->y){
"Error: attempt to subtract matrixes of different size.";
return m1;
}
else
{
Matrix *dif = CreateMatrix(m1->x,m1->y);
for(mX=0;mX<m1->x;mX++)
{
for(mY=0;mY<m1->y;mY++)
{
SetMatrix(dif,GetMatrix(m1,mX,mY)-GetMatrix(m2,mX,mY),mX,mY);
}
}
return dif;
}
}
//Calculate the dot product of two Matrix m1 and m2
F64 MultiplyMatrix(Matrix *m1, Matrix *m2)
{
if(m1->y != m2->x)
{
"Error: attempt for matrix multiplication of unknown size.";
return m1;
}
else
{
Matrix *product = CreateMatrix(m1->x,m2->y);
I64 pX,pY,sums;
for(pX=0;pX<product->x;pX++)
{
for(pY=0;pY<product->y;pY++)
{
F64 currentProduct = 0;
//sums holds the index value of the
//current row/column product (that gets added across the integral)
for(sums=m1->y;sums!=0;sums--)
{
currentProduct += GetMatrix(m1,pX,m1->y-sums) *GetMatrix(m2,m2->x-sums,pY);
}
SetMatrix(product,currentProduct, pX,pY);
}
}
return product;
}
}
//Multiply a matrix by a scalar
Matrix *ScaleMatrix(Matrix *m, F64 scalar)
{
Matrix* scaled = CreateMatrix(m->x,m->y);
I64 mX,mY;
for(mX =0;mX<m->x;mX++){
for(mY =0;mY<m->y;mY++){
SetMatrix(scaled,GetMatrix(m,mX,mY)*scalar,mX,mY);
}
}
return scaled;
}
//Free a created Matrix m
U0 DestroyMatrix(Matrix *m)
{
I64 i;
for(i =0;i<m->x;i++)
{
Vector *v = m->data;
DestroyVector(v);
m = m->last;
Free(m->next);
}
}
//Main to demo matrix
U0 MainMatrix()
{
I32 matrixSizeX = 3, matrixSizeY = 2;
Matrix *m1= CreateMatrix(matrixSizeX,matrixSizeY);
Matrix *m2= CreateMatrix(matrixSizeY,matrixSizeX);
Matrix *m3= CreateMatrix(matrixSizeY,matrixSizeY);
Matrix *m4= CreateMatrix(matrixSizeY,matrixSizeY);
"m1 = \n";
SetMatrix(m1,1,0,0);
SetMatrix(m1,1,0,1);
SetMatrix(m1,2,1,0);
SetMatrix(m1,3,1,1);
SetMatrix(m1,5,2,0);
SetMatrix(m1,8,2,1);
PrintMatrix(m1);
"\nm2 = \n";
SetMatrix(m2,2,0,0);
SetMatrix(m2,2,0,1);
SetMatrix(m2,4,0,2);
SetMatrix(m2,4,1,0);
SetMatrix(m2,8,1,1);
SetMatrix(m2,8,1,2);
PrintMatrix(m2);
"\nm2 * 2 =\n";
PrintMatrix(ScaleMatrix(m2,2));
"\nm1 * m2 = \n";
PrintMatrix(MultiplyMatrix(m1,m2));
DestroyMatrix(m1);
DestroyMatrix(m2);
"\nm3 = \n";
SetMatrix(m3,1,0,0);
SetMatrix(m3,3,0,1);
SetMatrix(m3,2,1,0);
SetMatrix(m3,4,1,1);
PrintMatrix(m3);
"\nm4 = \n";
SetMatrix(m4,4,0,0);
SetMatrix(m4,2,0,1);
SetMatrix(m4,3,1,0);
SetMatrix(m4,1,1,1);
PrintMatrix(m4);
"\nm3 - m4:\n";
PrintMatrix(SubMatrix(m3,m4));
DestroyMatrix(m3);
DestroyMatrix(m4);
}//MainMatrix;