IterationFormula.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(std::unique_ptr<Expression> expr, const std::vector<std::string>& variables = { "c", "z" });
  43. IterationFormula(Expression expr, const std::vector<std::string>& variables = { "c", "z" });
  44. std::optional<std::string> findUnknownVariables(const Expression& expr);
  45. void optimize(void);
  46. bool containsVariable(const std::string& name) const;
  47. IterationFormula clone(void) const;
  48. };
  49. struct mnd::Constant
  50. {
  51. mnd::Real re;
  52. mnd::Real im;
  53. inline Constant(const mnd::Real& value) :
  54. re{ value },
  55. im{ 0.0 }
  56. {}
  57. inline Constant(const mnd::Real& re, const mnd::Real& im) :
  58. re{ re },
  59. im{ im }
  60. {}
  61. };
  62. struct mnd::Variable
  63. {
  64. std::string name;
  65. };
  66. struct mnd::Negation
  67. {
  68. std::unique_ptr<Expression> operand;
  69. /*inline Negation(const Negation& other) :
  70. operand{ std::make_unique<Expression>(*other.operand) }
  71. {}*/
  72. };
  73. struct mnd::BinaryOperation
  74. {
  75. std::unique_ptr<Expression> left;
  76. std::unique_ptr<Expression> right;
  77. /*inline BinaryOperation(const BinaryOperation& other) :
  78. left{ std::make_unique<Expression>(*other.left) },
  79. right{ std::make_unique<Expression>(*other.right) }
  80. {}*/
  81. };
  82. struct mnd::Addition : mnd::BinaryOperation
  83. {
  84. bool subtraction = false;
  85. };
  86. struct mnd::Multiplication : mnd::BinaryOperation
  87. {
  88. };
  89. struct mnd::Division : mnd::BinaryOperation
  90. {
  91. };
  92. struct mnd::Pow : mnd::BinaryOperation
  93. {
  94. bool realExponent;
  95. bool integerExponent;
  96. };
  97. namespace mnd
  98. {
  99. }
  100. #endif // MANDEL_ITERATIONFORMULA_H