-
Notifications
You must be signed in to change notification settings - Fork 11
/
Tools.h
194 lines (165 loc) · 4.71 KB
/
Tools.h
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
/**
Copyright (c) 2016 Theodore Gast, Chuyuan Fu, Chenfanfu Jiang, Joseph Teran
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
If the code is used in an article, the following paper shall be cited:
@techreport{qrsvd:2016,
title={Implicit-shifted Symmetric QR Singular Value Decomposition of 3x3 Matrices},
author={Gast, Theodore and Fu, Chuyuan and Jiang, Chenfanfu and Teran, Joseph},
year={2016},
institution={University of California Los Angeles}
}
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
################################################################################
This file provides a random number generator and a timer.
Sample usage:
RandomNumber<float> rand;
float x = randReal(-0.5, 0.8);
Timer timer;
timer.start();
SOME CODE A
std::cout<<"CODE A took "<<timer.click()<<" seconds"<<std::endl;
SOME CODE B
std::cout<<"CODE B took "<<timer.click()<<" seconds"<<std::endl;
################################################################################
*/
#ifndef JIXIE_SVD_TOOLS_H
#define JIXIE_SVD_TOOLS_H
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <Eigen/Dense>
#include <Eigen/Core>
#include <Eigen/SVD>
#pragma GCC diagnostic pop
#include <mmintrin.h>
#include <xmmintrin.h>
#include <cmath>
#include <random>
#include <chrono>
#include <iostream>
#include <iomanip>
namespace JIXIE {
/**
Random number generator.
*/
template <class T>
class RandomNumber {
public:
std::mt19937 generator;
RandomNumber(unsigned s = 123)
: generator(s)
{
}
~RandomNumber()
{
}
/**
Random real number from an interval
*/
T randReal(T a, T b)
{
std::uniform_real_distribution<T> distribution(a, b);
return distribution(generator);
}
/**
Fill with uniform random numbers
*/
template <class Derived>
void fill(Eigen::DenseBase<Derived>& x, T a, T b)
{
for (typename Derived::Index i = 0; i < x.size(); i++)
x(i) = randReal(a, b);
}
};
namespace MATH_TOOLS {
/**
\brief Approximate inverse square root
A fast approximation to the inverse sqrt
The relative error is less than 1.5*2^-12
*/
inline float approx_rsqrt(float a)
{
return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(a)));
}
/**
\brief Inverse square root
computed from approx_rsqrt and one newton step
*/
inline float rsqrt(float a)
{
float b = approx_rsqrt(a);
// Newton step with f(x) = a - 1/x^2
b = 0.5f * b * (3.0f - a * (b * b));
return b;
}
/**
\brief Inverse square root
computed from 1/std::sqrt
*/
inline double rsqrt(double a)
{
using std::sqrt;
return 1 / sqrt(a);
}
}
/**
Timer. We can use either system timer or stready timer
*/
class Timer {
public:
Timer() {}
~Timer() {}
/**
\brief Start timing
*/
void start()
{
start_time = std::chrono::steady_clock::now();
}
/**
\return time elapsed since last click in seconds
*/
double click()
{
to_time = std::chrono::steady_clock::now();
elapsed_seconds = to_time - start_time;
start_time = to_time;
return elapsed_seconds.count();
}
private:
std::chrono::time_point<std::chrono::steady_clock> start_time;
std::chrono::time_point<std::chrono::steady_clock> to_time;
std::chrono::duration<double> elapsed_seconds;
};
namespace INTERNAL {
using namespace std;
template <class T, class Enable = void>
struct ScalarTypeHelper {
using type = typename T::Scalar;
};
template <class T>
struct ScalarTypeHelper<T, enable_if_t<is_arithmetic<T>::value> > {
using type = T;
};
}
template <class T>
using ScalarType = typename INTERNAL::ScalarTypeHelper<T>::type;
template <class MatrixType>
constexpr bool isSize(int m, int n)
{
return MatrixType::RowsAtCompileTime == m && MatrixType::ColsAtCompileTime == n;
}
}
#endif