As I mentioned in a comment, checking a rational representation of a number in a computer is pretty much impossible because of round-off errors. You may be able to work around it, but maybe you can just ignore the special case of negative numbers? One definition of exponentiation says that...

c++,c++11,complex-numbers,user-defined-literals

The behaviour of the program depends on the language standard mode of gcc: There is a gcc extension for a built-in literal suffix i that produces C99 complex numbers. Those are distinct built-in types like _Complex double, as opposed to the "user-defined" class (template specialization) std::complex<double> used in C++. In...

arguments,fortran,intel,complex-numbers,intrinsics

Fortran 2008 allows complex argument. Some compilers already allow this. If your does not (as, e.g., ifort 15.0), compute it using exp(). cosh(x) = ( exp(x) + exp(-x) ) / 2 or use the identity cosh(x+iy) = cosh(x) * cos(y) + i * sinh(x) * sin(y) ...

arrays,matlab,vector,double,complex-numbers

You can temporarily "remove" the minus sign, which causes the complex numbers: vec = [-2.5, 2, -1.5 , 1, -0.5, 0]; sign_vec = sign(vec); test = sign_vec.*((sign_vec.*vec).^1.4623); This results in: test = -3.8186 2.7555 -1.8092 1.0000 -0.3629 0 A generic way of your conversion involves the abs function: >> test...

python,numpy,decimal,complex-numbers

You should look at the gmpy2 library. It provides fast arbitrary precision integer, real, and complex arithmetic.

It looks to me like it works, but the line width of your arrows is too small for you to see. You can increase it by assigning a handle to the quiver plot like so: hQuiver = quiver(x,y1,Dx,Dy); And then, after the plot is created, change any of its many...

This can be done with a simple is_complex trait and SFINAE: template<class T> struct is_complex : std::false_type {}; template<class T> struct is_complex<std::complex<T>> : std::true_type {}; template<class Scalar> class A { public: A(const Scalar z) : z_(z) { } template<class S = Scalar, std::enable_if_t<is_complex<S>{}>* = nullptr> Scalar realPart() { return z_.real();...

c++,eclipse,c++11,complex-numbers

Does it work if you change int to double? The complex template is only meant to be used with floating point types (float, double, long double). I would suspect that this is the problem. (This question has some authoritative links on the subject.)...

Here's how you would do it using the C and C++ APIs. You can find the APIs here: http://www.arrayfire.com/docs/group__data__func__constant.htm // Using C++ API cfloat h_unit = {0, 1} // Host side af::array unit = af::constant(h_unit, 1, c32); // Creates an array of size 1 containing all {0, 1} on device...

matlab,sorting,complex-numbers,eigenvalue

e1 = e(imag(e) >= 0); e2 = e(imag(e) < 0); newe = cat(1,sort(e1),sort(e2)) newe = -0.0156 + 0.5645i -0.4094 + 3.9387i -0.0156 - 0.5645i -0.4094 - 3.9387i ...

python,class,methods,data-structures,complex-numbers

Division you should implement with __div__ and modulus with __abs__. Complex conjugate and phase you'll have to choose your own method names for. For example, def conj(self): return complex(self.re, - self.im) (to be called with z.conj()). Note that you won't be able to define new Python syntax for your class:...

c++,sorting,vector,complex-numbers

std::sort does not have a built-in function for sorting complex numbers, so you have to write your own comparator function and pass it as an argument in sort() as sort(vector.begin(),vector.end(),myWay); The myWay function is defined as bool myWay(complex<double> a, complex<double> b) { if (real(a) == real(b)) return imag(a) < imag(b);...

go,type-conversion,complex-numbers

You don't really want to convert a float64 to complex128 but rather you want to construct a complex128 value where you specify the real part. For that can use the builtin complex() function: func complex(r, i FloatType) ComplexType Using it your sqrt() function: func sqrt(x float64) string { if x...

wolfram-mathematica,complex-numbers

You first need to convert the polar form of a to real and imaginary parts: (r, θ) = r*(cos(θ) + i*sin(θ)) Head over to Wikipedia if you need a quick refresher. As for the Wolfram Alpha input, you can just write it verbatim: 440*(cos(10) + i*sin(10)) / (39 - 18i)...

matlab,matrix,transpose,complex-numbers

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 .'....

As you have noted, the fftw_plan_dft_1d function computes the standard FFT Yk of the complex input sequence Xn defined as where j=sqrt(-1), for all values k=0,...,N-1 (thus generating N complex outputs in the array out), . You may notice that since the input happens to be real, the output exhibits...

python,formatting,complex-numbers

You can use imag and real attributes of a complex number to get their respective values. >>> a = cmath.sqrt(-1000) >>> "%.12f + %.12fj"%(a.real,a.imag) '0.000000000000 + 31.622776601684j' This works for completely real numbers also >>> a = cmath.sqrt(1000) >>> "%.12f + %.12fj"%(a.real,a.imag) '31.622776601684 + 0.000000000000j' ...

I think I found what I was looking for in this answer. In sum, an array of complex numbers has two memory blocks for the data: one to store the real parts and one to store the imaginary parts. The best course of action should be to initialize y to...

I would calculate the absolute value of their difference and compare to float's epsilon: import math import sys def almost_equal(a, b): d = a - b return math.sqrt(d.real ** 2 + d.imag ** 2) < sys.float_info.epsilon ...

file-io,fortran,complex-numbers

I haven't tested this, but I'd expect READ(53,*) (H1(i), i=1,A) to try to read 3 complex numbers. It gets fed the line 1d0, 0d0, 0d0 % comment from which it gets 1½ complex numbers and then barfs on the % sign, misinterpreting it as a syntactically invalid repeat count. I'd...

c++,c++11,reference,complex-numbers,rvalue

Just do cplx = std::polar(1.0f, /*...*/); ...

There are at least two bugs I spot. The one causing your problem is that your base case for (^%) is too high, so > (1,1) ^% 0 *** Exception: stack overflow Fix it by changing the base case to k ^% 0 = (1, 0) The second is that...

javascript,math,set,complex-numbers,viewer

The problem stems from confusion over the use of the this pointer in Javascript. Change your Julia calculation in doesPointEscape() to complexNum = new addComplex(new multComplex(complexNum, complexNum), c); and it works. This will return a new complex number from multComplex, then add it to c and return a new complex...

matlab,transpose,complex-numbers

Use B = [(5.7955+1.5529i) 0].' which is actually element-wise transpose and not B = [(5.7955+1.5529i) 0]' which is conjugate transpose. One can also use an explicit call to transpose command - B = transpose([(5.7955+1.5529i) 0])...

arrays,fortran,fortran90,complex-numbers

You must use the correct way of continuing lines. If you use fixed form (usually .f,.form) place any character on the sixth column of the new line and then your statement. You probably use this, otherwise -3.496991526333D- 001 coudn't work, because spaces are important in the free form. But !...

matlab,sorting,matrix,vector,complex-numbers

Answer is also very similar :) negIm = imag(e) < 0; [e1,ie1] = sort(e(~negIm)); [e2,ie2] = sort(e(negIm)); newe = cat(1,e1,e2); v1 = v(:,~negIm); v2 = v(:,negIm); newv = cat(2,v1(:,ie1),v2(:,ie2)); ...

I'm afraid I'm going to be off topic (yes I fully read your post :-) ). Ok, Python do allow you to try to compare complex numbers that way because you can define separately all operators even if I strongly advice you not to redefine __eq__ like you did :...

Your have several problems. The calling sequence for fftw_plan_dft_c2r_1d is as follows: The inverse transforms, taking complex input (storing the non-redundant half of a logically Hermitian array) to real output, are given by: fftw_plan fftw_plan_dft_c2r_1d(int n0, fftw_complex *in, double *out, unsigned flags); Thus, you have the following problems: You never...

c++,matlab,complex-numbers,exp

That is called floating point approximation (or imprecision): If you include the header cfloat there are some definitions. In particular, DBL_EPSILON, which is the smallest number that 1.0 + DBL_EPSILON != 1.0, which is usually 1e-9 (and -2.068231e-013 is much smaller than that. If you do the following piece of...

python,matplotlib,complex-numbers,arrow,color-mapping

import numpy as np import pylab as plt import itertools n = 13 roots = np.roots( [1,] + [0,]*(n-1) + [-1,] ) colors = itertools.cycle(['r', 'g', 'b', 'y']) plt.figure(figsize=(6,6)) for root in roots: plt.arrow(0,0,root.real,root.imag,ec=colors.next()) plt.xlim(-1.5,1.5) plt.ylim(-1.5,1.5) plt.show() The roots of unity are calculated in a manner similar to this answer....

c++,performance,fortran,complex-numbers

All Fortran can do (too) is to emulate the type. There is no native machine type for complex numbers (on x86 at least). Your concern is temporary usage of some stack bytes, that´s it. If there is no other reason to keep it, even the stack resizing can be optimized...

c,gcc,warnings,implicit-conversion,complex-numbers

C11 final draft (N1570), 6.3.1.7 paragraph 2: When a value of complex type is converted to a real type, the imaginary part of the complex value is discarded and the value of the real part is converted according to the conversion rules for the corresponding real type. Real types are...

python,python-3.x,complex-numbers

Negative number raised to a fractional power either gives a complex number (in Python 3.x) (Even for an odd power like 1/3 or 1/5) or a Value error in Python 2.x (ValueError: negative number cannot be raised to a fractional power). Example - >>> (-27)**(1/3) (1.5000000000000004+2.598076211353316j) You would need to...

The documentation for sqrt() (if you read it) tells you it returns a domain error. You can find this out for yourself with a test case: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <errno.h> int main(int argc, char *argv[]) { double foo = -1.234; double foo_sqrt = sqrt(foo); if (errno...

c++11,g++,complex-numbers,clang++,icpc

The difference is that on Linux, you're using libstdc++ and glibc, and on MacOS you're using libc++ and whatever CRT MacOS uses. The MacOS version is correct. (Also, your workaround is completely broken and insanely dangerous.) Here's what I think happens. There are multiple overloads of conj in the environment....

C++ and C is not completely compatible with each other, especially for C99 features, which may or may not present in C++, and complex number is one of them. C++ and C compatibility If you are using GCC, you can use the flag -fext-numeric-literals to enable support for _Complex. Working...

c++,opencv,complex-numbers,eigenvector,eigenvalue

So I solved the problem using the 'ComplexEigenSolver' from the Eigen library. //create a multichannel matrix Mat a_com = Mat::zeros(4,4,CV_32FC2); for(int i = 0; i<4; i++) { for(int j = 0; j<4; j++) { a_com.at<Vec2f>(i,j)[0] = a.at<double>(i,j); a_com.at<Vec2f>(i,j)[1] = 0; } } MatrixXcf eigenA; cv2eigen(a_com,eigenA); //convert OpenCV to Eigen ComplexEigenSolver<MatrixXcf>...

python,memory,sizeof,complex-numbers

Because a complex contains two floats, not two integers: >>> import sys >>> z = 5 + 5j >>> z.imag 5.0 >>> z.real 5.0 >>> sys.getsizeof(z.imag) 16 >>> sys.getsizeof(z.real) 16 >>> sys.getsizeof(z) 24 You can see the complexobject source code in the Python repo....

python,c,numpy,cython,complex-numbers

The simplest way I can find to work around this issue is to simply switch the order of multiplication. If in testcplx.pyx I change varc128 = varc128 * varf64 to varc128 = varf64 * varc128 I change from the failing situation to described to one that works correctly. This scenario...

matrix,julia-lang,complex-numbers,sparse

Looking at what we can pass to spzeros: julia> methods(spzeros) # 5 methods for generic function "spzeros": spzeros(m::Integer,n::Integer) at sparse/sparsematrix.jl:406 spzeros(Tv::Type{T<:Top},m::Integer,n::Integer) at sparse/sparsematrix.jl:407 spzeros(Tv::Type{T<:Top},Ti::Type{T<:Top},m::Integer,n::Integer) at sparse/sparsematrix.jl:409 spzeros(m::Integer) at deprecated.jl:28 spzeros(Tv::Type{T<:Top},m::Integer) at deprecated.jl:28 We see we should be able to pass a type as the first argument: julia> a =...

c++,string-parsing,complex-numbers

You would need to store the real and imaginary components of the complex number in separate float variables, which may or may not be grouped using a struct/class or std::pair<>. Input should be something like this: std::cout << "Enter a complex number:\n"; float real, imaginary; char i; if (std::cin >>...

c++,complex-numbers,decimal-point,punctuation

I would like to create a new unary mark that designates a constant number in the code to be interpreted as a member of a created datatype. You can use user defined literals that were introduced in C++11. As an example, assuming you have a class type Type and...

python,string,file,complex-numbers

From the help information, for the complex builtin function: >>> help(complex) class complex(object) | complex(real[, imag]) -> complex number | | Create a complex number from a real part and an optional imaginary part. | This is equivalent to (real + imag*1j) where imag defaults to 0. So you need...

I'm not sure if there is a built-in operation for this, but I do see a speed increase by not using complex function: >> imag(vec) + real(vec)*1i ans = 11.0000 + 1.0000i 22.0000 + 2.0000i 33.0000 + 3.0000i and also this way >> conj(vec)*1i ans = 11.0000 + 1.0000i 22.0000...