Implement Power function

This commit is contained in:
Aik-Siong Koh
2025-08-07 20:37:33 -06:00
committed by Chris Hennes
parent 09d6175a2b
commit 1a8fdc32d3
27 changed files with 220 additions and 44 deletions

View File

@@ -5,7 +5,7 @@
* *
* See LICENSE file for details about copyright. *
***************************************************************************/
#include "Power.h"
#include "Constant.h"
#include "Ln.h"
@@ -35,13 +35,36 @@ Symsptr MbD::Power::differentiateWRTy()
return deriv->simplified();
}
Symsptr MbD::Power::simplifyUntil(Symsptr, std::shared_ptr<std::unordered_set<Symsptr>>)
Symsptr MbD::Power::copyWith(Symsptr argx, Symsptr argy)
{
assert(false);
return Symsptr();
return std::make_shared<Power>(argx, argy);
}
double MbD::Power::getValue()
{
return std::pow(x->getValue(), y->getValue());
}
Symsptr MbD::Power::clonesptr()
{
return std::make_shared<Power>(*this);
}
Symsptr MbD::Power::simplifyUntil(Symsptr sptr, std::shared_ptr<std::unordered_set<Symsptr>> set)
{
auto itr = std::find_if(set->begin(), set->end(), [sptr](Symsptr sym) {return sptr.get() == sym.get(); });
if (itr != set->end()) return sptr;
auto newx = x->simplifyUntil(x, set);
auto newy = y->simplifyUntil(y, set);
if (y->isConstant() && y->getValue() == 1) {
return newx;
}
auto copy = copyWith(newx, newy);
return copy;
}
std::ostream& MbD::Power::printOn(std::ostream& s) const
{
s << "pow(" << *x << "," << *y << ")";
return s;
}