Commit 44f32a03 authored by Cianciosa, Mark's avatar Cianciosa, Mark
Browse files

Reduce the cost of removing psuedo variables by avoiding reconstructing...

Reduce the cost of removing psuedo variables by avoiding reconstructing objects when there are no changes.
parent 0c40529f
Loading
Loading
Loading
Loading
+44 −14
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@
#ifndef arithmetic_h
#define arithmetic_h

#include "node.hpp"
#include "trigonometry.hpp"

namespace graph {
//******************************************************************************
@@ -354,8 +354,13 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return this->left->remove_pseudo() +
                   this->right->remove_pseudo();
            auto l = this->left->remove_pseudo();
            auto r = this->right->remove_pseudo();
            if (l->is_match(this->left) &&
                r->is_match(this->right)) {
                return this->shared_from_this();
            }
            return l + r;
        }

//------------------------------------------------------------------------------
@@ -870,8 +875,14 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return this->left->remove_pseudo() -
                   this->right->remove_pseudo();
            auto l = this->left->remove_pseudo();
            auto r = this->right->remove_pseudo();
            if (l->is_match(this->left) &&
                r->is_match(this->right)) {
                return this->shared_from_this();
            } else {
                return l - r;
            }
        }

//------------------------------------------------------------------------------
@@ -1100,8 +1111,8 @@ namespace graph {
            }

//  Move trig to the right.
            auto cl = cos_cast(this->left);
            auto sl = sin_cast(this->left);
            auto cl = graph::cos_cast(this->left);
            auto sl = graph::sin_cast(this->left);
            if ((cl.get() && !this->right->is_power_like() &&
                 !this->right->is_all_variables() &&
                 !sin_cast(this->right).get()) ||
@@ -1491,8 +1502,14 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return this->left->remove_pseudo() *
                   this->right->remove_pseudo();
            auto l = this->left->remove_pseudo();
            auto r = this->right->remove_pseudo();
            if (l->is_match(this->left) &&
                r->is_match(this->right)) {
                return this->shared_from_this();
            } else {
                return l*r;
            }
        }

//------------------------------------------------------------------------------
@@ -1923,8 +1940,14 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return this->left->remove_pseudo() /
                   this->right->remove_pseudo();
            auto l = this->left->remove_pseudo();
            auto r = this->right->remove_pseudo();
            if (l->is_match(this->left) &&
                r->is_match(this->right)) {
                return this->shared_from_this();
            } else {
                return l/r;
            }
        }

//------------------------------------------------------------------------------
@@ -2587,9 +2610,16 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return fma(this->left->remove_pseudo(),
                       this->middle->remove_pseudo(),
                       this->right->remove_pseudo());
            auto l = this->left->remove_pseudo();
            auto m = this->middle->remove_pseudo();
            auto r = this->right->remove_pseudo();
            if (l->is_match(this->left)   &&
                m->is_match(this->middle) &&
                r->is_match(this->right)) {
                return this->shared_from_this();
            } else {
                return fma(l, m, r);
            }
        }

//------------------------------------------------------------------------------
+10 −0
Original line number Diff line number Diff line
@@ -1190,6 +1190,16 @@ namespace dispersion {
            auto dDdy = this->D->df(y);
            auto dDdz = this->D->df(z);

            if (graph::pseudo_variable_cast(x).get()) {
                dDdw = dDdw->remove_pseudo();
                dDdkx = dDdkx->remove_pseudo();
                dDdky = dDdky->remove_pseudo();
                dDdkz = dDdkz->remove_pseudo();
                dDdx = dDdx->remove_pseudo();
                dDdy = dDdy->remove_pseudo();
                dDdz = dDdz->remove_pseudo();
            }

            auto neg_one = graph::none<typename DISPERSION_FUNCTION::base,
                                       DISPERSION_FUNCTION::safe_math> ();
            dxdt = neg_one*dDdkx/dDdw;
+32 −6
Original line number Diff line number Diff line
@@ -239,7 +239,12 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return sqrt(this->arg->remove_pseudo());
            auto temp = this->arg->remove_pseudo();
            if (temp->is_match(this->arg)) {
                return this->shared_from_this();
            } else {
                return sqrt(temp);
            }
        }

//------------------------------------------------------------------------------
@@ -466,7 +471,12 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return exp(this->arg->remove_pseudo());
            auto temp = this->arg->remove_pseudo();
            if (temp->is_match(this->arg)) {
                return this->shared_from_this();
            } else {
                return exp(temp);
            }
        }

//------------------------------------------------------------------------------
@@ -693,7 +703,12 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return log(this->arg->remove_pseudo());
            auto temp = this->arg->remove_pseudo();
            if (temp->is_match(this->arg)) {
                return this->shared_from_this();
            } else {
                return log(temp);
            }
        }

//------------------------------------------------------------------------------
@@ -1077,8 +1092,14 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return pow(this->left->remove_pseudo(),
                       this->right->remove_pseudo());
            auto l = this->left->remove_pseudo();
            auto r = this->right->remove_pseudo();
            if (l->is_match(this->left) &&
                r->is_match(this->right)) {
                return this->shared_from_this();
            } else {
                return pow(l, r);
            }
        }
    };

@@ -1276,7 +1297,12 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return erfi(this->arg->remove_pseudo());
            auto temp = this->arg->remove_pseudo();
            if (temp->is_match(this->arg)) {
                return this->shared_from_this();
            } else {
                return erfi(temp);
            }
        }

//------------------------------------------------------------------------------
+36 −36
Original line number Diff line number Diff line
@@ -506,12 +506,12 @@ namespace solver {
            auto dt_const = graph::constant<typename DISPERSION_FUNCTION::base,
                                            DISPERSION_FUNCTION::safe_math> (static_cast<typename DISPERSION_FUNCTION::base> (dt));

            this->kx1 = (dt_const*this->D.get_dkxdt())->remove_pseudo();
            this->ky1 = (dt_const*this->D.get_dkydt())->remove_pseudo();
            this->kz1 = (dt_const*this->D.get_dkzdt())->remove_pseudo();
            this->x1  = (dt_const*this->D.get_dxdt())->remove_pseudo();
            this->y1  = (dt_const*this->D.get_dydt())->remove_pseudo();
            this->z1  = (dt_const*this->D.get_dzdt())->remove_pseudo();
            this->kx1 = dt_const*this->D.get_dkxdt();
            this->ky1 = dt_const*this->D.get_dkydt();
            this->kz1 = dt_const*this->D.get_dkzdt();
            this->x1  = dt_const*this->D.get_dxdt();
            this->y1  = dt_const*this->D.get_dydt();
            this->z1  = dt_const*this->D.get_dzdt();

            dispersion::dispersion_interface<DISPERSION_FUNCTION> D2(this->w,
                                                                     graph::pseudo_variable(this->kx + kx1),
@@ -523,12 +523,12 @@ namespace solver {
                                                                     graph::pseudo_variable(this->t  + dt_const),
                                                                     eq);

            this->kx2 = (dt_const*D2.get_dkxdt())->remove_pseudo();
            this->ky2 = (dt_const*D2.get_dkydt())->remove_pseudo();
            this->kz2 = (dt_const*D2.get_dkzdt())->remove_pseudo();
            this->x2  = (dt_const*D2.get_dxdt())->remove_pseudo();
            this->y2  = (dt_const*D2.get_dydt())->remove_pseudo();
            this->z2  = (dt_const*D2.get_dzdt())->remove_pseudo();
            this->kx2 = dt_const*D2.get_dkxdt();
            this->ky2 = dt_const*D2.get_dkydt();
            this->kz2 = dt_const*D2.get_dkzdt();
            this->x2  = dt_const*D2.get_dxdt();
            this->y2  = dt_const*D2.get_dydt();
            this->z2  = dt_const*D2.get_dzdt();

            auto two = graph::two<typename DISPERSION_FUNCTION::base,
                                  DISPERSION_FUNCTION::safe_math> ();
@@ -741,12 +741,12 @@ namespace solver {
                                                                     graph::pseudo_variable(this->t_sub),
                                                                     eq);

            this->kx2 = (dt*D2.get_dkxdt())->remove_pseudo();
            this->ky2 = (dt*D2.get_dkydt())->remove_pseudo();
            this->kz2 = (dt*D2.get_dkzdt())->remove_pseudo();
            this->x2  = (dt*D2.get_dxdt())->remove_pseudo();
            this->y2  = (dt*D2.get_dydt())->remove_pseudo();
            this->z2  = (dt*D2.get_dzdt())->remove_pseudo();
            this->kx2 = dt*D2.get_dkxdt();
            this->ky2 = dt*D2.get_dkydt();
            this->kz2 = dt*D2.get_dkzdt();
            this->x2  = dt*D2.get_dxdt();
            this->y2  = dt*D2.get_dydt();
            this->z2  = dt*D2.get_dzdt();

            dispersion::dispersion_interface<DISPERSION_FUNCTION> D3(this->w,
                                                                     graph::pseudo_variable(this->kx + kx2/two),
@@ -758,12 +758,12 @@ namespace solver {
                                                                     graph::pseudo_variable(this->t_sub),
                                                                     eq);

            this->kx3 = (dt*D3.get_dkxdt())->remove_pseudo();
            this->ky3 = (dt*D3.get_dkydt())->remove_pseudo();
            this->kz3 = (dt*D3.get_dkzdt())->remove_pseudo();
            this->x3  = (dt*D3.get_dxdt())->remove_pseudo();
            this->y3  = (dt*D3.get_dydt())->remove_pseudo();
            this->z3  = (dt*D3.get_dzdt())->remove_pseudo();
            this->kx3 = dt*D3.get_dkxdt();
            this->ky3 = dt*D3.get_dkydt();
            this->kz3 = dt*D3.get_dkzdt();
            this->x3  = dt*D3.get_dxdt();
            this->y3  = dt*D3.get_dydt();
            this->z3  = dt*D3.get_dzdt();

            this->t_next = this->t + dt;

@@ -777,12 +777,12 @@ namespace solver {
                                                                     graph::pseudo_variable(this->t_next),
                                                                     eq);

            this->kx4 = (dt*D4.get_dkxdt())->remove_pseudo();
            this->ky4 = (dt*D4.get_dkydt())->remove_pseudo();
            this->kz4 = (dt*D4.get_dkzdt())->remove_pseudo();
            this->x4  = (dt*D4.get_dxdt())->remove_pseudo();
            this->y4  = (dt*D4.get_dydt())->remove_pseudo();
            this->z4  = (dt*D4.get_dzdt())->remove_pseudo();
            this->kx4 = dt*D4.get_dkxdt();
            this->ky4 = dt*D4.get_dkydt();
            this->kz4 = dt*D4.get_dkzdt();
            this->x4  = dt*D4.get_dxdt();
            this->y4  = dt*D4.get_dydt();
            this->z4  = dt*D4.get_dzdt();

            auto six = graph::constant<typename DISPERSION_FUNCTION::base,
                                       DISPERSION_FUNCTION::safe_math>(static_cast<typename DISPERSION_FUNCTION::base> (6.0));
@@ -1031,9 +1031,9 @@ namespace solver {
                                                                     graph::pseudo_variable(this->t),
                                                                     eq);

            this->kx_next = (this->kx + dt_const*D2.get_dkxdt())->remove_pseudo();
            this->ky_next = (this->ky + dt_const*D2.get_dkydt())->remove_pseudo();
            this->kz_next = (this->kz + dt_const*D2.get_dkzdt())->remove_pseudo();
            this->kx_next = this->kx + dt_const*D2.get_dkxdt();
            this->ky_next = this->ky + dt_const*D2.get_dkydt();
            this->kz_next = this->kz + dt_const*D2.get_dkzdt();

            dispersion::dispersion_interface<DISPERSION_FUNCTION> D3(this->w,
                                                                     graph::pseudo_variable(this->kx_next),
@@ -1045,9 +1045,9 @@ namespace solver {
                                                                     graph::pseudo_variable(this->t),
                                                                     eq);

            this->x_next  = (this->x1 + dt_const*D3.get_dxdt()/two)->remove_pseudo();
            this->y_next  = (this->y1 + dt_const*D3.get_dydt()/two)->remove_pseudo();
            this->z_next  = (this->z1 + dt_const*D3.get_dzdt()/two)->remove_pseudo();
            this->x_next  = this->x1 + dt_const*D3.get_dxdt()/two;
            this->y_next  = this->y1 + dt_const*D3.get_dydt()/two;
            this->z_next  = this->z1 + dt_const*D3.get_dzdt()/two;
        }
    };
}
+20 −4
Original line number Diff line number Diff line
@@ -178,7 +178,12 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return sin(this->arg->remove_pseudo());
            auto temp = this->arg->remove_pseudo();
            if (temp->is_match(this->arg)) {
                return this->shared_from_this();
            } else {
                return sin(temp);
            }
        }

//------------------------------------------------------------------------------
@@ -414,7 +419,12 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return cos(this->arg->remove_pseudo());
            auto temp = this->arg->remove_pseudo();
            if (temp->is_match(this->arg)) {
                return this->shared_from_this();
            } else {
                return cos(temp);
            }
        }

//------------------------------------------------------------------------------
@@ -658,8 +668,14 @@ namespace graph {
///  @returns A tree without variable nodes.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
            return atan(this->left->remove_pseudo(),
                        this->right->remove_pseudo());
            auto l = this->left->remove_pseudo();
            auto r = this->right->remove_pseudo();
            if (l->is_match(this->left) &&
                r->is_match(this->right)) {
                return this->shared_from_this();
            } else {
                return atan(l, r);
            }
        }

//------------------------------------------------------------------------------