0

I am programming in python and I want to plot a function defined by as "objFun (inputPoint, parameter2, parameter3)" with three arguments where the first argument is the point (a numpy array of 2 elements or coordinates) at which the function value is computed. My approach is

x = np.linspace(-10,10,50)
y = np.linspace(-10,10,50)
X,Y = np.meshgrid(x,y)

However I don't know how to pass X and Y as arguments to objFun since it accepts a single dimensional array of variables. I can't change the function to accept both X and Y as arguments. An example of objFun is a follows:

def objFun (x, alpha, beta):
   if alpha > 0 :
        return x[0]^2+x[1]^2
   else if beta > 0 :
        return x[0] + x[1]
   else:
        return 0

I want to feed mesh points X, Y to x[0] and x[1].

user402940
  • 315
  • 2
  • 10
  • @Arne: I am new to python. So forgive my limited understanding. How to map when we have extra arguments like parameter2 and parameter3. – user402940 Apr 08 '20 at 18:58
  • Are you allowed to edit the function inside? It would help if we could see what you actually want to achieve. – Mercury Apr 08 '20 at 19:15
  • 'a single dimensional array of variables' is too vague. `meshgrid` returns a tuple of arrays. `mgrid` returns the same, but as a higher dimensional array. Without a clearer idea of what the `objFun` accepts, we can't help. – hpaulj Apr 08 '20 at 19:43
  • Is `inputPoint` just a tuple of numbers? coordinates for just one (x,y) point? And `objFun` will have to called once for each `x,y` pair? – hpaulj Apr 08 '20 at 20:21
  • @hpaulj: I edited the question to give an example of objFun and its input. – user402940 Apr 09 '20 at 05:02
  • @hpaulj: Yes exactly. inputPoint is just a tuple (numpy array) ; x and y coordinates in 2D space and objFun has to be computed everytime a new [(x,y)] is supplied to it. – user402940 Apr 09 '20 at 05:11
  • In the third line of your function, you probably mean `return x[0]**2 + x[1]**2`. See an explanation here: https://stackoverflow.com/questions/12043913/python-and-powers-math – Arne Apr 09 '20 at 12:05

1 Answers1

0

Update: Answer now includes OP's example function and shows how to pass keyword argument values.


You can combine map() and zip(), while using a lambda function construction to get the parameter values across:

import numpy as np

def objFun (x, alpha, beta):
    """example function by OP without default parameter values"""
    if alpha > 0:
        return x[0]**2 + x[1]**2
    elif beta > 0:
        return x[0] + x[1]
    else:
        return 0

x = np.linspace(-10, 10, 3) # numbers reduced to 3
y = np.linspace(-10, 10, 3) # for convenience
X,Y = np.meshgrid(x,y)

for i in map(lambda x: objFun(x, alpha=1, beta=1), zip(X.flatten(), Y.flatten())):
    print(i) # just for testing/demonstration

The output is as expected:

200.0
100.0
200.0
100.0
0.0
100.0
200.0
100.0
200.0
Arne
  • 9,990
  • 2
  • 18
  • 28
  • what if parameter1 and parameter2 don't have default values but they are created during program execution. Will the map still work ? I have edited the question to give an example of objFun in case it helps. – user402940 Apr 09 '20 at 05:14
  • Yes, this is possible. I have updated my answer to show how. – Arne Apr 09 '20 at 12:24
  • 1
    Thanks. This is exactly what I was looking for. – user402940 Apr 10 '20 at 02:40