-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinalgHelpers.cpp
69 lines (58 loc) · 1.43 KB
/
LinalgHelpers.cpp
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
//
// Created by jakob on 23.06.24.
//
#include <cassert>
#include <cmath>
#include <algorithm>
#include "LinalgHelpers.h"
#include "DataCover.h"
#include <ostream>
namespace MapperLib {
Scalar euclididan_distance(Vector const &vec1, Vector const &vec2) {
assert(vec1.size() == vec2.size());
Scalar squared_sum = 0;
for(size_t i = 0; i < vec1.size(); i++) {
squared_sum += (vec1[i]-vec2[i])*(vec1[i]-vec2[i]);
}
return std::sqrt(squared_sum);
}
Scalar maximum_distance(Vector const &vec1, Vector const &vec2)
{
assert(vec1.size() == vec2.size());
Scalar max_distance = 0;
for(size_t i = 0; i < vec1.size(); i++) {
max_distance = std::max(max_distance, std::abs(vec1[i]-vec2[i]));
}
return max_distance;
}
bool check_data_equal_dimension(Matrix const &mat)
{
bool is_equal = true;
std::ranges::for_each(mat, [&](Vector const& v) {
is_equal = is_equal and (v.size() == mat[0].size());
});
return is_equal;
}
Dimension get_data_dimension(Matrix const &mat)
{
return mat[0].size();
}
} // Helper
std::ostream & operator<<(std::ostream &os, MapperLib::Vector const &vec)
{
os << "[";
for(auto const x : vec) {
os << x << ", ";
}
os << "\b\b]";
return os;
}
std::ostream & operator<<(std::ostream &os, MapperLib::Matrix const &mat)
{
os << "[";
for(auto const& row : mat) {
os << row << ", ";
}
os << "\b\b]";
return os;
}