How to make sqrt(-1) be 1j
tim.hochberg at ieee.org
Thu Oct 12 23:43:08 CDT 2006
Bill Baxter wrote:
> 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
> The proposed fix for those who want sqrt(-1) to return 1j is:
> from numpy.lib import scimath as SM
> 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
> 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?
Isn't that just going to make your students *more* confused later when
then run into the standard behavior of numpy? For this sort of thing, I
would just make a new module to pull together the function I want and
use that instead. It's then easy to explain that this new module bbeconf
(Bill Baxter's Excellent Collection Of Numeric Functions) is actually an
amalgamation of stuff from multiple sources.
from numpy import *
fromnumpy.scimath import sqrt
# possibly some other stuff to correctly handle subpackages...
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
More information about the Numpy-discussion