-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmlearning.h
788 lines (671 loc) · 18.4 KB
/
mlearning.h
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define ALLOC(p, n) do { \
if (!((p) = calloc((n), sizeof(*(p))))) { \
fprintf(stderr, "Memory allocation failure\n"); \
exit(1); \
} \
} while (0)
#define PI 3.14159265
#define float double
int size(int*arr){
int out = sizeof(arr)/sizeof(arr[0]);
}
//To create a function to calculate the length of an array
int length(int arr[]) {
int length = 0;
int i = 0;
while (arr[i] != '\0') {
length++;
i++;
}
return length;
}
//To create a function that tells the dimension of an array
int dimension(void *arr){
int dim = 1;
int size = sizeof(int);
while(1){
if((void*)arr != NULL){
dim++;
arr = ((void**)arr)[1]; //increasing the dimension of the array
}
else break;
}
if(dim>=1) return dim;
else return -1;
}
//To create a function to determine the shape of an array
int shape(void *arr)
{
int dim_size = size(arr);
int shape = dim_size / sizeof(int);
return shape;
}
//To return an equally spaced array
double* linspace(double start, double end, int n) {
double *x;
int i = 0;
double step = (end - start) / ((double)n-1); //the space between to consecutive elements
x = (double*)calloc(n, sizeof(double)); //allocation of memory and initialisation woth 0
x[i] = start;
i=1;
while(i<n){
x[i] = x[i - 1] + step;
i++;
}
x[n - 1] = end;
return x;
}
//to return an array equally spaced on the logarithmic scale
double* logspace(double start, double end, int n, double base)
{
double* result;
result = (double*)calloc(n,sizeof(double));
double log_start = log10(start) / log10(base);
double log_end = log10(end) / log10(base);
double step = (log_end - log_start) / (n - 1); //calculating the difference between 2 numbers ont he logarithmic scale
int i = 0;
while(i<n){
result[i] = pow(base, log_start + i * step);
i++;
}
return result;
}
// Values are generated within the half-open interval [start, stop), with spacing between values given by step.
double* arange(double start, double end, double step) {
int n = (int)((end - start) / step) + 1;
double* result = (double*)calloc(n , sizeof(double));
for (int i = 0; i < n; i++) {
result[i] = start + i * step;
}
return result;
}
//To calculate mean of an array
int mean(int*arr) {
int n = length(arr); // Number of data points
double sum = 0.0;
double mean;
int i=0;
while(i<n){
sum += arr[i];
i++;
}
mean = sum / n;
return mean;
}
typedef struct {
int row;
int col;
} Index;
Index* argwhere(int **indicesArray, int rows, int cols, int reqCondition, int *length) {
Index* indices = malloc(rows * cols * sizeof(Index));
if (indices == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
int count = 0;
// Loop through the array to find indices where the value is greater than the threshold
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (indicesArray[i][j] > reqCondition) {
indices[count].row = i;
indices[count].col = j;
count++;
}
}
}
*length = count;
return indices;
}
void pad_array(int *array, int rows, int cols, int pad_top, int pad_bottom, int pad_left, int pad_right) {
int new_rows = rows + pad_top + pad_bottom;
int new_cols = cols + pad_left + pad_right;
int *padded_array = (int *)malloc(new_rows * new_cols * sizeof(int));
// Fill padded_array with original array
for (int i = 0; i < new_rows; i++) {
for (int j = 0; j < new_cols; j++) {
if (i >= pad_top && i < rows + pad_top && j >= pad_left && j < cols + pad_left) {
padded_array[i * new_cols + j] = array[(i - pad_top) * cols + (j - pad_left)];
} else {
padded_array[i * new_cols + j] = 0; // Assuming padding with zeros
}
}
}
// Copy padded_array back to array
for (int i = 0; i < new_rows; i++) {
for (int j = 0; j < new_cols; j++) {
array[i * new_cols + j] = padded_array[i * new_cols + j];
}
}
free(padded_array);
}
// Function to convert degrees to radians
double toRadians(double degree) {
return degree * (PI / 180);
}
// Function to calculate factorial
double factorial(int n) {
double result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
// Function to calculate sine using Taylor series
double sine(double angle) {
angle = toRadians(angle);
double result = 0;
double power = angle;
double sign = 1;
for (int i = 1; i <= 10; ++i) {
result += sign * power / factorial(2 * i - 1);
power *= angle * angle;
sign = -sign;
}
return result;
}
// Function to calculate cosine using Taylor series
double cosine(double angle) {
angle = toRadians(angle);
double result = 0;
double power = 1;
double sign = 1;
for (int i = 0; i <= 10; ++i) {
result += sign * power / factorial(2 * i);
power *= angle * angle;
sign = -sign;
}
return result;
}
// Function to calculate tangent using Taylor series
double tangent(double angle) {
angle = toRadians(angle);
double result1 = 0;
double power1 = angle;
double sign1 = 1;
for (int i = 1; i <= 10; ++i) {
result1 += sign1 * power1 / factorial(2 * i - 1);
power1 *= angle * angle;
sign1 = -sign1;
}
angle = toRadians(angle);
double result2 = 0;
double power2 = 1;
double sign2 = 1;
for (int i = 0; i <= 10; ++i) {
result2 += sign2 * power2 / factorial(2 * i);
power2 *= angle * angle;
sign2 = -sign2;
}
double result=0;
result = result1/result2;
return result;
}
// Function to calculate cotangent using Taylor series
double cotangent(double angle) {
angle = toRadians(angle);
double result1 = 0;
double power1 = angle;
double sign1 = 1;
for (int i = 1; i <= 10; ++i) {
result1 += sign1 * power1 / factorial(2 * i - 1);
power1 *= angle * angle;
sign1 = -sign1;
}
angle = toRadians(angle);
double result2 = 0;
double power2 = 1;
double sign2 = 1;
for (int i = 0; i <= 10; ++i) {
result2 += sign2 * power2 / factorial(2 * i);
power2 *= angle * angle;
sign2 = -sign2;
}
double result=0;
result = result2/result1;
return result;
}
// Function to calculate cosecant using Taylor series
double cosecant(double angle) {
angle = toRadians(angle);
double result = 0;
double power = angle;
double sign = 1;
for (int i = 1; i <= 10; ++i) {
result += sign * power / factorial(2 * i - 1);
power *= angle * angle;
sign = -sign;
}
result =1/result;
return result;
}
// Function to calculate secant using Taylor series
double secant(double angle) {
angle = toRadians(angle);
double result = 0;
double power = 1;
double sign = 1;
for (int i = 0; i <= 10; ++i) {
result += sign * power / factorial(2 * i);
power *= angle * angle;
sign = -sign;
}
result=1/result;
return result;
}
void polynomial(int *arr, int length)
{
int deg = length - 1;
int n = 0;
int j = 0;
while (j < length)
{
if (arr[j] == 0)
break;
if (arr[j] != 0)
{
n++;
}
j++;
}
int terms = n; // No. of terms
int *exp = (int *)malloc(terms * sizeof(int));
int exponent[terms];
int k = 0;
while (k < terms)
{
exponent[k] = deg - k;
k++;
}
int i = 0;
while (i < length)
{
if (arr[i] != 0)
{
printf("%dx^%d", arr[i], exponent[i]);
if(i < n - 1){
printf(" + ");
}
}
i++;
}
free(exp);
}
void integrate(int *arr, int length)
{
int deg = length - 1;
int n = 0;
int j = 0;
while (j < length)
{
if (arr[j] == 0)
break;
if (arr[j] != 0)
{
n++;
}
j++;
}
int terms = n; // No. of terms
int *exp = (int *)malloc(terms * sizeof(int));
int exponent[terms];
int k = 0;
while (k < terms)
{
exponent[k] = deg - k;
k++;
}
int i = 0;
int exp, coeff;
while (i < length)
{
if (arr[i] != 0)
{
coeff = arr[i]/(exponent[i] + 1);
exp = exponent[i] + 1;
if(coeff != 0){
printf("%dx^%d", arr[i], exponent[i]);
}
if(i < n - 1){
printf(" + ");
}
}
i++;
}
free(exp);
}
void differentiate(int *arr, int length)
{
int deg = length - 1;
int n = 0;
int j = 0;
while (j < length)
{
if (arr[j] == 0)
break;
if (arr[j] != 0)
{
n++;
}
j++;
}
int terms = n; // No. of terms
int *exp = (int *)malloc(terms * sizeof(int));
int exponent[terms];
int k = 0;
while (k < terms)
{
exponent[k] = deg - k;
k++;
}
int i = 0;
int exp, coeff;
while (i < length)
{
if (arr[i] != 0)
{
coeff = arr[i]*exponent[i];
exp = exponent[i] - 1;
if(coeff != 0){
printf("%dx^%d", arr[i], exponent[i]);
}
if(i < n - 1){
printf(" + ");
}
}
i++;
}
free(exp);
}
void *reshape_2d_3d(size_t id1, size_t id2, int iar[][id2],
size_t od1, size_t od2, size_t od3) {
// oar is a pointer to a multidimensional array; in this case, it will point to the first element of an array of arrays (of arrays).
int (*oar)[od2][od3];
size_t size1 = id1 * id2;
size_t size2 = od1 * od2 * od3;
size_t min_size = (size1 <= size2) ? size1 : size2;
ALLOC(oar, od1);
for (size_t i = 0; i < min_size; i++) {
oar[i / (od2 * od3)][(i / od3) % od2][i % od3] = iar[i / id2][i % id2];
}
return oar;
}
void *reshape_1d_2d(size_t id1, int* iar,
size_t od1, size_t od2) {
// oar is a pointer to a multidimensional array; in this case, it will point to the first element of an array of arrays (of arrays).
int (*oar)[od2];
size_t size1 = id1;
size_t size2 = od1 * od2;
size_t min_size = (size1 <= size2) ? size1 : size2;
ALLOC(oar, od1);
for (size_t i = 0; i < min_size; i++) {
oar[i / (od2)][i % od2] = iar[i];
}
return oar;
}
void *reshape_1d_3d(size_t id1, int* iar,
size_t od1, size_t od2, size_t od3) {
// oar is a pointer to a multidimensional array; in this case, it will point to the first element of an array of arrays (of arrays).
int (*oar)[od2][od3];
size_t size1 = id1;
size_t size2 = od1 * od2 * od3;
size_t min_size = (size1 <= size2) ? size1 : size2;
ALLOC(oar, od1);
for (size_t i = 0; i < min_size; i++) {
oar[i / (od2 * od3)][(i / od3) % od2][i % od3] = iar[i];
}
return oar;
}
void regression(int n,int ax[],int ay[])
{
int i;
float x, y, m, c, d;
float sumx = 0, sumxsq = 0, sumy = 0, sumxy = 0;
float sumysq = 0;
float mae = 0;
float mse = 0;
float p = n;
for(int i=0;i<n;i++)
{
sumx = sumx + x;
sumxsq = sumxsq + (x * x);
sumy = sumy + y;
sumxy = sumxy + (x * y);
sumysq = sumysq + (y * y);
ax[i] = x;
ay[i] = y;
}
d = p * sumxsq - sumx * sumx;
m = (p* sumxy - sumx * sumy) / d;
c = (sumy * sumxsq - sumx * sumxy) / d;
while(1)
{
printf("1.compute sum\n");
printf("2.find best fitting line\n");
printf("3.end\n");
int num;
scanf("%d", &num);
if (num == 1)
{
printf("sum x = %lf\n", sumx);
printf("sum y = %lf\n", sumy);
printf("sum xy = %lf\n", sumxy);
printf("sum xsq = %lf\n", sumxsq);
printf("sum ysq = %lf\n", sumysq);
}
else if (num == 2)
{
float p = n;
printf("the best fitting line is y = %lfx+%lf\n", m, c);
for (int i = 0; i < n; i++)
{
mae += abs(ay[i] - (m * ax[i] + c));
mse += (ay[i] - (m * ax[i] + c)) * (ay[i] - (m * ax[i] + c));
}
printf("mean absolute error = %lf\n", mae / p);
printf("mean squared error = %lf\n", mse / p);
}
else if(num==3)
{
return 0;
}
}
}
// Function to calculate the square root of a number
double sqrt(double x) {
double guess = x / 2.0;
double prevGuess;
do {
prevGuess = guess;
guess = (guess + x / guess) / 2.0;
} while (prevGuess - guess > 0.00001); // Change the threshold for desired precision
return guess;
}
// Function to calculate the inverse trigonometric arcsine (asin)
double arcsin(double x) {
if (x < -1.0 || x > 1.0)
return NAN; // Not a Number for invalid input
double result = 0.0;
double term = x;
double xSquared = x * x;
double coefficient = 1.0;
for (int n = 1; n <= 100; ++n) { // Change the number of terms for desired precision
result += term * coefficient;
term *= xSquared * (2 * n - 1) / (2 * n);
coefficient *= (2 * n - 1) / (2 * n);
}
return PI / 2 - result;
}
// Function to calculate the inverse trigonometric arccosine (acos)
double arccos(double x) {
if (x < -1.0 || x > 1.0)
return NAN; // Not a Number for invalid input
double result = 0.0;
double term = x;
double xSquared = x * x;
double coefficient = 1.0;
for (int n = 1; n <= 100; ++n) { // Change the number of terms for desired precision
result += term * coefficient;
term *= xSquared * (2 * n - 1) / (2 * n);
coefficient *= (2 * n - 1) / (2 * n);
}
return result;
}
// Function to calculate the inverse trigonometric arctangent (atan)
double arctan(double x) {
if (x == 0.0)
return 0.0;
if (x < -1.0 || x > 1.0)
return NAN; // Not a Number for invalid input
double result = 0.0;
double term = x;
double xSquared = x * x;
for (int n = 1; n <= 100; ++n) { // Change the number of terms for desired precision
result += term / n;
term *= -xSquared;
}
return result;
}
// Function to calculate the inverse trigonometric arccotangent
double arccot(double x) {
if (x == 0.0)
return PI / 2;
if (x < -1.0 || x > 1.0)
return NAN; // Not a Number for invalid input
double result = 0.0;
double term = x;
double xSquared = x * x;
for (int n = 1; n <= 100; ++n) { // Change the number of terms for desired precision
result += term / n;
term *= -xSquared;
}
return PI / 2 - result;
}
// Function to calculate the inverse trigonometric arcsecant
double arcsec(double x) {
if (x <= -1.0 || x >= 1.0)
return NAN; // Not a Number for invalid input
double result = 0.0;
double term = 1/x;
double xSquared = 1/x * 1/x;
double coefficient = 1.0;
for (int n = 1; n <= 100; ++n) { // Change the number of terms for desired precision
result += term * coefficient;
term *= xSquared * (2 * n - 1) / (2 * n);
coefficient *= (2 * n - 1) / (2 * n);
}
return result;
}
// Function to calculate the inverse trigonometric arccosecant
double arccsc(double x) {
if (x <= -1.0 || x >= 1.0)
return NAN; // Not a Number for invalid input
double result = 0.0;
double term = 1/x;
double xSquared = 1/x * 1/x;
double coefficient = 1.0;
for (int n = 1; n <= 100; ++n) { // Change the number of terms for desired precision
result += term * coefficient;
term *= xSquared * (2 * n - 1) / (2 * n);
coefficient *= (2 * n - 1) / (2 * n);
}
return result;
}
void *flat(int *arr) {
int n = shape(arr);
int *flatArray = (int *)malloc(n * sizeof(int));
if (flatArray == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
int *ptr = flatArray; // Pointer to the beginning of flatArray
while (*arr != '\0') {
*ptr = *arr; // Assign the value pointed to by arr to the value pointed to by ptr
ptr++; // Move ptr to the next element in flatArray
arr++; // Move arr to the next element in arr
}
return flatArray;
}
// Function to print a matrix
void printMatrix(int rows, int cols, int matrix[rows][cols]) {
printf("Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
// Function to add two matrices
void matrixAddition(int rows, int cols, int matrix1[rows][cols], int matrix2[rows][cols], int result[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
// Function to subtract two matrices
void matrixSubtraction(int rows, int cols, int matrix1[rows][cols], int matrix2[rows][cols], int result[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
typedef struct
{
double real;
double complex;
} Complex;
double *real(double *arr)
{
double a = arr[0];
double b = arr[1];
double c = arr[2];
double D = pow(b, 2) - 4 * a * c;
if (D != 0)
{
double r1 = (-b - sqrt(D)) / (2 * a);
double r2 = (-b + sqrt(D)) / (2 * a);
double root[2] = {r1, r2};
return root;
}
else
{
double r = -b / (2 * a);
double root[1] = r;
return root;
}
}
Complex *comp(double *arr)
{
Complex r1, r2;
double a = arr[0];
double b = arr[1];
double c = arr[2];
double D = pow(b, 2) - 4 * a * c;
r1.real = (-b) / (2 * a);
r2.real = (-b) / (2 * a);
r1.complex = sqrt(-D) / (2 * a);
r2.complex = -sqrt(-D) / (2 * a);
Complex root[2] = {r1, r2};
return root;
}
void root(double *arr)
{
double a = arr[0];
double b = arr[1];
double c = arr[2];
double D = pow(b, 2) - 4 * a * c;
if (D >= 0)
{
real(arr);
}
else
{
comp(arr);
}
}