Python Forum
Translate some code form C++ (complex vector)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translate some code form C++ (complex vector)
#1
Hi, I'm try to trnslate some code form c++ with complex numbers stack in vector. In C++ I've

vector<complex<double>> test(vector<complex<double>> T)
{
    int N = T.size();

    complex<double> S;
    vector<complex<double>> out;

    for (int k=0; k<K; k++)
    {
        ...
        }
        output.push_back(S);
    }
    return out;
}
I did in Python:
T = np.array([])
test = T = complex(0,0)
N = 1000
S = complex(0,0)
k = 0
n = 0

for k in range(k, 10):
    for n in range(n, 10):
       ...
        S += T[n] * w
And I've an error
Error:
'complex' object is not subscriptable
Does anyone will help me here?
Reply
#2
First you bind T as empty numpy array, but then you do test = T = complex(0,0). Both test and T are now bind to object complex(0, 0), i.e. single value.

>>> test = T = complex(0, 0)
>>> test
0j
>>> T
0j
>>> type(T)
<class 'complex'>
>>>
What exactly do you want to achieve - explain in plain words, not with C++ code, i.e. one-to-one translation is not always the best approach.
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
In big shortcut, I wanna to insert numbers from for loop to empty complex array. When I do '.insert' or '.append' thats show me an error
Reply
#4
(Jan-05-2021, 01:25 PM)CarnariusXx Wrote: In big shortcut, I wanna to insert numbers from for loop to empty complex array. When I do '.insert' or '.append' thats show me an error
Sorry, it's unclear. In your code you never try to insert anything in the array. Do you try to initialize teh array with some values before trying to access some element on this line S += T[n] * w - here you try to access element at index n, but we never see when/how you insert anything in the array. Also, we never see where/how you use k from the loop, nor where w comes from.
If you have some code that produce error, other than the one in the first post - post it, incl. the full traceback you get. Also, as I stated - replicating one-to-one, i.e. with loops and so on MAY not be the best approach in python.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Sorry for the code sample, I was in a hurry and I was wrong. This is the code where I tried to insert complex values.

N = 1000
K = N
S = complex(0,0)
k = 0
n = 0

for k in range(k, K):
    for n in range(n, N):
        realPart = np.cos(((2*pi)/N)*k*n)
        imagPart = np.sin(((2*pi)/N)*k*n)
        w = complex(realPart, -imagPart)
        S.insert(w)
And this is an error
Error:
S.insert(w) AttributeError: 'complex' object has no attribute 'insert'
When I did
S += X[n]*w
Appears that error
Error:
S += X[n]*w TypeError: 'complex' object is not subscriptable
Reply
#6
Several alternatives

make a list and then convert it to arary:
from itertools import product
import numpy as np

k = n = 0
K = N = 5 # you can change back to 1000
my_list = []
 
for k, n in product(range(k, K), range(n, N)):
    real_part = np.cos(((2 * np.pi) / N) * k * n)
    imag_part = np.sin(((2 * np.pi) / N) * k * n)
    my_list.append(np.complex(real_part, -imag_part))

my_array = np.array(my_list)

print(my_array)
work directly with np.array:
from itertools import product
import numpy as np

k = n = 0
K = N = 5 # you can change back to 1000
my_array = np.empty((K * N), dtype=np.complex)
 
for idx, (k, n) in enumerate(product(range(k, K), range(n, N))):
    real_part = np.cos(((2 * np.pi) / N) * k * n)
    imag_part = np.sin(((2 * np.pi) / N) * k * n)
    my_array[idx] = np.complex(real_part, -imag_part)

print(my_array)
make function that returns complex number and use list comprehension
from itertools import product
import numpy as np


def cmplx(k, n):
    real_part = np.cos(((2 * np.pi) / N) * k * n)
    imag_part = np.sin(((2 * np.pi) / N) * k * n)
    return np.complex(real_part, -imag_part)

k = n = 0
K = N = 5 # you can change back to 1000
my_array = np.array([cmplx(k, n) for k, n in product(range(k, K), range(n, N))])
print(my_array)
make a generator and use np.fromiter() to create the array

from itertools import product
import numpy as np


def cmplx(k, K, n, N):
    for k, n in product(range(k, K), range(n, N)):
        real_part = np.cos(((2 * np.pi) / N) * k * n)
        imag_part = np.sin(((2 * np.pi) / N) * k * n)
        yield np.complex(real_part, -imag_part)

k = n = 0
K = N = 5 # you can change back to 1000
my_array = np.fromiter(cmplx(k, K, n, N), dtype=np.complex)

print(my_array)
now, because n=k and K=N, you can pass repeat to itertools.product(). Basically in all example you can replace product(range(k, K), range(n, N)) with product(range(start, end), repeat=2)


from itertools import product
import numpy as np


def cmplx(start, end):
    N = end
    for k, n in product(range(start, end), repeat=2):
        real_part = np.cos(((2 * np.pi) / N) * k * n)
        imag_part = np.sin(((2 * np.pi) / N) * k * n)
        yield np.complex(real_part, -imag_part)

start = 0
end = 5 # you can change back to 1000
my_array = np.fromiter(cmplx(start, end), dtype=np.complex)

print(my_array)
all of the above will produce

Output:
[ 1. -0.j 1. -0.j 1. -0.j 1. -0.j 1. -0.j 1. -0.j 0.30901699-0.95105652j -0.80901699-0.58778525j -0.80901699+0.58778525j 0.30901699+0.95105652j 1. -0.j -0.80901699-0.58778525j 0.30901699+0.95105652j 0.30901699-0.95105652j -0.80901699+0.58778525j 1. -0.j -0.80901699+0.58778525j 0.30901699-0.95105652j 0.30901699+0.95105652j -0.80901699-0.58778525j 1. -0.j 0.30901699+0.95105652j -0.80901699+0.58778525j -0.80901699-0.58778525j 0.30901699-0.95105652j]
Others may suggest yet another/even better ways to do it.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thank you, that's great! But unfortunately I think I can do with this part of your code, but I was wrong. Can you try help me translate this into Python? This is the Fourier transform. I know it's one line in numpy, but I need code very similar to this in Python. I know C ++ well, but Python is new to me and I don't have time to get to know it as much as I would like.
There is full code
https://pastebin.com/Z31DdPp7bq
Reply
#8
No, I don't know C++. But even if I did - this is not how things work here on the forum. We are glad to help, but we are not going to do your work for you. You need to make attempt yourself, post your code in python tags, any error - in error tags, ask specific questions.
And by the way - Data Science is better suited sub-forum, so I will move this thread there.
Also, google is your friend - https://www.google.com/search?q=Fourier+...orm+python
Note that scientific packages and their support of Fourier transform
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
I understand. It's not for work, it's out of curiosity.
Reply
#10
(Jan-06-2021, 01:09 PM)CarnariusXx Wrote: It's not for work
"work" as in "something you need to do yourself", not "something you need to do for your employer"
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a function vector Cooketaker 4 1,007 Dec-27-2022, 08:22 PM
Last Post: Cooketaker
Photo How do you translate this in Python? pythonflea 5 2,686 Jan-24-2021, 07:58 PM
Last Post: Serafim
  How do I translate this into python code? (excel vlookup) eeps24 6 2,835 May-29-2020, 05:54 PM
Last Post: eeps24
  matrix by vector mcgrim 8 3,894 May-02-2019, 10:39 AM
Last Post: ichabod801
  Vector field cross product Eduard 2 2,581 Aug-20-2018, 02:54 PM
Last Post: Eduard

Forum Jump:

User Panel Messages

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