Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loops in python
#1
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)
Reply
#2
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]
Reply
#3
You have write second for loop inside sum()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020