How to make sqrt(-1) be 1j

Bill Baxter wbaxter at gmail.com
Thu Oct 12 21:49:51 EDT 2006


On 10/12/06, Stefan van der Walt <stefan at sun.ac.za> wrote:
> On Thu, Oct 12, 2006 at 08:58:21AM -0500, Greg Willden wrote:
> > On 10/11/06, Bill Baxter <wbaxter at gmail.com> wrote:
> I tried to explain the argument at
>
> http://www.scipy.org/NegativeSquareRoot
>

The proposed fix for those who want sqrt(-1) to return 1j is:

   from numpy.lib import scimath as SM
   SM.sqrt(-1)


But that creates a new namespace alias, different from numpy.  So I'll
call numpy.array() to create a new array, but SM.sqrt() when I want a
square root.
Am I wrong to want some simple way to change the behavior of
numpy.sqrt itself?

Seems like you can get that effect via something like:

    for n in numpy.lib.scimath.__all__:
        numpy.__dict__[n] = numpy.lib.scimath.__dict__[n]

If that sort of function were available as "numpy.use_scimath()", then
folks who want numpy to be like scipy can achieve that with just one
line at the top of their files.   The import under a different name
doesn't quite achieve the goal of making that behavior numpy's
"default".

I guess I'm thinking mostly of the educational uses of numpy, where
you may have users that haven't learned about much about numerical
computing yet.  I can just imagine the instructor starting off by
saying "ok everyone we're going to learn numpy today!  First everyone
type this:  'import numpy, from numpy.lib import scimath as SM' --
Don't worry about all the things there you don't understand."
Whereas "import numpy, numpy.use_scimath()" seems easier to explain
and much less intimidating as your first two lines of numpy to learn.

Or is that just a bad idea for some reason?

--bb

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the NumPy-Discussion mailing list