"Fossies" - the Fresh Open Source Software Archive

Member "libqalculate-3.6.0/libqalculate/MathStructure.h" (16 Nov 2019, 42555 Bytes) of package /linux/privat/libqalculate-3.6.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MathStructure.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.5.0a_vs_3.6.0.

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