complex-numbers

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

这一生的挚爱 提交于 2019-12-05 00:48:39
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 3.205472 seconds. Any ideas? Could it be a version-related issue? After comments by @TryHard and

Square root of complex numbers in python

末鹿安然 提交于 2019-12-04 23:39:15
I have run across some confusing behaviour with square roots of complex numbers in python. Running this code: from cmath import sqrt a = 0.2 b = 0.2 + 0j print(sqrt(a / (a - 1))) print(sqrt(b / (b - 1))) gives the output 0.5j -0.5j A similar thing happens with print(sqrt(-1 * b)) print(sqrt(-b)) It appears these pairs of statements should give the same answer? tzaman Both answers ( +0.5j and -0.5j ) are correct, since they are complex conjugates -- i.e. the real part is identical, and the imaginary part is sign-flipped. Looking at the code makes the behavior clear - the imaginary part of the

Python: Making a class to use complex numbers

*爱你&永不变心* 提交于 2019-12-04 22:09:22
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 numbers. I'm new to making methods and functions and I'm not sure if I used (self) in the correct places. I

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

丶灬走出姿态 提交于 2019-12-04 18:01:29
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. You can use the C++ complex numbers library Also, Boost provides linear Algebra package 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::complex I haven't used any of these packages personally, but head over to Object-Oriented Numerics for a great

Fastest way to calculate the abs()-values of a complex array

限于喜欢 提交于 2019-12-04 16:20:07
问题 I want to calculate the absolute values of the elements of a complex array in C or C++. The easiest way would be for(int i = 0; i < N; i++) { b[i] = cabs(a[i]); } But for large vectors that will be slow. Is there a way to speed that up (by using parallelization, for example)? Language can be either C or C++. 回答1: Given that all loop iterations are independent, you can use the following code for parallelization: #pragma omp parallel for for(int i = 0; i < N; i++) { b[i] = cabs(a[i]); } Of

Complex numbers in Cython

只愿长相守 提交于 2019-12-04 15:03:30
问题 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

How to use complex number “i” in C++

☆樱花仙子☆ 提交于 2019-12-04 13:10:18
问题 I am coding a simple DFT algorithm now and I want to use the complex number i in complex exponential. I saw somebody use #include<complex> and #include<cmath> , and then they used the overloaded symbol I such as exp(2*I) . But it seems it doesn't work in my visual studio compiler. So, can anyone give a simple example of using complex exponential? Thanks! 回答1: Here is a short complete example: #include <iostream> #include <complex> #include <cmath> using namespace std; typedef complex<double>

Scipy: Speeding up calculation of a 2D complex integral

梦想与她 提交于 2019-12-04 11:04:07
问题 I want to repeatedly calculate a two-dimensional complex integral using dblquad from scipy.integrate. As the number of evaluations will be quite high I would like to increase the evaluation speed of my code. Dblquad does not seem to be able to handle complex integrands. Thus, I have split the complex integrand into a real and an imaginary part: def integrand_real(x, y): R1=sqrt(x**2 + (y-y0)**2 + z**2) R2=sqrt(x**2 + y**2 + zxp**2) return real(exp(1j*k*(R1-R2)) * (-1j*z/lam/R2/R1**2) * (1+1j

Element-by-element vector multiplication with CUDA

妖精的绣舞 提交于 2019-12-04 08:45:38
I have build a rudimentary kernel in CUDA to do an elementwise vector-vector multiplication of two complex vectors. The kernel code is inserted below ( multiplyElementwise ). It works fine, but since I noticed that other seemingly straightforward operations (like scaling a vector) are optimized in libraries like CUBLAS or CULA, I was wondering if it is possible to replace my code by a library call? To my surprise, neither CUBLAS nor CULA have this option, I tried to fake it by making one of the vectors the diagonal of a diagonal matrix-vector product, but the result was really slow. As a

Why is complex conjugate transpose the default in Matlab

送分小仙女□ 提交于 2019-12-04 07:18:00
if A matrix has complex element and I want to transpose A to A' using the command >>A' Why it is design that a+bi be transformed into a-bi ? What it use for? From here : for complex matrices, it is almost always the case that the combined operation of taking the transpose and complex conjugate arises in physical or computation contexts and virtually never the transpose in isolation ( Strang 1988 , pp. 220-221). In matlab if you want to transpose without conjugating use .' . Actually I'd argue that there are deep reasons why the transpose IS the conjugate . Consider a matrix representation of