complex-numbers

Finding complex roots from set of non-linear equations in python

北城以北 提交于 2019-12-04 06:11:43
I have been testing an algorithm that has been published in literature that involves solving a set of 'm' non-linear equations in both Matlab and Python. The set of non-linear equations involves input variables that contain complex numbers, and therefore the resulting solutions should also be complex. As of now, I have been able to get pretty good results in Matlab by using the following lines of code: lambdas0 = ones(1,m)*1e-5; options = optimset('Algorithm','levenberg-marquardt',... 'MaxFunEvals',1000000,'MaxIter',10000,'TolX',1e-20,... 'TolFun',1e-20); Eq = @(lambda)maxentfun(lambda,m,h,g);

Is it guaranteed that Complex Float variables will be 8-byte aligned in memory?

末鹿安然 提交于 2019-12-04 02:04:13
问题 In C99 the new complex types were defined. I am trying to understand whether a compiler can take advantage of this knowledge in optimizing memory accesses. Are these objects ( A - F ) of type complex float guaranteed to be 8-byte aligned in memory? #include "complex.h" typedef complex float cfloat; cfloat A; cfloat B[10]; void func(cfloat C, cfloat *D) { cfloat E; cfloat F[10]; } Note that for D , the question relates to the object pointed to by D , not to the pointer storage itself. And, if

Does ICC satisfy C99 specs for multiplication of complex numbers?

早过忘川 提交于 2019-12-04 00:25:56
Consider this simple code: #include <complex.h> complex float f(complex float x) { return x*x; } If you compile it with -O3 -march=core-avx2 -fp-model strict using the Intel Compiler you get: f: vmovsldup xmm1, xmm0 #3.12 vmovshdup xmm2, xmm0 #3.12 vshufps xmm3, xmm0, xmm0, 177 #3.12 vmulps xmm4, xmm1, xmm0 #3.12 vmulps xmm5, xmm2, xmm3 #3.12 vaddsubps xmm0, xmm4, xmm5 #3.12 ret This is much simpler code than you get from both gcc and clang and also much simpler than the code you will find online for multiplying complex numbers. It doesn't, for example appear explicitly to deal with complex

Division by complex<double> in clang++ versus g++

纵然是瞬间 提交于 2019-12-04 00:17:52
When I compile the following code with g++ (4.8.1 or 4.9.0) or clang++ (3.4) I get different outputs. #include <iostream> #include <complex> int main() { std::complex<double> c = {1.e-162,0}; std::cout << 1.0/c << std::endl; return 0; } g++: (1e+162,0) clang++: (inf,-nan) Is this a bug in clang? Update: Thank you for your answers! I reported the bug: http://llvm.org/bugs/show_bug.cgi?id=19820 The standard says in [complex.numbers] (26.4/3): If the result of a function is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. There are no

How to save and load an array of complex numbers using numpy.savetxt?

笑着哭i 提交于 2019-12-03 15:31:23
问题 I want to use numpy.savetxt() to save an array of complex numbers to a text file. Problems: If you save the complex array with the default format string, the imaginary part is discarded. If you use fmt='%s' , then numpy.loadtxt() can't load it unless you specify dtype=complex, converters={0: lambda s: complex(s)} . Even then, if there are NaN's in the array, loading still fails. It looks like someone has inquired about this multiple times on the Numpy mailing list and even filed a bug, but

inconsistent results using isreal

 ̄綄美尐妖づ 提交于 2019-12-03 14:15:49
Take this simple example: a = [1 2i]; x = zeros(1,length(a)); for n=1:length(a) x(n) = isreal(a(n)); end In an attempt to vectorize the code, I tried: y = arrayfun(@isreal,a); But the results are not the same: x = 1 0 y = 0 0 What am I doing wrong? This certainly appears to be a bug, but here's a workaround: >> y = arrayfun(@(x) isreal(x(1)),a) ans = 1 0 Why does this work? I'm not totally sure, but it appears that when you perform an indexing operation on the variable before calling ISREAL it removes the "complex" attribute from the array element if the imaginary component is zero. Try this

Complex numbers in Cython

一笑奈何 提交于 2019-12-03 09:20:33
What is the correct way to work with complex numbers in Cython? I would like to write a pure C loop using a numpy.ndarray of dtype np.complex128. In Cython, the associated C type is defined in Cython/Includes/numpy/__init__.pxd as ctypedef double complex complex128_t so it seems this is just a simple C double complex. However, it's easy to obtain strange behaviors. In particular, with these definitions cimport numpy as np import numpy as np np.import_array() cdef extern from "complex.h": pass cdef: np.complex128_t varc128 = 1j np.float64_t varf64 = 1. double complex vardc = 1j double vard = 1.

Using metaclasses to override methods of complex builtin

非 Y 不嫁゛ 提交于 2019-12-03 07:37:57
问题 As a learning exercise, I'm trying to implement a class which will emulate the behavior of python's complex builtin, but with different behavior of the __str__ and __repr__ methods: I want them to print in the format... (1.0,2.0) ...instead of: (1+2j) I first tried simply subclassing from complex and redefining __str__ and __repr__ , but this has the problem that when non-overridden methods are called, a standard complex is returned, and printed in the standard format: >>> a = ComplexWrapper

How to save and load an array of complex numbers using numpy.savetxt?

谁都会走 提交于 2019-12-03 05:08:04
I want to use numpy.savetxt() to save an array of complex numbers to a text file. Problems: If you save the complex array with the default format string, the imaginary part is discarded. If you use fmt='%s' , then numpy.loadtxt() can't load it unless you specify dtype=complex, converters={0: lambda s: complex(s)} . Even then, if there are NaN's in the array, loading still fails. It looks like someone has inquired about this multiple times on the Numpy mailing list and even filed a bug , but has not gotten a response. Before I put something together myself, is there a canonical way to do this?

Using metaclasses to override methods of complex builtin

淺唱寂寞╮ 提交于 2019-12-02 21:06:58
As a learning exercise, I'm trying to implement a class which will emulate the behavior of python's complex builtin, but with different behavior of the __str__ and __repr__ methods: I want them to print in the format... (1.0,2.0) ...instead of: (1+2j) I first tried simply subclassing from complex and redefining __str__ and __repr__ , but this has the problem that when non-overridden methods are called, a standard complex is returned, and printed in the standard format: >>> a = ComplexWrapper(1.0,1.0) >>> a (1.0,1.0) >>> b = ComplexWrapper(2.0,3.0) >>> b (2.0,3.0) >>> a + b (3+4j) When the