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
|
/****************************************************************************
* Core Library Version 1.7, August 2004
* Copyright (c) 1995-2004 Exact Computation Project
* All rights reserved.
*
* This file is part of CORE (http://cs.nyu.edu/exact/core/); you may
* redistribute it under the terms of the Q Public License version 1.0.
* See the file LICENSE.QPL distributed with CORE.
*
* Licensees holding a valid commercial license may use this file in
* accordance with the commercial license agreement provided with the
* software.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*
* File: Expr.h
* Synopsis: a class of Expression in Level 3
*
* Written by
* Koji Ouchi <ouchi@simulation.nyu.edu>
* Chee Yap <yap@cs.nyu.edu>
* Igor Pechtchanski <pechtcha@cs.nyu.edu>
* Vijay Karamcheti <vijayk@cs.nyu.edu>
* Chen Li <chenli@cs.nyu.edu>
* Zilin Du <zilin@cs.nyu.edu>
* Sylvain Pion <pion@cs.nyu.edu>
* Vikram Sharma<sharma@cs.nyu.edu>
*
* WWW URL: http://cs.nyu.edu/exact/
* Email: exact@cs.nyu.edu
*
* $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Core/include/CORE/Expr.h $
* $Id: Expr.h 28567 2006-02-16 14:30:13Z lsaboret $
***************************************************************************/
#ifndef _CORE_EXPR_H_
#define _CORE_EXPR_H_
#include <CORE/ExprRep.h>
CORE_BEGIN_NAMESPACE
/// \class Expr Expr.h
/// \brief Expr is a class of Expression in Level 3
typedef RCImpl<ExprRep> RCExpr;
class Expr : public RCExpr {
public:
/// \name Constructors and Destructor
//@{
/// default constructor
Expr() : RCExpr(new ConstDoubleRep()) {}
/// constructor for <tt>int</tt>
Expr(int i) : RCExpr(new ConstDoubleRep(i)) {}
/// constructor for <tt>unsigned int</tt>
Expr(unsigned int ui) : RCExpr(new ConstDoubleRep(ui)) {}
/// constructor for <tt>long</tt>
Expr(long l) : RCExpr(new ConstRealRep(Real(l))) {}
/// constructor for <tt>unsigned long</tt>
Expr(unsigned long ul) : RCExpr(new ConstRealRep(Real(ul))) {}
/// constructor for <tt>float</tt>
/** \note the results of this constructor may appear unpredictable to the
* user. E.g., one may assume that new Expr(.1) is exactly equal to .1,
* but it will be print as
* .1000000000000000055511151231257827021181583404541015625.
* This is so because .1 cannot be represented exactly as a double
* (or, for that matter, as a binary fraction of any finite length).
* The value is the closest double value determined by the compiler.
*/
Expr(float f) : RCExpr(NULL) { // check for valid numbers
// (i.e., not infinite and not NaN)
if (!finite(f)) {
std::cerr << " ERROR : constructed an invalid float! " << std::endl;
if (AbortFlag)
abort();
InvalidFlag = -1;
}
rep = new ConstDoubleRep(f);
}
/// constructor for <tt>double</tt>
Expr(double d) : RCExpr(NULL) { // check for valid numbers
// (i.e., not infinite and not NaN)
if (!finite(d)) {
std::cerr << " ERROR : constructed an invalid double! " << std::endl;
if (AbortFlag)
abort();
InvalidFlag = -2;
}
rep = new ConstDoubleRep(d);
}
/// constructor for <tt>BigInt</tt>
Expr(const BigInt& I) : RCExpr(new ConstRealRep(Real(I))) {}
/// constructor for <tt>BigRat</tt>
Expr(const BigRat& R) : RCExpr(new ConstRealRep(Real(R))) {}
/// constructor for <tt>BigFloat</tt>
Expr(const BigFloat& F) : RCExpr(new ConstRealRep(Real(F))) {}
/// constructor for <tt>const char*</tt>
/** construct Expr from a string representation \a s
* with precision \a prec. It is perfectly predictable:
* new Expr(".1") is exactly equal to .1, as one would expect. Therefore,
* it is generally recommended that the (String) constructor be used in
* preference to the (double) constructor.
*/
Expr(const char *s, const extLong& p = defInputDigits)
: RCExpr(new ConstRealRep(Real(s, p))) {}
/// constructor for <tt>std::string</tt>
Expr(const std::string& s, const extLong& p = defInputDigits)
: RCExpr(new ConstRealRep(Real(s, p))) {}
/// constructor for <tt>Real</tt>
Expr(const Real &r) : RCExpr(new ConstRealRep(r)) {}
/// constructor for Polynomial node (n-th root)
/** default value n=0 means the first positive root */
template <class NT>
Expr(const Polynomial<NT>& p, int n = 0)
: RCExpr(new ConstPolyRep<NT>(p, n)) {}
/// constructor for Polynomial node (root in Interval <tt>I</tt>)
template <class NT>
Expr(const Polynomial<NT>& p, const BFInterval& I)
: RCExpr(new ConstPolyRep<NT>(p, I)) {}
/// constructor for ExprRep
Expr(ExprRep* p) : RCExpr(p) {}
//@}
/// \name Copy-Assignment-Destructors
//@{
/// copy constructor
Expr(const Expr& rhs) : RCExpr(rhs) {
rep->incRef();
}
/// = operator
Expr& operator=(const Expr& rhs) {
if (this != &rhs) {
rep->decRef();
rep = rhs.rep;
rep->incRef();
}
return *this;
}
/// destructor
~Expr() {
rep->decRef();
}
//@}
/// \name Compound Assignment Operators
//@{
/// += operator
Expr& operator+=(const Expr& e) {
*this = new AddRep(rep, e.rep);
return *this;
}
/// -= operator
Expr& operator-=(const Expr& e) {
*this = new SubRep(rep, e.rep);
return *this;
}
/// *= operator
Expr& operator*=(const Expr& e) {
*this = new MultRep(rep, e.rep);
return *this;
}
/// /= operator
Expr& operator/=(const Expr& e) {
if ((e.rep)->getSign() == 0) {
std::cerr << " ERROR : division by zero ! " << std::endl;
if (AbortFlag)
abort();
InvalidFlag = -3;
}
*this = new DivRep(rep, e.rep);
return *this;
}
//@}
/// \name Unary Minus, Increment and Decrement Operators
//@{
/// unary plus
Expr operator+() const {
return Expr(*this);
}
/// unary minus
Expr operator-() const {
return Expr(new NegRep(rep));
}
/// left increment operator (++i)
Expr& operator++() {
*this += 1;
return *this;
}
/// right increment operator (i++)
Expr operator++(int) {
Expr t(*this);
*this += 1;
return t;
}
/// left decrement operator (--i)
Expr& operator--() {
*this -= 1;
return *this;
}
/// right deccrement operator (i--)
Expr operator--(int) {
Expr t(*this);
*this -= 1;
return t;
}
//@}
/// \name String Conversion Functions
//@{
/// set value from <tt>const char*</tt>
void fromString(const char* s, const extLong& prec = defInputDigits) {
*this = Expr(s, prec);
}
/// convert to <tt>std::string</tt>
/** give decimal string representation */
std::string toString(long prec=defOutputDigits, bool sci=false) const {
return rep->toString(prec, sci);
}
//@}
//
/// \name Conversion Functions
//@{
/// convert to \c int
int intValue() const {
return approx(64, 1024).intValue();
}
/// convert to \c long
long longValue() const {
return approx(64, 1024).longValue();
}
/// convert to \c float
float floatValue() const {
return approx(53, 1024).floatValue();
}
/// convert to \c double
/** chen: - use equivalent precision (rel:53, abs: 1024)
as in IEEE double. enforce an evaluation in case
before this has been done before casting. */
double doubleValue() const {
return approx(53, 1024).doubleValue();
}
/// convert to an interval defined by a pair of \c double
/** If value is exact, the two \c double will coincide
*/
void doubleInterval(double & lb, double & ub) const;
/// convert to \c BigInt (approximate it first!)
BigInt BigIntValue() const {
return rep->BigIntValue();
}
/// convert to \c BigRat (approximate it first!)
BigRat BigRatValue() const {
return rep->BigRatValue();
}
/// convert to \c BigFloat (approximate it first!)
/** Ought to allow BigFloatValue() take an optional precision argument */
BigFloat BigFloatValue() const {
return rep->BigFloatValue();
}
//@}
/// \name Approximation Function
//@{
/// Compute approximation to combined precision [\a r, \a a].
/** Here is the definition of what this means:
If e is the exact value and ee is the approximate value,
then |e - ee| <= 2^{-a} or |e - ee| <= 2^{-r} |e|. */
const Real & approx(const extLong& relPrec = defRelPrec,
const extLong& absPrec = defAbsPrec) const {
return rep->getAppValue(relPrec, absPrec);
}
//@}
/// \name Helper Functions
//@{
//CONSTANTS:
/// return Expr(0)
static const Expr& getZero();
/// return Expr(1)
static const Expr& getOne();
/// Has Exact Division
static bool hasExactDivision() {
return true;
}
/// get the sign
int sign() const {
return rep->getSign();
}
/// is zero?
bool isZero() const {
return sign() == 0;
}
/// absolute value
Expr abs() const {
return (sign() >= 0) ? +(*this) : -(*this);
}
/// compare function
int cmp(const Expr& e) const {
return rep == e.rep ? 0 : SubRep(rep, e.rep).getSign();
}
/// return the internal representation
ExprRep* Rep() const {
return rep;
}
/// get exponent of current approximate value
long getExponent() const {
return BigFloatValue().exp();
}
/// get mantissa of current approximate value
BigInt getMantissa() const {
return BigFloatValue().m();
}
//@}
public:
/// \name Debug Helper Function
//@{
/// debug function
void debug(int mode = TREE_MODE, int level = DETAIL_LEVEL,
int depthLimit = INT_MAX) const;
//@}
/// debug information levels
enum {LIST_MODE, TREE_MODE, SIMPLE_LEVEL, DETAIL_LEVEL};
};// class Expr
#define CORE_EXPR_ZERO Expr::getZero()
/// I/O Stream operator<<
inline std::ostream& operator<<(std::ostream& o, const Expr& e) {
o << *(const_cast<ExprRep*>(&e.getRep()));
return o;
}
/// I/O Stream operator>>
inline std::istream& operator>>(std::istream& i, Expr& e) {
Real rVal;
i >> rVal; // precision is = defInputDigits
if (i)
e = rVal; // only assign when reading is successful.
return i;
}
/// floor function
BigInt floor(const Expr&, Expr&);
/// power function
Expr pow(const Expr&, unsigned long);
/// addition
inline Expr operator+(const Expr& e1, const Expr& e2) {
return Expr(new AddRep(e1.Rep(), e2.Rep()));
}
/// substraction
inline Expr operator-(const Expr& e1, const Expr& e2) {
return Expr(new SubRep(e1.Rep(), e2.Rep()));
}
/// multiplication
inline Expr operator*(const Expr& e1, const Expr& e2) {
return Expr(new MultRep(e1.Rep(), e2.Rep()));
}
/// division
inline Expr operator/(const Expr& e1, const Expr& e2) {
if (e2.sign() == 0) {
std::cerr << " ERROR : division by zero ! " << std::endl;
if (AbortFlag)
abort();
InvalidFlag = -4;
}
return Expr(new DivRep(e1.Rep(), e2.Rep()));
}
/// modulo operator
inline Expr operator%(const Expr& e1, const Expr& e2) {
Expr result;
floor(e1/e2, result);
return result;
}
/// operator ==
/** this is inefficient if you compare to zero:
* e.g., if (e != 0) {...} use e.isZero() instead */
inline bool operator==(const Expr& e1, const Expr& e2) {
return e1.cmp(e2) == 0;
}
/// operator !=
inline bool operator!=(const Expr& e1, const Expr& e2) {
return e1.cmp(e2) != 0;
}
/// operator <
inline bool operator< (const Expr& e1, const Expr& e2) {
return e1.cmp(e2) < 0;
}
/// operator <=
inline bool operator<=(const Expr& e1, const Expr& e2) {
return e1.cmp(e2) <= 0;
}
/// operator <
inline bool operator> (const Expr& e1, const Expr& e2) {
return e1.cmp(e2) > 0;
}
/// operator >=
inline bool operator>=(const Expr& e1, const Expr& e2) {
return e1.cmp(e2) >= 0;
}
/// return sign
inline int sign(const Expr& e) {
return e.sign();
}
/// is zero?
inline bool isZero(const Expr& e) {
return e.isZero();
}
/// compare
/** compare two Expr \a e1 and \a e2, return
* \retval -1 if e1 < e2,
* \retval 0 if e1 = e2,
* \retval 1 if e1 > e2. */
inline int cmp(const Expr& e1, const Expr& e2) {
return e1.cmp(e2);
}
/// absolute value
inline Expr abs(const Expr& x) {
return x.abs();
}
/// absolute value (same as abs)
inline Expr fabs(const Expr& x) {
return abs(x);
}
/// floor
inline BigInt floor(const Expr& e) {
Expr tmp;
return floor(e, tmp);
}
/// ceiling
inline BigInt ceil(const Expr& e) {
return -floor(-e);
}
/// floorLg
inline long floorLg(const Expr& e) {
Expr tmp;
return floorLg(floor(e));
}
/// ceilLg
inline long ceilLg(const Expr& e) {
Expr tmp;
return ceilLg(ceil(e));
}
/// power
inline Expr power(const Expr& e, unsigned long p) {
return pow(e, p);
}
/// divisibility predicate
/** We do not check if e2 is 0.
* */
// NOTE: The name "isDivisible" is not consistent
// with the analogous "divisible" predicate in BigInt!
inline bool isDivisible(const Expr& e1, const Expr& e2) {
Expr result;
floor(e1/e2, result);
return (result.sign() == 0);
}
/// square root
inline Expr sqrt(const Expr& e) {
if (e.sign() < 0) {
std::cerr << " ERROR : sqrt of negative value ! " << std::endl;
if (AbortFlag)
abort();
InvalidFlag = -5;
}
return Expr(new SqrtRep(e.Rep()));
}
//Following two have been added to make NT=Expr work for Polynomial<NT>
/// gcd
inline Expr gcd(const Expr& a, const Expr& b) {
return Expr(1);
}
inline Expr div_exact(const Expr& x, const Expr& y) {
return x/y - x%y;
}
/// helper function for constructing Polynomial node (n-th node)
template <class NT>
inline Expr rootOf(const Polynomial<NT>& p, int n = 0) {
return Expr(p, n);
}
/// helper function for constructing Polynomial node witb BFInterval
template <class NT>
inline Expr rootOf(const Polynomial<NT>& p, const BFInterval& I) {
return Expr(p, I);
}
/// helper function for constructing Polynomial node with pair of BigFloats
template <class NT>
inline Expr rootOf(const Polynomial<NT>& p, const BigFloat& x,
const BigFloat& y) {
return Expr(p, BFInterval(x, y) );
}
/// helper function for constructing Polynomial node with pair of doubles
template <class NT>
inline Expr rootOf(const Polynomial<NT>& p, double x, double y) {
return Expr(p, BFInterval(BigFloat(x), BigFloat(y)) );
}
/// helper function for constructing Polynomial node with pair of ints
template <class NT>
inline Expr rootOf(const Polynomial<NT>& p, int x, int y) {
return Expr(p, BFInterval(BigFloat(x), BigFloat(y)) );
}
/// constructor for Polynomial node of the form x^m - n (i.e., radicals)
/** We assume that n >= 0 and m >= 1
* */
template <class NT>
inline Expr radical(const NT& n, int m) {
assert(n>=0 && m>=1);
if (n == 0 || n == 1 || m == 1)
return Expr(n);
Polynomial<NT> Q(m);
Q.setCoeff(0, -n);
Q.setCoeff(m, 1);
return Expr(Q, 0);
}
// We include this file here and not from inside Poly.h,
// because otherwise VC++.net2003 can't compile Expr.cpp
#include <CORE/poly/Poly.tcc>
CORE_END_NAMESPACE
#endif // _CORE_EXPR_H_
|