1
0

IterationFormula.h 2.5 KB

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