libqalculate-ruby 0.0.1

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