1
0

IterationFormula.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef MANDEL_ITERATIONFORMULA_H
  2. #define MANDEL_ITERATIONFORMULA_H
  3. #include <variant>
  4. #include <memory>
  5. #include <string>
  6. #include <stdexcept>
  7. #include "Types.h"
  8. namespace mnd
  9. {
  10. struct IterationFormula;
  11. struct Constant;
  12. struct Variable;
  13. struct Negation;
  14. struct BinaryOperation;
  15. struct Addition;
  16. struct Multiplication;
  17. struct Division;
  18. struct Pow;
  19. using Expression = std::variant<
  20. Constant,
  21. Variable,
  22. Negation,
  23. Addition,
  24. Multiplication,
  25. Division,
  26. Pow
  27. >;
  28. struct ParseError : std::runtime_error
  29. {
  30. ParseError(const std::string& err) :
  31. std::runtime_error(err.c_str())
  32. {}
  33. };
  34. Expression parse(const std::string& formula);
  35. std::string toString(const mnd::Expression&);
  36. }
  37. struct mnd::IterationFormula
  38. {
  39. std::unique_ptr<Expression> expr;
  40. IterationFormula(Expression expr);
  41. void optimize(void);
  42. };
  43. struct mnd::Constant
  44. {
  45. mnd::Real re;
  46. mnd::Real im;
  47. inline Constant(const mnd::Real& value) :
  48. re{ value },
  49. im{ 0.0 }
  50. {}
  51. inline Constant(const mnd::Real& re, const mnd::Real& im) :
  52. re{ re },
  53. im{ im }
  54. {}
  55. };
  56. struct mnd::Variable
  57. {
  58. std::string name;
  59. };
  60. struct mnd::Negation
  61. {
  62. std::unique_ptr<Expression> operand;
  63. /*inline UnaryOperation(const UnaryOperation& other) :
  64. operand{ std::make_unique<Expression>(*other.operand) }
  65. {}*/
  66. };
  67. struct mnd::BinaryOperation
  68. {
  69. std::unique_ptr<Expression> left;
  70. std::unique_ptr<Expression> right;
  71. /*inline BinaryOperation(const BinaryOperation& other) :
  72. left{ std::make_unique<Expression>(*other.left) },
  73. right{ std::make_unique<Expression>(*other.right) }
  74. {}*/
  75. };
  76. struct mnd::Addition : mnd::BinaryOperation
  77. {
  78. bool subtraction = false;
  79. };
  80. struct mnd::Multiplication : mnd::BinaryOperation
  81. {
  82. };
  83. struct mnd::Division : mnd::BinaryOperation
  84. {
  85. };
  86. struct mnd::Pow : mnd::BinaryOperation
  87. {
  88. bool realExponent;
  89. bool integerExponent;
  90. };
  91. namespace mnd
  92. {
  93. }
  94. #endif // MANDEL_ITERATIONFORMULA_H