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,300 @@
1
+ /*
2
+ Qalculate
3
+
4
+ Copyright (C) 2003-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 UNIT_H
13
+ #define UNIT_H
14
+
15
+ /** @file */
16
+
17
+ /// Type of unit
18
+ typedef enum {
19
+ /// class Unit
20
+ SUBTYPE_BASE_UNIT,
21
+ /// class AliasUnit
22
+ SUBTYPE_ALIAS_UNIT,
23
+ /// class CompositeUnit
24
+ SUBTYPE_COMPOSITE_UNIT
25
+ } UnitSubtype;
26
+
27
+ #include <libqalculate/ExpressionItem.h>
28
+ #include <libqalculate/includes.h>
29
+
30
+ /// A unit for measurement.
31
+ /**
32
+ * The Unit class both represents a base unit and is the base class for other unit types.
33
+ * Base units are units defined as basis for other units. Meters and seconds are typical base units.
34
+ *
35
+ * For base units, a name is all that is needed.
36
+ * Base units do however normally have three different names defined for use in expressions - abbreviation (ex. "m"), singular ("meter") and plural ("meters").
37
+ */
38
+ class Unit : public ExpressionItem {
39
+
40
+ protected:
41
+
42
+ string ssystem;
43
+ bool b_si;
44
+
45
+ public:
46
+
47
+ Unit(string cat_, string name_, string plural_ = "", string singular_ = "", string title_ = "", bool is_local = true, bool is_builtin = false, bool is_active = true);
48
+ Unit();
49
+ Unit(const Unit *unit);
50
+ virtual ~Unit();
51
+
52
+ virtual ExpressionItem *copy() const;
53
+ virtual void set(const ExpressionItem *item);
54
+
55
+ /** Returns if the unit is part of the SI standard.
56
+ *
57
+ * @returns true if the unit is part of the SI standard.
58
+ */
59
+ bool isSIUnit() const;
60
+ /** State that the unit is part of the SI standard.
61
+ * Sets system to "SI".
62
+ */
63
+ void setAsSIUnit();
64
+ /** Sets which system/standard ("SI", "SGC", etc.) the unit is part of.
65
+ * Setting system to "SI" (case-insensitive), is equivalent to setAsSIUnit().
66
+ */
67
+ void setSystem(string s_system);
68
+ /** Returns the system/standard that the unit is part of.
69
+ *
70
+ * @returns System string.
71
+ */
72
+ const string &system() const;
73
+ /** Returns if the unit is a currency (Euro is base unit).
74
+ *
75
+ * @returns true if the unit is a currency.
76
+ */
77
+ bool isCurrency() const;
78
+ /** Returns a display string representing the unit in an expression.
79
+ *
80
+ * Equivalent to preferredName() for Unit and AliasUnit, but closer to MathStructure::print() for CompositeUnit (prints out base expression).
81
+ */
82
+ virtual string print(bool plural_, bool short_, bool use_unicode = false, bool (*can_display_unicode_string_function) (const char*, void*) = NULL, void *can_display_unicode_string_arg = NULL) const;
83
+ virtual const string &plural(bool return_singular_if_no_plural = true, bool use_unicode = false, bool (*can_display_unicode_string_function) (const char*, void*) = NULL, void *can_display_unicode_string_arg = NULL) const;
84
+ virtual const string &singular(bool return_abbreviation_if_no_singular = true, bool use_unicode = false, bool (*can_display_unicode_string_function) (const char*, void*) = NULL, void *can_display_unicode_string_arg = NULL) const;
85
+ virtual const string &abbreviation(bool return_singular_if_no_abbreviation = true, bool use_unicode = false, bool (*can_display_unicode_string_function) (const char*, void*) = NULL, void *can_display_unicode_string_arg = NULL) const;
86
+ virtual bool isUsedByOtherUnits() const;
87
+ virtual Unit* baseUnit() const;
88
+ virtual MathStructure &convertToBaseUnit(MathStructure &mvalue, MathStructure &mexp) const;
89
+ virtual MathStructure &convertFromBaseUnit(MathStructure &mvalue, MathStructure &mexp) const;
90
+ virtual MathStructure &convertToBaseUnit(MathStructure &mvalue) const;
91
+ virtual MathStructure &convertFromBaseUnit(MathStructure &mvalue) const;
92
+ virtual MathStructure convertToBaseUnit() const;
93
+ virtual MathStructure convertFromBaseUnit() const;
94
+ virtual int baseExponent(int exp = 1) const;
95
+ virtual int type() const;
96
+ /** Returns the subtype of the unit, corresponding to which subsubclass the object belongs to.
97
+ *
98
+ * @returns ::UnitSubtype.
99
+ */
100
+ virtual int subtype() const;
101
+ /** If specified unit is a base unit for this unit, directly or with other units in between.
102
+ * Equivalent to u->isParentOf(this).
103
+ */
104
+ virtual bool isChildOf(Unit *u) const;
105
+ /** If this unit is a base unit for specified unit, directly or with other units in between.
106
+ * Equivalent to u->isChildOf(this).
107
+ */
108
+ virtual bool isParentOf(Unit *u) const;
109
+ virtual bool hasComplexRelationTo(Unit *u) const;
110
+ /** Converts a value from specified unit and exponent to this unit.
111
+ * value * (unit^exponent) = new value * (this^new exponent)
112
+ * This function cannot convert to or from CompositeUnit.
113
+ *
114
+ * @param u Unit to convert from.
115
+ * @param[in,out] mvalue Quantity value.
116
+ * @param[in,out] exp Exponent.
117
+ * @returns true if the value was successfully converted.
118
+ */
119
+ bool convert(Unit *u, MathStructure &mvalue, MathStructure &exp) const;
120
+ /** Converts a value from specified unit and exponent to this unit.
121
+ * value * unit = new value * this
122
+ * This function cannot convert to or from CompositeUnit.
123
+ *
124
+ * @param u Unit to convert from.
125
+ * @param[in,out] mvalue Quantity value.
126
+ * @returns true if the value was successfully converted.
127
+ */
128
+ bool convert(Unit *u, MathStructure &mvalue) const;
129
+ MathStructure convert(Unit *u, bool *converted = NULL) const;
130
+
131
+ };
132
+
133
+
134
+ /// An unit with relation to another unit
135
+ /**
136
+ * Alias units is defined in relation to another unit.
137
+ * For example, hour are defined as an alias unit that equals 60 minutes which in turn is defined in relation to seconds.
138
+ *
139
+ * Alias units have an associated base unit, exponent and relation expression.
140
+ * For more complex relations an inverse relation can also be specified for conversion back from the base unit.
141
+ * The base unit must not necessarily be of the base unit class and it is recommended that an alias unit is defined in relation to the closest unit
142
+ * (ex. 1ft = 3 hands, 1 hand = 4 in, and 1 in = 0.0254 m).
143
+ *
144
+ * The relation is usually just a number that tells how large quantity of the base unit is needed to get the alias unit (alias unit = base unit * relation).
145
+ * More complex units can specify the relation as a full-blown expression where '\x' is replaced by the quantity of the base unit and
146
+ * '\y' is the exponent.
147
+ * For example, Degrees Celsius has the relation "\x + 273.15" and the inverse relation "\x - 273.15" to the base unit Kelvin.
148
+ * For simple relations, the reversion is automatic and ought not be defined separately.
149
+ *
150
+ * The precision property inherited from ExpressionItem defines the precision of the relation.
151
+ *
152
+ * The exponent defines the exponential relation to the base unit, so that the alias unit equals the base unit raised to the exponent.
153
+ * For simple unit relations this gives: alias unit = relation * base unit^exponent.
154
+ *
155
+ * Alias units normally have three different names defined for use in expressions - abbreviation (ex. "m"), singular ("meter") and plural ("meters").
156
+ */
157
+ class AliasUnit : public Unit {
158
+
159
+ protected:
160
+
161
+ string svalue, sinverse;
162
+ int i_exp;
163
+ Unit *o_unit;
164
+
165
+ public:
166
+
167
+ AliasUnit(string cat_, string name_, string plural_, string singular_, string title_, Unit *alias, string relation = "1", int exp = 1, string inverse = "", bool is_local = true, bool is_builtin = false, bool is_active = true);
168
+ AliasUnit(const AliasUnit *unit);
169
+ AliasUnit();
170
+ virtual ~AliasUnit();
171
+
172
+ virtual ExpressionItem *copy() const;
173
+ virtual void set(const ExpressionItem *item);
174
+
175
+ virtual Unit* baseUnit() const;
176
+ virtual Unit* firstBaseUnit() const;
177
+ virtual void setBaseUnit(Unit *alias);
178
+ virtual string expression() const;
179
+ virtual string inverseExpression() const;
180
+ /**
181
+ * Sets the relation expression.
182
+ */
183
+ virtual void setExpression(string relation);
184
+ /**
185
+ * Sets the inverse relation expression.
186
+ */
187
+ virtual void setInverseExpression(string inverse);
188
+ virtual MathStructure &convertToFirstBaseUnit(MathStructure &mvalue, MathStructure &mexp) const;
189
+ virtual MathStructure &convertFromFirstBaseUnit(MathStructure &mvalue, MathStructure &mexp) const;
190
+ virtual MathStructure &convertToBaseUnit(MathStructure &mvalue, MathStructure &mexp) const;
191
+ virtual MathStructure &convertFromBaseUnit(MathStructure &mvalue, MathStructure &mexp) const;
192
+ virtual MathStructure &convertToBaseUnit(MathStructure &mvalue) const;
193
+ virtual MathStructure &convertFromBaseUnit(MathStructure &mvalue) const;
194
+ virtual MathStructure convertToBaseUnit() const;
195
+ virtual MathStructure convertFromBaseUnit() const;
196
+ virtual int baseExponent(int exp = 1) const;
197
+ virtual void setExponent(int exp);
198
+ virtual int firstBaseExponent() const;
199
+ virtual int subtype() const;
200
+ virtual bool isChildOf(Unit *u) const;
201
+ virtual bool isParentOf(Unit *u) const;
202
+ virtual bool hasComplexExpression() const;
203
+ virtual bool hasComplexRelationTo(Unit *u) const;
204
+
205
+ };
206
+
207
+ /// A subunit in a CompositeUnit
208
+ /**
209
+ * Should normally not be used directly.
210
+ */
211
+ class AliasUnit_Composite : public AliasUnit {
212
+
213
+ protected:
214
+
215
+ Prefix *prefixv;
216
+
217
+ public:
218
+
219
+ AliasUnit_Composite(Unit *alias, int exp = 1, Prefix *prefix_ = NULL);
220
+ AliasUnit_Composite(const AliasUnit_Composite *unit);
221
+ virtual ~AliasUnit_Composite();
222
+
223
+ virtual ExpressionItem *copy() const;
224
+ virtual void set(const ExpressionItem *item);
225
+
226
+ virtual string print(bool plural_, bool short_, bool use_unicode = false, bool (*can_display_unicode_string_function) (const char*, void*) = NULL, void *can_display_unicode_string_arg = NULL) const;
227
+ virtual Prefix *prefix() const;
228
+ virtual int prefixExponent() const;
229
+ virtual void set(Unit *u, int exp = 1, Prefix *prefix_ = NULL);
230
+ virtual MathStructure &convertToFirstBaseUnit(MathStructure &mvalue, MathStructure &mexp) const;
231
+ virtual MathStructure &convertFromFirstBaseUnit(MathStructure &mvalue, MathStructure &mexp) const;
232
+
233
+ };
234
+
235
+ /// A unit consisting of a number of other units
236
+ /**
237
+ * Composite units are defined by a unit expression with multiple units.
238
+ * Composite units often have an alias unit associated with them, as they do not have a reference name on their own.
239
+ * For example, a joule is defined as an alias defined in relation to a composite unit defined as "Newton * meter".
240
+ *
241
+ * The names of composite units is only used to reference the unit in definitions of other units.
242
+ * They can not be used in expressions.
243
+ *
244
+ * Composite units is definited as a composition of units.
245
+ * The units, with prefixes and exponents, can either be added one by one with add() or
246
+ * parsed from an expression (ex. "cm^3/g) with setBaseExpression().
247
+ */
248
+ class CompositeUnit : public Unit {
249
+
250
+ protected:
251
+
252
+ string sshort;
253
+ vector<AliasUnit_Composite*> units;
254
+
255
+ public:
256
+
257
+ CompositeUnit(string cat_, string name_, string title_ = "", string base_expression_ = "", bool is_local = true, bool is_builtin = false, bool is_active = true);
258
+ CompositeUnit(const CompositeUnit *unit);
259
+ virtual ~CompositeUnit();
260
+ virtual ExpressionItem *copy() const;
261
+ virtual void set(const ExpressionItem *item);
262
+ /** Adds a sub/base unit with specified exponent and an optional prefix.
263
+ *
264
+ * @param u Unit.
265
+ * @param exp Exponent.
266
+ * @param prefix Prefix.
267
+ */
268
+ virtual void add(Unit *u, int exp = 1, Prefix *prefix = NULL);
269
+ /** Retrieves information about a sub/base unit
270
+ *
271
+ * @param index Index starting at 1.
272
+ * @param[out] exp Exponent.
273
+ * @param[out] prefix Prefix.
274
+ * @returns Sub/base unit (AliasUnit_Composite::firstBaseUnit()).
275
+ */
276
+ virtual Unit *get(size_t index, int *exp = NULL, Prefix **prefix = NULL) const;
277
+ virtual void setExponent(size_t index, int exp);
278
+ virtual void setPrefix(size_t index, Prefix *prefix);
279
+ /** Returns the number of sub/base units */
280
+ virtual size_t countUnits() const;
281
+ virtual size_t find(Unit *u) const;
282
+ virtual void del(size_t index);
283
+ /** Prints out the sub/base units with prefixes and exponents.
284
+ * This is the representation of the unit in expressions.
285
+ */
286
+ virtual string print(bool plural_, bool short_, bool use_unicode = false, bool (*can_display_unicode_string_function) (const char*, void*) = NULL, void *can_display_unicode_string_arg = NULL) const;
287
+ virtual int subtype() const;
288
+ /** If this unit contains a sub/base unit with a relation to the specified unit.
289
+ */
290
+ virtual bool containsRelativeTo(Unit *u) const;
291
+ /** Creates a MathStructure with the sub/base units of the unit.
292
+ */
293
+ virtual MathStructure generateMathStructure(bool make_division = false) const;
294
+ virtual void setBaseExpression(string base_expression_);
295
+ /** Removes all sub/base units. */
296
+ virtual void clear();
297
+ };
298
+
299
+
300
+ #endif
@@ -0,0 +1,380 @@
1
+ /*
2
+ Qalculate
3
+
4
+ Copyright (C) 2003-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 VARIABLE_H
13
+ #define VARIABLE_H
14
+
15
+ #include <libqalculate/ExpressionItem.h>
16
+ #include <libqalculate/includes.h>
17
+
18
+ /** @file */
19
+
20
+ #define DECLARE_BUILTIN_VARIABLE(x) class x : public DynamicVariable { \
21
+ private: \
22
+ void calculate() const; \
23
+ public: \
24
+ x(); \
25
+ x(const x *variable) {set(variable);} \
26
+ ExpressionItem *copy() const {return new x(this);} \
27
+ };
28
+
29
+ /// Type assumption.
30
+ /**
31
+ * Each type is a subset of the type above.
32
+ */
33
+ typedef enum {
34
+ ASSUMPTION_TYPE_NONE = 0,
35
+ /// Multiplication is NOT commutative
36
+ ASSUMPTION_TYPE_NONMATRIX = 1,
37
+ ASSUMPTION_TYPE_NUMBER = 2,
38
+ /// im(x) != 0
39
+ ASSUMPTION_TYPE_COMPLEX = 3,
40
+ ASSUMPTION_TYPE_REAL = 4,
41
+ ASSUMPTION_TYPE_RATIONAL = 5,
42
+ ASSUMPTION_TYPE_INTEGER = 6
43
+ } AssumptionType;
44
+
45
+ /// Signedness assumption.
46
+ typedef enum {
47
+ /// x = ?
48
+ ASSUMPTION_SIGN_UNKNOWN,
49
+ /// x > 0
50
+ ASSUMPTION_SIGN_POSITIVE,
51
+ /// x >= 0
52
+ ASSUMPTION_SIGN_NONNEGATIVE,
53
+ /// x < 0
54
+ ASSUMPTION_SIGN_NEGATIVE,
55
+ /// x <= 0
56
+ ASSUMPTION_SIGN_NONPOSITIVE,
57
+ /// x != 0
58
+ ASSUMPTION_SIGN_NONZERO
59
+ } AssumptionSign;
60
+
61
+ /// Type of variable
62
+ typedef enum {
63
+ /// class Variable
64
+ SUBTYPE_VARIABLE,
65
+ /// class UnknownVariable
66
+ SUBTYPE_UNKNOWN_VARIABLE,
67
+ /// class KnownVariable
68
+ SUBTYPE_KNOWN_VARIABLE
69
+ } VariableSubtype;
70
+
71
+ /// An assumption about an unknown mathematical value.
72
+ /** Assumptions have a type and a sign. The type describes the type of the value -- if it represents a number or something else, and what type of number is represented.
73
+ * The sign restricts the signedness of a number. The sign generally only applies the assumptions representing a number.
74
+ * The assumption class also includes max and min values, which however are not used anywhere yet.
75
+ */
76
+ class Assumptions {
77
+
78
+ protected:
79
+
80
+ AssumptionType i_type;
81
+ AssumptionSign i_sign;
82
+ Number *fmin, *fmax;
83
+ bool b_incl_min, b_incl_max;
84
+
85
+ public:
86
+
87
+ Assumptions();
88
+ ~Assumptions();
89
+
90
+ bool isPositive();
91
+ bool isNegative();
92
+ bool isNonNegative();
93
+ bool isNonPositive();
94
+ bool isInteger();
95
+ bool isNumber();
96
+ bool isRational();
97
+ bool isReal();
98
+ bool isComplex();
99
+ bool isNonZero();
100
+ bool isNonMatrix();
101
+
102
+ AssumptionType type();
103
+ AssumptionSign sign();
104
+ void setType(AssumptionType ant);
105
+ void setSign(AssumptionSign as);
106
+
107
+ void setMin(const Number *nmin);
108
+ void setIncludeEqualsMin(bool include_equals);
109
+ bool includeEqualsMin() const;
110
+ const Number *min() const;
111
+ void setMax(const Number *nmax);
112
+ void setIncludeEqualsMax(bool include_equals);
113
+ bool includeEqualsMax() const;
114
+ const Number *max() const;
115
+
116
+ };
117
+
118
+ /// Abstract base class for variables.
119
+ /** A variable is an alpha-numerical representation of a known or unknown value.
120
+ */
121
+ class Variable : public ExpressionItem {
122
+
123
+ public:
124
+
125
+ Variable(string cat_, string name_, string title_ = "", bool is_local = true, bool is_builtin = false, bool is_active = true);
126
+ Variable();
127
+ Variable(const Variable *variable);
128
+ virtual ~Variable();
129
+ virtual ExpressionItem *copy() const = 0;
130
+ virtual void set(const ExpressionItem *item);
131
+ virtual int type() const {return TYPE_VARIABLE;}
132
+ /** Returns the subtype of the variable, corresponding to which subsubclass the object belongs to.
133
+ *
134
+ * @returns ::VariableSubtype.
135
+ */
136
+ virtual int subtype() const {return SUBTYPE_VARIABLE;}
137
+ /** Returns if the variable has a known value (as oppossed to assumptions).
138
+ *
139
+ * @returns true if the variable is of class KnownVariable, false if UnknownVariable.
140
+ */
141
+ virtual bool isKnown() const = 0;
142
+
143
+ /** Returns if the variable represents a positive value.
144
+ */
145
+ virtual bool representsPositive(bool = false) {return false;}
146
+ virtual bool representsNegative(bool = false) {return false;}
147
+ virtual bool representsNonNegative(bool = false) {return false;}
148
+ virtual bool representsNonPositive(bool = false) {return false;}
149
+ virtual bool representsInteger(bool = false) {return false;}
150
+ virtual bool representsNumber(bool = false) {return false;}
151
+ virtual bool representsRational(bool = false) {return false;}
152
+ virtual bool representsReal(bool = false) {return false;}
153
+ virtual bool representsComplex(bool = false) {return false;}
154
+ virtual bool representsNonZero(bool = false) {return false;}
155
+ virtual bool representsEven(bool = false) {return false;}
156
+ virtual bool representsOdd(bool = false) {return false;}
157
+ virtual bool representsUndefined(bool = false, bool = false, bool = false) {return false;}
158
+ virtual bool representsBoolean() {return false;}
159
+ virtual bool representsNonMatrix() {return false;}
160
+
161
+ };
162
+
163
+ /// A variable with unknown value.
164
+ /** Unknown variables have an associated assumption object.
165
+ */
166
+ class UnknownVariable : public Variable {
167
+
168
+ protected:
169
+
170
+ Assumptions *o_assumption;
171
+
172
+ public:
173
+
174
+ /** Create an unknown.
175
+ *
176
+ * @param cat_ Category that the variable belongs to.
177
+ * @param name_ Initial name of the variable.
178
+ * @param title_ Descriptive name.
179
+ * @param is_local If the variable is local/user-defined or global.
180
+ * @param is_builtin If the variable is builtin and not modifiable.
181
+ * @param is_active If the variable is active and can be used in expressions.
182
+ */
183
+ UnknownVariable(string cat_, string name_, string title_ = "", bool is_local = true, bool is_builtin = false, bool is_active = true);
184
+ /** Create an empty unknown variable.
185
+ */
186
+ UnknownVariable();
187
+ /** Create a copy of an unknown variable.
188
+ *
189
+ * @param variable Unknown variable to copy.
190
+ */
191
+ UnknownVariable(const UnknownVariable *variable);
192
+ virtual ~UnknownVariable();
193
+ virtual ExpressionItem *copy() const;
194
+ virtual void set(const ExpressionItem *item);
195
+ bool isKnown() const {return false;}
196
+
197
+ /** Sets the assumptions of the unknown variable.
198
+ *
199
+ * @param ass Assumptions.
200
+ */
201
+ void setAssumptions(Assumptions *ass);
202
+ /** Returns the assumptions of the unknown variable.
203
+ *
204
+ * @returns Assumptions of the unknown variable.
205
+ */
206
+ Assumptions *assumptions();
207
+ int subtype() const {return SUBTYPE_UNKNOWN_VARIABLE;}
208
+
209
+ virtual bool representsPositive(bool = false);
210
+ virtual bool representsNegative(bool = false);
211
+ virtual bool representsNonNegative(bool = false);
212
+ virtual bool representsNonPositive(bool = false);
213
+ virtual bool representsInteger(bool = false);
214
+ virtual bool representsNumber(bool = false);
215
+ virtual bool representsRational(bool = false);
216
+ virtual bool representsReal(bool = false);
217
+ virtual bool representsComplex(bool = false);
218
+ virtual bool representsNonZero(bool = false);
219
+ virtual bool representsNonMatrix();
220
+
221
+ };
222
+
223
+ /// A variable with a known value.
224
+ /** Known variables have an associated value. The value can be a simple number or a full mathematical expression. The known variable class is used both for variable values and constants.
225
+ *
226
+ * The value can be provided as an expression in the form of a text string or as a mathematical value in the form of an object of the MathStructure class.
227
+ * The text string is parsed when needed, which saves time when loading many variable definitions which might not be used, at least not immediately.
228
+ */
229
+ class KnownVariable : public Variable {
230
+
231
+ protected:
232
+
233
+ MathStructure *mstruct;
234
+ bool b_expression;
235
+ int calculated_precision;
236
+ string sexpression;
237
+
238
+ public:
239
+
240
+ /** Create a known variable with a value.
241
+ *
242
+ * @param cat_ Category that the variable belongs to.
243
+ * @param name_ Initial name of the variable.
244
+ * @param o Value.
245
+ * @param title_ Descriptive name.
246
+ * @param is_local If the variable is local/user-defined or global.
247
+ * @param is_builtin If the variable is builtin and not modifiable.
248
+ * @param is_active If the variable is active and can be used in expressions.
249
+ */
250
+ KnownVariable(string cat_, string name_, const MathStructure &o, string title_ = "", bool is_local = true, bool is_builtin = false, bool is_active = true);
251
+ /** Create a known variable with an text string expression.
252
+ *
253
+ * @param cat_ Category that the variable belongs to.
254
+ * @param name_ Initial name of the variable.
255
+ * @param expression_ Expression.
256
+ * @param title_ Descriptive name.
257
+ * @param is_local If the variable is local/user-defined or global.
258
+ * @param is_builtin If the variable is builtin and not modifiable.
259
+ * @param is_active If the variable is active and can be used in expressions.
260
+ */
261
+ KnownVariable(string cat_, string name_, string expression_, string title_ = "", bool is_local = true, bool is_builtin = false, bool is_active = true);
262
+ /** Create an empty known variable. Primarily for internal use.
263
+ */
264
+ KnownVariable();
265
+ /** Create a copy of a known variable.
266
+ *
267
+ * @param variable Known variable to copy.
268
+ */
269
+ KnownVariable(const KnownVariable *variable);
270
+ virtual ~KnownVariable();
271
+
272
+ virtual ExpressionItem *copy() const;
273
+ virtual void set(const ExpressionItem *item);
274
+ bool isKnown() const {return true;}
275
+ /** Returns if the variable has an text string expression instead of a value.
276
+ *
277
+ * @returns True if the variable has an expression instead of a value.
278
+ */
279
+ virtual bool isExpression() const;
280
+ /** Returns the variable's string expression or an empty string if it has not got an expression.
281
+ *
282
+ * @returns The variable's expression.
283
+ */
284
+ virtual string expression() const;
285
+ int subtype() const {return SUBTYPE_KNOWN_VARIABLE;}
286
+
287
+ /** Sets the value of the variable. If expression is set, it is cleared.
288
+ *
289
+ * @param o Value.
290
+ */
291
+ virtual void set(const MathStructure &o);
292
+ /** Sets the text string expression of the variable. The value is cleared.
293
+ *
294
+ * @param expression_ Expression.
295
+ */
296
+ virtual void set(string expression_);
297
+
298
+ /** Returns the value of the variable. If no value is set or parsed and an expression is set, the expression is parsed and resulting value returned.
299
+ *
300
+ * @returns The value of the variable..
301
+ */
302
+ virtual const MathStructure &get();
303
+
304
+ virtual bool representsPositive(bool = false);
305
+ virtual bool representsNegative(bool = false);
306
+ virtual bool representsNonNegative(bool = false);
307
+ virtual bool representsNonPositive(bool = false);
308
+ virtual bool representsInteger(bool = false);
309
+ virtual bool representsNumber(bool = false);
310
+ virtual bool representsRational(bool = false);
311
+ virtual bool representsReal(bool = false);
312
+ virtual bool representsComplex(bool = false);
313
+ virtual bool representsNonZero(bool = false);
314
+ virtual bool representsEven(bool = false);
315
+ virtual bool representsOdd(bool = false);
316
+ virtual bool representsUndefined(bool = false, bool = false, bool = false);
317
+ virtual bool representsBoolean();
318
+ virtual bool representsNonMatrix();
319
+
320
+ };
321
+
322
+ /// Abstract base class for variables with a value which is recalculated when the precision has changed.
323
+ /**
324
+ */
325
+ class DynamicVariable : public KnownVariable {
326
+
327
+ protected:
328
+
329
+ virtual void calculate() const = 0;
330
+
331
+ public:
332
+
333
+ DynamicVariable(string cat_, string name_, string title_ = "", bool is_local = false, bool is_builtin = true, bool is_active = true);
334
+ DynamicVariable(const DynamicVariable *variable);
335
+ DynamicVariable();
336
+ virtual ~DynamicVariable();
337
+
338
+ ExpressionItem *copy() const = 0;
339
+ void set(const ExpressionItem *item);
340
+
341
+ const MathStructure &get();
342
+
343
+ void set(const MathStructure &o);
344
+ void set(string expression_);
345
+
346
+ /** Returns the precision of the calculated value.
347
+ *
348
+ * @returns Precision of the calculated value or zero if the value has not yet been calculated.
349
+ */
350
+ int calculatedPrecision() const;
351
+
352
+ virtual bool representsPositive(bool = false) {return true;}
353
+ virtual bool representsNegative(bool = false) {return false;}
354
+ virtual bool representsNonNegative(bool = false) {return true;}
355
+ virtual bool representsNonPositive(bool = false) {return false;}
356
+ virtual bool representsInteger(bool = false) {return false;}
357
+ virtual bool representsNumber(bool = false) {return true;}
358
+ virtual bool representsRational(bool = false) {return false;}
359
+ virtual bool representsReal(bool = false) {return true;}
360
+ virtual bool representsComplex(bool = false) {return false;}
361
+ virtual bool representsNonZero(bool = false) {return true;}
362
+ virtual bool representsEven(bool = false) {return false;}
363
+ virtual bool representsOdd(bool = false) {return false;}
364
+ virtual bool representsUndefined(bool = false, bool = false, bool = false) {return false;}
365
+ virtual bool representsBoolean() {return false;}
366
+ virtual bool representsNonMatrix() {return true;}
367
+
368
+ };
369
+
370
+ /// Dynamic variable for Pi
371
+ DECLARE_BUILTIN_VARIABLE(PiVariable)
372
+ /// Dynamic variable for e, the base of natural logarithms
373
+ DECLARE_BUILTIN_VARIABLE(EVariable)
374
+ /// Dynamic variable for Euler's constant
375
+ DECLARE_BUILTIN_VARIABLE(EulerVariable)
376
+ /// Dynamic variable for Catalan's constant
377
+ DECLARE_BUILTIN_VARIABLE(CatalanVariable)
378
+
379
+
380
+ #endif