From: Bruno Luong on
I read at the help and doc but I could not find any specification of SQRT for complex numbers.

It seems the output keeps the same sign of the imaginary part as the input, and the discontinuity (in the complex plane) is the negative real axis. This is what I want, but I love to see it documented somewhere.

Any opinion?

Bruno
From: Matt J on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hsc0es$988$1(a)fred.mathworks.com>...
> I read at the help and doc but I could not find any specification of SQRT for complex numbers.
>
> It seems the output keeps the same sign of the imaginary part as the input, and the discontinuity (in the complex plane) is the negative real axis. This is what I want, but I love to see it documented somewhere.
>
> Any opinion?
===============

Probably the closest thing you can get right now is

doc angle

After some experimentation it is pretty clear that the underlying algorithm is

sqrt(A)=sqrt(abs(A))*exp(-i*angle(A)/2);
From: Bruno Luong on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hsc2m5$38l$1(a)fred.mathworks.com>...

> After some experimentation it is pretty clear that the underlying algorithm is
>
> sqrt(A)=sqrt(abs(A))*exp(-i*angle(A)/2);

I'm not sure. It does not give exactly the same output, and it's also slower

n = 1e6;
A=randn(1,n)+1i*rand(1,n);

tic
B=sqrt(abs(A)).*exp(-1i*angle(A)/2);
toc % Elapsed time is 0.292321 seconds.

tic
C=sqrt(A);
toc % Elapsed time is 0.082047 seconds.

isequal(B,C) % 0

%%%%%%%

I would prefer using stock SQRT function. My program expects the result having imaginary part with the same sign. Of course I can test the sign and reverse if necessary but why spending time for something that is true but not documented?

% Bruno
From: Doug Schwarz on
In article <hsc56o$ee6$1(a)fred.mathworks.com>,
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote:

> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message
> <hsc2m5$38l$1(a)fred.mathworks.com>...
>
> > After some experimentation it is pretty clear that the underlying algorithm
> > is
> >
> > sqrt(A)=sqrt(abs(A))*exp(-i*angle(A)/2);
>
> I'm not sure. It does not give exactly the same output, and it's also slower
>
> n = 1e6;
> A=randn(1,n)+1i*rand(1,n);
>
> tic
> B=sqrt(abs(A)).*exp(-1i*angle(A)/2);
> toc % Elapsed time is 0.292321 seconds.
>
> tic
> C=sqrt(A);
> toc % Elapsed time is 0.082047 seconds.
>
> isequal(B,C) % 0
>
> %%%%%%%
>
> I would prefer using stock SQRT function. My program expects the result
> having imaginary part with the same sign. Of course I can test the sign and
> reverse if necessary but why spending time for something that is true but not
> documented?
>
> % Bruno

I can't answer your question, but on my machine the algorithm uses +1i
in the exponent, not -1i. (R2009a on Mac OS X)

sqrt(A) = sqrt(abs(A))*exp(1i*angle(A)/2);

It still does not match exactly.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
From: Roger Stafford on
Doug Schwarz <see(a)sig.for.address.edu> wrote in message <see-FA4995.14584511052010(a)news.frontiernet.net>...
> In article <hsc56o$ee6$1(a)fred.mathworks.com>,
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote:
>
> > "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message
> > <hsc2m5$38l$1(a)fred.mathworks.com>...
> >
> > > After some experimentation it is pretty clear that the underlying algorithm
> > > is
> > >
> > > sqrt(A)=sqrt(abs(A))*exp(-i*angle(A)/2);
> >
> > I'm not sure. It does not give exactly the same output, and it's also slower
> >
> > n = 1e6;
> > A=randn(1,n)+1i*rand(1,n);
> >
> > tic
> > B=sqrt(abs(A)).*exp(-1i*angle(A)/2);
> > toc % Elapsed time is 0.292321 seconds.
> >
> > tic
> > C=sqrt(A);
> > toc % Elapsed time is 0.082047 seconds.
> >
> > isequal(B,C) % 0
> >
> > %%%%%%%
> >
> > I would prefer using stock SQRT function. My program expects the result
> > having imaginary part with the same sign. Of course I can test the sign and
> > reverse if necessary but why spending time for something that is true but not
> > documented?
> >
> > % Bruno
>
> I can't answer your question, but on my machine the algorithm uses +1i
> in the exponent, not -1i. (R2009a on Mac OS X)
>
> sqrt(A) = sqrt(abs(A))*exp(1i*angle(A)/2);
>
> It still does not match exactly.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Perhaps they use:

sqrt(z) = exp(1/2*log(z))

That should always give a non-negative imaginary part. Actually I suspect they have their own special algorithm, however.

Roger Stafford