1 /* 2 Qalculate (library) 3 4 Copyright (C) 2003-2007, 2008, 2016-2018 Hanna Knutsson (hanna.knutsson@protonmail.com) 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 */ 11 12 #ifndef MATH_STRUCTURE_H 13 #define MATH_STRUCTURE_H 14 15 #include <libqalculate/includes.h> 16 #include <libqalculate/Number.h> 17 #include <libqalculate/QalculateDateTime.h> 18 #include <sys/time.h> 19 20 class QalculateDate; 21 22 /** @file */ 23 24 /// Types for MathStructure 25 typedef enum { 26 STRUCT_MULTIPLICATION, 27 STRUCT_INVERSE, 28 STRUCT_DIVISION, 29 STRUCT_ADDITION, 30 STRUCT_NEGATE, 31 STRUCT_POWER, 32 STRUCT_NUMBER, 33 STRUCT_UNIT, 34 STRUCT_SYMBOLIC, 35 STRUCT_FUNCTION, 36 STRUCT_VARIABLE, 37 STRUCT_VECTOR, 38 STRUCT_BITWISE_AND, 39 STRUCT_BITWISE_OR, 40 STRUCT_BITWISE_XOR, 41 STRUCT_BITWISE_NOT, 42 STRUCT_LOGICAL_AND, 43 STRUCT_LOGICAL_OR, 44 STRUCT_LOGICAL_XOR, 45 STRUCT_LOGICAL_NOT, 46 STRUCT_COMPARISON, 47 STRUCT_UNDEFINED, 48 STRUCT_ABORTED, 49 STRUCT_DATETIME 50 } StructureType; 51 52 enum { 53 MULTIPLICATION_SIGN_NONE, 54 MULTIPLICATION_SIGN_SPACE, 55 MULTIPLICATION_SIGN_OPERATOR, 56 MULTIPLICATION_SIGN_OPERATOR_SHORT 57 }; 58 59 enum { 60 TAG_TYPE_HTML, 61 TAG_TYPE_TERMINAL 62 }; 63 64 /// A structure representing a mathematical value/expression/result 65 /** 66 * A MathStructure can both be container representing an operation with an ordered list of children or simple value representing 67 * a number, , variable etc. The children of a container might be of any type, allowing a tree-like nested structure. 68 * 69 * These are the most common conatiner/operation types: 70 * - \b Addition: contains two or more children, representing terms (x+y+...) 71 * - \b Multiplication: contains two or more children, representing factors (x*y*...) 72 * - \b Power: contains exactly two children, representing base and exponent (x^y) 73 * - \b Function: contains a two or more children, representing arguments, and a reference to a MathFunction object ( f(x,y,...) ) 74 * - \b Comparison: an equality or inequality containing exactly two children, represening the expressions right and left of the sign, specified with a ComparisonType (x=y, x!=y, x>y, ...) 75 * - \b Vector: contains zero or more children, representing elements in a vector ( [x, y, z, ...] ) 76 * 77 * Also available are containers representing logical and bitwise operations. 78 * Subtraction is represented by an addition structure with negated children and division by a multiplication structure with inverted children. 79 * Matrices is represented by a vector with vectors as children. 80 * 81 * For formatted structures, the following types is also available: 82 * - \b Negation: contains exactly one child (-x) 83 * - \b Invertion: contains exactly one child (1/x) 84 * - \b Division: contains exactly two children representing numerator and denominator (x/y) 85 * 86 * The following value types are available: 87 * - \b Number: has a Number object, representing a rational, floating point, complex or infinite numeric value 88 * - \b Variable: has a reference to a Variable object, with a known or unknown value 89 * - \b Symbolic: has an associated text string, with assumptions about the represented value controlled by the default assumptions 90 * - \b Unit: has a reference to a Unit object, and might in a formatted structure also have a reference to a Prefix object 91 * - \b Undefined: represents an undefined value 92 * 93 * To create a MathStructure, you can either create a simple structure using the constructors and then expanding it with structural operations, 94 * or use the parse or calculation functions of the global Calculator object to convert an expression string. 95 * 96 * The expression "(5x + 2) * 3" can be turned into a MathStructure either using 97 * \code 98 * MathStructure mstruct = CALCULATOR->parse("(5x + 2) * 3"); 99 * \endcode 100 * or 101 * \code 102 * MathStructure mstruct(5); 103 * mstruct *= CALCULATOR->v_x; 104 * mstruct += 2; 105 * mstruct *= 3: 106 * \endcode 107 * The first variant is obviously simpler, but slower and allows less control. 108 * 109 * Then, to evaluate/calculate/simplify (whatever) a structure, eval() should normally be used. The EvaluationOptions passed to eval() allows much control over the process 110 * and the outcome. 111 * \code 112 * EvaluationOptions eo; 113 * mstruct.eval(eo); 114 * \endcode 115 * 116 * After that, to display the result, you should first format the structure using format() and then display it using print(), passing the PrintOptions to both. 117 * \code 118 * PrintOptions po; 119 * mstruct.format(po); 120 * std::cout << mstruct.print(po) << std::endl; 121 * \endcode 122 * 123 * Most low-level functions expect the structure to be unformatted och require that unformat() is called after an expression string has been parsed or format() has been called. 124 * 125 * To access a child structure either the [] operator or the safer getChild() can be used. 126 * Note however that the index passed to the operator start at zero and the index argument for getChild() starts at one. 127 * \code 128 * MathStructure mstruct(5); 129 * mstruct += 2; 130 * std::cout << mstruct.print() << std::endl; // output: "5 + 2" 131 * std::cout << mstruct.getChild(1)->print() << std::endl; // output: "5" 132 * std::cout << mstruct[1].print() << std::endl; // output: "2" 133 * \endcode 134 * 135 * MathStructure uses reference count for management of objects allocated with new. 136 * Call ref() when starting to use the object and unref() when done. 137 * Note that the reference count is initialized to 1 in the constructors, so ref() should not be called after the object creation. 138 * This system is used for all child objects, so the following is perfectly legal: 139 * \code 140 * MathStructure *mchild_p = mstruct->getChild(1); 141 * mchild_p->ref(); // mchild_p reference count = 2 142 * mstruct->unref(); //mstruct reference count = 0, mstruct deleted, mchild_p reference count = 1 143 * (...) 144 * mchild_p->unref(); // mchild_p reference count = 0, mchild_p deleted 145 * \endcode 146 */ 147 148 class MathStructure { 149 150 protected: 151 152 size_t i_ref; 153 154 StructureType m_type; 155 bool b_approx; 156 int i_precision; 157 158 std::vector<MathStructure*> v_subs; 159 std::vector<size_t> v_order; 160 std::string s_sym; 161 Number o_number; 162 Variable *o_variable; 163 164 Unit *o_unit; 165 Prefix *o_prefix; 166 bool b_plural; 167 168 MathFunction *o_function; 169 MathStructure *function_value; 170 QalculateDateTime *o_datetime; 171 172 ComparisonType ct_comp; 173 bool b_protected; 174 175 bool b_parentheses; 176 177 bool isolate_x_sub(const EvaluationOptions &eo, EvaluationOptions &eo2, const MathStructure &x_var, MathStructure *morig = NULL); 178 void init(); 179 180 public: 181 182 /** @name Constructors */ 183 //@{ 184 /** Create a new structure, initialized to zero. */ 185 MathStructure(); 186 /** Create a copy of a structure. Child structures are copied. 187 * 188 * @param o The structure to copy. 189 */ 190 MathStructure(const MathStructure &o); 191 /** Create a new numeric structure (value=num/den*10^exp10). Equivalent to MathStructure(Number(num, den, exp10)). 192 * 193 * @param num The numerator of the numeric value. 194 * @param den The denominator of the numeric value. 195 * @param exp10 The base 10 exponent of the numeric value. 196 */ 197 MathStructure(int num, int den = 1, int exp10 = 0); 198 MathStructure(long int num, long int den, long int exp10 = 0L); 199 /** Create a new symbolic/text structure. 200 * 201 * @param sym Symbolic/text value. 202 * @param force_symbol Do not check for undefined or date value. 203 */ 204 MathStructure(std::string sym, bool force_symbol = false); 205 /** Create a new date and time structure. 206 * 207 * @param sym Date and time value. 208 */ 209 MathStructure(const QalculateDateTime &o_dt); 210 /** Create a new numeric structure with floating point value. Uses Number::setFloat(). 211 * 212 * @param o Numeric value. 213 */ 214 MathStructure(double float_value); 215 /** Create a new vector. 216 * 217 * @param o The first element (copied) in the vector. 218 * @param ... Elements (copied) in the vector. End with NULL. 219 */ 220 MathStructure(const MathStructure *o, ...); 221 /** Create a new function structure. 222 * 223 * @param o Function value. 224 * @param ... Arguments (copied) to the function. End with NULL. 225 */ 226 MathStructure(MathFunction *o, ...); 227 /** Create a new unit structure. 228 * 229 * @param u The unit value. 230 * @param p Prefix of the unit. 231 */ 232 MathStructure(Unit *u, Prefix *p = NULL); 233 /** Create a new variable structure. 234 * 235 * @param o Variable value. 236 */ 237 MathStructure(Variable *o); 238 /** Create a new numeric structure. 239 * 240 * @param o Numeric value. 241 */ 242 MathStructure(const Number &o); 243 ~MathStructure(); 244 //@} 245 246 /** @name Functions/operators for setting type and content */ 247 //@{ 248 /** Set the structure to a copy of another structure. Child structures are copied. 249 * 250 * @param o The structure to copy. 251 * @param merge_precision Preserve the current precision (unless the new value has a lower precision). 252 */ 253 void set(const MathStructure &o, bool merge_precision = false); 254 /** Set the structure to a copy of another structure. Pointers to child structures are copied. 255 * 256 * @param o The structure to copy. 257 * @param merge_precision Preserve the current precision (unless the new value has a lower precision). 258 */ 259 void set_nocopy(MathStructure &o, bool merge_precision = false); 260 /** Set the structure to a number (num/den*10^exp10). Equivalent to set(Number(num, den, exp10), precerve_precision). 261 * 262 * @param num The numerator of the new numeric value. 263 * @param den The denominator of the new numeric value. 264 * @param exp10 The base 10 exponent of the new numeric value. 265 * @param preserve_precision Preserve the current precision (unless the new value has a lower precision). 266 */ 267 void set(int num, int den = 1, int exp10 = 0, bool preserve_precision = false); 268 void set(long int num, long int den, long int exp10 = 0L, bool preserve_precision = false); 269 /** Set the structure to a symbolic/text value. 270 * 271 * @param o The new symolic/text value. 272 * @param preserve_precision Preserve the current precision. 273 * @param force_symbol Do not check for undefined or date value. 274 */ 275 void set(std::string sym, bool preserve_precision = false, bool force_symbol = false); 276 /** Set the structure to a date and time value. 277 * 278 * @param o The new data and time value. 279 * @param preserve_precision Preserve the current precision. 280 */ 281 void set(const QalculateDateTime &o_dt, bool preserve_precision = false); 282 /** Set the structure to a number with a floating point value. Uses Number::setFloat(). 283 * 284 * @param o The new numeric value. 285 * @param preserve_precision Preserve the current precision (unless the new value has a lower precision). 286 */ 287 void set(double float_value, bool preserve_precision = false); 288 /** Set the structure to a vector. 289 * 290 * @param o The first element (copied) in the new vector. 291 * @param ... Elements (copied) in the new vector. End with NULL. 292 */ 293 void setVector(const MathStructure *o, ...); 294 /** Set the structure to a mathematical function. 295 * 296 * @param o The new function value. 297 * @param ... Arguments (copied) to the function. End with NULL. 298 */ 299 void set(MathFunction *o, ...); 300 /** Set the structure to a unit. 301 * 302 * @param u The new unit value. 303 * @param p Prefix of the unit. 304 * @param preserve_precision Preserve the current precision (unless the new value has a lower precision). 305 */ 306 void set(Unit *u, Prefix *p = NULL, bool preserve_precision = false); 307 /** Set the structure to a variable. 308 * 309 * @param o The new variable value. 310 * @param preserve_precision Preserve the current precision. 311 */ 312 void set(Variable *o, bool preserve_precision = false); 313 /** Set the structure to a number. 314 * 315 * @param o The new numeric value. 316 * @param preserve_precision Preserve the current precision (unless the new value has a lower precision). 317 */ 318 void set(const Number &o, bool preserve_precision = false); 319 /** Set the value of the structure to undefined. 320 * 321 * @param preserve_precision Preserve the current precision. 322 */ 323 void setUndefined(bool preserve_precision = false); 324 /** Mark that calculation was aborted. 325 * 326 * @param preserve_precision Preserve the current precision. 327 */ 328 void setAborted(bool preserve_precision = false); 329 /** Reset the value (to zero) and parameters of the structure. 330 * 331 * @param preserve_precision Preserve the current precision. 332 */ 333 void clear(bool preserve_precision = false); 334 /** Set the structure to an empty vector. 335 * 336 * @param preserve_precision Preserve the current precision. 337 */ 338 void clearVector(bool preserve_precision = false); 339 /** Set the structure to an empty matrix. 340 * 341 * @param preserve_precision Preserve the current precision. 342 */ 343 void clearMatrix(bool preserve_precision = false); 344 345 /** Explicitely sets the type of the structure. 346 * setType() is dangerous and might crash the program if used unwisely. 347 * 348 * @param mtype The new structure type 349 */ 350 void setType(StructureType mtype); 351 352 void operator = (const MathStructure &o); 353 void operator = (const Number &o); 354 void operator = (int i); 355 void operator = (Unit *u); 356 void operator = (Variable *v); 357 void operator = (std::string sym); 358 //@} 359 360 /** @name Functions to keep track of referrers */ 361 //@{ 362 void ref(); 363 void unref(); 364 size_t refcount() const; 365 //@} 366 367 /** @name Functions for numbers */ 368 //@{ 369 const Number &number() const; 370 Number &number(); 371 void numberUpdated(); 372 //@} 373 374 /** @name Functions for symbols */ 375 //@{ 376 const std::string &symbol() const; 377 //@} 378 379 /** @name Functions for date and time */ 380 //@{ 381 const QalculateDateTime *datetime() const; 382 QalculateDateTime *datetime(); 383 //@} 384 385 /** @name Functions for units */ 386 //@{ 387 Unit *unit() const; 388 Unit *unit_exp_unit() const; 389 Prefix *prefix() const; 390 Prefix *unit_exp_prefix() const; 391 void setPrefix(Prefix *p); 392 bool isPlural() const; 393 void setPlural(bool is_plural); 394 void setUnit(Unit *u); 395 //@} 396 397 /** @name Functions for mathematical functions */ 398 //@{ 399 void setFunction(MathFunction *f); 400 void setFunctionId(int id); 401 MathFunction *function() const; 402 const MathStructure *functionValue() const; 403 //@} 404 405 /** @name Functions for variables */ 406 //@{ 407 void setVariable(Variable *v); 408 Variable *variable() const; 409 //@} 410 411 /** @name Functions for nested structures (power, muliplication, addition, vector, etc) */ 412 //@{ 413 /** Call this function when you have updated a child. Updates the precision. 414 * 415 * @param index Index (starting at 1) of the updated child. 416 * @recursive If true, do the same for each child of the child. 417 */ 418 void childUpdated(size_t index, bool recursive = false); 419 /** Call this function when you have updated children. Updates the precision. 420 * 421 * @recursive If true, do the same for each child of the children. 422 */ 423 void childrenUpdated(bool recursive = false); 424 /** Returns a child. Does not check if a child exists at the index. 425 * 426 * @param index Index (starting at zero). 427 */ 428 MathStructure &operator [] (size_t index); 429 /** Returns a child. Does not check if a child exists at the index. 430 * 431 * @param index Index (starting at zero). 432 */ 433 const MathStructure &operator [] (size_t index) const; 434 MathStructure &last(); 435 const MathStructure last() const; 436 void setToChild(size_t index, bool merge_precision = false, MathStructure *mparent = NULL, size_t index_this = 1); 437 void swapChildren(size_t index1, size_t index2); 438 void childToFront(size_t index); 439 void addChild(const MathStructure &o); 440 void addChild_nocopy(MathStructure *o); 441 void delChild(size_t index, bool check_size = false); 442 void insertChild(const MathStructure &o, size_t index); 443 void insertChild_nocopy(MathStructure *o, size_t index); 444 void setChild(const MathStructure &o, size_t index = 1, bool merge_precision = false); 445 void setChild_nocopy(MathStructure *o, size_t index = 1, bool merge_precision = false); 446 const MathStructure *getChild(size_t index) const; 447 MathStructure *getChild(size_t index); 448 size_t countChildren() const; 449 size_t countTotalChildren(bool count_function_as_one = true) const; 450 size_t size() const; 451 //@} 452 453 /** @name Functions for power */ 454 //@{ 455 const MathStructure *base() const; 456 const MathStructure *exponent() const; 457 MathStructure *base(); 458 MathStructure *exponent(); 459 //@} 460 461 /** @name Functions for comparisons */ 462 //@{ 463 ComparisonType comparisonType() const; 464 void setComparisonType(ComparisonType comparison_type); 465 //@} 466 467 /** @name Functions checking type and value */ 468 //@{ 469 StructureType type() const; 470 471 bool isAddition() const; 472 bool isMultiplication() const; 473 bool isPower() const; 474 bool isSymbolic() const; 475 bool isDateTime() const; 476 bool isAborted() const; 477 bool isEmptySymbol() const; 478 bool isVector() const; 479 bool isMatrix() const; 480 bool isFunction() const; 481 bool isUnit() const; 482 bool isUnit_exp() const; 483 bool isUnknown() const; 484 bool isUnknown_exp() const; 485 bool isNumber_exp() const; 486 bool isVariable() const; 487 bool isComparison() const; 488 bool isBitwiseAnd() const; 489 bool isBitwiseOr() const; 490 bool isBitwiseXor() const; 491 bool isBitwiseNot() const; 492 bool isLogicalAnd() const; 493 bool isLogicalOr() const; 494 bool isLogicalXor() const; 495 bool isLogicalNot() const; 496 bool isInverse() const; 497 bool isDivision() const; 498 bool isNegate() const; 499 bool isInfinity() const; 500 bool isUndefined() const; 501 bool isInteger() const; 502 bool isInfinite(bool ignore_imag = true) const; 503 bool isNumber() const; 504 bool isZero() const; 505 bool isApproximatelyZero() const; 506 bool isOne() const; 507 bool isMinusOne() const; 508 509 bool hasNegativeSign() const; 510 511 bool representsBoolean() const; 512 bool representsPositive(bool allow_units = false) const; 513 bool representsNegative(bool allow_units = false) const; 514 bool representsNonNegative(bool allow_units = false) const; 515 bool representsNonPositive(bool allow_units = false) const; 516 bool representsInteger(bool allow_units = false) const; 517 bool representsNonInteger(bool allow_units = false) const; 518 bool representsNumber(bool allow_units = false) const; 519 bool representsRational(bool allow_units = false) const; 520 bool representsFraction(bool allow_units = false) const; 521 bool representsReal(bool allow_units = false) const; 522 bool representsNonComplex(bool allow_units = false) const; 523 bool representsComplex(bool allow_units = false) const; 524 bool representsNonZero(bool allow_units = false) const; 525 bool representsZero(bool allow_units = false) const; 526 bool representsApproximatelyZero(bool allow_units = false) const; 527 bool representsEven(bool allow_units = false) const; 528 bool representsOdd(bool allow_units = false) const; 529 bool representsUndefined(bool include_children = false, bool include_infinite = false, bool be_strict = false) const; 530 bool representsNonMatrix() const; 531 bool representsScalar() const; 532 //@} 533 534 /** @name Functions for precision */ 535 //@{ 536 void setApproximate(bool is_approx = true, bool recursive = false); 537 bool isApproximate() const; 538 void setPrecision(int prec, bool recursive = false); 539 int precision() const; 540 void mergePrecision(const MathStructure &o); 541 void mergePrecision(bool approx, int prec); 542 543 //@} 544 545 /** @name Operators for structural transformations and additions 546 * These operators transforms or adds to the structure without doing any calculations 547 */ 548 //@{ 549 550 MathStructure operator - () const; 551 MathStructure operator * (const MathStructure &o) const; 552 MathStructure operator / (const MathStructure &o) const; 553 MathStructure operator + (const MathStructure &o) const; 554 MathStructure operator - (const MathStructure &o) const; 555 MathStructure operator ^ (const MathStructure &o) const; 556 MathStructure operator && (const MathStructure &o) const; 557 MathStructure operator || (const MathStructure &o) const; 558 MathStructure operator ! () const; 559 560 void operator *= (const MathStructure &o); 561 void operator /= (const MathStructure &o); 562 void operator += (const MathStructure &o); 563 void operator -= (const MathStructure &o); 564 void operator ^= (const MathStructure &o); 565 566 void operator *= (const Number &o); 567 void operator /= (const Number &o); 568 void operator += (const Number &o); 569 void operator -= (const Number &o); 570 void operator ^= (const Number &o); 571 572 void operator *= (int i); 573 void operator /= (int i); 574 void operator += (int i); 575 void operator -= (int i); 576 void operator ^= (int i); 577 578 void operator *= (Unit *u); 579 void operator /= (Unit *u); 580 void operator += (Unit *u); 581 void operator -= (Unit *u); 582 void operator ^= (Unit *u); 583 584 void operator *= (Variable *v); 585 void operator /= (Variable *v); 586 void operator += (Variable *v); 587 void operator -= (Variable *v); 588 void operator ^= (Variable *v); 589 590 void operator *= (std::string sym); 591 void operator /= (std::string sym); 592 void operator += (std::string sym); 593 void operator -= (std::string sym); 594 void operator ^= (std::string sym); 595 596 //@} 597 598 /** @name Functions for structural transformations and additions 599 * These functions transforms or adds to the structure without doing any calculations 600 */ 601 //@{ 602 void add(const MathStructure &o, MathOperation op, bool append = false); 603 void add(const MathStructure &o, bool append = false); 604 void subtract(const MathStructure &o, bool append = false); 605 void multiply(const MathStructure &o, bool append = false); 606 void divide(const MathStructure &o, bool append = false); 607 void raise(const MathStructure &o); 608 void add(const Number &o, bool append = false); 609 void subtract(const Number &o, bool append = false); 610 void multiply(const Number &o, bool append = false); 611 void divide(const Number &o, bool append = false); 612 void raise(const Number &o); 613 void add(int i, bool append = false); 614 void subtract(int i, bool append = false); 615 void multiply(int i, bool append = false); 616 void divide(int i, bool append = false); 617 void raise(int i); 618 void add(Variable *v, bool append = false); 619 void subtract(Variable *v, bool append = false); 620 void multiply(Variable *v, bool append = false); 621 void divide(Variable *v, bool append = false); 622 void raise(Variable *v); 623 void add(Unit *u, bool append = false); 624 void subtract(Unit *u, bool append = false); 625 void multiply(Unit *u, bool append = false); 626 void divide(Unit *u, bool append = false); 627 void raise(Unit *u); 628 void add(std::string sym, bool append = false); 629 void subtract(std::string sym, bool append = false); 630 void multiply(std::string sym, bool append = false); 631 void divide(std::string sym, bool append = false); 632 void raise(std::string sym); 633 void add_nocopy(MathStructure *o, MathOperation op, bool append = false); 634 void add_nocopy(MathStructure *o, bool append = false); 635 void subtract_nocopy(MathStructure *o, bool append = false); 636 void multiply_nocopy(MathStructure *o, bool append = false); 637 void divide_nocopy(MathStructure *o, bool append = false); 638 void raise_nocopy(MathStructure *o); 639 void inverse(); 640 void negate(); 641 void setLogicalNot(); 642 void setBitwiseNot(); 643 644 void transform(StructureType mtype, const MathStructure &o); 645 void transform(StructureType mtype, const Number &o); 646 void transform(StructureType mtype, int i); 647 void transform(StructureType mtype, Unit *u); 648 void transform(StructureType mtype, Variable *v); 649 void transform(StructureType mtype, std::string sym); 650 void transform_nocopy(StructureType mtype, MathStructure *o); 651 void transform(StructureType mtype); 652 void transform(MathFunction *o); 653 void transformById(int id); 654 void transform(ComparisonType ctype, const MathStructure &o); 655 //@} 656 657 /** @name Functions/operators for comparisons */ 658 //@{ 659 660 bool equals(const MathStructure &o, bool allow_interval = false, bool allow_infinity = false) const; 661 bool equals(const Number &o, bool allow_interval = false, bool allow_infinity = false) const; 662 bool equals(int i) const; 663 bool equals(Unit *u) const; 664 bool equals(Variable *v) const; 665 bool equals(std::string sym) const; 666 667 ComparisonResult compare(const MathStructure &o) const; 668 ComparisonResult compareApproximately(const MathStructure &o, const EvaluationOptions &eo = default_evaluation_options) const; 669 670 bool mergeInterval(const MathStructure &o, bool set_to_overlap = false); 671 672 bool operator == (const MathStructure &o) const; 673 bool operator == (const Number &o) const; 674 bool operator == (int i) const; 675 bool operator == (Unit *u) const; 676 bool operator == (Variable *v) const; 677 bool operator == (std::string sym) const; 678 679 bool operator != (const MathStructure &o) const; 680 681 //@} 682 683 /** @name Functions for calculation/evaluation */ 684 //@{ 685 MathStructure &eval(const EvaluationOptions &eo = default_evaluation_options); 686 bool calculateMergeIndex(size_t index, const EvaluationOptions &eo, const EvaluationOptions &feo, MathStructure *mparent = NULL, size_t index_this = 1); 687 bool calculateLogicalOrLast(const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 688 bool calculateLogicalOrIndex(size_t index, const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 689 bool calculateLogicalOr(const MathStructure &mor, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 690 bool calculateLogicalXorLast(const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 691 bool calculateLogicalXor(const MathStructure &mxor, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 692 bool calculateLogicalAndLast(const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 693 bool calculateLogicalAndIndex(size_t index, const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 694 bool calculateLogicalAnd(const MathStructure &mand, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 695 bool calculateLogicalNot(const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 696 bool calculateBitwiseNot(const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 697 bool calculateInverse(const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 698 bool calculateNegate(const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 699 bool calculateRaiseExponent(const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 700 bool calculateRaise(const MathStructure &mexp, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 701 bool calculateBitwiseOrLast(const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 702 bool calculateBitwiseOrIndex(size_t index, const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 703 bool calculateBitwiseOr(const MathStructure &mor, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 704 bool calculateBitwiseXorLast(const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 705 bool calculateBitwiseXorIndex(size_t index, const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 706 bool calculateBitwiseXor(const MathStructure &mxor, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 707 bool calculateBitwiseAndLast(const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 708 bool calculateBitwiseAndIndex(size_t index, const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 709 bool calculateBitwiseAnd(const MathStructure &mand, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 710 bool calculateMultiplyLast(const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 711 bool calculateMultiplyIndex(size_t index, const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 712 bool calculateMultiply(const MathStructure &mmul, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 713 bool calculateDivide(const MathStructure &mdiv, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 714 bool calculateAddLast(const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 715 bool calculateAddIndex(size_t index, const EvaluationOptions &eo, bool check_size = true, MathStructure *mparent = NULL, size_t index_this = 1); 716 bool calculateAdd(const MathStructure &madd, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 717 bool calculateSubtract(const MathStructure &msub, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1); 718 bool calculateFunctions(const EvaluationOptions &eo, bool recursive = true, bool do_unformat = true); 719 int merge_addition(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false); 720 int merge_multiplication(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false, bool do_append = true); 721 int merge_power(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false); 722 int merge_logical_and(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false); 723 int merge_logical_or(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false); 724 int merge_logical_xor(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false); 725 int merge_bitwise_and(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false); 726 int merge_bitwise_or(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false); 727 int merge_bitwise_xor(MathStructure &mstruct, const EvaluationOptions &eo, MathStructure *mparent = NULL, size_t index_this = 1, size_t index_that = 2, bool reversed = false); 728 bool calculatesub(const EvaluationOptions &eo, const EvaluationOptions &feo, bool recursive = true, MathStructure *mparent = NULL, size_t index_this = 1); 729 void evalSort(bool recursive = false, bool absolute = false); 730 bool integerFactorize(); 731 //@} 732 733 /** @name Functions for protection from changes when evaluating */ 734 //@{ 735 void setProtected(bool do_protect = true); 736 bool isProtected() const; 737 //@} 738 739 /** @name Functions for format and display */ 740 //@{ 741 void sort(const PrintOptions &po = default_print_options, bool recursive = true); 742 bool improve_division_multipliers(const PrintOptions &po = default_print_options, MathStructure *parent = NULL); 743 void setPrefixes(const PrintOptions &po = default_print_options, MathStructure *parent = NULL, size_t pindex = 0); 744 void prefixCurrencies(const PrintOptions &po = default_print_options); 745 void format(const PrintOptions &po = default_print_options); 746 void formatsub(const PrintOptions &po = default_print_options, MathStructure *parent = NULL, size_t pindex = 0, bool recursive = true, MathStructure *top_parent = NULL); 747 void postFormatUnits(const PrintOptions &po = default_print_options, MathStructure *parent = NULL, size_t pindex = 0); 748 bool factorizeUnits(); 749 void unformat(const EvaluationOptions &eo = default_evaluation_options); 750 bool needsParenthesis(const PrintOptions &po, const InternalPrintStruct &ips, const MathStructure &parent, size_t index, bool flat_division = true, bool flat_power = true) const; 751 bool removeDefaultAngleUnit(const EvaluationOptions &eo = default_evaluation_options); 752 753 int neededMultiplicationSign(const PrintOptions &po, const InternalPrintStruct &ips, const MathStructure &parent, size_t index, bool par, bool par_prev, bool flat_division = true, bool flat_power = true) const; 754 755 std::string print(const PrintOptions &po = default_print_options, const InternalPrintStruct &ips = top_ips) const; 756 std::string print(const PrintOptions &po, bool format, int colorize = 0, int tagtype = TAG_TYPE_HTML, const InternalPrintStruct &ips = top_ips) const; 757 //@} 758 759 760 /** @name Functions for vectors */ 761 //@{ 762 763 MathStructure &flattenVector(MathStructure &mstruct) const; 764 765 bool rankVector(bool ascending = true); 766 bool sortVector(bool ascending = true); 767 768 MathStructure &getRange(int start, int end, MathStructure &mstruct) const; 769 770 void resizeVector(size_t i, const MathStructure &mfill); 771 772 //@} 773 774 775 /** @name Functions for matrices */ 776 //@{ 777 778 size_t rows() const; 779 size_t columns() const; 780 const MathStructure *getElement(size_t row, size_t column) const; 781 MathStructure *getElement(size_t row, size_t column); 782 MathStructure &getArea(size_t r1, size_t c1, size_t r2, size_t c2, MathStructure &mstruct) const; 783 MathStructure &rowToVector(size_t r, MathStructure &mstruct) const; 784 MathStructure &columnToVector(size_t c, MathStructure &mstruct) const; 785 MathStructure &matrixToVector(MathStructure &mstruct) const; 786 void setElement(const MathStructure &mstruct, size_t row, size_t column); 787 void addRows(size_t r, const MathStructure &mfill); 788 void addColumns(size_t c, const MathStructure &mfill); 789 void addRow(const MathStructure &mfill); 790 void addColumn(const MathStructure &mfill); 791 void resizeMatrix(size_t r, size_t c, const MathStructure &mfill); 792 bool matrixIsSquare() const; 793 bool isNumericMatrix() const; 794 int pivot(size_t ro, size_t co, bool symbolic = true); 795 int gaussianElimination(const EvaluationOptions &eo = default_evaluation_options, bool det = false); 796 MathStructure &determinant(MathStructure &mstruct, const EvaluationOptions &eo) const; 797 MathStructure &permanent(MathStructure &mstruct, const EvaluationOptions &eo) const; 798 void setToIdentityMatrix(size_t n); 799 MathStructure &getIdentityMatrix(MathStructure &mstruct) const; 800 bool invertMatrix(const EvaluationOptions &eo); 801 bool adjointMatrix(const EvaluationOptions &eo); 802 bool transposeMatrix(); 803 MathStructure &cofactor(size_t r, size_t c, MathStructure &mstruct, const EvaluationOptions &eo) const; 804 //@} 805 806 /** @name Functions for unit conversion */ 807 //@{ 808 int isUnitCompatible(const MathStructure &mstruct) const; 809 bool syncUnits(bool sync_nonlinear_relations = false, bool *found_nonlinear_relations = NULL, bool calculate_new_functions = false, const EvaluationOptions &feo = default_evaluation_options); 810 bool testDissolveCompositeUnit(Unit *u); 811 bool testCompositeUnit(Unit *u); 812 bool dissolveAllCompositeUnits(); 813 bool setPrefixForUnit(Unit *u, Prefix *new_prefix); 814 bool convertToBaseUnits(bool convert_nonlinear_relations = false, bool *found_nonlinear_relations = NULL, bool calculate_new_functions = false, const EvaluationOptions &feo = default_evaluation_options, bool avoid_approximate_variables = false); 815 bool convert(Unit *u, bool convert_nonlinear_relations = false, bool *found_nonlinear_relations = NULL, bool calculate_new_functions = false, const EvaluationOptions &feo = default_evaluation_options, Prefix *new_prefix = NULL); 816 bool convert(const MathStructure unit_mstruct, bool convert_nonlinear_relations = false, bool *found_nonlinear_relations = NULL, bool calculate_new_functions = false, const EvaluationOptions &feo = default_evaluation_options); 817 //@} 818 819 /** @name Functions for recursive search and replace */ 820 //@{ 821 int contains(const MathStructure &mstruct, bool structural_only = true, bool check_variables = false, bool check_functions = false, bool loose_equals = false) const; 822 size_t countOccurrences(const MathStructure &mstruct) const; 823 int containsRepresentativeOf(const MathStructure &mstruct, bool check_variables = false, bool check_functions = false) const; 824 int containsType(StructureType mtype, bool structural_only = true, bool check_variables = false, bool check_functions = false) const; 825 int containsRepresentativeOfType(StructureType mtype, bool check_variables = false, bool check_functions = false) const; 826 int containsFunction(MathFunction *f, bool structural_only = true, bool check_variables = false, bool check_functions = false) const; 827 int containsFunctionId(int id, bool structural_only = true, bool check_variables = false, bool check_functions = false) const; 828 int containsInterval(bool structural_only = true, bool check_variables = false, bool check_functions = false, int ignore_high_precision_interval = 0, bool include_interval_function = false) const; 829 int containsInfinity(bool structural_only = true, bool check_variables = false, bool check_functions = false) const; 830 bool containsOpaqueContents() const; 831 bool containsAdditionPower() const; 832 bool containsUnknowns() const; 833 bool containsDivision() const; 834 size_t countFunctions(bool count_subfunctions = true) const; 835 void findAllUnknowns(MathStructure &unknowns_vector); 836 bool replace(const MathStructure &mfrom, const MathStructure &mto, bool once_only = false, bool exclude_function_arguments = false); 837 bool replace(Variable *v, const MathStructure &mto); 838 bool calculateReplace(const MathStructure &mfrom, const MathStructure &mto, const EvaluationOptions &eo, bool exclude_function_arguments = false); 839 bool replace(const MathStructure &mfrom1, const MathStructure &mto1, const MathStructure &mfrom2, const MathStructure &mto2); 840 bool removeType(StructureType mtype); 841 //@} 842 843 /** @name Functions to generate vectors for plotting */ 844 //@{ 845 MathStructure generateVector(MathStructure x_mstruct, const MathStructure &min, const MathStructure &max, int steps, MathStructure *x_vector = NULL, const EvaluationOptions &eo = default_evaluation_options) const; 846 MathStructure generateVector(MathStructure x_mstruct, const MathStructure &min, const MathStructure &max, const MathStructure &step, MathStructure *x_vector = NULL, const EvaluationOptions &eo = default_evaluation_options) const; 847 MathStructure generateVector(MathStructure x_mstruct, const MathStructure &x_vector, const EvaluationOptions &eo = default_evaluation_options) const; 848 //@} 849 850 /** @name Differentiation and integration */ 851 //@{ 852 bool differentiate(const MathStructure &x_var, const EvaluationOptions &eo); 853 bool integrate(const MathStructure &lower_limit, const MathStructure &upper_limit, const MathStructure &x_var_pre, const EvaluationOptions &eo = default_evaluation_options, bool force_numerical = false, bool simplify_first = true); 854 int integrate(const MathStructure &x_var, const EvaluationOptions &eo, bool simplify_first = true, int use_abs = 1, bool definite_integral = false, bool try_abs = true, int max_part_depth = 5, std::vector<MathStructure*> *parent_parts = NULL); 855 //@} 856 857 /** @name Functions for polynomials */ 858 //@{ 859 bool expand(const EvaluationOptions &eo = default_evaluation_options, bool unfactorize = true); 860 bool simplify(const EvaluationOptions &eo = default_evaluation_options, bool unfactorize = true); 861 bool factorize(const EvaluationOptions &eo = default_evaluation_options, bool unfactorize = true, int term_combination_levels = 0, int max_msecs = 1000, bool only_integers = true, int recursive = 1, struct timeval *endtime_p = NULL, const MathStructure &force_factorization = m_undefined, bool complete_square = false, bool only_sqrfree = false, int max_degree_factor = -1); 862 bool expandPartialFractions(const EvaluationOptions &eo); 863 bool structure(StructuringMode structuring, const EvaluationOptions &eo, bool restore_first = true); 864 /** If the structure represents a rational polynomial. 865 * This is true for 866 * - rational numbers; 867 * - functions, units, variables and symbols that do not represent a matrix or undefined; 868 * - a power with a positive integer exponent and any of the previous as base; 869 * - a multiplication with the previous as factors; or 870 * - an addition with the previous as terms. 871 * 872 * @returns true if structure represents a rational polynomial. 873 */ 874 bool isRationalPolynomial(bool allow_non_rational_coefficient = false, bool allow_interval_coefficient = false) const; 875 const Number &overallCoefficient() const; 876 const Number °ree(const MathStructure &xvar) const; 877 const Number &ldegree(const MathStructure &xvar) const; 878 void lcoefficient(const MathStructure &xvar, MathStructure &mcoeff) const; 879 void tcoefficient(const MathStructure &xvar, MathStructure &mcoeff) const; 880 void coefficient(const MathStructure &xvar, const Number &pownr, MathStructure &mcoeff) const; 881 Number maxCoefficient(); 882 int polynomialUnit(const MathStructure &xvar) const; 883 void polynomialContent(const MathStructure &xvar, MathStructure &mcontent, const EvaluationOptions &eo) const; 884 void polynomialPrimpart(const MathStructure &xvar, MathStructure &mprim, const EvaluationOptions &eo) const; 885 void polynomialPrimpart(const MathStructure &xvar, const MathStructure &c, MathStructure &mprim, const EvaluationOptions &eo) const; 886 void polynomialUnitContentPrimpart(const MathStructure &xvar, int &munit, MathStructure &mcontent, MathStructure &mprim, const EvaluationOptions &eo) const; 887 //@} 888 889 /** @name Functions for conversion of complex numbers */ 890 //@{ 891 bool complexToExponentialForm(const EvaluationOptions &eo); 892 bool complexToPolarForm(const EvaluationOptions &eo); 893 bool complexToCisForm(const EvaluationOptions &eo); 894 //@} 895 896 bool calculateLimit(const MathStructure &x_var, const MathStructure &limit, const EvaluationOptions &eo_pre, int approach_direction = 0); 897 898 bool decomposeFractions(const MathStructure &x_var, const EvaluationOptions &eo); 899 900 static bool polynomialDivide(const MathStructure &mnum, const MathStructure &mden, MathStructure &mquotient, const EvaluationOptions &eo, bool check_args = true); 901 static bool polynomialQuotient(const MathStructure &mnum, const MathStructure &mden, const MathStructure &xvar, MathStructure &mquotient, const EvaluationOptions &eo, bool check_args = true); 902 static bool lcm(const MathStructure &m1, const MathStructure &m2, MathStructure &mlcm, const EvaluationOptions &eo, bool check_args = true); 903 static bool gcd(const MathStructure &m1, const MathStructure &m2, MathStructure &mresult, const EvaluationOptions &eo, MathStructure *ca = NULL, MathStructure *cb = NULL, bool check_args = true); 904 905 /** @name Functions for equations */ 906 //@{ 907 const MathStructure &find_x_var() const; 908 bool isolate_x(const EvaluationOptions &eo, const MathStructure &x_var = m_undefined, bool check_result = false); 909 bool isolate_x(const EvaluationOptions &eo, const EvaluationOptions &feo, const MathStructure &x_var = m_undefined, bool check_result = false); 910 //@} 911 912 bool inParentheses() const; 913 void setInParentheses(bool b = true); 914 915 }; 916 917 std::ostream& operator << (std::ostream &os, const MathStructure&); 918 919 #endif 920