/usr/include/polymake/next/TropicalNumber.h is in libpolymake-dev-common 3.2r2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* Copyright (c) 1997-2018
Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
http://www.polymake.org
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
--------------------------------------------------------------------------------
*/
/** @file TropicalNumber.h
@brief Implementation of pm::TropicalNumber class
*/
#ifndef POLYMAKE_TROPICALNUMBER_H
#define POLYMAKE_TROPICALNUMBER_H
#include "polymake/internal/converters.h"
#include "polymake/internal/comparators.h"
#include "polymake/Rational.h"
#include "polymake/GenericIO.h"
namespace pm {
/**
* Defines Min and Max such that they can be used as Addition by TropicalNumber
*/
struct Max;
struct Min {
template <typename T>
static
const T& apply(const T& x, const T& y)
{
return std::min(x,y);
}
template <typename T, typename T2>
static
T& assign(T& x, T2&& y)
{
if (x > y) x=std::forward<T2>(y);
return x;
}
static
constexpr int orientation() { return 1; }
typedef Max dual;
};
struct Max {
template <typename T>
static
const T& apply(const T& x, const T& y)
{
return std::max(x,y);
}
template <typename T, typename T2>
static
T& assign(T& x, T2&& y)
{
if (x < y) x=std::forward<T2>(y);
return x;
}
static
constexpr int orientation() { return -1; }
typedef Min dual;
};
template <typename Addition, typename Scalar>
class TropicalNumber;
template <typename Addition, typename Scalar>
int isinf(const TropicalNumber<Addition, Scalar>& x) noexcept;
/**
* TropicalNumber models a modification of a chosen scalar type. Its multiplication is the
* inherent addition of the scalar, the addition is passed as a template parameter (Usually it is
* Max or Min). The Addition must have methods apply(x,y), assign(x,y), and orientation(). The latter determines
* the tropical zero of the TropicalNumber type: It is Addition::orientation() * infinity.
*
* The Scalar has to know its zero and its own infinity value via std::numeric_limits.
*/
template <typename Addition, typename Scalar = Rational>
class TropicalNumber {
public:
template <typename T>
struct can_initialize_scalar
: bool_constant<can_initialize<pure_type_t<T>, Scalar>::value && !std::is_same<pure_type_t<T>, TropicalNumber>::value> {};
template <typename T>
struct can_assign_scalar
: bool_constant<isomorphic_types<pure_type_t<T>, Scalar>::value && can_assign_to<pure_type_t<T>, Scalar>::value && !std::is_same<pure_type_t<T>, TropicalNumber>::value> {};
template <typename T>
struct fits_as_scalar
: bool_constant<can_upgrade<pure_type_t<T>, Scalar>::value && !std::is_same<pure_type_t<T>, TropicalNumber>::value> {};
template <typename T>
struct fits_as_operand
: bool_constant<can_upgrade<pure_type_t<T>, Scalar>::value || std::is_same<pure_type_t<T>, TropicalNumber>::value> {};
// *** CONSTRUCTORS ***
// initialize with tropical zero
TropicalNumber()
: TropicalNumber(zero()) {}
TropicalNumber(const TropicalNumber&) = default;
TropicalNumber(TropicalNumber&&) = default;
template <typename T, typename=typename std::enable_if<can_initialize_scalar<T>::value>::type>
explicit TropicalNumber(T&& x)
: scalar(std::forward<T>(x)) {}
// *** ZERO ***
static
const TropicalNumber& zero()
{
return spec_object_traits<TropicalNumber>::zero();
}
// *** DUAL_ZERO ***
static
const TropicalNumber& dual_zero()
{
return spec_object_traits<TropicalNumber>::dual_zero();
}
// *** ONE ***
static
const TropicalNumber& one()
{
return spec_object_traits<TropicalNumber>::one();
}
// *** METHODS ***
static
int orientation() noexcept
{
return Addition::orientation();
}
explicit operator const Scalar& () const noexcept { return scalar; }
template <typename T,
typename=typename std::enable_if<!std::is_same<T, Scalar>::value && can_initialize<Scalar, T>::value>::type>
explicit operator T () const
{
return static_cast<T>(scalar);
}
// *** assignment ***
TropicalNumber& operator= (const TropicalNumber&) = default;
TropicalNumber& operator= (TropicalNumber&&) = default;
template <typename T, typename=typename std::enable_if<can_assign_scalar<T>::value>::type>
TropicalNumber& operator= (T&& a)
{
scalar=std::forward<T>(a);
return *this;
}
void swap(TropicalNumber& a)
{
std::swap(scalar, a.scalar);
}
// *** addition ***
// looks suspicious... shouldn't it call one() instead?
TropicalNumber& operator++ (int)
{
Addition::assign(scalar, spec_object_traits<Scalar>::zero());
return *this;
}
TropicalNumber& operator+= (const TropicalNumber& b)
{
Addition::assign(scalar, b.scalar);
return *this;
}
TropicalNumber& operator+= (TropicalNumber&& b)
{
Addition::assign(scalar, std::move(b.scalar));
return *this;
}
friend
TropicalNumber operator+ (const TropicalNumber& a, const TropicalNumber& b)
{
return TropicalNumber(Addition::apply(a.scalar, b.scalar));
}
friend
TropicalNumber&& operator+ (TropicalNumber&& a, const TropicalNumber& b)
{
return std::move(a+=b);
}
friend
TropicalNumber&& operator+ (const TropicalNumber& a, TropicalNumber&& b)
{
return std::move(b+=a);
}
friend
TropicalNumber&& operator+ (TropicalNumber&& a, TropicalNumber&& b)
{
return std::move(a+=std::move(b));
}
// *** multiplication ***
TropicalNumber& operator*= (const TropicalNumber& b)
{
if (!std::numeric_limits<Scalar>::has_infinity) {
const int i1=isinf(*this), i2=isinf(b);
if (i1 || i2) {
if (i1+i2==0) throw GMP::NaN();
if (!i1) *this=b;
return *this;
}
}
scalar += b.scalar;
return *this;
}
friend
TropicalNumber operator* (const TropicalNumber& a, const TropicalNumber& b)
{
if (!std::numeric_limits<Scalar>::has_infinity) {
const int i1=isinf(a), i2=isinf(b);
if (i1 || i2) {
if (i1+i2==0) throw GMP::NaN();
return i1 ? a : b;
}
}
return TropicalNumber(a.scalar + b.scalar);
}
friend
TropicalNumber&& operator* (TropicalNumber&& a, const TropicalNumber& b)
{
return std::move(a*=b);
}
friend
TropicalNumber&& operator* (const TropicalNumber& a, TropicalNumber&& b)
{
return std::move(b*=a);
}
friend
TropicalNumber&& operator* (TropicalNumber&& a, TropicalNumber&& b)
{
return std::move(a*=b);
}
// *** division ***
TropicalNumber& operator/= (const TropicalNumber& b)
{
if (!std::numeric_limits<Scalar>::has_infinity) {
const int i1=isinf(*this), i2=isinf(b);
if (i1 || i2) {
if (i1==i2) throw GMP::NaN();
if (!i1) scalar=-b.scalar;
}
}
scalar -= b.scalar;
return *this;
}
friend
TropicalNumber operator/ (const TropicalNumber& a, const TropicalNumber& b)
{
if (!std::numeric_limits<Scalar>::has_infinity) {
const int i1=isinf(a), i2=isinf(b);
if (i1 || i2) {
if (i1==i2) throw GMP::NaN();
return TropicalNumber(i1 ? a.scalar : -b.scalar);
}
}
return TropicalNumber(a.scalar - b.scalar);
}
friend
TropicalNumber&& operator/ (TropicalNumber&& a, const TropicalNumber& b)
{
return std::move(a/=b);
}
friend
TropicalNumber&& operator/ (const TropicalNumber& a, TropicalNumber&& b)
{
b /= a;
negate(b.scalar);
return std::move(b);
}
friend
TropicalNumber&& operator/ (TropicalNumber&& a, TropicalNumber&& b)
{
return std::move(a/=b);
}
// *** comparisons ***
int compare(const TropicalNumber& a) const
{
return operations::cmp()(scalar, a.scalar);
}
friend
bool operator== (const TropicalNumber& a, const TropicalNumber& b)
{
return a.scalar == b.scalar;
}
template <typename T, typename=typename std::enable_if<fits_as_scalar<T>::value>::type>
friend
bool operator== (const TropicalNumber& a, const T& b)
{
return a.scalar==b;
}
template <typename T, typename=typename std::enable_if<fits_as_scalar<T>::value>::type>
friend
bool operator== (const T& a, const TropicalNumber& b)
{
return b==a;
}
template <typename T, typename=typename std::enable_if<fits_as_operand<T>::value>::type>
friend
bool operator!= (const TropicalNumber& a, const T& b)
{
return !(a==b);
}
template <typename T, typename=typename std::enable_if<fits_as_scalar<T>::value>::type>
friend
bool operator!= (const T& a, const TropicalNumber& b)
{
return !(b==a);
}
friend
bool operator< (const TropicalNumber& a, const TropicalNumber& b)
{
return a.scalar < b.scalar;
}
friend
bool operator> (const TropicalNumber& a, const TropicalNumber& b)
{
return b<a;
}
friend
bool operator<= (const TropicalNumber& a, const TropicalNumber& b)
{
return !(b<a);
}
friend
bool operator>= (const TropicalNumber& a, const TropicalNumber& b)
{
return !(a<b);
}
// *** input/output ***
template <typename Input>
friend
Input& operator>> (GenericInput<Input>& in, TropicalNumber& me)
{
return me.read(in.top(), bool_constant<is_derived_from_instance_of<Input, PlainParser>::value && !std::numeric_limits<Scalar>::has_infinity>());
}
template <typename Output>
friend
Output& operator<< (GenericOutput<Output>& out, const TropicalNumber& me)
{
return me.write(out.top(), bool_constant<is_derived_from_instance_of<Output, PlainPrinter>::value && !std::numeric_limits<Scalar>::has_infinity>());
}
protected:
template <typename Input>
Input& read(Input& in, std::false_type)
{
return in >> scalar;
}
template <typename Input>
Input& read(Input& in, std::true_type)
{
if (const int inf=in.probe_inf())
scalar=inf*std::numeric_limits<Scalar>::max();
else
in >> scalar;
return in;
}
template <typename Output>
Output& write(Output& out, std::false_type) const
{
return out << scalar;
}
template <typename Output>
Output& write(Output& out, std::true_type) const
{
if (const int inf=isinf(*this))
out << (inf>0 ? "inf" : "-inf");
else
out << scalar;
return out;
}
/// numerical value
Scalar scalar;
};
/**
* Object traits for TropicalNumber: Defines when a TropicalNumber is tropically zero/one and
* what tropical zero and one are.
*/
template <typename Addition, typename Scalar>
struct spec_object_traits<TropicalNumber<Addition, Scalar> >
: spec_object_traits<is_scalar> {
static
bool is_zero(const TropicalNumber<Addition, Scalar>& a) noexcept(noexcept(isinf(std::declval<Scalar>())))
{
return std::numeric_limits<Scalar>::has_infinity
? Addition::orientation() * isinf(static_cast<const Scalar&>(a)) == 1
: static_cast<const Scalar&>(a) == Addition::orientation() * std::numeric_limits<Scalar>::max();
}
static
bool is_one(const TropicalNumber<Addition, Scalar>& a) noexcept(noexcept(pm::is_zero(std::declval<Scalar>())))
{
return pm::is_zero(static_cast<const Scalar&>(a));
}
static
const TropicalNumber<Addition, Scalar>& zero()
{
static const TropicalNumber<Addition, Scalar> t_zero(Addition::orientation() *
(std::numeric_limits<Scalar>::has_infinity
? std::numeric_limits<Scalar>::infinity()
: std::numeric_limits<Scalar>::max()));
return t_zero;
}
static const TropicalNumber<Addition, Scalar>& dual_zero()
{
static const TropicalNumber<Addition, Scalar> t_d_zero(-Addition::orientation() *
(std::numeric_limits<Scalar>::has_infinity
? std::numeric_limits<Scalar>::infinity()
: std::numeric_limits<Scalar>::max()));
return t_d_zero;
}
static const TropicalNumber<Addition, Scalar>& one()
{
static const TropicalNumber<Addition, Scalar> t_one(spec_object_traits<Scalar>::zero());
return t_one;
}
};
template <typename Addition, typename Scalar>
inline
bool isfinite(const TropicalNumber<Addition, Scalar>& x) noexcept
{
return std::numeric_limits<Scalar>::has_infinity
? isfinite(static_cast<const Scalar&>(x))
: static_cast<const Scalar&>(x) != std::numeric_limits<Scalar>::max() &&
static_cast<const Scalar&>(x) != std::numeric_limits<Scalar>::min();
}
template <typename Addition, typename Scalar>
inline
int isinf(const TropicalNumber<Addition, Scalar>& x) noexcept
{
return std::numeric_limits<Scalar>::has_infinity
? isinf(static_cast<const Scalar&>(x)) :
static_cast<const Scalar&>(x) == std::numeric_limits<Scalar>::min()
? -1 : static_cast<const Scalar&>(x) == std::numeric_limits<Scalar>::max();
}
// for comparisons with implicit zero in sparse containers
template <typename Addition, typename Scalar>
inline
int sign(const TropicalNumber<Addition, Scalar>& x) noexcept
{
return is_zero(x) ? 0 : -Addition::orientation();
}
template <typename TPrimitive, typename Addition, typename Scalar>
constexpr
typename std::enable_if<std::is_arithmetic<Scalar>::value, TPrimitive>::type
max_value_as(mlist<TropicalNumber<Addition, Scalar>>)
{
return max_value_as<TPrimitive>(mlist<Scalar>());
}
template <typename TPrimitive, typename Addition, typename Scalar>
constexpr
typename std::enable_if<std::is_arithmetic<Scalar>::value, TPrimitive>::type
min_value_as(mlist<TropicalNumber<Addition, Scalar>>)
{
return min_value_as<TPrimitive>(mlist<Scalar>());
}
}
namespace std {
template <typename Addition, typename Scalar>
inline
void swap(pm::TropicalNumber<Addition, Scalar>& a, pm::TropicalNumber<Addition, Scalar>& b)
{
a.swap(b);
}
template <typename Addition, typename Scalar>
class numeric_limits<pm::TropicalNumber<Addition, Scalar>>
: public numeric_limits<Scalar> {
typedef numeric_limits<Scalar> base_t;
public:
typedef pm::TropicalNumber<Addition, Scalar> value_type;
static const bool has_infinity=true;
static value_type min() noexcept(base_t::min()) { return value_type(base_t::min()); }
static value_type infinity() noexcept(base_t::infinity()) { return value_type(-Addition::orientation() * base_t::infinity()); }
static value_type max() noexcept(base_t::max()) { return value_type(base_t::max()); }
};
}
namespace polymake {
using pm::Min;
using pm::Max;
using pm::TropicalNumber;
}
#endif // POLYMAKE_TROPICALNUMBER_H
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|