Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Update

OK, Please forgive the messiness of this, but I am working with something like:

d = (q = 8;
f = (lop = 
  Transpose[{Flatten[
     Reverse[Table[
        Gamma[y], {y, 1, q + 1}]] /. {ComplexInfinity -> 0}], 
    Flatten[Table[Binomial[n, k], {n, q, q}, {k, 0, n}]], 
    Flatten[Table[x^n, {n, 0, q}]]}];
 {#1*#2*#3} & @@@ lop);
Flatten[f];
Total[f]);
d

which gives:

{40320 + 40320 x + 20160 x^2 + 6720 x^3 + 1680 x^4 + 336 x^5 + 56 x^6 + 8 x^7 + x^8}

I am then comparing the roots with another series, eg:

Normal[Series[E^x, {x, 0, 8}]]

which, in this case are the same.

However, I have lots of different series & was wondering if I could manipulate them as stated below.

(NB - I realise there is no 'ComplexInfinity' output in the above example, but have left it in to demonstrate that I have tried several replace techniques for the subsequent questions.)

Original question

I have several questions on list manipulation & at the risk of asking the same question multiple times, I have compiled them into the following single question:

(a) Changing alternate (or some other pattern) signs / operators:

From this:

1 + x + (x^2)/2! +  (x^3)/3! + (x^4)/4! +  (x^5)/5! ...

I would like to do get this:

1 - x + (x^2)/2! -  (x^3)/3! + (x^4)/4! -  (x^5)/5! ...

(b) Multiplying alternate (or some other pattern) elements by a given value (eg - I):

From this:

1 + x + (x^2)/2! +  (x^3)/3! + (x^4)/4! +  (x^5)/5! ...

I would like to do get this:

1 - I x + (x^2)/2! -  I(x^3)/3! + (x^4)/4! -  I(x^5)/5! ...

(c) Convert all Imaginary values in a list to real values (or vice versa), noting that there may be no pattern in the distribution here:

From this:

1 + I x + (x^2)/2! +  (x^3)/3! + I(x^4)/4! +  I(x^5)/5! ...

I would like to do get this:

1 + x + (x^2)/2! +  (x^3)/3! + (x^4)/4! +  (x^5)/5! ...

or this:

I + I x + I(x^2)/2! +  I(x^3)/3! + I (x^4)/4! +  I(x^5)/5! ...

I have tried various select & replace methods, but have had no luck so far.

share|improve this question
    
Maybe related: mathematica.stackexchange.com/q/30907/131 –  Yves Klett Nov 6 '13 at 10:24

1 Answer 1

up vote 5 down vote accepted

Well you can begin by wrapping your expression in Hold or HoldForm then using ReleaseHold when you want to Evaluate them. For your first example:

expr = HoldForm[1 + x + (x^2)/2! + (x^3)/3! + (x^4)/4! + (x^5)/5!]

Now you can do:

Replace[expr /. {Times[c : Power[x, n_?OddQ], d_] :> Times[-1, c, d]}, x -> - x, 2]

To get:

1 - x + x^2/2! - x^3/3! + x^4/4! - x^5/5!

For the next one:

Replace[expr /. {Times[c : Power[x, n_?OddQ], d_] :> Times[-I, c, d]}, x -> - I x, 2]

Which gives:

1 - I x + x^2/2! - (I x^3)/3! + x^4/4! - (I x^5)/5!

For the third:

expr2 = 1 + I x + (x^2)/2! + (x^3)/3! + I (x^4)/4! + I (x^5)/5!;

To change the imaginary numbers do:

expr2 /. Complex[0, c_] :> c

To change negatives to positives in e.g.

expr3 = -1 - x + (x^2)/2! - (x^3)/3! + (x^4)/4! - (x^5)/5!

Simply do:

expr3 /. {Times[-1, c_] :> Times[1, c], Rational[-1, d_] :> Rational[1, d], n_?Negative :> -1 n}

To get:

1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120
share|improve this answer
    
@ RunnyKine,Great, this works well for alternating series, but I was wondering if I could select all negative values & change them, or all imaginary values, etc. –  martin Nov 6 '13 at 10:21
    
@ RunnyKine, Many thanks. - Is it possible to change the negative values to positive? (Would I use an 'If' statement?) –  martin Nov 6 '13 at 10:38
1  
@martin, yes you can change negatives to positives. I'll add that now. –  RunnyKine Nov 6 '13 at 10:39
    
@ RunnyKine, Many thanks :-) –  martin Nov 6 '13 at 15:50
    
@martin. Glad I could help. –  RunnyKine Nov 6 '13 at 15:56

Your Answer

 
discard

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.