# All the power series functions could share a base class

I was looking at the hypergeometric functions and related power-series calculations and I feel like there’s a common theme that could get broken out so I’m throwing this down here to get suggestions about whether there’s a design that makes sense. I will likely not get to it until after the survival-model related math functions are accurate and tested (a few weeks).

The motivation is that @thel suggested in the hypergeometric function PR to break out the “exp-log-sum-with-signs logic” into its own inline function but I can’t see how given that the logic needs to be state-aware over iterations (there’s a bool tracking the sign and it needs to be flipped when a negative term is multiplied by the product-so-far term. Looking at the other power-series based functions they all:

1. have known or readily derived convergence conditions based on arguments
2. set some initial values for accumulators
3. maintain state in a product-so-far term
4. maintain state in a sum-so-far term
5. use or could use exp-log-sum-with-signs logic
6. test for convergence based on the product-so-far term going to zero
7. could rely on standard acceleration techniques for power series

There is a separate issue where these functions have optimizations or continuations that are relevant for portions of the range but my thought is to break those out into a function that just deals with branching to the right implementation and then write separate functions or functors for the implementations. The piece above just deals with power-series based implementations.

How many are there? I’m all for pulling out shared code. The next step is to say what the implementation would look like. Some kind of base functor with inheritance of a virtual function to fill in the inner loop pieces?

No – as we go through and try to clean up these implementations we’ll very quickly see that we need bespoke power series for which there will be minimal overlap in code. In particular, there are no “standard acceleration techniques” and convergence tests are more subtle than just checking the last term in the expansion.