I have a function which has a smooth non-analytic term, a simplified version of which is
$$
f(\lambda) = \frac{1}{1 + k \lambda + e^{-1/\lambda}}.
$$
I want a series expansion of this, in Mathematica, in the sense that the non-analytic term is simply discarded, i.e. in this example the first order result I want would be
$$
f(\lambda) = 1 - k \lambda + \mathcal{O}(\lambda^2).
$$
However, if I try to use Series
as in
Series[1/(1 + k λ + Exp[-1/λ]), {λ, 0, 1}]
I get $$ \frac{e^{1/\lambda}}{1 + e^{1/\lambda}(1 + k\lambda + \mathcal{O}(\lambda^2))}. $$
Is there a way to get what I want? Notes:
- the above is just a simple example, in reality I need to be able to do this for a reasonably general set of functions.
- the non-analyticity is, at least for now, limited to the form $a(\lambda) e^{-b /\lambda}$, where $a$ and $b$ are possibly complicated expressions of various factors, and $a$ is an analytic function of $\lambda$.
- I will further want to specifically extract the coefficients of the series, i.e. $k$ in this example.
- a next step in the computation may also numerically set $\lambda = 0$, so there should be no $1/\lambda$ left in any of the subexpressions.
Update:
The above is just a simplified example, and it seems to be misleading. So let me give a brief overview of what I want to do with this, to clarify the constraints and why it's not feasible to just delete the problematic terms:
The larger goal here is to numerically solve a system of ODE's, where the function $f(\lambda)$ (actually, a set of four functions) appears as a potential. Further, the boundary conditions and further processing of the results depends on the first few coefficients of the Taylor series of $f(\lambda)$. In general, $f(\lambda)$ does not need to be (and is not) a polynomial in $\lambda$.
Note that when I write Taylor series above, I really mean the Taylor series, even if it does not converge to the original function. This is of course at the heart of the problem, since the Taylor series of the example above, when constructed by differentiation, is exactly what I need. However, Mathematica is too smart here, and gives me a series which keeps the non-analytic term. When actually solving the system of ODE's, the non-analytic part must be kept though, since the potentials will be evaluated at large $\lambda$.
Why I cannot simply manually remove these terms when expanding: solving this problem has been encapsulated in a package, which in principle takes a black-box set of potentials $f(\lambda)$, extracts the necessary coefficients, numerically solves the ODE's, and constructs the final observables we want to look at from those results. Currently we are actually tuning the potentials to get certain results, so it is important that trying out a potential does not need manual intervention.
Even further, this package is used also by other people than me, so it is preferable that I won't add any hidden features which might trap an unwary user: for example, if I silently remove exponentials by /. Exp[__/λ] -> 0
before expanding, and the user enters a potential which contains for example $e^{-(a\lambda^2 + b \lambda^3)/\lambda}$ (which will give zero with the above replacement), I will not only produce the wrong result, but I will do so without producing an obvious error! While this example is maybe a bit contrived, and could be solved by Simplify
ing before the replacement, I'm afraid there could be a more realistic example whose existence I'll realize only after the damage is done.
So, the various classes of solutions I can think of, in order of preference:
- extract the coefficients of the Taylor series exactly according to the definition, i.e. smooth non-analytic parts get discarded. The more general the set of functions $f(\lambda)$ and forms of non-analyticity this works for, the better.
- Come up with a replacement rule or some other manipulation applied to the potentials to remove specific non-analyticies, such as $e^{-a/\lambda}$ (where $a$ is a positive real number) in a way which is reasonably robust against accidentally removing needed terms. Bonus if there's a way to detect suspect cases so I can give a warning to the user.
- Simply let the user enter a separate analytic form for the potential, i.e. have them manually do this work. This I can of course do immediately, but it makes the package more laborious to use.
Having written the above, I'm starting to lean toward just implementing option 1. directly:
taylorSeriesCoefficient[expr_, {x_, x0_, ord_}] := Limit[1/Factorial[ord] D[expr, {x, ord}], x -> x0]
but I'm still open for other suggestions?
Limit[Series[expr /. Exp[_] :> 0, {\[Lambda], a, 2}], a -> 0]
. Off the top of my head, I'm not sure why that worked, other than that we're avoiding the bad pole; Id' have to think about it for a while to figure out whether this works in general or not, which is why I'm not yet making it an answer. $\endgroup$Limit[Series[expr, {\[Lambda], a, 2}], a -> 0]
$\endgroup$