Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <iostream>
- #include <string>
- #include <unordered_map>
- #include <map>
- #include <math.h>
- #include <vector>
- #include <initializer_list>
- struct NodeObj;
- typedef std::shared_ptr<NodeObj> Node;
- Node NodeNull(0);
- class NodeObj {
- protected:
- NodeObj() {}
- public:
- virtual Node unwrap() const;
- virtual bool isNeg() const;
- virtual bool isRecip() const;
- virtual Node negate() const;
- virtual Node reciprocate() const;
- virtual bool isConst() const;
- virtual double getConst() const;
- virtual bool isLog() const;
- virtual bool isExp() const;
- virtual bool isPower() const;
- virtual bool isSum() const;
- virtual bool isDiff() const;
- public:
- virtual Node derivative(const Node& wrt) = 0;
- virtual void print(std::ostream& os) const = 0;
- virtual ~NodeObj() {}
- };
- class Const : public NodeObj {
- private:
- double value;
- static std::map<double, Node> intern;
- protected:
- Const(double v) : value(v) {}
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(double v);
- virtual void print(std::ostream& os) const;
- virtual Node negate() const;
- virtual Node reciprocate() const;
- virtual bool isConst() const;
- virtual double getConst() const;
- };
- inline
- Node number(double n) {
- return Const::make(n);
- }
- class Variable : public NodeObj {
- private:
- std::string name;
- static std::unordered_map<std::string, Node> intern;
- protected:
- Variable(const std::string& n) : name(n) {}
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const std::string& n);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node var(const std::string& n) {
- return Variable::make(n);
- }
- class Diff;
- class Sum : public NodeObj {
- private:
- std::vector<Node> a;
- Node d;
- protected:
- Sum() {}
- Sum(const Node& p, const Node& q) {
- a.push_back(p);
- a.push_back(q);
- }
- virtual bool isSum() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p, const Node& q);
- static Node make(std::initializer_list<Node> l);
- static Node make(const std::vector<Node>& l);
- template<typename It> static Node make(It start, It end);
- virtual void print(std::ostream& os) const;
- friend class Diff;
- };
- inline
- Node& operator+=(Node& a1, const Node& a2) {
- Node n = Sum::make(a1, a2);
- a1.swap(n);
- return a1;
- }
- inline
- Node operator +(const Node& p, const Node& q) {
- return Sum::make(p, q);
- }
- inline
- Node sum(std::initializer_list<Node> l)
- {
- return Sum::make(l);
- }
- inline
- Node sum(const std::vector<Node>& l)
- {
- return Sum::make(l);
- }
- class Neg : public NodeObj {
- private:
- Node a;
- Node d;
- protected:
- Neg(const Node& p) : a(p) {}
- virtual Node unwrap() const;
- virtual bool isNeg() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p);
- virtual void print(std::ostream& os) const;
- virtual Node negate() const;
- };
- inline
- Node operator -(const Node& p) {
- return Neg::make(p);
- }
- class Log : public NodeObj {
- private:
- Node a;
- Node d;
- protected:
- Log(const Node& p) : a(p) {}
- virtual Node unwrap() const;
- virtual bool isLog() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node log(const Node& p) {
- return Log::make(p);
- }
- class Exp : public NodeObj {
- private:
- Node a;
- Node d;
- protected:
- Exp(const Node& p) : a(p) {}
- virtual Node unwrap() const;
- virtual bool isExp() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node exp(const Node& p) {
- return Exp::make(p);
- }
- class Sin : public NodeObj {
- private:
- Node a;
- Node d;
- protected:
- Sin(const Node& p) : a(p) {}
- virtual Node unwrap() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node sin(const Node& p) {
- return Sin::make(p);
- }
- class Cos : public NodeObj {
- private:
- Node a;
- Node d;
- protected:
- Cos(const Node& p) : a(p) {}
- virtual Node unwrap() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node cos(const Node& p) {
- return Cos::make(p);
- }
- class Tan : public NodeObj {
- private:
- Node a;
- Node d;
- protected:
- Tan(const Node& p) : a(p) {}
- virtual Node unwrap() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node tan(const Node& p) {
- return Tan::make(p);
- }
- class Reciprocal : public NodeObj {
- private:
- Node a;
- Node d;
- protected:
- Reciprocal(const Node& p) : a(p) {}
- virtual Node unwrap() const;
- virtual bool isRecip() const;
- virtual Node reciprocate() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node recip(const Node& p) {
- return Reciprocal::make(p);
- }
- class Diff : public NodeObj {
- private:
- Node a, b;
- Node d;
- protected:
- Diff(const Node& p, const Node& q) : a(p), b(q) {}
- bool isDiff() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p, const Node& q);
- virtual void print(std::ostream& os) const;
- virtual Node negate() const;
- friend class Sum;
- };
- inline
- Node operator -(const Node& p, const Node& q) {
- return Diff::make(p, q);
- }
- inline
- Node& operator-=(Node& a1, const Node& a2) {
- Node n = Diff::make(a1, a2);
- a1.swap(n);
- return a1;
- }
- class Product : public NodeObj {
- private:
- Node a, b;
- Node d;
- protected:
- Product(const Node& p, const Node& q) : a(p), b(q) {}
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p, const Node& q);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node operator *(const Node& p, const Node& q) {
- return Product::make(p, q);
- }
- inline
- Node& operator*=(Node& a1, const Node& a2) {
- Node n = Product::make(a1, a2);
- a1.swap(n);
- return a1;
- }
- class Power : public NodeObj {
- private:
- Node a, b;
- Node d;
- protected:
- Power(const Node& p, const Node& q) : a(p), b(q) {}
- virtual Node reciprocate() const;
- virtual bool isPower() const;
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p, const Node& q);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node pow(const Node& p, const Node& q) {
- return Power::make(p, q);
- }
- class Quotient : public NodeObj {
- private:
- Node a, b;
- Node d;
- protected:
- virtual Node reciprocate() const;
- Quotient(const Node& p, const Node& q) : a(p), b(q) {}
- public:
- virtual Node derivative(const Node& wrt);
- static Node make(const Node& p, const Node& q);
- virtual void print(std::ostream& os) const;
- };
- inline
- Node operator /(const Node& p, const Node& q) {
- return Quotient::make(p, q);
- }
- inline
- Node& operator/=(Node& a1, const Node& a2) {
- Node n = Quotient::make(a1, a2);
- a1.swap(n);
- return a1;
- }
- bool isPowerOfTwo(double n)
- {
- if (n==0) return false;
- return (ceil(log2(n)) == floor(log2(n)));
- }
- /* Node */
- std::ostream& operator<<(std::ostream& os, const NodeObj& n)
- {
- n.print(os);
- return os;
- }
- Node NodeObj::unwrap() const
- {
- return NodeNull;
- }
- bool NodeObj::isNeg() const
- {
- return false;
- }
- bool NodeObj::isRecip() const
- {
- return false;
- }
- Node NodeObj::negate() const
- {
- return NodeNull;
- }
- Node NodeObj::reciprocate() const
- {
- return NodeNull;
- }
- bool NodeObj::isConst() const
- {
- return false;
- }
- double NodeObj::getConst() const
- {
- return 0;
- }
- bool NodeObj::isLog() const
- {
- return false;
- }
- bool NodeObj::isExp() const
- {
- return false;
- }
- bool NodeObj::isPower() const
- {
- return false;
- }
- bool NodeObj::isSum() const
- {
- return false;
- }
- bool NodeObj::isDiff() const
- {
- return false;
- }
- /* Const */
- std::map<double, Node> Const::intern;
- Node Const::make(double v)
- {
- std::map<double, Node>::iterator ii = intern.find(v);
- if (ii != intern.end()) return ii->second;
- Node p(new Const(v));
- intern[v] = p;
- return p;
- }
- Node Const::negate() const
- {
- return Const::make(-value);
- }
- void Const::print(std::ostream& os) const
- {
- os << value;
- }
- Node ConstZero = Const::make(0);
- Node ConstOne = Const::make(1);
- Node ConstTwo = Const::make(2);
- Node ConstNeg = Const::make(-1);
- Node ConstInf = Const::make(1.0 / 0.0);
- Node Const::reciprocate() const
- {
- if (value == 0) return ConstInf;
- if (value == 1) return ConstOne;
- if (isPowerOfTwo(value)) return Const::make(1.0 / value);
- if (isinf(value)) return ConstZero;
- return NodeNull;
- }
- bool Const::isConst() const
- {
- return true;
- }
- double Const::getConst() const
- {
- return value;
- }
- Node Const::derivative(const Node& wrt)
- {
- return ConstZero;
- }
- /* Variable */
- std::unordered_map<std::string, Node> Variable::intern;
- Node Variable::make(const std::string& n)
- {
- std::unordered_map<std::string, Node>::iterator ii = intern.find(n);
- if (ii != intern.end()) return ii->second;
- Node p(new Variable(n));
- intern[n] = p;
- return p;
- }
- void Variable::print(std::ostream& os) const
- {
- os << name;
- }
- Node Variable::derivative(const Node& wrt)
- {
- if (wrt.get() == this) return ConstOne;
- return ConstZero;
- }
- /* Sum */
- Node Sum::make(const Node& p, const Node& q)
- {
- if (p->isConst() && q->isConst()) {
- return Const::make(p->getConst() + q->getConst());
- }
- if (p == ConstZero) return q;
- if (q == ConstZero) return p;
- if (p == q) return Product::make(ConstTwo, p);
- if (p->isSum() || p->isDiff() || q->isSum() || q->isDiff()) {
- std::vector<Node> addends;
- if (p->isSum()) {
- Sum *s = static_cast<Sum*>(p.get());
- addends.insert(addends.end(), s->a.begin(), s->a.end());
- } else if (p->isDiff()) {
- Diff *s = static_cast<Diff*>(p.get());
- addends.push_back(s->a);
- addends.push_back(Neg::make(s->b));
- } else {
- addends.push_back(p);
- }
- if (q->isSum()) {
- Sum *s = static_cast<Sum*>(q.get());
- addends.insert(addends.end(), s->a.begin(), s->a.end());
- } else if (q->isDiff()) {
- Diff *s = static_cast<Diff*>(q.get());
- addends.push_back(s->a);
- addends.push_back(Neg::make(s->b));
- } else {
- addends.push_back(q);
- }
- return Sum::make(addends);
- }
- if (p->isNeg() && q->isNeg()) return Neg::make(Sum::make(p->unwrap(), q->unwrap()));
- if (p->isNeg()) return Diff::make(q, p->unwrap());
- if (q->isNeg()) return Diff::make(p, q->unwrap());
- return Node(new Sum(p, q));
- }
- template<typename It>
- Node Sum::make(It first, It last)
- {
- double const_total = 0;
- Sum *s = new Sum();
- for (auto it = first; it != last; ++it) {
- const Node& n(*it);
- if(n->isConst()) {
- const_total += n->getConst();
- } else {
- s->a.push_back(n);
- }
- }
- if (const_total != 0) {
- s->a.push_back(Const::make(const_total));
- }
- if (s->a.size() == 0) {
- delete s;
- return ConstZero;
- } else if (s->a.size() == 1) {
- Node n(s->a[0]);
- delete s;
- return n;
- } else if (s->a.size() == 2) {
- Node n(Sum::make(s->a[0], s->a[1]));
- delete s;
- return n;
- } else {
- return Node(s);
- }
- }
- Node Sum::make(std::initializer_list<Node> l)
- {
- return Sum::make(l.begin(), l.end());
- }
- Node Sum::make(const std::vector<Node>& l)
- {
- return Sum::make(l.begin(), l.end());
- }
- bool Sum::isSum() const
- {
- return true;
- }
- void Sum::print(std::ostream& os) const
- {
- bool first = true;
- os << '(';
- for (const Node& n : a) {
- if (!first) os << " + ";
- first = false;
- os << (*n);
- }
- os << ')';
- // os << '(' << (*a) << " + " << (*b) << ')';
- }
- Node Sum::derivative(const Node& wrt)
- {
- if (d) return d;
- std::vector<Node> v;
- for (const Node& n : a) {
- v.push_back(n->derivative(wrt));
- }
- return Sum::make(v);
- // Node p = a->derivative(wrt);
- // Node q = b->derivative(wrt);
- // return (d = Sum::make(p, q));
- }
- /* Diff */
- Node Diff::make(const Node& p, const Node& q)
- {
- if (p->isConst() && q->isConst()) {
- return Const::make(p->getConst() - q->getConst());
- }
- if (p == ConstZero) return Neg::make(q);
- if (q == ConstZero) return p;
- if (p == q) return ConstZero;
- if (p->isSum() || p->isDiff() || q->isSum() || q->isDiff()) {
- std::vector<Node> addends;
- if (p->isSum()) {
- Sum *s = static_cast<Sum*>(p.get());
- addends.insert(addends.end(), s->a.begin(), s->a.end());
- } else if (p->isDiff()) {
- Diff *s = static_cast<Diff*>(p.get());
- addends.push_back(s->a);
- addends.push_back(Neg::make(s->b));
- } else {
- addends.push_back(p);
- }
- if (q->isSum()) {
- Sum *s = static_cast<Sum*>(q.get());
- for (const Node& n : s->a) {
- addends.push_back(Neg::make(n));
- }
- } else if (q->isDiff()) {
- Diff *s = static_cast<Diff*>(q.get());
- addends.push_back(Neg::make(s->a));
- addends.push_back(s->b);
- } else {
- addends.push_back(Neg::make(q));
- }
- return Sum::make(addends);
- }
- if (p->isNeg() && q->isNeg()) return Diff::make(q->unwrap(), p->unwrap());
- if (q->isNeg()) return Sum::make(p, q->unwrap());
- if (p->isNeg()) return Neg::make(Sum::make(p->unwrap(), q));
- return Node(new Diff(p, q));
- }
- bool Diff::isDiff() const
- {
- return true;
- }
- Node Diff::negate() const
- {
- return Diff::make(b, a);
- }
- void Diff::print(std::ostream& os) const
- {
- os << '(' << (*a) << " - " << (*b) << ')';
- }
- Node Diff::derivative(const Node& wrt)
- {
- if (d) return d;
- Node p = a->derivative(wrt);
- Node q = b->derivative(wrt);
- return (d = Diff::make(p, q));
- }
- /* Neg */
- Node Neg::make(const Node& p)
- {
- Node neg = p->negate();
- if (neg) return neg;
- return Node(new Neg(p));
- }
- void Neg::print(std::ostream& os) const
- {
- os << '-' << (*a);
- }
- Node Neg::derivative(const Node& wrt)
- {
- if (d) return d;
- Node p = a->derivative(wrt);
- return (d = Neg::make(p));
- }
- Node Neg::unwrap() const
- {
- return a;
- }
- Node Neg::negate() const
- {
- return a;
- }
- bool Neg::isNeg() const
- {
- return true;
- }
- /* Log */
- Node Log::make(const Node& p)
- {
- if (p->isExp()) return p->unwrap();
- /// XXX handle constant
- return Node(new Log(p));
- }
- void Log::print(std::ostream& os) const
- {
- os << "log" << (*a);
- }
- Node Log::derivative(const Node& wrt)
- {
- if (d) return d;
- Node p = a->derivative(wrt);
- return (d = Quotient::make(p, a));
- }
- Node Log::unwrap() const
- {
- return a;
- }
- bool Log::isLog() const
- {
- return true;
- }
- /* Exp */
- Node Exp::make(const Node& p)
- {
- if (p->isLog()) return p->unwrap();
- /// XXX handle constant
- return Node(new Exp(p));
- }
- void Exp::print(std::ostream& os) const
- {
- os << "exp" << (*a);
- }
- Node Exp::derivative(const Node& wrt)
- {
- if (d) return d;
- Node p = a->derivative(wrt);
- Node e = Exp::make(a);
- return (d = Product::make(p, e));
- }
- Node Exp::unwrap() const
- {
- return a;
- }
- bool Exp::isExp() const
- {
- return true;
- }
- /* Reciprocal */
- Node Reciprocal::make(const Node& p)
- {
- Node rec = p->reciprocate();
- if (rec) return rec;
- if (p->isRecip()) return p->unwrap();
- return Node(new Reciprocal(p));
- }
- void Reciprocal::print(std::ostream& os) const
- {
- os << "1/" << (*a);
- }
- Node Reciprocal::derivative(const Node& wrt)
- {
- if (d) return d;
- Node ap = a->derivative(wrt);
- return (d = Quotient::make(ap, Product::make(ConstTwo, a)));
- }
- Node Reciprocal::unwrap() const
- {
- return a;
- }
- Node Reciprocal::reciprocate() const
- {
- return a;
- }
- bool Reciprocal::isRecip() const
- {
- return true;
- }
- /* Product */
- Node Product::make(const Node& p, const Node& q)
- {
- if (p->isConst() && q->isConst()) {
- return Const::make(p->getConst() * q->getConst());
- }
- if (p == ConstZero) return ConstZero;
- if (q == ConstZero) return ConstZero;
- if (p == ConstOne) return q;
- if (q == ConstOne) return p;
- if (p == ConstNeg) return Neg::make(q);
- if (q == ConstNeg) return Neg::make(p);
- if (p->isRecip() && q->isRecip()) return Reciprocal::make(Product::make(p->unwrap(), q->unwrap()));
- if (p->isRecip()) return Quotient::make(q, p);
- if (q->isRecip()) return Quotient::make(p, q);
- return Node(new Product(p, q));
- }
- void Product::print(std::ostream& os) const
- {
- os << '(' << (*a) << " * " << (*b) << ')';
- }
- Node Product::derivative(const Node& wrt)
- {
- if (d) return d;
- Node ap = a->derivative(wrt);
- Node bp = b->derivative(wrt);
- Node r = Product::make(a, bp);
- Node s = Product::make(b, ap);
- return (d = Sum::make(r, s));
- }
- /* Quotient */
- Node Quotient::make(const Node& p, const Node& q)
- {
- // if (p->isConst() && q->isConst()) {
- // return Const::make(p->getConst() / q->getConst());
- // }
- if (p == ConstZero) return ConstZero;
- if (q == ConstZero) return ConstInf;
- if (p == ConstOne) return Reciprocal::make(q);
- if (q == ConstOne) return p;
- if (p == ConstNeg) return Neg::make(Reciprocal::make(q));
- if (q == ConstNeg) return Neg::make(p);
- if (p->isRecip() && q->isRecip()) return Quotient::make(q->unwrap(), p->unwrap());
- if (p->isRecip()) return Reciprocal::make(Product::make(p->unwrap(), q));
- if (q->isRecip()) return Product::make(p, q->unwrap());
- // Negate power
- return Node(new Quotient(p, q));
- }
- void Quotient::print(std::ostream& os) const
- {
- os << '(' << (*a) << " / " << (*b) << ')';
- }
- Node Quotient::derivative(const Node& wrt)
- {
- if (d) return d;
- Node ap = a->derivative(wrt);
- Node bp = b->derivative(wrt);
- Node r = Product::make(b, ap);
- Node s = Product::make(a, bp);
- Node dif = Diff::make(r, s);
- Node sqr = Power::make(b, ConstTwo);
- return (d = Quotient::make(dif, sqr));
- }
- Node Quotient::reciprocate() const
- {
- return Quotient::make(b, a);
- }
- /* Power */
- Node Power::make(const Node& p, const Node& q)
- {
- // if (p->isConst() && q->isConst()) {
- // return Const::make(pow(p->getConst(), q->getConst()));
- // }
- if (p == ConstZero) return ConstZero;
- if (q == ConstZero) return ConstOne;
- if (p == ConstOne) return ConstOne;
- if (q == ConstOne) return p;
- if (q == ConstNeg) return Reciprocal::make(p);
- // Multiply powers
- if (p->isPower()) {
- Power *pw = static_cast<Power*>(p.get());
- return Power::make(pw->a, Product::make(pw->b, q));
- }
- if (p->isRecip()) return Reciprocal::make(Power::make(p->unwrap(), q));
- return Node(new Power(p, q));
- }
- Node Power::reciprocate() const
- {
- return Power::make(a, Neg::make(b));
- }
- void Power::print(std::ostream& os) const
- {
- os << '(' << (*a) << " ^ " << (*b) << ')';
- }
- Node Power::derivative(const Node& wrt)
- {
- if (d) return d;
- // g(x) f(x)^(g(x) - 1) f'(x) + f(x)^g(x) log(f(x)) g'(x)
- Node ap = a->derivative(wrt);
- Node bp = b->derivative(wrt);
- Node bm1 = Diff::make(b, ConstOne);
- Node lg = Log::make(a);
- Node p1 = Power::make(a, bm1);
- Node n1 = Product::make(Product::make(b, p1), ap);
- Node p2 = Power::make(a, b);
- Node n2 = Product::make(Product::make(p2, lg), bp);
- return (d = Sum::make(n1, n2));
- }
- bool Power::isPower() const
- {
- return true;
- }
- /* Sin */
- Node Sin::make(const Node& p)
- {
- return Node(new Sin(p));
- }
- void Sin::print(std::ostream& os) const
- {
- os << "sin" << (*a);
- }
- Node Sin::derivative(const Node& wrt)
- {
- if (d) return d;
- Node p = a->derivative(wrt);
- Node c = Cos::make(a);
- return (d = Product::make(c, p));
- }
- Node Sin::unwrap() const
- {
- return a;
- }
- /* Cos */
- Node Cos::make(const Node& p)
- {
- return Node(new Cos(p));
- }
- void Cos::print(std::ostream& os) const
- {
- os << "cos" << (*a);
- }
- Node Cos::derivative(const Node& wrt)
- {
- if (d) return d;
- Node p = a->derivative(wrt);
- Node c = Sin::make(a);
- return (d = Product::make(c, p));
- }
- Node Cos::unwrap() const
- {
- return a;
- }
- /* Tan */
- Node Tan::make(const Node& p)
- {
- return Node(new Tan(p));
- }
- void Tan::print(std::ostream& os) const
- {
- os << "tan" << (*a);
- }
- Node Tan::derivative(const Node& wrt)
- {
- if (d) return d;
- Node p = a->derivative(wrt);
- Node c = Power::make(Cos::make(a), Const::make(-2));
- return (d = Product::make(p, c));
- }
- Node Tan::unwrap() const
- {
- return a;
- }
- int main()
- {
- // Node n = Product::make(Variable::make("x"), ConstTwo);
- // Node n = Diff::make(Variable::make("x"), Neg::make(Variable::make("x")));
- // Node n = Product::make(Reciprocal::make(Variable::make("x")), Reciprocal::make(Variable::make("y")));
- // Node n = Power::make(Reciprocal::make(Const::make(3)), Reciprocal::make(Const::make(5)));
- // Node n = Power::make(Variable::make("x"), Variable::make("y"));
- // Node m = Power::make(n, Variable::make("z"));
- //Node n = Sin::make(Power::make(Variable::make("x"), ConstTwo));
- // Node n = Product::make(Reciprocal::make(Const::make(3)), Const::make(3));
- // std::vector<Node> terms;
- // Node x = Variable::make("x");
- // terms.push_back(Power::make(x, Const::make(4)));
- // terms.push_back(Power::make(x, Const::make(3)));
- // terms.push_back(Power::make(x, Const::make(2)));
- // terms.push_back(x);
- // terms.push_back(Const::make(10));
- // terms.push_back(Sin::make(Power::make(x, Const::make(5))));
- // Node n = Sum::make(terms);
- // std::vector<Node> terms;
- // Node x = var("x");
- // terms.push_back(pow(x, number(4)));
- // terms.push_back(pow(x, number(3)));
- // terms.push_back(pow(x, number(2)));
- // terms.push_back(x);
- // terms.push_back(number(10));
- // terms.push_back(sin(pow(x, number(5))));
- // Node n = sum(terms);
- Node x = var("x");
- Node y = var("y");
- Node z = var("z");
- Node w = var("w");
- // Node a = x-y;
- // Node b = z-w;
- // Node n = a-b;
- Node n = ConstZero;
- n += x;
- n -= y;
- n -= (z-w);
- // Node n = ConstZero;
- // n += pow(x, number(4));
- // n += pow(x, number(3));
- // n += pow(x, number(2));
- // n += x;
- // n += number(10);
- // n += sin(pow(x, number(5)));
- // terms.push_back(pow(x, number(4)));
- // terms.push_back(pow(x, number(3)));
- // terms.push_back(pow(x, number(2)));
- // terms.push_back(x);
- // terms.push_back(number(10));
- // terms.push_back(sin(pow(x, number(5))));
- // Node n = sum(terms);
- std::cout << (*n) << std::endl;
- Node d = n->derivative(var("x"));
- std::cout << (*d) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement