Python Forum
loops in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: loops in python (/thread-5136.html)



loops in python - nuncio - Sep-20-2017

This seems to be rather simple, but I am not getting there.  I am trying to execute the math for Fourier transform.  Here is the code I wrote

import numpy as np
x=[1,2,3,4] # my data values
fn=np.arange(0,len(x),dtype=np.float)
for k in fn:
        for xi in x:
            print(k)
            print(xi)
            xx=np.sum(xi*(np.exp(-1j*2.0*3.14*fn/len(x))))
            print(xx)
What I need id the sum for the x1,x2,x3,x4 when fn = 0
                                                 x1,x2,x3,x4 when fn=1
                                                 --------------
                                                  x1,x2,x3,x4 when fn=3

what I get instead is the sum as follows
Appreciate any suggestions
Output:
nuncio  0.0 1 (-0.00159138312909-0.0015951894606j) 0.0 2 (-0.00318276625818-0.00319037892121j) 0.0 3 (-0.00477414938726-0.00478556838181j) 0.0 4 (-0.00636553251635-0.00638075784241j) 1.0 1 (-0.00159138312909-0.0015951894606j) 1.0 2 (-0.00318276625818-0.00319037892121j) 1.0 3 (-0.00477414938726-0.00478556838181j) 1.0 4 (-0.00636553251635-0.00638075784241j) 2.0 1 (-0.00159138312909-0.0015951894606j) 2.0 2 (-0.00318276625818-0.00319037892121j) 2.0 3 (-0.00477414938726-0.00478556838181j) 2.0 4 (-0.00636553251635-0.00638075784241j) 3.0 1 (-0.00159138312909-0.0015951894606j) 3.0 2 (-0.00318276625818-0.00319037892121j) 3.0 3 (-0.00477414938726-0.00478556838181j) 3.0 4 (-0.00636553251635-0.00638075784241j)



RE: loops in python - gnanasekar - Sep-21-2017

In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement −

[Image: loop_architecture.jpg]


RE: loops in python - Sagar - Sep-21-2017

You have write second for loop inside sum()