|
| 1 | +#ifndef POINCARE_EXPRESSION_API_H |
| 2 | +#define POINCARE_EXPRESSION_API_H |
| 3 | + |
| 4 | +namespace Poincare { |
| 5 | + |
| 6 | +class UserExpression; |
| 7 | +class SystemExpression; |
| 8 | +class SystemFunction; |
| 9 | + |
| 10 | +class Dimension; |
| 11 | +class Symbol; |
| 12 | + |
| 13 | +/* TODO */ |
| 14 | +class Layout { |
| 15 | + /* TODO */ |
| 16 | + void render(); |
| 17 | + /* TODO */ |
| 18 | + UserExpression parse(); |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Used to: |
| 23 | + * - display expressions via layouts |
| 24 | + * - save user input as it have been parsed |
| 25 | + * - make some simple enhancements |
| 26 | + * - guess the context |
| 27 | + * |
| 28 | + * Equivalent to old ReductionTarget::User + Beautification |
| 29 | + * Dimension may be invalid. |
| 30 | + */ |
| 31 | +class UserExpression { |
| 32 | + /** |
| 33 | + * Create a layout to represent the expression |
| 34 | + * |
| 35 | + * Includes: |
| 36 | + * - Adding parentheses if needed Mult(Add(…,…),…) -> (…+…)*… |
| 37 | + * - Chose the suitable multiplication symbol and apply it everywhere |
| 38 | + * - Insert separators everywhere if they are needed somewhere |
| 39 | + * For instance thousand separators if a integer has >= 5 digits |
| 40 | + * |
| 41 | + * @param linearMode Produce a rack layout containing only codepoints, that |
| 42 | + * may be turned into a char * easily. |
| 43 | + * Div(a,b) will be rendered as a/b instead of Fraction(a,b) for instance. |
| 44 | + */ |
| 45 | + Layout createLayout(bool linearMode) const; |
| 46 | + |
| 47 | + /** |
| 48 | + * Turn the UserExpression into a SystemExpression |
| 49 | + * |
| 50 | + * This includes : |
| 51 | + * - Check that the dimension is correct |
| 52 | + * - Normalize some types to simpler equivalents |
| 53 | + * Sub(a,b) -> Add(a, Mult(-1, b)) |
| 54 | + * - Index local variables with an id instead of a name |
| 55 | + * - Replace unit by their ratio in SI e.g. cm -> 1/100 |
| 56 | + * |
| 57 | + * TODO: what if the dimension is invalid ? |
| 58 | + */ |
| 59 | + SystemExpression projected() const; |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Used to: |
| 64 | + * - manipulate the expression |
| 65 | + * - ask for advanced simplifications |
| 66 | + * - save expression in an already processed and efficient form |
| 67 | + * |
| 68 | + * Equivalent to old ReductionTarget::SystemForAnalysis once reduced. |
| 69 | + * Dimension is consistent and may be asked at any time. |
| 70 | + */ |
| 71 | +class SystemExpression { |
| 72 | + /** Turn the expression back into a more user-friendly form. |
| 73 | + * |
| 74 | + * This includes: |
| 75 | + * - Turning a*b^-1*c^-2 into a/(b*c^2) |
| 76 | + * - Inserting the most appropriate unit |
| 77 | + */ |
| 78 | + UserExpression beautified() const; // + old target::User behavior |
| 79 | + |
| 80 | + /** Return an expression where every known value has been replaced by a |
| 81 | + * floating point approximation. Mult(π,x) -> Mult(3.14159,x) */ |
| 82 | + SystemExpression approximated(); |
| 83 | + |
| 84 | + SystemExpression clone() const; |
| 85 | + |
| 86 | + bool advancedSimplify(); // -> move to a "factory" on SE -> SE ? |
| 87 | + bool expand(); |
| 88 | + |
| 89 | + bool systematicReduce(); // automatic ? or private ? |
| 90 | + |
| 91 | + SystemExpression simplified() const { // take a &&this to mark moved to ? |
| 92 | + SystemExpression e = clone(); |
| 93 | + e.systematicReduce(); |
| 94 | + return e; |
| 95 | + } |
| 96 | + |
| 97 | + SystemFunction preparedForApproximation(Symbol symbol); |
| 98 | + |
| 99 | + private: |
| 100 | + Dimension m_dimension; |
| 101 | + // Variables m_variables ? -> probably simpler to hold them inside the tree |
| 102 | + // and make tree point to the tree inside the contexts |
| 103 | +}; |
| 104 | + |
| 105 | +/** |
| 106 | + * Compute as much stuff as possible before grapher or solver. |
| 107 | + * |
| 108 | + * Dimension is correct, only a scalars and points are allowed. |
| 109 | + */ |
| 110 | +class SystemFunction { |
| 111 | + /* A projected tree with a global VarX and dim = scalar */ |
| 112 | + double evaluate(double x); |
| 113 | + |
| 114 | + private: |
| 115 | + // name of the unique variable ? |
| 116 | +}; |
| 117 | + |
| 118 | +/* OR specialize function with its return type : */ |
| 119 | + |
| 120 | +/* should be approximable with a const Tree * */ |
| 121 | +/* ex reduction target forApproximation */ |
| 122 | +/* correct dimension, only a subset are allowed */ |
| 123 | +class SystemCartesianFunction { |
| 124 | + /* A projected tree with a global VarX and dim = scalar */ |
| 125 | + double evaluate(double x); |
| 126 | + |
| 127 | + private: |
| 128 | + // name of the unique variable ? |
| 129 | +}; |
| 130 | + |
| 131 | +class SystemParametricFunction { |
| 132 | + /* A projected tree with a global VarX and dim = point */ |
| 133 | + std::pair<double, double> evaluate(double t); |
| 134 | + |
| 135 | + private: |
| 136 | + // name of the unique variable ? |
| 137 | +}; |
| 138 | + |
| 139 | +/* TODO: |
| 140 | + derivative on function or SE ? |
| 141 | + don't forget to prepare for approx in a integral approx |
| 142 | + */ |
| 143 | + |
| 144 | +} // namespace Poincare |
| 145 | + |
| 146 | +#endif |
0 commit comments