-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumerical.cc
109 lines (95 loc) · 2.59 KB
/
numerical.cc
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
// Navigating with grid and place cells in cluttered environments
// Edvardsen et al. (2020). Hippocampus, 30(3), 220-232.
//
// Licensed under the EUPL-1.2-or-later.
// Copyright (c) 2019 NTNU - Norwegian University of Science and Technology.
// Author: Vegard Edvardsen (https://github.com/evegard).
#include "numerical.h"
#include <cassert>
int round_up_to_nearest_multiple(int size, int multiple)
{
int rest = size % multiple;
if (rest == 0) {
return size;
} else {
return size + (multiple - rest);
}
}
Matrix::Matrix(int width, int height)
: Matrix(width, height, 0.0)
{
}
Matrix::Matrix(int width, int height, real initial_value)
: width(width), height(height)
{
this->raw_values = new real[width * height];
this->values = new real *[height];
for (int y = 0; y < height; y++) {
this->values[y] = &this->raw_values[y * width];
for (int x = 0; x < width; x++) {
this->values[y][x] = initial_value;
}
}
}
Vector::Vector(int size)
: Vector(size, 0.0)
{
}
Vector::Vector(int size, real initial_value)
: size(size)
{
int allocation_result = posix_memalign(
(void **)&this->values,
REAL_ALIGNMENT,
round_up_to_nearest_multiple(size, REAL_STRIDE) * sizeof(real));
for (int x = 0; x < size; x++) {
this->values[x] = initial_value;
}
}
void Vector::clear()
{
for (int x = 0; x < this->size; x++) {
this->values[x] = 0.0;
}
}
void Vector::copy_from(Vector *other)
{
assert(this->size == other->size);
for (int x = 0; x < this->size; x++) {
this->values[x] = other->values[x];
}
}
real Vector::sum()
{
real sum = 0.0;
for (int x = 0; x < this->size; x++) {
sum += this->values[x];
}
return sum;
}
bool Random::initialized = false;
std::mt19937 *Random::engine = nullptr;
std::uniform_real_distribution<real> *Random::uniform_distribution = nullptr;
std::normal_distribution<real> *Random::normal_distribution = nullptr;
double Random::uniform()
{
if (!Random::initialized) {
Random::initialize();
}
return (*Random::uniform_distribution)(*Random::engine);
}
double Random::normal()
{
if (!Random::initialized) {
Random::initialize();
}
return (*Random::normal_distribution)(*Random::engine);
}
void Random::initialize()
{
std::random_device random_device;
Random::engine = new std::mt19937(random_device());
Random::uniform_distribution = new std::uniform_real_distribution<real>();
Random::normal_distribution = new std::normal_distribution<real>();
Random::initialized = true;
}