Milo  0.1
Milo:EquationManipulationProgram
nodes.h
Go to the documentation of this file.
1 #ifndef __NODES_H
2 #define __NODES_H
3 
4 /* Copyright (C) 2016 - James Terman
5  *
6  * milo is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * milo is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
27 #include <vector>
28 #include <exception>
29 #include <unordered_map>
30 #include "milo.h"
31 
32 // Forward class decleration
33 class Parser;
34 
39 class Binary : public Node
40 {
41 public:
53  Binary(char op, Node* one, Node* two, Node* parent, bool neg, Node::Select s) :
54  Node(parent, neg, s), m_op(op), m_first(one), m_second(two) {}
55 
62  Binary(EqnXMLParser& in, Node* parent);
63 
68  virtual ~Binary() {}
70 
78  std::string toString() const { return m_first->toString() + std::string(1, m_op) + m_second->toString(); }
79 
84  bool isLeaf() const { return false; }
85 
92  Node* findNode(int x, int y);
93 
100  Node* findNode(const Box& b);
101 
106  int numFactors() const { return m_first->numFactors() + m_second->numFactors(); }
108 
114 
120 
128  static Node* parse(Parser& p, Node* one, Node* parent);
129 
130 protected:
131  char m_op;
135 
136 private:
144  Node* downLeft() { return m_first; }
145 
151  Node* downRight() { return m_second; }
152 
159  Node* getLeftSibling(Node* node) { return (m_second == node) ? m_first->last() : nullptr; }
160 
167  Node* getRightSibling(Node* node) { return (m_first == node) ? m_second->first() : nullptr; }
168 
173  void xml_out(XML::Stream& xml) const;
175 };
176 
181 class Divide : public Binary
182 {
183 public:
194  Divide(Node* one, Node* two, Node* parent, bool neg = false, Node::Select s = Node::Select::NONE) :
195  Binary('/', one, two, parent, neg, s)
196  {
197  m_first->setParent(this); m_second->setParent(this);
198  m_first->setDrawParenthesis(false); m_second->setDrawParenthesis(false);
199  }
200 
207  Divide(EqnXMLParser& in, Node* parent) : Binary(in, parent)
208  {
209  m_op = '/'; m_first->setDrawParenthesis(false); m_second->setDrawParenthesis(false);
210  }
211 
215  ~Divide() {}
217 
224  const std::string& getName() const { return name; }
225 
230  std::type_index getType() const { return type; }
231 
236  void normalize();
237 
242  bool simplify();
244 
251  static bool create(Equation& eqn);
252 
253  static const std::string name;
254  static const std::type_index type;
255 
256  private:
265 
272  void calcOrig(UI::Graphics& gc, int x, int y);
273 
278  void drawNode(UI::Graphics& gc) const;
279 
284  Complex getNodeValue() const;
286 
292  static Node* normalize(Node* n);
293 };
294 
300 class Power : public Binary
301 {
302 public:
313  Power(Node* one, Node* two, Node* parent, bool neg = false, Node::Select s = Node::Select::NONE) :
314  Binary('^', one, two, parent, neg, s)
315  {
316  m_first->setParent(this); m_second->setParent(this);
317  m_first->setDrawParenthesis(m_first->numFactors()>1); m_second->setDrawParenthesis(false);
318  }
319 
326  Power(EqnXMLParser& in, Node* parent) : Binary(in, parent)
327  {
328  m_op = '^'; m_first->setDrawParenthesis(m_first->numFactors()>1); m_second->setDrawParenthesis(false);
329  }
330 
334  ~Power() {}
336 
343  const std::string& getName() const { return name; }
344 
349  std::type_index getType() const { return type; }
350 
355  void normalize();
356 
361  bool simplify();
363 
370  static bool create(Equation& eqn);
371 
377  static bool simplify(NodeVector& factors);
378 
379  static const std::string name;
380  static const std::type_index type;
381 
382 private:
391 
398  void calcOrig(UI::Graphics& gc, int x, int y);
399 
404  void drawNode(UI::Graphics& gc) const;
405 
410  Complex getNodeValue() const;
412 
421 };
422 
427 class Constant : public Node
428 {
429 public:
437  Constant(Parser& p, Node* parent);
438 
447  Constant(char name, Complex value, Node* parent, bool neg = false, Node::Select s = Node::Select::NONE) :
448  Node(parent, neg, s), m_name(name), m_value(value) {}
449 
456  Constant(EqnXMLParser& in, Node* parent);
457 
463 
464  using const_map = std::unordered_map<char, Complex>;
465 
472  std::string toString() const { return std::string() + m_name; }
473 
478  const std::string& getName() const { return name; }
479 
484  std::type_index getType() const { return type; }
486 
487  static const std::string name;
488  static const std::type_index type;
489 
496  static Constant* parse(Parser& p, Node* parent);
497 private:
498  char m_name;
501 
502  static const const_map constants;
503 
510  void xml_out(XML::Stream& xml) const;
511 
518 
525  void calcOrig(UI::Graphics& gc, int x, int y);
526 
531  void drawNode(UI::Graphics& gc) const;
532 
537  Complex getNodeValue() const { return constants.at(m_name); }
539 };
540 
541 
545 class Variable : public Node
546 {
547 public:
555  Variable(Parser& p, Node* parent);
556 
564  Variable(char name, Node* parent, bool neg = false, Node::Select s = Node::Select::NONE) :
565  Node(parent, neg, s), m_name(name)
566  {
567  values.emplace(name, Complex(0, 0));
568  }
569 
576  Variable(EqnXMLParser& in, Node* parent);
577 
583 
584  using var_map = std::unordered_map<char, Complex>;
585 
592  std::string toString() const { return std::string(1, m_name); }
593 
598  const std::string& getName() const { return name; }
599 
604  std::type_index getType() const { return type; }
606 
607  static const std::string name;
608  static const std::type_index type;
609 
616  static Variable* parse(Parser& p, Node* parent);
617 
623  static void setValue(char name, Complex value);
624 
625 private:
626  char m_name;
628 
629  static var_map values;
630 
637  void xml_out(XML::Stream& xml) const;
638 
645 
652  void calcOrig(UI::Graphics& gc, int x, int y);
653 
658  void drawNode(UI::Graphics& gc) const;
659 
664  Complex getNodeValue() const { return values[m_name]; }
666 };
667 
672 class Number : public Node
673 {
674 public:
682  Number(Parser& p, Node* parent) : Node(parent), m_isInteger(true) { getNumber(p); }
683 
691  Number(std::string real, Node* parent, bool neg = false, Node::Select s = Node::Select::NONE) :
692  Node(parent, neg, s), m_value(stod(real)), m_isInteger(isInteger(real)) {}
693 
701  Number(int n, Node* parent, bool neg = false, Node::Select s = Node::Select::NONE) :
702  Node(parent, neg, s), m_value(n), m_isInteger(true) {}
703 
711  Number(double d, Node* parent, bool neg = false, Node::Select s = Node::Select::NONE) :
712  Node(parent, neg, s), m_value(d), m_isInteger(isInteger(d)) {}
713 
720  Number(EqnXMLParser& in, Node* parent);
721 
725  ~Number() {}
727 
732  void getNumber(Parser& p);
733 
740  std::string toString() const;
741 
746  const std::string& getName() const { return name; }
747 
752  std::type_index getType() const { return type; }
753 
758  bool simplify();
760 
761  static const std::string name;
762  static const std::type_index type;
763 
770  static Number* parse(Parser& p, Node* parent);
771 
772 private:
773  double m_value;
774  bool m_isInteger;
776 
784  static std::string getInteger(Parser& p);
785 
790  void xml_out(XML::Stream& xml) const;
791 
798 
805  void calcOrig(UI::Graphics& gc, int x, int y);
806 
811  void drawNode(UI::Graphics& gc) const;
812 
817  Complex getNodeValue() const { return {m_value, 0}; }
819 };
820 
824 class Function : public Node
825 {
826 public:
830  using func_ptr = Complex (*)(Complex);
831 
835  using func_map = std::unordered_map<std::string, func_ptr>;
836 
843  static Function* parse(Parser& p, Node* parent);
844 
852  std::string toString() const { return m_name + m_arg->toString(); }
853 
858  bool isLeaf() const { return false; }
859 
866  Node* findNode(int x, int y);
867 
874  Node* findNode(const Box& b);
875 
880  int numFactors() const { return m_arg->numFactors() + 1; }
881 
886  void normalize() { m_arg->normalize(); }
887 
894  bool less(NodePtr b) const;
895 
900  const std::string& getName() const { return name; }
901 
906  std::type_index getType() const { return type; }
908 
909  static const std::string name;
910  static const std::type_index type;
911 
923  Function(const std::string& name, func_ptr fp, Parser& p, Node* parent,
924  bool neg = false, Node::Select s = Node::Select::NONE) :
925  Node(parent, neg, s), m_name(name), m_func(fp), m_arg(new Expression(p, this)) {}
926 
933  Function(EqnXMLParser& in, Node* parent);
934 
940 private:
941  std::string m_name;
945 
953  Node* downLeft() { return m_arg->first(); }
954 
960  Node* downRight() { return m_arg->last(); }
965  void xml_out(XML::Stream& xml) const;
966 
973 
980  void calcOrig(UI::Graphics& gc, int x, int y);
981 
986  void drawNode(UI::Graphics& gc) const;
987 
992  Complex getNodeValue() const;
994 
997  static const func_map functions;
998 
999  static Complex sinZ(Complex z);
1000  static Complex cosZ(Complex z);
1001  static Complex tanZ(Complex z);
1002  static Complex logZ(Complex z);
1003  static Complex expZ(Complex z);
1004 };
1005 
1009 class Differential : public Node
1010 {
1011 public:
1019  Differential(Parser& p, Node* parent);
1020 
1027  Differential(EqnXMLParser& in, Node* parent);
1028 
1034 
1041  bool isLeaf() const { return false; }
1042 
1047  int numFactors() const { return m_function->numFactors()+1; }
1048 
1055  Node* findNode(int x, int y) { return m_function->findNode(x, y); }
1056 
1063  Node* findNode(const Box& b);
1064 
1069  std::string toString() const;
1070 
1075  const std::string& getName() const { return name; }
1076 
1081  std::type_index getType() const { return type; }
1082 
1087  void normalize() { m_function->normalize(); }
1088 
1093  bool simplify() { return false; }
1095 
1096  static const std::string name;
1097  static const std::type_index type;
1098 
1105  static Differential* parse(Parser& p, Node* parent);
1106 
1107 private:
1109  char m_variable;
1111 
1119  Node* downLeft() { return m_function->first(); }
1120 
1126  Node* downRight() { return m_function->last(); }
1131  void xml_out(XML::Stream& xml) const;
1132 
1139 
1146  void calcOrig(UI::Graphics& gc, int x, int y);
1147 
1152  void drawNode(UI::Graphics& gc) const;
1153 
1158  Complex getNodeValue() const { return {0, 0}; }
1160 };
1161 
1162 #endif // __NODES_H
int numFactors() const
Definition: nodes.h:106
Power(EqnXMLParser &in, Node *parent)
Definition: nodes.h:326
Function(const std::string &name, func_ptr fp, Parser &p, Node *parent, bool neg=false, Node::Select s=Node::Select::NONE)
Definition: nodes.h:923
Binary(char op, Node *one, Node *two, Node *parent, bool neg, Node::Select s)
Definition: nodes.h:53
Box m_internal
Bounding box of this node.
Definition: nodes.h:134
ExpressionPtr getSecondExpression()
Definition: symbol.cpp:358
Definition: smart.h:38
virtual Complex getNodeValue() const =0
char m_variable
Variable of Differential.
Definition: nodes.h:1109
const std::string & getName() const
Definition: nodes.h:478
Definition: nodes.h:824
NodePtr m_second
Second expression owned by this object.
Definition: nodes.h:133
static const std::string name
Name of Power class.
Definition: nodes.h:379
virtual ~Binary()
Definition: nodes.h:68
const std::string & getName() const
Definition: nodes.h:746
Node * downRight()
Definition: nodes.h:151
double m_value
Value of Number.
Definition: nodes.h:773
static const const_map constants
Mapping of all constant name to their values.
Definition: nodes.h:502
std::string m_name
Name of function.
Definition: nodes.h:941
Definition: xml.h:165
char m_name
Name of constant.
Definition: nodes.h:498
Complex m_value
Value of constant.
Definition: nodes.h:499
Definition: milo.h:72
NodePtr m_first
First expression owned by this object.
Definition: nodes.h:132
static const std::type_index type
Type of Function type.
Definition: nodes.h:910
Definition: nodes.h:545
~Variable()
Definition: nodes.h:581
const std::string & getName() const
Definition: nodes.h:343
static const std::string name
Name of Variable class.
Definition: nodes.h:607
static const std::type_index type
Type of Number class.
Definition: nodes.h:762
std::complex< double > Complex
Specialized as complex double.
Definition: milo.h:58
bool isLeaf() const
Definition: nodes.h:1041
typename std::vector< SmartPtr< Node > >::iterator iterator
Definition: smart.h:88
virtual void normalize()
Definition: milo.h:175
const std::string & getName() const
Definition: nodes.h:224
Definition: nodes.h:300
Box m_internal
Bounding box of this node.
Definition: nodes.h:1108
~Constant()
Definition: nodes.h:461
~Number()
Definition: nodes.h:725
Constant(char name, Complex value, Node *parent, bool neg=false, Node::Select s=Node::Select::NONE)
Definition: nodes.h:447
Definition: parser.cpp:85
std::type_index getType() const
Definition: nodes.h:349
Divide(Node *one, Node *two, Node *parent, bool neg=false, Node::Select s=Node::Select::NONE)
Definition: nodes.h:194
virtual bool simplify()
Definition: milo.h:181
Complex getNodeValue() const
Definition: nodes.h:537
Node * downRight()
Definition: nodes.h:960
std::string toString() const
Definition: nodes.h:852
std::unordered_map< char, Complex > var_map
Specialization for mapping name to value.
Definition: nodes.h:584
std::string toString() const
Definition: nodes.h:472
Number(std::string real, Node *parent, bool neg=false, Node::Select s=Node::Select::NONE)
Definition: nodes.h:691
Divide(EqnXMLParser &in, Node *parent)
Definition: nodes.h:207
virtual void drawNode(UI::Graphics &gc) const =0
Box m_internal
Bounding box of this node.
Definition: nodes.h:500
Power(Node *one, Node *two, Node *parent, bool neg=false, Node::Select s=Node::Select::NONE)
Definition: nodes.h:313
Complex getNodeValue() const
Definition: nodes.h:817
Definition: milo.h:744
static const std::string name
Name of Divide class.
Definition: nodes.h:253
Node * downLeft()
Definition: nodes.h:953
Number(int n, Node *parent, bool neg=false, Node::Select s=Node::Select::NONE)
Definition: nodes.h:701
void normalize()
Definition: nodes.h:886
ExpressionPtr getFirstExpression()
Definition: symbol.cpp:352
Node * findNode(int x, int y)
Definition: nodes.cpp:582
virtual void calcOrig(UI::Graphics &gc, int x, int y)=0
const std::string & getName() const
Definition: nodes.h:900
static const std::type_index type
Type of Differential class.
Definition: nodes.h:1097
static const func_map functions
Definition: nodes.h:997
static Node * parse(Parser &p, Node *one, Node *parent)
Definition: parser.cpp:433
Node * downLeft()
Definition: nodes.h:144
~Power()
Definition: nodes.h:334
static const std::string name
Name of constant.
Definition: nodes.h:487
int numFactors() const
Definition: nodes.h:880
Node * findNode(int x, int y)
Definition: nodes.h:1055
Complex getNodeValue() const
Definition: nodes.h:664
std::string toString() const
Definition: nodes.h:592
Definition: nodes.h:1009
virtual Node * findNode(int x, int y)
Definition: milo.cpp:393
char m_name
Name of Variable.
Definition: nodes.h:626
Definition: nodes.h:39
Number(Parser &p, Node *parent)
Definition: nodes.h:682
Complex getNodeValue() const
Definition: nodes.h:1158
Definition: nodes.h:181
Box m_internal
Bounding box of this node.
Definition: nodes.h:944
Definition: milo.h:1326
Node * downLeft()
Definition: nodes.h:1119
Definition: milo.h:99
std::type_index getType() const
Definition: nodes.h:752
static const std::string name
Name of Differential class.
Definition: nodes.h:1096
std::unordered_map< std::string, func_ptr > func_map
Map that associates function name with its function call.
Definition: nodes.h:835
void xml_out(XML::Stream &xml) const
Definition: parser.cpp:356
std::type_index getType() const
Definition: nodes.h:1081
bool m_isInteger
True, if integer.
Definition: nodes.h:774
bool simplify()
Definition: nodes.h:1093
Definition: ui.h:357
static const std::type_index type
Type of Variable class.
Definition: nodes.h:608
static const std::type_index type
Type of Divide class.
Definition: nodes.h:254
virtual bool less(NodePtr b) const
Definition: milo.h:189
Node * getLeftSibling(Node *node)
Definition: nodes.h:159
~Divide()
Definition: nodes.h:215
std::type_index getType() const
Definition: nodes.h:230
Node * getRightSibling(Node *node)
Definition: nodes.h:167
std::type_index getType() const
Definition: nodes.h:604
const std::string & getName() const
Definition: nodes.h:1075
static Node * create(EqnXMLParser &in, Node *parent)
Definition: parser.cpp:272
static const std::string name
Name of Function class.
Definition: nodes.h:909
Definition: nodes.h:672
Select
Definition: milo.h:86
Definition: nodes.h:427
char m_op
Character repesentation of operator.
Definition: nodes.h:131
std::type_index getType() const
Definition: nodes.h:906
static var_map values
Mapping of all variable name to their values.
Definition: nodes.h:629
Box m_internal
Bounding box of this node.
Definition: nodes.h:775
NodePtr m_function
Function to be differentiated.
Definition: nodes.h:1110
~Function()
Definition: nodes.h:938
std::string toString() const
Definition: nodes.h:78
func_ptr m_func
Function pointer to evaluate function.
Definition: nodes.h:942
static const std::string name
Name of Number class.
Definition: nodes.h:761
NodePtr m_arg
Function own this tree.
Definition: nodes.h:943
~Differential()
Definition: nodes.h:1032
std::unordered_map< char, Complex > const_map
Specialization for mapping name to value.
Definition: nodes.h:464
Number(double d, Node *parent, bool neg=false, Node::Select s=Node::Select::NONE)
Definition: nodes.h:711
Variable(char name, Node *parent, bool neg=false, Node::Select s=Node::Select::NONE)
Definition: nodes.h:564
void normalize()
Definition: nodes.h:1087
Box m_internal
Bounding box of this node.
Definition: nodes.h:627
static const std::type_index type
Type of Power class.
Definition: nodes.h:380
static const std::type_index type
Type of constant.
Definition: nodes.h:488
Definition: parser.cpp:43
const std::string & getName() const
Definition: nodes.h:598
std::type_index getType() const
Definition: nodes.h:484
Node * downRight()
Definition: nodes.h:1126
virtual Frame calcSize(UI::Graphics &gc)=0
bool isLeaf() const
Definition: nodes.h:84
bool isLeaf() const
Definition: nodes.h:858
Complex(*)(Complex) func_ptr
Definition: nodes.h:830
int numFactors() const
Definition: nodes.h:1047