Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global name 'sqrt' not defined

I've created a function, potential(x,K,B,N), where x,K,B are numpy arrays and N is an integer. I'm trying to test the function in iPython but I keep getting the error "global name 'sqrt' not defined".

Here's a look at my code:

def potential(x,K,B,N):

    x = x.reshape((3,N),order='F')

U_b = 0.0
for i in xrange(0,N-1):
    for j in xrange(i+1,N):
        if K[i,j] == 1.0:
            U_b += sum((x[:,i]-x[:,j])**2)
U_b = 0.5*U_b

U_a = 0.0
for i in xrange(0,N-2):
    for j in xrange(i+1,N-1):
        for l in xrange(j+1,N):
            if B[i,j,l] == 1.0:
                U_a += B[i,j,l]*sum((x[:,i]-x[:,j])*(x[:,j]-x[:,l]))/(sqrt(sum((x[:,i]-x[:,j])**2))*sqrt(sum((x[:,j]-x[:,l])**2)))
U_a = -U_a

U_r = 0.0
d = 0.0
for i in xrange(0,N-1):
    for j in xrange(i+1,N):
        d = sqrt(sum((x[:,i]-x[:,j])**2))
        if d > sqrt(0.2):
            U_r += (1.0/6.0)*(1/(d**6))
        else:
            U_r += -0.2**(-7.0/2.0)*d + (7.0/6.0)*(0.2)**(-3)

return U_b + U_a + U_r

I've tried using from math import * but that doesn't seem to help. Any suggestions would be greatly appreciated!

like image 789
user3758890 Avatar asked Jun 20 '14 05:06

user3758890


People also ask

How do you define a square root in Python?

sqrt() function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.sqrt(x) Parameter: x is any number such that x>=0 Returns: It returns the square root of the number passed in the parameter.

Is not defined Did you mean Python?

The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.


2 Answers

Add the missing line:

from numpy import sqrt 

(or in non-NumPy code from math import sqrt. Note that these are two different functions:numpy.sqrt also accepts vectors and arrays , but plain math.sqrt doesn't)

I've tried using from math import * but that doesn't seem to help.

(Possibly you did that after defining the function. Anyway, fuhgeddaboutit, just reload the code in a clean session, it will work.)

UPDATES:

  • strictly, in Python you're supposed to do import package not from package import identifier1 [,identifier2, identifier3...] and never from package import *. But from package import identifier1 is ok when judiciously used, if you don't overdo it, and locally inside a function. If it's unambiguous, and you're going to be doing a lot of it, it shortens the code e.g. sqrt() instead of math.sqrt(), log instead of math.log10()

  • sqrt is not a builtin in Python, unlike R. So yes in Python you need either from numpy import sqrt or import math or from math import sqrt before you can use it.

like image 93
smci Avatar answered Sep 20 '22 07:09

smci


Since you tagged numpy,

import numpy as np

Then use np.sqrt instead of sqrt. Always works.

like image 26
Adarsh Chavakula Avatar answered Sep 19 '22 07:09

Adarsh Chavakula