Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have many outputs of one-dimensional Series expansions for which I am only interested in the general tending with the variable.

For example, I would like to be able to transform something like

3.4235 + (4.22-5.2342 I) a + 7.543 a^6 + O[a]^7

into just

1 + a + a^6 + O[a]^7


I have tried many things, including trying to collect the SeriesCoefficients and reassign them to 1, but I haven't been able to make it work yet.

Here's an example of an attempt that isn't yet working (note, if it did work as expected, I would still need to add a simple If[] to exclude assigning SeriesCoefficients or Chop@SeriesCoefficients that are 0 to 1).

 clear[test]; test = Series[1/(1 - b x), {x, 0, 4}];
 Do[test = ReplaceAll[test, SeriesCoefficient[test, s] -> 3], {s, 0, 4}];

Probably a much more efficient solution to this problem would be a more general rule that could be applied to the final output that simply checks the expression for "numbers" and makes them all 1. Surely there is a way to get mathematica to recognize numbers compared to symbols?

share|improve this question
up vote 6 down vote accepted

Rule suggestion:

3.4235 + (4.22 - 5.2342 I) a + 7.543 a^6 + O[a]^7 /. _Rational|_Real|_Complex -> 1

(* Out: 1+a+a^6+O[a]^7 *)

This simple rule unfortunately cannot easily be extended to handle integer coefficients. However, Mr. Wizard's solution does that.

share|improve this answer
Beautiful, this is exactly what I was looking for. – Steve Jan 27 '14 at 21:48
Look out for Integer coefficients. ;) – Kuba Jan 27 '14 at 21:50
And rationals... – ciao Jan 27 '14 at 21:55
@Pickett: cleaner perhaps: Replace[3/4 + (4.22 - 5.2342 I) a + 7.543 a^6 + O[a]^7, _?NumberQ -> 1, {2}] - NVM - this does not do it. – ciao Jan 27 '14 at 21:59
@Mr.Wizard Very true, it seems it will take some work to include Integer due to it being used in the hidden representation of series data. I removed Integer in the meantime. rasher: I think beginners like rules the way I wrote it more. – C. E. Jan 27 '14 at 22:02

Let's look at the InputForm of your expression:

expr = 3.4235 + (4.22 - 5.2342 I) a + 7.543 a^6 + O[a]^7;
SeriesData[a, 0, {3.4235, 4.22 - 5.2342*I, 0, 0, 0, 0, 7.543}, 0, 7, 1]

You want to set all the coefficients to one, so let's do that with Unitize:

MapAt[Unitize, expr, 3]
1 + a + a^6 + O[a]^7

Or, if you prefer, as a Rule:

expr /. c_List :> Unitize[c]
1 + a + a^6 + O[a]^7
share|improve this answer
Beat me to it ;-) – ciao Jan 27 '14 at 22:11
@rasher I need to still win a few of those races or I'll feel old. :o) – Mr.Wizard Jan 27 '14 at 22:14
Your rule method is not working for me in the general context, but the /. _Rational|_Real|_Complex -> 1 rule from above is. Perhaps there is more at work, but just letting people know. The Pickett solution will hit everything but integer coefficients, I suppose. For me, exact integer coefficients are absurdly rare, but for other problems that might be a concern. – Steve Jan 28 '14 at 17:44
@Steve Please give me an example where it fails. Also, if it is only the Rule method is there any reason not to use MapAt? I feel the latter is a distinctly superior method and I am prepared to argue that case. – Mr.Wizard Jan 28 '14 at 23:50
Hmm, I'm not sure I can provide a simple example where it fails, but in my research problem neither the MapAt or Unitize rules seem to work when implemented in the same way as the other rule. – Steve Jan 30 '14 at 17:33

I like the solutions offered above. This my answer is only in order to have one more solution, though it is not as good as those above. If the O[a]^7 term is not dear to you, there is also such a way:

expr = 3.4235 + (4.22 - 5.2342 I) a + 7.543 a^6 + O[a]^7;

         CoefficientRules[expr // Normal] /. ({u_} -> v_) -> ({u} -> 1), a]

    (*   1 + a + a^6  *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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