-1
import numpy as np
from ufl import cofac, sqrt

def f(X):
  fx = sqrt(X[0]**2+X[1]**2)
  return fx
  
X = np.array([1.0, 2.0])
Y = f(X)
print(Y)

if f(X)<=3:
 f(X)=0.0
  
print(f(X))
exit()

The above function calculates the distance of a point from the origin. If the distance is less than 3 units, I need to assign the value 0.0 to the function.

But the above code gives me no difference in results before and after the "if" loop. How to assign values to functions inside an if loop?

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • 2
    "if" is not a loop. – mkrieger1 May 24 '22 at 00:11
  • 1
    Did you mean that you want to assign the value 0.0 to `Y`? `==` is comparison, not assignment. Look how you did assignment above. – mkrieger1 May 24 '22 at 00:12
  • 1
    Hi @mkrieger1, Sorry, "if" is a condition statement, not a loop. Do you mean "f(X)=0.0" is correct? That raises the error "SyntaxError: cannot assign to function call" – user53927 May 24 '22 at 00:30
  • Welcome to Stack Overflow. The question as asked, and the code as shown, do not make much sense. I recommend that you follow a Python tutorial from start to finish, and *make sure you learn proper terminology*, before trying to learn Numpy. In particular, "assign a value... to a function" is nonsensical, and as noted, `if` isn't a loop. If you mean "if the function would return something less than 3, it should return 0 instead", then the way to implement that is to put that logic *into the function* - fix the value of `fx` before `return`ing it. – Karl Knechtel May 24 '22 at 00:55
  • If you mean "if the function returned a value that is less than 3, then `Y` should become `0` instead", then you do that by assigning `0` **to `Y`**. That means, `Y` goes on the left-hand side, and `0` goes on the right-hand side, thus: `Y = 0`. – Karl Knechtel May 24 '22 at 00:57
  • The syntax error tells us that what you trying to do is wrong - at a fundamental Python level. Making the function return 0 under certain circumstances, or changing its result, is quite another thing. – hpaulj May 24 '22 at 01:10

2 Answers2

1

Sounds like you want the f() function to return different results depending on the values in X.

def f(X):
  fx = sqrt(X[0]**2+X[1]**2)
  if fx > 3:
      return fx
  else:
      return 0.0
John Gordon
  • 29,573
  • 7
  • 33
  • 58
1

As @mkrieger1 said; if you are trying to assign the value to f(X) you have to use = operator.

Moreover, you cannot assign the value to function, instead you can assign value to Y and print.

import numpy as np
from ufl import cofac, sqrt

def f(X):
  fx = sqrt(X[0]**2+X[1]**2)
  return fx
  
X = np.array([1.0, 2.0])
Y = f(X)
print(f(X))
if f(X)<=3:
  Y=0.0

 
print(Y)

Here, is the link:

SyntaxError: "can't assign to function call"

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • Hi @Talha Tayyab, "you cannot assign the value to function" is the comment I was looking for. So assigning only works if there's an auxiliary variable assigned to the function. – user53927 May 24 '22 at 00:44
  • @user53927 Yes, I have edited my answer and put a link to a question for assigning a value to function. – Talha Tayyab May 24 '22 at 00:58
  • @user53927 It seems like you have the right idea now. Please make sure you understand *what "assign" means*. – Karl Knechtel May 24 '22 at 00:59