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 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
|
/* Copyright 2023 Yann ORLAREY
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <climits>
#include <iostream>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include "check.hh"
#include "interval_algebra.hh"
#include "precision_utils.hh"
/**
* @brief check we have the expected interval
*
* @param expected interval as a string
* @param exp interval to test
*/
void check(const std::string& expected, const itv::interval& exp)
{
std::stringstream ss;
ss << exp;
if (ss.str().compare(expected) == 0) {
std::cout << "\033[32m"
<< "OK: " << expected << "\033[0m" << std::endl;
} else {
std::cout << "\033[31m"
<< "ERR: We got " << ss.str() << " instead of " << expected << "\033[0m"
<< std::endl;
}
}
/**
* @brief check that an interval result is the expected one
*
* @param testname
* @param exp
* @param res
*/
void check(const std::string& testname, const itv::interval& exp, const itv::interval& res)
{
if (exp == res) {
std::cout << "\033[32m"
<< "OK: " << testname << " " << exp << " = " << res << "\033[0m" << std::endl;
if (exp.lsb() != res.lsb()) {
std::cout << "\033[33m"
<< "\t But precisions differ by " << exp.lsb() - res.lsb() << "\033[0m"
<< std::endl;
}
} else {
std::cout << "\033[31m"
<< "ERR:" << testname << " FAILED. We got " << exp << " instead of " << res
<< "\033[0m" << std::endl;
}
}
/**
* @brief check that a boolean result is the expected one
*
* @param testname
* @param exp
* @param res
*/
void check(const std::string& testname, bool exp, bool res)
{
if (exp == res) {
std::cout << "\033[32m"
<< "OK: " << testname << "\033[0m" << std::endl;
} else {
std::cout << "\033[31m"
<< "ERR:" << testname << " FAILED. We got " << exp << " instead of " << res
<< "\033[0m" << std::endl;
}
}
/**
* @brief Approximate the resulting interval of a function
*
* @param N the number of iterations
* @param f the function to test
* @param x the interval of its first argument
* @param y the interval of its second argument
* @return interval the interval of the results
*/
itv::interval testfun(int N, bfun f, const itv::interval& x, const itv::interval& y)
{
std::random_device rd; // used to generate a random seed, based on some hardware randomness
std::default_random_engine generator(rd());
std::uniform_real_distribution rx(x.lo(), x.hi());
std::uniform_real_distribution ry(y.lo(), y.hi());
double a = f(x.lo(), y.lo());
double b = f(x.lo(), y.hi());
double c = f(x.hi(), y.lo());
double d = f(x.hi(), y.hi());
double l = itv::min4(a, b, c, d);
double h = itv::max4(a, b, c, d);
for (int i = 0; i < N; i++) {
double u = rx(generator);
double v = ry(generator);
double r = f(u, v);
if (r < l) {
l = r;
}
if (r > h) {
h = r;
}
}
return {l, h};
}
/**
* @brief analyze the Mod function, print the simulated and computed resulting interval.
* The two should be close enough.
*
* @param x first argument interval
* @param y second argument interval
*/
void analyzemod(itv::interval x, itv::interval y)
{
itv::interval_algebra A;
std::cout << "simulated fmod(" << x << "," << y << ") = " << testfun(10000, fmod, x, y)
<< std::endl;
std::cout << "computed fmod(" << x << "," << y << ") = " << A.Mod(x, y) << std::endl;
std::cout << std::endl;
}
/**
* @brief Compute numerically the output interval of a function
*
* @param E number of intervals/experiments
* @param M number of measurements used to estimate the resulting interval
* @param title name of the tested function
* @param D maximal interval for the argument
* @param f the numerical function of reference
*/
void analyzeUnaryFunction(int E, int M, const char* title, const itv::interval& D, ufun f)
{
std::random_device R; // used to generate a random seed, based on some hardware randomness
std::default_random_engine generator(R());
std::uniform_real_distribution rd(D.lo(), D.hi());
std::cout << "Analysis of " << title << " in domain " << D << std::endl;
for (int e = 0; e < E; e++) { // E experiments
// X: random input interval X < I
double a = rd(generator);
double b = rd(generator);
itv::interval X(std::min(a, b), std::max(a, b));
// [ylo,yhi] initial f(X) interval
double t0 = f(X.lo());
double t1 = f(X.hi());
double y0 = std::min(t0, t1);
double y1 = std::max(t0, t1);
// random values in X
std::uniform_real_distribution rx(X.lo(), X.hi());
for (int m = 0; m < M; m++) { // M measurements
double y = f(rx(generator));
if (y < y0) {
y0 = y;
}
if (y > y1) {
y1 = y;
}
}
itv::interval Y(y0, y1);
std::cout << e << ": " << title << "(" << X << ") = " << Y << std::endl;
}
std::cout << std::endl;
}
/**
* @brief Check the unary interval function gives a good approximation of the numerical function
*
* @param E number of intervals/experiments
* @param M number of measurements used to estimate the resulting interval
* @param title name of the tested function
* @param D maximal interval for the argument
* @param f the numerical function of reference
* @param mp the interval method corresponding to f
*/
void analyzeUnaryMethod(int E, int M, const char* title, const itv::interval& D, ufun f, umth mp)
{
std::random_device R; // used to generate a random seed, based on some hardware randomness
std::default_random_engine generator(R());
std::uniform_real_distribution rd(D.lo(), D.hi());
itv::interval_algebra A;
std::cout << "Analysis of " << title << " in domain " << D << " (u = " << pow(2, D.lsb()) << ")"
<< std::endl;
for (int e = 0; e < E; e++) { // E experiments
// X: random input interval X < I
double a = truncate(rd(generator), D.lsb());
double b = truncate(rd(generator), D.lsb());
itv::interval X(std::min(a, b), std::max(a, b), D.lsb());
// boundaries of the resulting interval
double y0 = HUGE_VAL; // std::min(t0, t1);
double y1 = -HUGE_VAL; // std::max(t0, t1);
// precision of the resulting interval
int lsb = INT_MAX;
// random values in X
std::uniform_real_distribution rx(X.lo(), X.hi());
// std::vector<double> measurements;
std::set<double> measurements;
// the loop has almost no chance of drawing X.hi(): we manually add it
double sample = X.hi(); // not truncated since morally the interval boundaries should
// already have the right precision
double y = f(sample);
// y = truncate(y, -30);
measurements.insert(y);
if (!std::isnan(static_cast<double>(y))) {
if (y < y0) {
y0 = y;
}
if (y > y1) {
y1 = y;
}
}
for (int m = 0; m < M; m++) { // M measurements
double presample = rx(generator); // non-truncated sample
sample = truncate(presample, D.lsb()); // truncated sample
double pre_y = f(presample);
y = f(sample);
// y = truncate(y, -30); // workaround to avoid artefacts in trigonometric
// functions
measurements.insert(y);
// interval bounds
if (!std::isnan(pre_y)) {
if (pre_y < y0) {
y0 = pre_y;
}
if (pre_y > y1) {
y1 = pre_y;
}
}
}
double meas = *(measurements.begin());
for (auto it = std::next(measurements.begin()); it != measurements.end(); ++it) {
double next = *it;
double l = log2(next - meas);
if (l < lsb) {
lsb = floor(l);
}
meas = next;
}
itv::interval Y(y0, y1, lsb);
if (y0 > y1) {
Y = itv::empty(); // if we didn't manage to draw any samples
}
itv::interval Z = (A.*mp)(X);
if ((Z >= Y) && (Z.lsb() <= Y.lsb())) {
double precision = (Z.size() == 0) ? 1 : Y.size() / Z.size();
std::cout << "\033[32m"
<< "OK " << e << ": " << title << "(" << X << ") = \n"
<< Z << "(c)\t >= \t" << Y << "(m)\t (precision " << precision
<< ", LSB diff = " << Y.lsb() - Z.lsb() << ")"
<< "\033[0m" << std::endl;
} else {
std::cout << "\033[31m"
<< "ERROR " << e << ": " << title << "(" << X << ") = \n"
<< Z << "(c)\t INSTEAD OF \t" << Y
<< "(m), \t LSB diff = " << Y.lsb() - Z.lsb() << "\033[0m" << std::endl;
}
std::cout << std::endl;
}
std::cout << std::endl << std::endl;
}
/**
* @brief Check the binary interval function gives a good approximation of the numerical function.
*
* @param E number of intervals/experiments
* @param M number of measurements used to estimate the resulting interval
* @param title, name of the tested function
* @param Dx maximal interval for x
* @param Dy maximal interval for y
* @param f the numerical function of reference
* @param bm the interval method corresponding to f
*/
void analyzeBinaryMethod(int E, int M, const char* title, const itv::interval& Dx,
const itv::interval& Dy, bfun f, bmth bm)
{
std::random_device R; // used to generate a random seed, based on some hardware randomness
std::default_random_engine generator(R());
std::uniform_real_distribution rdx(Dx.lo(), Dx.hi());
std::uniform_real_distribution rdy(Dy.lo(), Dy.hi());
itv::interval_algebra A;
std::cout << "Analysis of " << title << " in domains " << Dx << " x " << Dy << std::endl;
for (int e = 0; e < E; e++) { // for each experiment
// store output values in order to measure the output precision
std::set<double> measurements;
if ((Dx.lsb() < 0) || (Dy.lsb() < 0)) { // if we're not doing an integer operation
// X: random input interval X < Dx
double x0 = truncate(rdx(generator), Dx.lsb());
double x1 = truncate(rdx(generator), Dx.lsb());
itv::interval X(std::min(x0, x1), std::max(x0, x1), Dx.lsb());
// Y: random input interval Y < Dy
double y0 = truncate(rdy(generator), Dy.lsb());
double y1 = truncate(rdy(generator), Dy.lsb());
itv::interval Y(std::min(y0, y1), std::max(y0, y1), Dy.lsb());
// boundaries of the resulting interval Z
double zlo = HUGE_VAL; // std::min(t0, t1);
double zhi = -HUGE_VAL; // std::max(t0, t1);
// precision of the resulting interval
int lsb = INT_MAX;
// random values in X
std::uniform_real_distribution rvx(X.lo(), X.hi());
std::uniform_real_distribution rvy(Y.lo(), Y.hi());
// draw the upper bounds manually
double z = f(X.hi(),
Y.hi()); // no need to truncate: interval boundaries are already truncated
measurements.insert(z);
if (!std::isnan(z)) {
if (z < zlo) {
zlo = z;
}
if (z > zhi) {
zhi = z;
}
}
// measure the interval Z using the numerical function f
for (int m = 0; m < M; m++) { // M measurements
z = f(truncate(rvx(generator), Dx.lsb()), truncate(rvy(generator), Dy.lsb()));
measurements.insert(z);
if (!std::isnan(z)) {
if (z < zlo) {
zlo = z;
}
if (z > zhi) {
zhi = z;
}
}
}
double meas = *(measurements.begin());
for (auto it = std::next(measurements.begin()); it != measurements.end(); ++it) {
double next = *it;
double l = log2(next - meas);
if (l < lsb) {
lsb = floor(l);
}
meas = next;
}
itv::interval Zm(zlo, zhi, lsb); // the measured Z
if (zlo > zhi) {
Zm = itv::empty(); // if we didn't manage to draw any samples
}
itv::interval Zc = (A.*bm)(X, Y); // the computed Z
double precision = (Zm.size() == Zc.size()) ? 1 : Zm.size() / Zc.size();
if ((Zc >= Zm) && (Zc.lsb() <= Zm.lsb())) {
std::string color = "\033[32m";
if ((precision < 0.8) || (Zm.lsb() - Zc.lsb() >= 10)) {
color = "\033[36m"; // cyan instead of green if approximation is technically
// correct but of poor quality
}
std::cout << color << "OK " << e << ": " << title << "(" << X << ",\t" << Y
<< ")\n =c=> " << Zc << "(c) >= " << Zm << "(m)"
<< "\t (precision " << precision
<< "), \t LSB diff = " << Zm.lsb() - Zc.lsb() << "\033[0m" << std::endl;
} else {
std::cout << "\033[31m"
<< "ERROR " << e << ": " << title << "(" << X << ",\t" << Y << ")\n =c=> "
<< Zc << "(c) != " << Zm << "(m)"
<< "\t LSB diff = " << Zm.lsb() - Zc.lsb() << "\033[0m" << std::endl;
}
} else { // integer operation
// std::cout << "Testing integer version of " << title << std::endl;
// X: random input interval X < Dx
double x0 = truncate(rdx(generator), Dx.lsb());
double x1 = truncate(rdx(generator), Dx.lsb());
itv::interval X(std::min(x0, x1), std::max(x0, x1), Dx.lsb());
// Y: random input interval Y < Dy
double y0 = truncate(rdy(generator), Dy.lsb());
double y1 = truncate(rdy(generator), Dy.lsb());
itv::interval Y(std::min(y0, y1), std::max(y0, y1), Dy.lsb());
// boundaries of the resulting interval Z
int zlo = INT_MAX; // std::min(t0, t1);
int zhi = INT_MIN; // std::max(t0, t1);
// precision of the resulting interval
int lsb = INT_MAX;
// random values in X and Y
std::uniform_int_distribution ivx((int)X.lo(), (int)X.hi());
std::uniform_int_distribution ivy((int)Y.lo(), (int)Y.hi());
// draw the upper bounds manually
int z = f(X.hi(),
Y.hi()); // no need to truncate: interval boundaries are already truncated
measurements.insert((double)z);
if (z < zlo) {
zlo = z;
}
if (z > zhi) {
zhi = z;
}
// measure the interval Z using the numerical function f
for (int m = 0; m < M; m++) { // M measurements
int pre_x = ivx(generator);
int x = truncate(pre_x, Dx.lsb());
int pre_y = ivy(generator);
int y = truncate(pre_y, Dy.lsb());
z = f(x, y);
int pre_z = f(pre_x, pre_y);
measurements.insert(z);
if (!std::isnan(static_cast<double>(pre_z))) {
if (z < zlo) {
zlo = pre_z;
}
if (z > zhi) {
zhi = pre_z;
}
}
}
double meas = *(measurements.begin());
for (auto it = std::next(measurements.begin()); it != measurements.end(); ++it) {
double next = *it;
double l = log2(next - meas);
if (l < lsb) {
lsb = floor(l);
}
meas = next;
}
itv::interval Zm(zlo, zhi, lsb); // the measured Z
itv::interval Zc = (A.*bm)(X, Y); // the computed Z
double precision = (Zm.size() == Zc.size()) ? 1 : Zm.size() / Zc.size();
if ((Zc >= Zm) && (Zc.lsb() <= Zm.lsb())) {
std::string color = "\033[32m";
// cyan instead of green if approximation is technically correct but of poor quality
if ((precision < 0.8) || (Zm.lsb() - Zc.lsb() >= 10)) {
color = "\033[36m";
}
std::cout << color << "OK " << e << ": " << title << "(" << X << ",\t" << Y
<< ")\n =c=> " << Zc << "(c) >= " << Zm << "(m)"
<< "\t (precision " << precision
<< "), \t LSB diff = " << Zm.lsb() - Zc.lsb() << "\033[0m" << std::endl;
} else {
std::cout << "\033[31m"
<< "ERROR " << e << ": " << title << "(" << X << ",\t" << Y << ")\n =c=> "
<< Zc << "(c) != " << Zm << "(m)"
<< "\t LSB diff = " << Zm.lsb() - Zc.lsb() << "\033[0m" << std::endl;
}
}
}
std::cout << std::endl;
}
/**
* @brief Adjusts the lsb of an input interval to match a target output lsb
*
* @param title name of the tested function
* @param mp the interval method of the studied function
* @param X the input interval
* @param l the target lsb for the output
*/
void propagateBackwardsUnaryMethod(const char* title, umth mp, itv::interval& X, int l)
{
std::cout << "Shaving input " << X << " of " << title << " to achieve an output lsb of " << l
<< std::endl;
itv::interval_algebra A;
// itv::interval X = itv::interval(D.lo(), D.hi(), D.lsb());
itv::interval Z = (A.*mp)(X);
while (Z.lsb() < l) { // the lsb of Z is more precise than l
X = itv::interval(X.lo(), X.hi(), X.lsb() + 1);
Z = (A.*mp)(X);
std::cout << X.lsb() << " -> " << Z.lsb() << std::endl;
}
if (Z.lsb() > l) { // if we've overshot the target lsb
X = itv::interval(X.lo(), X.hi(), X.lsb() - 1);
}
std::cout << "Input interval " << X << " is sufficient" << std::endl;
}
/**
* @brief Adjusts the lsbs of two input intervals to a binary function to match a target output lbs
*
* @param title name of the tested function
* @param bm the interval method of the studied function
* @param X the first input interval
* @param Y the second input interval
* @param l the target lsb for the output
*/
void propagateBackwardsBinaryMethod(const char* title, bmth bm, itv::interval& X, itv::interval& Y,
int l)
{
std::cout << "Shaving inputs " << X << " and " << Y << " of " << title
<< " to achieve an output lsb of " << l << std::endl;
itv::interval_algebra A;
itv::interval Z = (A.*bm)(X, Y);
while (Z.lsb() < l) {
// std::cout << "X = " << X << "; Y = " << Y << std::endl;
if (X.lsb() < Y.lsb()) {
X = itv::interval(X.lo(), X.hi(), X.lsb() + 1);
std::cout << "Shaving interval X = " << X << std::endl;
} else {
Y = itv::interval(Y.lo(), Y.hi(), Y.lsb() + 1);
std::cout << "Shaving interval Y = " << Y << std::endl;
}
Z = (A.*bm)(X, Y);
}
std::cout << "Input intervals " << X << " and " << Y << " are sufficient" << std::endl;
}
/**
* @brief Adjusts the lsb of an input iterval to a list of composed functions to match a target
* output lsb
*
* @param titles names of the tested functions, from outermost to innermost
* @param mps the interval methods of the functions, from outermost to innermost
* @param X the input interval
* @param l the target lsb for the output
*/
void propagateBackwardsComposition(std::vector<const char*> titles, std::vector<umth> mps,
itv::interval& X, int l)
{
if (titles.size() != mps.size()) {
std::cout << "Incompatible vector sizes" << std::endl;
return;
}
size_t n = titles.size();
std::cout << "Shaving input " << X << " of ";
for (const auto* t : titles) {
std::cout << t << " ○ ";
}
std::cout << "\b\b\b";
std::cout << " to achieve an output lsb of " << l << std::endl << std::endl;
itv::interval_algebra A;
std::vector<itv::interval> intermediate_intervals{
X}; // should be one element bigger than titles and mps
for (size_t i = 0; i < n; i++) {
intermediate_intervals.push_back((A.*(mps[n - i - 1]))(intermediate_intervals[i]));
std::cout << titles[n - i - 1] << "(" << intermediate_intervals[i]
<< ") = " << intermediate_intervals[i + 1] << std::endl;
}
std::cout << std::endl << "Intermediate intervals before shaving:" << std::endl;
for (auto Y : intermediate_intervals) {
std::cout << Y << std::endl;
}
std::cout << std::endl;
int li = l;
for (size_t i = 0; i < n - 1; i++) {
propagateBackwardsUnaryMethod(titles[i], mps[i], intermediate_intervals[n - i - 1], li);
li = intermediate_intervals[n - i - 1].lsb();
std::cout << std::endl;
}
propagateBackwardsUnaryMethod(titles[n - 1], mps[n - 1], X, li);
// propagateBackwardsUnaryMethod(titles[0], mps[0], X, li);
// propagateBackwardsUnaryMethod(titles[1], mps[1], X, intermediate_intervals[1].lsb());
std::cout << std::endl;
itv::interval Y = X;
for (size_t i = 0; i < n; i++) {
std::cout << titles[n - i - 1] << "(" << Y << ") = ";
Y = (A.*mps[n - i - 1])(Y);
std::cout << Y << std::endl;
}
std::cout << std::endl << "Intermediate intervals after shaving:" << std::endl;
std::cout << X << std::endl;
for (auto Y : intermediate_intervals) {
std::cout << Y << std::endl;
}
}
|