-
Notifications
You must be signed in to change notification settings - Fork 0
/
PAZ_Math
605 lines (559 loc) · 18.1 KB
/
PAZ_Math
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#ifndef PAZ_MATH
#define PAZ_MATH
#include <cmath>
#include <vector>
#include <ostream>
#include <numeric>
#include <algorithm>
namespace paz
{
template<typename T>
using require_iterable = std::enable_if_t<std::is_same<decltype(std::begin(
std::declval<const T&>())), decltype(std::end(std::declval<const
T&>()))>::value>;
template<typename T>
using require_integral = std::enable_if_t<std::is_integral<T>::value>;
template<typename T>
using require_not_integral = std::enable_if_t<!std::is_integral<T>::value>;
constexpr double NatBase = 2.71828182845904523536028747135; // M_E
constexpr double SqrtTwo = 1.41421356237309504880168872421; // M_SQRT2
constexpr double SqrtHalf = 0.70710678118654752440084436210; // M_SQRT1_2
constexpr double SqrtThree = 1.73205080756887729352744634151; // M_SQRT3
constexpr double Pi = 3.14159265358979323846264338328; // M_PI
constexpr double TwoPi = 6.28318530717958647692528676656; // 2.*M_PI
constexpr std::size_t None = std::numeric_limits<std::size_t>::max();
std::size_t random_seed();
double randn();
std::size_t randi(std::size_t n);
double uniform();
double uniform(double a, double b);
std::size_t pmf_rand(const std::vector<double>& probs);
std::vector<std::size_t> rand_seq(std::size_t length);
std::size_t poissrnd(double lambda);
// log(w) -> w
void normalize_log_weights(std::vector<double>& logWeights);
// w -> w
void normalize_weights(std::vector<double>& weights);
template<typename T0, typename T1, typename T2>
constexpr auto mix(T0 a, T1 b, T2 k)
{
return a + k*(b - a);
}
template<typename T>
constexpr T clamp(T x, T a, T b)
{
return std::max(a, std::min(b, x));
}
template<typename T, require_integral<T>* = nullptr>
constexpr T fract(T /* x */)
{
return 0;
}
template<typename T, require_not_integral<T>* = nullptr>
T fract(T x)
{
return x - std::floor(x);
}
template<typename T>
constexpr T sign(T x)
{
return (T{0} < x) - (x < T{0});
}
template<typename T, require_not_integral<T>* = nullptr>
T normalize_angle(T x)
{
return fract(x/TwoPi)*TwoPi;
}
template<typename T = double, require_not_integral<T>* = nullptr>
constexpr T inf()
{
return std::numeric_limits<T>::infinity();
}
template<typename T = double, require_not_integral<T>* = nullptr>
constexpr T eps()
{
return std::numeric_limits<T>::epsilon();
}
template<typename T = double, require_not_integral<T>* = nullptr>
T eps(T x)
{
const T x0 = std::abs(x);
return std::nextafter(x0, inf<T>()) - x0;
}
template<typename T = double, require_not_integral<T>* = nullptr>
constexpr T nan()
{
return std::numeric_limits<T>::quiet_NaN();
}
class Vec;
class MatRef;
class Mat
{
friend class MatRef;
std::vector<double> _vals;
std::size_t _rows = 0;
std::size_t _cols = 0;
public:
static Mat Constant(std::size_t rows, std::size_t cols, double c);
static Mat Constant(std::size_t side, double c);
static Mat Zero(std::size_t rows, std::size_t cols);
static Mat Zero(std::size_t side);
static Mat Ones(std::size_t rows, std::size_t cols);
static Mat Ones(std::size_t side);
static Mat Identity(std::size_t side);
static Mat Diag(const MatRef& vals);
static Mat BlockDiag(const MatRef& a, const MatRef& b);
static Mat Cross(const MatRef& vals);
static Mat Hcat(const MatRef& a, const MatRef& b);
static Mat Vcat(const MatRef& a, const MatRef& b);
static Mat Randn(std::size_t rows, std::size_t cols);
static Mat Randn(std::size_t side);
Mat() = default;
Mat(std::size_t rows, std::size_t cols);
Mat(std::size_t side);
Mat(const Vec& v);
Mat(const MatRef& m);
Mat(const std::initializer_list<std::initializer_list<double>>& list);
double det() const;
Mat inv() const;
Mat solve(const Mat& b) const; // x : (*this)*x = b (least squares)
Mat chol() const;
Mat cholUpdate(const Mat& m, double a) const;
Vec eig() const;
Vec eig(Mat& vecs) const;
void qr(Mat& q, Mat& r) const;
void qr(Mat& q, Mat& r, std::vector<std::size_t>& p) const;
Mat trans() const;
Vec diag() const;
Mat rep(std::size_t m, std::size_t n) const;
double& operator()(std::size_t i, std::size_t j)
{
if(i >= rows())
{
throw std::runtime_error("Row index out of range.");
}
if(j >= cols())
{
throw std::runtime_error("Column index out of range.");
}
return _vals[i + _rows*j];
}
double operator()(std::size_t i, std::size_t j) const
{
if(i >= rows())
{
throw std::runtime_error("Row index out of range.");
}
if(j >= cols())
{
throw std::runtime_error("Column index out of range.");
}
return _vals[i + _rows*j];
}
double& operator()(std::size_t i)
{
if(i >= size())
{
throw std::runtime_error("Index is out of range.");
}
return _vals[i];
}
double operator()(std::size_t i) const
{
if(i >= size())
{
throw std::runtime_error("Index is out of range.");
}
return _vals[i];
}
std::size_t size() const
{
return _vals.size();
}
std::size_t rows() const
{
return _rows;
}
std::size_t cols() const
{
return _cols;
}
double* data()
{
return _vals.data();
}
const double* data() const
{
return _vals.data();
}
bool empty() const
{
return _vals.empty();
}
auto begin()
{
return _vals.begin();
}
auto begin() const
{
return _vals.begin();
}
auto end()
{
return _vals.end();
}
auto end() const
{
return _vals.end();
}
double normSq() const;
double norm() const;
double sum() const;
Vec rowSum() const;
Mat colSum() const;
double min() const;
double max() const;
Mat normalized() const;
Mat prod(const MatRef& rhs) const; // elementwise
Mat quot(const MatRef& rhs) const; // elementwise
Mat& operator*=(const MatRef& rhs);
Mat operator*(const MatRef& rhs) const;
Mat& operator+=(const MatRef& rhs);
Mat operator+(const MatRef& rhs) const;
Mat& operator-=(const MatRef& rhs);
Mat operator-(const MatRef& rhs) const;
Mat& operator*=(double rhs);
Mat operator*(double rhs) const;
Mat& operator/=(double rhs);
Mat operator/(double rhs) const;
Mat operator-() const;
double dot(const MatRef& rhs) const;
Vec cross(const MatRef& rhs) const;
MatRef block(std::size_t startRow, std::size_t startCol, std::size_t
numRows, std::size_t numCols) const;
void setBlock(std::size_t startRow, std::size_t startCol, std::size_t
numRows, std::size_t numCols, const MatRef& rhs);
MatRef row(std::size_t m) const;
void setRow(std::size_t m, const MatRef& rhs);
MatRef col(std::size_t n) const;
void setCol(std::size_t n, const MatRef& rhs);
void resize(std::size_t newRows, std::size_t newCols);
void resizeRows(std::size_t newRows);
void resizeCols(std::size_t newCols);
bool hasNan() const;
void shuffleCols();
};
std::ostream& operator<<(std::ostream& out, const MatRef& x);
class Vec : public Mat
{
public:
static Vec Constant(std::size_t rows, double c);
static Vec Zero(std::size_t rows);
static Vec Ones(std::size_t rows);
static Vec IdQuat();
static Vec Randn(std::size_t rows);
static Vec Cat(const MatRef& a, const MatRef& b);
Vec() = default;
Vec(std::size_t rows);
Vec(const Mat& m);
Vec(const MatRef& m);
Vec(const std::initializer_list<std::initializer_list<double>>& list);
MatRef segment(std::size_t start, std::size_t n) const;
void setSegment(std::size_t start, std::size_t n, const MatRef& rhs);
MatRef head(std::size_t n) const;
void setHead(std::size_t n, const Vec& rhs);
MatRef tail(std::size_t n) const;
void setTail(std::size_t n, const Vec& rhs);
void resize(std::size_t newRows);
};
class MatRef
{
friend class Mat;
public:
struct iterator
{
using value_type = double;
using difference_type = std::ptrdiff_t;
using reference = const double&;
using pointer = const double*;
using iterator_category = std::random_access_iterator_tag;
pointer ptr;
difference_type row;
difference_type origRows;
difference_type blockRows;
iterator& operator--()
{
--row;
if(row < 0)
{
ptr += blockRows;
ptr -= origRows + 1;
row = blockRows - 1;
}
else
{
--ptr;
}
return *this;
}
iterator operator--(int)
{
auto temp = *this;
--(*this);
return temp;
}
iterator& operator++()
{
++row;
if(row == static_cast<difference_type>(blockRows))
{
ptr += origRows + 1;
ptr -= blockRows;
row = 0;
}
else
{
++ptr;
}
return *this;
}
iterator operator++(int)
{
auto temp = *this;
++(*this);
return temp;
}
iterator& operator-=(difference_type n)
{
return *this += -n;
}
iterator& operator+=(difference_type n)
{
const difference_type temp = row + n;
difference_type deltaCol = temp/blockRows;
difference_type deltaRow = n - blockRows*deltaCol;
if(temp < 0 && temp%blockRows)
{
--deltaCol;
deltaRow += blockRows;
}
ptr += deltaRow + origRows*deltaCol;
row += deltaRow;
return *this;
}
iterator operator-(difference_type n) const
{
auto temp = *this;
temp -= n;
return temp;
}
iterator operator+(difference_type n) const
{
auto temp = *this;
temp += n;
return temp;
}
difference_type operator-(const iterator& it) const
{
difference_type deltaPtr;
if(ptr < it.ptr)
{
deltaPtr = -static_cast<difference_type>(it.ptr - ptr);
}
else
{
deltaPtr = ptr - it.ptr;
}
difference_type deltaCol = deltaPtr/origRows;
if(deltaPtr < 0 && deltaPtr%origRows)
{
--deltaCol;
}
const difference_type deltaRow = row - it.row;
return deltaRow + blockRows*deltaCol;
}
reference operator*() const
{
return *ptr;
}
reference operator[](difference_type n) const
{
return *(*this + n);
}
bool operator==(const iterator& it) const
{
return ptr == it.ptr;
}
bool operator!=(const iterator& it) const
{
return ptr != it.ptr;
}
bool operator<=(const iterator& it) const
{
return ptr <= it.ptr;
}
bool operator>=(const iterator& it) const
{
return ptr >= it.ptr;
}
bool operator<(const iterator& it) const
{
return ptr < it.ptr;
}
bool operator>(const iterator& it) const
{
return ptr > it.ptr;
}
};
private:
const iterator _begin;
const std::size_t _origCols;
const std::size_t _blockCols;
MatRef(const double* ptr, std::size_t origRows, std::size_t origCols,
std::size_t blockRows, std::size_t blockCols);
public:
MatRef(const Mat& m);
// All `const` methods of `paz::Mat` except `paz::Mat::data`.
double det() const;
Mat inv() const;
Mat solve(const Mat& b) const; // x : (*this)*x = b (least squares)
Mat chol() const;
Mat cholUpdate(const Mat& m, double a) const;
Vec eig() const;
Vec eig(Mat& vecs) const;
void qr(Mat& q, Mat& r) const;
void qr(Mat& q, Mat& r, std::vector<std::size_t>& p) const;
Mat trans() const;
Vec diag() const;
Mat rep(std::size_t m, std::size_t n) const;
double operator()(std::size_t i, std::size_t j) const
{
if(i >= rows())
{
throw std::runtime_error("Row index out of range.");
}
if(j >= cols())
{
throw std::runtime_error("Column index out of range.");
}
return _begin[i + rows()*j];
}
double operator()(std::size_t i) const
{
if(i >= size())
{
throw std::runtime_error("Index is out of range.");
}
return _begin[i];
}
std::size_t size() const
{
return rows()*cols();
}
std::size_t rows() const
{
return _begin.blockRows;
}
std::size_t cols() const
{
return _blockCols;
}
bool empty() const
{
return !rows() || !cols();
}
const iterator& begin() const
{
return _begin;
}
iterator end() const
{
return _begin + size();
}
double normSq() const;
double norm() const;
double sum() const;
Vec rowSum() const;
Mat colSum() const;
double min() const;
double max() const;
Mat normalized() const;
Mat prod(const MatRef& rhs) const; // elementwise
Mat quot(const MatRef& rhs) const; // elementwise
Mat operator*(const MatRef& rhs) const;
Mat operator+(const MatRef& rhs) const;
Mat operator-(const MatRef& rhs) const;
Mat operator*(double rhs) const;
Mat operator/(double rhs) const;
Mat operator-() const;
double dot(const MatRef& rhs) const;
Vec cross(const MatRef& rhs) const;
MatRef block(std::size_t startRow, std::size_t startCol, std::size_t
numRows, std::size_t numCols) const;
MatRef row(std::size_t m) const;
MatRef col(std::size_t n) const;
bool hasNan() const;
};
inline MatRef::iterator operator+(MatRef::iterator::difference_type n, const
MatRef::iterator& it)
{
return it + n;
}
inline Mat operator*(double lhs, const MatRef& rhs)
{
return rhs*lhs;
}
inline bool operator==(const MatRef& a, const MatRef& b)
{
if(a.rows() != b.rows() || a.cols() != b.cols())
{
return false;
}
return std::equal(a.begin(), a.end(), b.begin());
}
inline bool operator!=(const MatRef& a, const MatRef& b)
{
return !(a == b);
}
Mat to_mat(const MatRef& q);
Vec to_quat(const MatRef& m);
Vec qinv(const MatRef& q);
Mat xi(const MatRef& q);
Vec qmult(const MatRef& p, const MatRef& q);
Vec axis_angle(const MatRef& axis, double angle);
Vec nlerp(const MatRef& p, const MatRef& q, double k);
template<typename T, require_iterable<T>* = nullptr>
bool any_true(const T& v)
{
return std::any_of(std::begin(v), std::end(v), [](bool x){ return x; });
}
template<typename T, require_iterable<T>* = nullptr>
bool all_true(const T& v)
{
return std::all_of(std::begin(v), std::end(v), [](bool x){ return x; });
}
template<typename T, require_iterable<T>* = nullptr>
bool none_true(const T& v)
{
return std::none_of(std::begin(v), std::end(v), [](bool x){ return x;
});
}
template<typename T, require_iterable<T>* = nullptr>
std::size_t num_true(const T& v)
{
return std::accumulate(std::begin(v), std::end(v), std::size_t{0});
}
double gaussian_pdf(const MatRef& mean, const MatRef& cholCov, const MatRef&
x);
double cs_divergence(const MatRef& meanA, const MatRef& cholCovA, const
MatRef& meanB, const MatRef& cholCovB);
Vec gmm_rand(const std::vector<double>& weights, const std::vector<Vec>&
means, const std::vector<Mat>& cholCovs);
double jv(const MatRef& costMat, std::vector<std::size_t>& rowSols);
void murty(const MatRef& costMat, std::size_t numBest, std::vector<std::
vector<std::size_t>>& rowSols, std::vector<double>& costs);
Mat rot1(double angle);
Mat rot2(double angle);
Mat rot3(double angle);
}
#endif