0
votes

I have some equation for a type of step function which I obtained with wolfram alpha:

a_n = 1/8 (2 n+(-1)^n-(1+i) (-i)^n-(1-i) i^n+9) 

Using in wolfram with any positive integer will yield me a positive integer result however when I try the following in python

import numpy as np
n = 5
i = complex(0,1)
a = (1/8)*((2*n)+(np.power(-1,n))-(1+i)*(np.power(-i,n))-(1-i)*(np.power(i,n))+9)

I'm always stuck with some real + imaginary part. I need to be able to obtain an integer output for a for use in other equations.

2
Do you need to discard the imaginary part, or do you need the magnitude of the complex number? - John Percival Hackworth

2 Answers

0
votes

Maybe you want int(a.real) at the end.

Also be aware that by default 1/8 will be evaluated as 0 in Python 2.x

0
votes
(1+i) (-i)^n+(1-i) i^n

is two times the real part of (1-i) i^n which is, for instance

2*cos(pi/2*n)-2*cos(pi/2*(n+1))

or as values

n            0  1  2  3  4  5  6  7  8
expression   2  2 -2 -2  2  2 -2 -2  2

this is subtracted from the alternating sequence to give

n            0  1  2  3  4  5  6  7  8
(-1)^n-expr -1 -3  3  1 -1 -3  3  1 -1

periodically with period 4

This can be computed avoiding all powers and saveguarding for negative n as

3-2*(((n+2) mod 4 +4) mod 4)

adding 2n+9 to complete the expression gives

12+2*n-2*(((n+2) mod 4 +4) mod 4)

which is indeed divisible by 8, so

a = 1+(n+2-(((n+2) % 4 +4) % 4) )/4

now if one considers that this just reduces (n+2) to the next lower multiple of 4, this is equivalent to the simplified

a = 1 + (n+2)/4

using integer division.