-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibmath.h
More file actions
222 lines (176 loc) · 8.58 KB
/
libmath.h
File metadata and controls
222 lines (176 loc) · 8.58 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
#ifndef LIBMATH_H
#define LIBMATH_H
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <math.h>
#include <string.h>
#ifdef __cplusplus //macro only for c++ compilers
extern "C" { //is a C code...
#endif
// ********** Number Theory **********
int gcd(int num1, int num2);
/* Calculates the greatest common divisor of the given 2 numbers
arguments: num1,num2 -> int,int
return: gcd -> int
*/
int lcm(int num1, int num2);
/* Calculates the least common multiple of the given 2 numbers
arguments: num1,num2 -> int,int
return: lcm -> int*/
bool is_prime(int num);
/* Shows if the number is prime or not.
argument: num -> int
return: prime_cond -> bool*/
int prime_factorize(int num, int *primes);
/*Does Prime Factorization to the given number
arguments: num, primes -> int, int*
Note: primes is an array of integers from the main function to store the prime factors
return: idx -> int (the number of prime factors)*/
int factorial(int n);
/*Calculate the factorial of n
argument: n -> int
return: factorial value, after all the recursions.*/
unsigned int divisor_sum(unsigned int n);
/*Calculates sum of positive divisors by using an efficient way of first checking powers of 2. Then checks all other numbers till sqrt(n). Time complexity: O(sqrt(n))
argument: n -> unsigned int -> given number has to be positive, so we can use unsigned integer for computing bigger positive integers
return: sum -> unsigned int -> also the sum has to be positive since all divisors must be positive*/
unsigned int phi(unsigned int n);
/*Finds Euler's totient (phi) function. Counts the integers that are coprime with n till n
argument: n -> unsigned int
return: count -> unsigned int*/
/* ********** Matrices ********** */
int **create_matrix(int row, int col);
/*Dynamically allocates memory for the matrix (and for the each row) and returns the created matrix.
arguments: row, col -> int, int
return: mat -> int** */
void free_matrix(int **mat, int row);
/*frees the allocated memory for the matrix and the elements
arguments: **mat(the matrix we will free), row -> int**, int
*/
void matprint(int **mat, int row, int col);
/*Displays the given matrix.
arguments: **mat, row, col -> int**, int, int*/
void matrix_add(int **mat1, int **mat2, int **result, int row, int col);
/*Adds matrices term by term
arguments: mat1,mat2,result,row,col -> int** int** int** int int
*/
void matmul(int **mat1, int **mat2, int **result, int row_A, int col_A, int col_B);
/*Multiplies matrices, row * col
arguments: mat1,mat2,result,row_A,col_A, col_B -> int**, int**, int**, int, int, int
*/
void transpose(void *mat_transposed, void *mat_original, int row, int col);
/*Transposes a matrix on diagonal -> Converts mat[row][col] to mat[col][row]
arguments: mat_transposed, mat_original, row, col -> int* , int* , int , int*/
double det(double *mat, int size, int perm);
/*Acts as a wrapper function for det_in() function. Takes the given 1D array, converts it to size x size 2D array (matrix)
arguments: mat(1D array), size, perm -> int*, int , int*/
double det_in(double **mat, int size, int perm);
/*Calculates the determinant (also the permanent depending on the last parameter of the function) of a given size x size matrix. See Laplace expansion theory to understand how the function works.
Note: perm == 1 calculates the permanent
perm == 0 calculates the determinant
arguments: mat, size, perm -> double*, int, int
return: sum(determinant/permanent) -> double*/
void ring(unsigned int n);
/*Generates a matrix of n x n size that is like a ring(Boundaries are all 1, but inside it is just 0s)
Can be used to define grid/border in game development(especially for 2D games), or to use as initialization of a complex matrix algorithm like mosaic matrix.
argument: n -> unsigned int
*/
/* ********** Statistics and Probability ********** */
void permutation(mpz_t result, int n, int k);
/*A function that can calculate permutations of extremely large numbers (by using GMP library)
arguments: result, n, k -> mpz_t(very large integer), int, int
!!!WARNING!!! DON'T FORGET TO INCLUDE GMP LIBRARY WHILE COMPILING, ADD -lgmp IN THE TERMINAL, OTHERWISE IT WON'T WORK*/
void combination(mpz_t result, int n, int k);
/*A function that can calculate combinations of extremely large numbers (by using GMP library)
arguments: result, n, k -> mpz_t(very large integer), int, int
!!!WARNING!!! DON'T FORGET TO INCLUDE GMP LIBRARY WHILE COMPILING, ADD -lgmp IN THE TERMINAL, OTHERWISE IT WON'T WORK*/
double mean(double *arr, int len);
/*Calculates the mean of the given sample.
arguments: arr, len -> int*, int
return: the mean value -> double*/
double median(int* arr, int len);
/*Sorts given unsorted array by using quicksort algorithm implemented in this library and finds the median.
argument: arr, len -> int*, int
return: the median value -> double*/
float* benford();
/*Benford's law shows the frequency distribution of digits in many real life datasets. This function generates a Benford distribution that you can compare with an actual dataset. If frequencies doesn't match this means your dataset has an anomaly(can be useful in finance for determining fraud)
return: digits array -> float*
*/
/* ********** Helper functions for other functions ********** */
void quicksort(int *arr, int len);
/*Sorts the given array by comparing to a chosen pivot. Time complexity: O(nlogn). A function that will be used for finding median in the median() function below.
arguments: arr, len -> int*, int
*/
int b_search(int *arr, int num, int idx_start, int idx_end);
/*Performs binary seach algorithm in a recursive way to find the desired value.
arguments: arr, num, idx_start, idx_end -> int* , int, int, int
return: index of the desired value(if not found, return -1)*/
double sqrt(double n);
/*Uses newtonian method to calculate the squareroot of the given number n.
Error tolerance: 1e-6
argument: n -> double
return: x -> double*/
/* ********** Vectors ********** */
typedef struct{
double x,y,z;
}vector; //declaring vector structure in cartesian coordinate system.
#define pi M_PI
vector to_cartesian(double r,double theta, double z);
/*Convert from polar coordinate system to the cartesian coordinate system
arguments: r (distance from the center), theta(angle between the r and x) -> double, double
return: a vector in cartesian coordinates*/
vector add_vectors(vector A,vector B);
/*Add given vectors
arguments: A, B -> vector, vector
return: result vector*/
vector subtract_vectors(vector A,vector B);
/*Substract given vectors
arguments: A, B -> vector, vector
return: result vector*/
vector multiply_vector(vector A,double k);
/*Multiply given vector by some coefficient k
arguments: A, k -> vector, double
return: result vector*/
vector divide_vector(vector A,double k);
/*Divide given vector by some coefficient k
arguments: A, k -> vector, double
return: result vector*/
void print_vector(vector A);
/*Display the taken vector
argument: A -> vector*/
double dot_product(vector A, vector B);
/*Calculates the dot product of 2 vectors
arguments: A, B -> vector, vector
return: product -> double*/
double vector_module(vector A);
/*Finds the norm(module) of a given 3D vector A
argument: A -> vector
return: module -> double*/
double vector_angle(vector A, vector B);
/*Finds the angle between two given vectors.
arguments: A, B -> vector, vector
return: theta -> double -> the angle between the given vectors.*/
double distance(vector A, vector B);
/*Calculates the distance between 2 vectors
arguments: A, B -> vector, vector
return: distance -> double*/
vector reflection(vector A, vector n);
/*Finds the reflection vector of a given vector. The wall is shown to code by giving its normal vector. This function is a MUST for game developers.
arguments: A, n -> vector, vector
return: A_r -> vector -> The reflection vector*/
vector lerp(vector A, vector B, double dt);
/*Makes smooth transition. For example:
A is my color A represented as a vector in RGB <1,0,0> (red)
B is my color B represented in the same manner <0,0,1> (blue)
Especially in game development you want to avoid sharp changes between the colors to give it a realistic look. lerp function got it for u. Evaluate dt between 0 and 1 to
enjoy a smooth palette of colors that go from A to B in a very realistic way. Ofcourse, less dt between 0 and 1 means more accurate colors.
Also in character movement! If you need realistic movement, handles turns better. See this video for detailed theory: https://www.youtube.com/watch?v=LSNQuFEDOyQ
arguments: A, B, dt -> vector, vector, dt
return: lerp vector
*/
#ifdef __cplusplus
}
#endif
#endif