-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
102 lines (91 loc) · 3.33 KB
/
Copy pathutils.cpp
File metadata and controls
102 lines (91 loc) · 3.33 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
#include "utils.h"
#include "logger.hpp"
#include <map>
// Taylor series approximation of e^x — 100 terms, sufficient for ML use
long double custom_exp(long double x) {
long double sum = 1.0L, term = 1.0L;
for (int n = 1; n < 100; ++n) {
term *= x / n;
sum += term;
}
return sum;
}
// Gauss-Jordan elimination to compute matrix inverse in-place
std::vector<std::vector<double>> matinv(std::vector<std::vector<double>> A) {
int n = static_cast<int>(A.size());
std::vector<std::vector<double>> result(n, std::vector<double>(n, 0.0));
for (int i = 0; i < n; ++i) {
result[i][i] = 1.0;
}
// step 1 - eliminate the lower half
for (int i = 0; i < n - 1; i++) {
int pivot = i;
for (int j = i + 1; j < n; j++)
if (std::abs(A[j][i]) > std::abs(A[pivot][i])) pivot = j;
if (pivot != i) {
std::swap(A[i], A[pivot]);
std::swap(result[i], result[pivot]);
}
if (A[i][i] == 0) throw std::runtime_error("Matrix is singular");
for (int j = i + 1; j < n; j++) {
double factor = A[j][i] / A[i][i];
for (int k = 0; k < n; k++) {
A[j][k] -= factor * A[i][k];
result[j][k] -= factor * result[i][k];
}
}
}
// step 2 - eliminate the upper half
for (int i = n - 1; i > 0; i--) {
for (int j = i - 1; j >= 0; j--) {
double factor = A[j][i] / A[i][i];
for (int k = 0; k < n; k++) {
A[j][k] -= factor * A[i][k];
result[j][k] -= factor * result[i][k];
}
}
}
// step 3 - fix the diagonal
for (int i = 0; i < n; i++) {
double factor = A[i][i];
for (int k = 0; k < n; k++) {
A[i][k] /= factor;
result[i][k] /= factor;
}
}
return result;
}
void help() {
Log::header("ClassicML Utils");
Log::divider();
Log::info("pwr, custom_exp, sigmoid, dot, manhattan_distance, euclidean_dist");
Log::info("cosine, square_distance, factorial, ncr, matinv");
Log::info("Call docs(\"name\") for details on a specific function.");
Log::divider();
}
void docs(const std::string &func_name) {
static const std::map<std::string, std::string> kDocs = {
{"matinv",
"matinv(A) → A⁻¹\n Gauss-Jordan inverse. Square matrices only.\n Complexity: O(n³). Example: auto inv = matinv(A);"},
{"sigmoid",
"sigmoid(z) → (0,1)\n 1/(1+e^{-z}) using custom_exp.\n Complexity: O(1)."},
{"dot",
"dot(a,b) → scalar\n Inner product of equal-length vectors.\n Complexity: O(n)."},
{"euclidean_dist",
"euclidean_dist(a,b) → L2 distance\n sqrt(sum (a_i-b_i)²).\n Complexity: O(n)."},
{"manhattan_distance",
"manhattan_distance(a,b) → L1 distance\n sum |a_i-b_i|.\n Complexity: O(n)."},
{"cosine",
"cosine(a,b) → [-1,1]\n Cosine similarity.\n Complexity: O(n)."},
{"pwr",
"pwr(x,n) → x^n\n Fast exponentiation (int or real exponent).\n Complexity: O(log n) for int n."},
};
auto it = kDocs.find(func_name);
if (it == kDocs.end()) {
Log::warn("Unknown function. Call help() for list.");
return;
}
Log::header(func_name);
Log::info(it->second);
Log::divider();
}