Translate some code form C++ (complex vector) - 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: Translate some code form C++ (complex vector) (/thread-31824.html) Pages:
1
2
|
Translate some code form C++ (complex vector) - CarnariusXx - Jan-05-2021 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] * wAnd I've an error Does anyone will help me here?
RE: Translate some code form C++ (complex vector) - buran - Jan-05-2021 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. RE: Translate some code form C++ (complex vector) - CarnariusXx - Jan-05-2021 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 RE: Translate some code form C++ (complex vector) - buran - Jan-05-2021 (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 errorSorry, 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. RE: Translate some code form C++ (complex vector) - CarnariusXx - Jan-05-2021 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 When I did S += X[n]*wAppears that error
RE: Translate some code form C++ (complex vector) - buran - Jan-05-2021 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 Others may suggest yet another/even better ways to do it.
RE: Translate some code form C++ (complex vector) - CarnariusXx - Jan-05-2021 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 RE: Translate some code form C++ (complex vector) - buran - Jan-05-2021 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+transform+python Note that scientific packages and their support of Fourier transform RE: Translate some code form C++ (complex vector) - CarnariusXx - Jan-06-2021 I understand. It's not for work, it's out of curiosity. RE: Translate some code form C++ (complex vector) - buran - Jan-06-2021 (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" |