Skip to content

Commit

Permalink
implement more power functions
Browse files Browse the repository at this point in the history
to avoid std::pow
  • Loading branch information
Alexander Voigt authored and Alexander Voigt committed Sep 20, 2017
1 parent 8d6763c commit 5b62087
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
17 changes: 16 additions & 1 deletion meta/CConversion.m
Expand Up @@ -153,7 +153,8 @@
https://stackoverflow.com/questions/3136604/evaluate-beyond-one-level-within-hold-in-mathematica
";

{ Sqr, Cube, Quad, Power2, Power3, Power4, Power5, Power6, Power7, Power8 };
{ Sqr, Cube, Quad, Cbrt, Power2, Power3, Power4, Power5, Power6,
Power7, Power8, Power9, Power10, Power11, Power12 };

Begin["`Private`"];

Expand Down Expand Up @@ -684,6 +685,12 @@
Unprotect[Power];
Format[Power[E,z_],CForm] :=
Format["Exp(" <> ToString[CForm[z]] <> ")", OutputForm];
Format[Power[b_,2],CForm] :=
Format["Sqr(" <> ToString[CForm[b]] <> ")", OutputForm];
Format[Power[b_,0.5 | 1/2],CForm] :=
Format["Sqrt(" <> ToString[CForm[b]] <> ")", OutputForm];
Format[Power[b_,1/3],CForm] :=
Format["Cbrt(" <> ToString[CForm[b]] <> ")", OutputForm];
Protect[Power];

Unprotect[If];
Expand Down Expand Up @@ -884,6 +891,14 @@
Power[a_,-7] :> 1/Power7[a] /.
Power[a_,8] :> Power8[a] /.
Power[a_,-8] :> 1/Power8[a] /.
Power[a_,9] :> Power9[a] /.
Power[a_,-9] :> 1/Power9[a] /.
Power[a_,10] :> Power10[a] /.
Power[a_,-10] :> 1/Power10[a] /.
Power[a_,11] :> Power11[a] /.
Power[a_,-11] :> 1/Power11[a] /.
Power[a_,12] :> Power12[a] /.
Power[a_,-12] :> 1/Power12[a] /.
Sqrt[x_]/Sqrt[y_] :> Sqrt[x/y];
result = Apply[Function[code, Hold[CForm[code]], HoldAll],
Hold[#] &[result /. { (p:(Dot | SARAH`MatMul))[a__] :> times @@ p[a],
Expand Down
42 changes: 37 additions & 5 deletions src/wrappers.hpp
Expand Up @@ -522,19 +522,19 @@ Base Power(Base base, Exponent exp) noexcept
template <typename Base>
constexpr Base Power2(Base b) noexcept
{
return Sqr(b);
return b * b;
}

template <typename Base>
constexpr Base Power3(Base b) noexcept
{
return Cube(b);
return b * b * b;
}

template <typename Base>
constexpr Base Power4(Base b) noexcept
{
return Quad(b);
return b * b * b * b;
}

template <typename Base>
Expand All @@ -552,13 +552,45 @@ constexpr Base Power6(Base b) noexcept
template <typename Base>
constexpr Base Power7(Base b) noexcept
{
return b * b * b * b * b * b * b;
return b * b * b * b * b *
b * b;
}

template <typename Base>
constexpr Base Power8(Base b) noexcept
{
return b * b * b * b * b * b * b * b;
return b * b * b * b * b *
b * b * b;
}

template <typename Base>
constexpr Base Power9(Base b) noexcept
{
return b * b * b * b * b *
b * b * b * b;
}

template <typename Base>
constexpr Base Power10(Base b) noexcept
{
return b * b * b * b * b *
b * b * b * b * b;
}

template <typename Base>
constexpr Base Power11(Base b) noexcept
{
return b * b * b * b * b *
b * b * b * b * b *
b;
}

template <typename Base>
constexpr Base Power12(Base b) noexcept
{
return b * b * b * b * b *
b * b * b * b * b *
b * b;
}

inline constexpr double Re(double x) noexcept
Expand Down

0 comments on commit 5b62087

Please sign in to comment.