21 #include <boost/optional.hpp> 42 bool fpEqual(
double a,
double b,
double tol,
bool check_relative_also) {
60 else if(a == 0 || b == 0 || (
abs(a) +
abs(b)) < DOUBLE_MIN_NORMAL) {
61 return abs(a-b) <= tol * DOUBLE_MIN_NORMAL;
65 else if (
abs(a - b) <= tol) {
69 else if (
abs(a - b) <=
71 check_relative_also) {
84 for(
size_t i=0;
i<
n;
i++) {
85 stream << setprecision(9) <<
v(
i) << (
i<n-1 ?
"; " :
"");
87 stream <<
"];" << endl;
98 fstream
stream(filename.c_str(), fstream::out | fstream::app);
105 if (vec1.size() != vec2.size())
return false;
106 size_t m = vec1.size();
107 for(
size_t i=0;
i<
m;
i++)
108 if(vec1[
i] != vec2[
i])
115 size_t m = vec1.size();
116 for(
size_t i=0;
i<
m;
i++)
117 if(!(vec1[
i] >= vec2[
i]))
124 if (vec1.size()!=vec2.size())
return false;
125 size_t m = vec1.size();
126 for(
size_t i=0;
i<
m; ++
i) {
127 if (!
fpEqual(vec1[
i], vec2[i], tol))
135 if (vec1.size()!=vec2.size())
return false;
136 size_t m = vec1.size();
137 for(
size_t i=0;
i<
m; ++
i) {
138 if (!
fpEqual(vec1[
i], vec2[i], tol))
147 cout <<
"not equal:" << endl;
148 print(expected,
"expected");
149 print(actual,
"actual");
156 cout <<
"Erroneously equal:" << endl;
157 print(expected,
"expected");
158 print(actual,
"actual");
165 cout <<
"not equal:" << endl;
166 print(static_cast<Vector>(expected),
"expected");
167 print(static_cast<Vector>(actual),
"actual");
174 cout <<
"not equal:" << endl;
182 if (vec1.size()!=vec2.size())
return false;
183 bool flag =
false;
double scale = 1.0;
184 size_t m = vec1.size();
185 for(
size_t i=0;
i<
m;
i++) {
188 if(vec1[i] == 0 && vec2[i] == 0)
continue;
190 scale = vec1[
i] / vec2[
i];
193 else if (
std::abs(vec1[i] - vec2[i]*scale) > tol)
return false;
201 assert (b.size()==a.size());
203 for(
size_t i = 0;
i <
n;
i++ ) {
204 const double &
ai =
a(
i), &bi =
b(
i);
205 c(
i) = (bi==0.0 && ai==0.0) ? 0.0 : ai/bi;
213 const double x0 =
v(0);
214 const double x02 = x0*
x0;
217 const double sigma = v.squaredNorm() - x02;
224 double mu =
sqrt(x02 + sigma);
228 v(0) = -sigma / (x0 +
mu);
230 const double v02 =
v(0)*
v(0);
232 return 2.0 * v02 / (sigma + v02);
240 return make_pair(beta, v);
249 size_t m = weights.size();
250 static const double inf = std::numeric_limits<double>::infinity();
254 for (
size_t i = 0;
i <
m; ++
i) isZero.push_back(
std::abs(a[
i]) < 1
e-9);
257 for (
size_t i = 0; i <
m; ++
i) {
258 if (weights[i] == inf && !isZero[i]) {
262 pseudo = Vector::Unit(m,i)*(1.0/a[
i]);
270 for (
size_t i = 0; i <
m; i++) {
273 precision += weights[
i] * (ai *
ai);
277 if (precision < 1
e-9)
278 for (
size_t i = 0; i <
m; i++)
283 for (
size_t i = 0; i <
m; i++)
284 pseudo[i] = isZero[i] ? 0 : variance * weights[i] * a[i];
295 throw invalid_argument(
"a and weights have different sizes!");
298 return make_pair(pseudo, precision);
311 for(
int d = 0;
d <
v.size();
d++)
324 va_start(ap, nrVectors);
325 for(
size_t i = 0 ;
i < nrVectors ;
i++) {
bool linear_dependent(const Vector &vec1, const Vector &vec2, double tol)
Vector ediv_(const Vector &a, const Vector &b)
const mpreal ai(const mpreal &x, mp_rnd_t r=mpreal::get_default_rnd())
Vector concatVectors(size_t nrVectors,...)
pair< Vector, double > weightedPseudoinverse(const Vector &a, const Vector &weights)
static const double sigma
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
bool assert_equal(const ConstSubVector &expected, const ConstSubVector &actual, double tol)
bool equal_with_abs_tol(const SubVector &vec1, const SubVector &vec2, double tol)
bool assert_inequal(const Vector &expected, const Vector &actual, double tol)
ptrdiff_t DenseIndex
The index type for Eigen objects.
bool fpEqual(double a, double b, double tol, bool check_relative_also)
void print(const Vector &v, const string &s)
void save(const Vector &v, const string &s, const string &filename)
Expression of a fixed-size or dynamic-size sub-vector.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
bool greaterThanOrEqual(const Vector &vec1, const Vector &vec2)
const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t r=mpreal::get_default_rnd())
typedef and functions to augment Eigen's VectorXd
bool operator==(const NonZeroIterator< std::pair< A, B >> &it, const NonZeroSentinel &)
pair< double, Vector > house(const Vector &x)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics scale
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
double houseInPlace(Vector &v)