Skip to content

Commit 40297ec

Browse files
Added integer POW routines
1 parent bbb2483 commit 40297ec

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

include/integratorxx/quadratures/radial/lmg.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#include <integratorxx/util/lambert_w.hpp>
22
#include <integratorxx/util/gamma.hpp>
3+
#include <integratorxx/util/pow.hpp>
34
#include <integratorxx/quadratures/radial/radial_transform.hpp>
45

56
namespace IntegratorXX {
67
namespace lmg {
78

89
// Eq 19 of LMG paper (DOI 10.1007/s002140100263)
910
inline double r_upper_obj(int m, double alpha, double r) {
10-
const double am_term = (m + 1.0) / 2.0;
11-
//const double g_term = std::tgamma((m + 3.0) / 2.0);
1211
const double g_term = half_integer_tgamma<double>(m + 3);
1312
const double x = alpha * r * r;
14-
return g_term * std::pow(x, am_term) * std::exp(-x);
13+
return g_term * half_integer_pow(x, m+1) * std::exp(-x);
1514
}
1615

1716
// Solve Eq 19 of LMG paper (DOI 10.1007/s002140100263)
@@ -21,7 +20,6 @@ inline double r_upper(int m, double alpha, double prec) {
2120
// X = -L * LAMBERT_W( - (P/G)^(1/L) / L )
2221
// R = SQRT(X / ALPHA)
2322
const double am_term = (m + 1.0) / 2.0;
24-
//const double g_term = std::tgamma((m + 3.0) / 2.0);
2523
const double g_term = half_integer_tgamma<double>(m + 3);
2624
const double arg = std::pow(prec/g_term, 1.0 / am_term) / am_term;
2725
const double wval = lambert_wm1(-arg); // W_(-1) is the larger value here

include/integratorxx/util/pow.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include <cmath>
3+
#include <integratorxx/util/type_traits.hpp>
4+
5+
namespace IntegratorXX {
6+
7+
template <typename FloatType, typename IntegralType>
8+
FloatType integer_pow(FloatType x, IntegralType n) {
9+
if(n == 0) return 1;
10+
if(n == 1) return x;
11+
FloatType v = x;
12+
for(IntegralType i = 1; i < n; ++i) v *= x;
13+
return v;
14+
}
15+
16+
template <typename FloatType, typename IntegralType>
17+
FloatType half_integer_pow(FloatType x, IntegralType n) {
18+
assert(n >= 0);
19+
if(n%2 == 0) return integer_pow(x, n/2);
20+
else return std::sqrt(x) * integer_pow(x, n/2);
21+
}
22+
23+
}

test/util.cxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "catch2/catch_all.hpp"
22
#include <integratorxx/util/factorial.hpp>
33
#include <integratorxx/util/gamma.hpp>
4+
#include <integratorxx/util/pow.hpp>
45
#include <integratorxx/util/pow_2.hpp>
56
#include <iostream>
67

@@ -34,6 +35,17 @@ TEST_CASE("Factorial", "[util]") {
3435
}
3536
}
3637

38+
TEST_CASE("Pow", "[util]") {
39+
40+
using namespace IntegratorXX;
41+
using namespace Catch::Matchers;
42+
for(int i = 0; i < 10; ++i) {
43+
REQUIRE_THAT(integer_pow(2.0, i), WithinAbs(std::pow(2.0,i), 1e-16));
44+
REQUIRE_THAT(half_integer_pow(2.0, i), WithinAbs(std::pow(2.0,i/2.0), 1e-16));
45+
}
46+
47+
}
48+
3749
TEST_CASE("Pow 2", "[util]") {
3850
using namespace IntegratorXX;
3951

0 commit comments

Comments
 (0)