I have a huge expression where I want to replace the variables $a,b,z$ in terms of other variables.

This is the original expression:

ExpandAll[ (Gamma[a-b+1]/Gamma[1-b])E^(z+p) Gamma[1 - a] ((-(Gamma[b - 1]/Pi)) ((-z^(1 - b)) Sin[Pi a] + (-z)^(1 - b) Sin[Pi (b - a)]) Series[Hypergeometric1F1[1 - a, 2 - b, -z],{z,0,3}] + (1/Gamma[1 + a - b]) Series[HypergeometricU[-a + b, b, -z],{z,0,3}] )]

This is my attempt.

rules = {a :> -r/D, b:> -1/g,  z:> (D*t)/g , p :> -r*t};

ExpandAll[ (Gamma[a-b+1]/Gamma[1-b])E^(z+p) Gamma[1 - a] ((-(Gamma[b - 1]/Pi)) ((-z^(1 - b)) Sin[Pi a] + (-z)^(1 - b) Sin[Pi (b - a)]) Series[Hypergeometric1F1[1 - a, 2 - b, -z],{z,0,3}] + (1/Gamma[1 + a - b]) Series[HypergeometricU[-a + b, b, -z],{z,0,3}] )]//.rules

For some reason, there is an error displayed that first argument is invalid.

Any thoughts on why this error appears?

enter image description here


closed as off-topic by Henrik Schumacher, MarcoB, Coolwater, m_goldberg, José Antonio Díaz Navas Apr 29 '18 at 9:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Henrik Schumacher, MarcoB, Coolwater, m_goldberg, José Antonio Díaz Navas
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ Don't use D it's a reserved name in Mathematica. $\endgroup$ – ulvi Apr 28 '18 at 5:08
  • 1
    $\begingroup$ Also, you may need to apply Normal[] before the /. rules substitution to get rid of the warning message. $\endgroup$ – ulvi Apr 28 '18 at 5:16
  • $\begingroup$ I tried changing $D$ to some other alphabet like $X/Y/H$ etc. Still the problem persists. $\endgroup$ – kasa Apr 28 '18 at 5:24
  • 1
    $\begingroup$ As ulvi said, also apply Normal to convert the SeriesData objects generated by Series into polynomials: Normal[ExpandAll[(Gamma[a - b + 1]/Gamma[1 - b]) E^(z + p) Gamma[ 1 - a] ((-(Gamma[b - 1]/Pi)) ((-z^(1 - b)) Sin[ Pi a] + (-z)^(1 - b) Sin[Pi (b - a)]) Series[ Hypergeometric1F1[1 - a, 2 - b, -z], {z, 0, 3}] + (1/ Gamma[1 + a - b]) Series[ HypergeometricU[-a + b, b, -z], {z, 0, 3}])]] //. rules $\endgroup$ – Henrik Schumacher Apr 28 '18 at 6:25
  • 1
    $\begingroup$ and with the help of @ulvi $\endgroup$ – kasa Apr 28 '18 at 7:16

In one of the replacement rules there is the following expression z:> (D*t)/g (there's actually no need to use :> as there doesn't seem to be anything present that requires delayed evaluation on the rhs of the rule; using -> will suffice). Now there are two problems with this rule:

  1. D is a reserved symbol used by Mathematica (try D[x^2,x]); this in itself is not detrimental, as it is possible to evaluate expressions like eg (D-2)^2 but in the present case, that is not a feature of the language that is required; like the comments indicate, it could be less error prone (or future-proof) to replace D with another symbol, preferably not used as a system function.
  2. I will abstract from the provided code for ease of exposition: evaluating an expression like Series[f[a,x],{x,b,3}]/.x->d t/g produces a message SeriesData::sdatv with a text reading "First argument $\frac{d t}{g}$ is not a valid variable".

    Let's break it down:

    • first of all, what is the Head SeriesData? Afterall, we just evaluated a Series expression. It turns out that

      "SeriesData is the head of the basic expressions generated by Series"

    • why then is it not possible for SeriesData to have a first argument like d t/g? Well, that's how it works. Notice how

      "Most expressions other than numbers, sums, products, powers, inequalities, and logical expressions can be used as variables."

    In the present case, using the replacement rule x->d t/g replaces x in the resulting SeriesData[x,...] expression with dt/g which is in fact a product involving a Power expression (evaluate FullForm[d t/g]).

In order to resolve the problem we need to supply SeriesData with an expression as a first argument that is acceptable or we can turn the SeriesData into a Normal expression and then ReplaceAll the rules.

A way for doing the former is Normal[Series[f[a,x],{x,b,3}]/.x->head[d t/g]]/.head->Identity while the later can be achieved with Normal[Series[f[a,x],{x,b,3}]]/.x->d t/g. Lastly applying ExpandAll to either of those expression will produce the expected result.


Not the answer you're looking for? Browse other questions tagged or ask your own question.