Python: Want to get both (plus and minus) square roots -


I have to find the roots of a number square using python. My simple code is:

#! / Usr / bin / env python import numpy as np x = 4.0 print 'roots of square', x, 'are:', np.sqrt (x)

Gives:

  Square roots of 4.0: 2.0  

But in the form of the answer I also want to -2.0 To see, i.e. the roots of the square of 4 +/- 2, not just +2

In a wider problem, I have a function f (x) = sqrt (x) I want to plot I want to hold both branches of Parabol in one shot. The code is now:

#! / Usr / bin / env python import matplotlib.pyplot as plt import numpy np x = np.arange (0.0, 16.0, 0.2) f = np .sqrt (x) plt.plot (x, f) plt.show ( )

which does not conspirate the values ​​in the negative y-axis. Is it possible to do this without any trick (for example, plotting to specify it for a second time)? It is not necessary that I have to use numpy

thanks.

I think OP is a bit confused about that: the root of the class of +/- 2 , Why numpy.sqrt just give positive?

But this is clearly defined in the function description:

  returns ------- y: the same size as ndarray `x` An array, in which there is a positive square root for each element in `x`. ...  

You can view this message by using numpy.info (numpy.sqrt) in the Python shell.

Since everybody wants to print them all, then just you have to change the print statement:

  of the print category Roots ', x,' 'are: +/-', np.sqrt (x)  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -