Skip to content

Commit e5e5fc1

Browse files
authored
Merge pull request #66 from lnls-fac/fix-tpsa-pow
Fix Tpsa pow function
2 parents 1804f30 + 675cce1 commit e5e5fc1

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

include/trackcpp/tpsa.h

+21-9
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,26 @@ Tpsa<V,N,TYPE> operator / (const T& o1, const Tpsa<V,N,TYPE>& o2) { return o2.in
486486

487487
template <unsigned int V, unsigned int N, typename TYPE>
488488
Tpsa<V,N,TYPE> pow(const Tpsa<V,N,TYPE>& a_, const int n) {
489-
Tpsa<V,N,TYPE> r;
490-
Tpsa<V,N,TYPE> x(a_); x.c[0] = 0; x /= a_.c[0];
491-
int n_ = N < n ? N : n;
492-
for(unsigned int i=0; i<=n_; i++) {
493-
r *= 1 + x;
494-
}
495-
r *= std::pow(a_.c[0], n);
496-
return r;
489+
if (n <= N) {
490+
// simple tpsa multiplication
491+
Tpsa<V,N,TYPE> r(a_);
492+
for(unsigned int i=0; i<n-1; i++) r *= a_;
493+
return r;
494+
} else {
495+
// for more efficient computation when n >~ N,
496+
// multiplication implemented as N-truncated binomial expansion a0^n*(1 + x/a0)^n, x^(N+1) = 0
497+
Tpsa<V,N,TYPE> r;
498+
Tpsa<V,N,TYPE> x(a_); x.c[0] = 0; x /= a_.c[0];
499+
Tpsa<V,N,TYPE> p(1);
500+
TYPE f = 1; // binomial coefficient
501+
for(unsigned int i=0; i<=N; i++) {
502+
r += p * f;
503+
f *= TYPE (n - i) / (i + 1);
504+
p *= x;
505+
}
506+
r *= std::pow(a_.c[0], n);
507+
return r;
508+
};
497509
}
498510

499511
template <unsigned int V, unsigned int N, typename TYPE>
@@ -507,7 +519,7 @@ Tpsa<V,N,TYPE> sqrt(const Tpsa<V,N,TYPE>& a_) {
507519
Tpsa<V,N,TYPE> r;
508520
Tpsa<V,N,TYPE> x(a_); x.c[0] = 0; x /= a_.c[0];
509521
Tpsa<V,N,TYPE> p(1);
510-
TYPE f = 1;
522+
TYPE f = 1;
511523
for(unsigned int i=0; i<=N; i++) {
512524
r += p * f;
513525
f *= (0.5 - i)/(i+1);

python_package/trackcpp/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.10.3
1+
4.10.4

0 commit comments

Comments
 (0)