complex-numbers

a function returning reference to real or imag values of a complex number in C++11

心不动则不痛 提交于 2019-12-07 10:06:03
问题 I'm looking for a function that returns a reference to real or imag values of a complex number in C++11. In C++03 I could say: complex<double> C; cin >> C.real(); But in C++11 that gives me a compile error since the C.real() returns a value not a reference. I found out that I can write this: double t; cin >> t; C.real(t); but it isn't straightforward and for example if I want to multiply the real part of c by 2 and ad it by 1 I should say: C.real(2*C.real() + 1); That is not clean. Is there

c++ display complex number with i in imaginary part

匆匆过客 提交于 2019-12-07 09:56:30
I am doing something like: int real_part, imaginary_part; cout<<"Please enter realpart and imaginary part"<<endl; cin>>real_part; cin>>imaginary_part; complex<double> mycomplex (real_part, imaginary_part); cout<<mycomplex<<endl; // I want to display like -2+5i but I get (-2, 5) I am very new to c++ How can I display with i like -2+5i ? Or I have to add i char with imagginary part ? You can use std::real() and std::imag() to format as you like, see complex here . Of course, you will have to check for sign yourself. Something like this: std::cout << std::real(mycomplex) << (std::imag(mycomplex)

Computing e^(-j) in C

≡放荡痞女 提交于 2019-12-07 07:06:49
问题 I need to compute imaginary exponential in C. As far as I know, there is no complex number library in C. It is possible to get e^x with exp(x) of math.h , but how can I compute the value of e^(-i) , where i = sqrt(-1) ? 回答1: Note that exponent of complex number equals: e^(ix) = cos(x)+i*sin(x) Then: e^(-i) = cos(-1)+i*sin(-1) 回答2: In C99, there is a complex type. Include complex.h ; you may need to link with -lm on gcc. Note that Microsoft Visual C does not support complex ; if you need to

Complex type with C linkage in C++11

╄→гoц情女王★ 提交于 2019-12-07 06:11:45
问题 I need to include the header of a C library into my C++11 code. Now, the header provides routines and data structures that involve plenty of double complex all over the place. E.g., #include <complex.h> //.. typedef struct parameters { // ... double complex Vud; } parameters; // ... double complex polylog(int n, int m, double x); I bring this file into my C++11 source wrapped in extern "C" { #include "include.h" } (that's the actual filename, believe it or not). And g++ (tried 4.7.3 and 4.8.2

More on using i and j as variables in Matlab: speed

妖精的绣舞 提交于 2019-12-06 19:47:56
问题 The Matlab documentation says that For speed and improved robustness, you can replace complex i and j by 1i. For example, instead of using a = i; use a = 1i; The robustness part is clear, as there might be variables called i or j. However, as for speed , I have made a simple test in Matlab 2010b and I obtain results which seem to contradict the claim: >>clear all >> a=0; tic, for n=1:1e8, a=i; end, toc Elapsed time is 3.056741 seconds. >> a=0; tic, for n=1:1e8, a=1i; end, toc Elapsed time is

Casting complex to real without data copy in MATLAB R2018a and newer

丶灬走出姿态 提交于 2019-12-06 19:06:01
问题 Since MATLAB R2018a, complex-valued matrices are stored internally as a single data block, with the real and imaginary component of each matrix element stored next to each other -- they call this "interleaved complex". (Previously such matrices had two data blocks, one for all real components, one for all imaginary components -- "separate complex".) I figure, since the storage now allows for it, that it should be possible to cast a complex-valued array to a real-valued array with twice as

Python Numpy - Complex Numbers - Is there a function for Polar to Rectangular conversion?

偶尔善良 提交于 2019-12-06 17:17:29
问题 Is there a built-in Numpy function to convert a complex number in polar form, a magnitude and an angle (degrees) to one in real and imaginary components? Clearly I could write my own but it seems like the type of thing for which there is an optimised version included in some module? More specifically, I have an array of magnitudes and an array of angles: >>> a array([1, 1, 1, 1, 1]) >>> b array([120, 121, 120, 120, 121]) And what I would like is: >>> c [(-0.5+0.8660254038j),(-0.515038074+0

Python: Making a class to use complex numbers

China☆狼群 提交于 2019-12-06 16:26:32
问题 I am currently trying to write a program that deals with complex numbers. I have to use classes and methods. I am trying to be able to add, subtract, multiply etc., complex numbers, as well as compare them to one another. I think I have gotten a good start but have some issues. I have started each method, and I think I just need to fill in the gaps. In the method, I used self.x as a placeholder. I'm not really sure what goes there. First off, the program needs to create it's own complex

What is a good open source C/C++ math library that supports vector math and complex numbers?

假装没事ソ 提交于 2019-12-06 11:54:21
问题 I am working on a project that needs vector math and complex numbers. I am looking a for a good open source API that supports C/C++ and hopefully has decent performance. I can write these functions myself but it will be ugly and slow. 回答1: You can use the C++ complex numbers library Also, Boost provides linear Algebra package 回答2: I regularly use Sony Vector Math library bundled with bullet physics. It's fast has many linear algebra algorithms. For complex numbers you can simply use std:

Python: How to write a complex number to excel using xlwt?

ε祈祈猫儿з 提交于 2019-12-06 11:07:50
I am trying to write a Python list to an excel file using xlwt library. import xlwt from tempfile import TemporaryFile book = xlwt.Workbook() sheet1 = book.add_sheet('sheet1') for i in range(len(newdata)): for j in range(len(newdata[i])): sheet1.write(i,j,newdata[i][j]) name = "my_file.xls" book.save(name) book.save(TemporaryFile()) It work for common variable types (e.g. int, float, string) but when I try to write a complex number to the excel file, I get the following error: Exception: Unexpected data type <type 'complex'> As I understand write does not support complex numbers. Does anyone