forked from tripsycodes/CS-PRJOECT-10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlearning.h
70 lines (61 loc) · 1.9 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
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
//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)
{
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;
}