complex-numbers

Formatting Complex Numbers

一曲冷凌霜 提交于 2019-11-28 10:50:41
For a project in one of my classes we have to output numbers up to five decimal places.It is possible that the output will be a complex number and I am unable to figure out how to output a complex number with five decimal places. For floats I know it is just: print "%0.5f"%variable_name Is there something similar for complex numbers? For questions like this, the Python documentation should be your first stop. Specifically, have a look at the section on string formatting . It lists all the string format codes; there isn't one for complex numbers. What you can do is format the real and imaginary

Format of complex number in Python

我只是一个虾纸丫 提交于 2019-11-28 02:33:17
问题 I am wondering about the way Python (3.3.0) prints complex numbers. I am looking for an explanation, not a way to change the print. Example: >>> complex(1,1)-complex(1,1) 0j Why doesn't it just print "0"? My guess is: to keep the output of type complex. Next example: >>> complex(0,1)*-1 (-0-1j) Well, a simple "-1j" or "(-1j)" would have done. And why "-0"?? Isn't that the same as +0? It doesn't seem to be a rounding problem: >>> (complex(0,1)*-1).real == 0.0 True And when the imaginary part

C Complex Numbers in C++?

荒凉一梦 提交于 2019-11-27 23:44:16
The following code compiles and runs just fine in C (at least according to 'gcc -std=gnu99'), but it fails to compile under C++, giving "line 5: error: cannot convert 'double' to 'double complex ' in initialization". Does anybody know why? #include "/usr/include/complex.h" #include <stdio.h> int main(int argc, char * argv[]) { double complex a = 3; // ERROR ON THIS LINE printf("%lf\n", creal(a)); return 0; } I realize there is another way of doing complex numbers in C++, but I have to use C complex numbers in C++, because that is how the legacy code I was given does things. Thanks if you can

Compiling C code in Visual Studio 2013 with complex.h library

被刻印的时光 ゝ 提交于 2019-11-27 23:05:44
http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx C99 support added visual studio 2013, but I cant use complex.h in my "C" code. #include <stdio.h> #include <complex.h> int main(void) { double complex dc1 = 3 + 2 * I; double complex dc2 = 4 + 5 * I; double complex result; result = dc1 + dc2; printf(" ??? \n", result); return 0; } I get syntax errors. Edit: Sorry for the missing part. error C2146: syntax error : missing ';' before identifier 'dc1' error C2065: 'dc1' : undeclared identifier error C2088: '*' : illegal for struct error C2086: 'double

Why does C++ mandate that complex only be instantiated for float, double, or long double?

送分小仙女□ 提交于 2019-11-27 15:29:38
问题 According to the C++ ISO spec, §26.2/2: The effect of instantiating the template complex for any type other than float , double or long double is unspecified. Why would the standard authors explicitly add this restriction? This makes it unspecified, for example, what happens if you make complex<int> or a complex<MyCustomFixedPointType> and seems like an artificial restriction. Is there a reason for this limitation? Is there a workaround if you want to instantiate complex with your own custom

C99 complex support with visual studio

天涯浪子 提交于 2019-11-27 15:14:42
I would like to use complex numbers as defined in C99, but I need to support compilers which do not support it (MS compilers come to mind). I don't need many functions, and implementing the needed functions on compilers without support is not too difficult. But I have a hard time implementing the 'type' itself. Ideally, I would like to do something like: #ifndef HAVE_CREAL double creal(complex z) { /* .... */ } #endif #ifndef HAVE_CREALF float creal(float complex z) { /* ... */ } #endif But I am not sure I see how to do this if the compiler cannot recognize 'float complex'. I would actually

Is there any way to use bivariate colormaps in matplotlib?

喜你入骨 提交于 2019-11-27 14:04:58
In other words, I want to make a heatmap (or surface plot) where the color varies as a function of 2 variables. (Specifically, luminance = magnitude and hue = phase.) Is there any native way to do this? Some examples of similar plots: Several good examples of exactly(?) what I want to do. More examples from astronomy, but with non-perceptual hue imshow will take an NxMx3 (rbg) or NxMx4 (grba) array so you can do your color mapping 'by hand'. You might be able to get a bit of traction by sub-classing Normalize to map your vector to a scaler and laying out a custom color map very cleverly (but I

Numpy: Creating a complex array from 2 real ones?

时间秒杀一切 提交于 2019-11-27 11:25:50
I swear this should be so easy... Why is it not? :( In fact, I want to combine 2 parts of the same array to make a complex array: Data[:,:,:,0] , Data[:,:,:,1] These don't work: x = np.complex(Data[:,:,:,0], Data[:,:,:,1]) x = complex(Data[:,:,:,0], Data[:,:,:,1]) Am I missing something? Does numpy not like performing array functions on complex numbers? Here's the error: TypeError: only length-1 arrays can be converted to Python scalars This seems to do what you want: numpy.apply_along_axis(lambda args: [complex(*args)], 3, Data) Here is another solution: # The ellipsis is equivalent here to "

C complex number and printf

别来无恙 提交于 2019-11-27 08:33:16
How to print ( with printf ) complex number? For example, if I have this code: #include <stdio.h> #include <complex.h> int main(void) { double complex dc1 = 3 + 2*I; double complex dc2 = 4 + 5*I; double complex result; result = dc1 + dc2; printf(" ??? \n", result); return 0; } ..what conversion specifiers ( or something else ) should I use instead "???" printf("%f + i%f\n", creal(result), cimag(result)); I don't believe there's a specific format specifier for the C99 complex type. Let %+f choose the correct sign for you for imaginary part: printf("%f%+fi\n", crealf(I), cimagf(I)); Output: 0

C Complex Numbers in C++?

吃可爱长大的小学妹 提交于 2019-11-26 21:34:30
问题 The following code compiles and runs just fine in C (at least according to 'gcc -std=gnu99'), but it fails to compile under C++, giving "line 5: error: cannot convert 'double' to 'double complex ' in initialization". Does anybody know why? #include "/usr/include/complex.h" #include <stdio.h> int main(int argc, char * argv[]) { double complex a = 3; // ERROR ON THIS LINE printf("%lf\n", creal(a)); return 0; } I realize there is another way of doing complex numbers in C++, but I have to use C