Pass numpy array as function argument

Steven Shi

I want to pass a numpy array arr to what_a_function and print the resulting array, and this should be done without using loop. I know it is ugly and unfriendly, but I have to do so. I tried with vectorize but kept failing. May anyone share some pointers please? Thanks!

import numpy as np
def what_a_function(x):
    return -np.cos(x.all()) * (0.5 < np.sin(x) < 2) + (np.sin(x) <= 0.5) + (x ** 2) * (np.sin(x) >= 2)

a=1
b=5

vfunc = np.vectorize(what_a_function)
arr = np.arange(a,b+0.1,0.1)

print(arr)
print(vfunc(arr))

And it will complain AttributeError: 'float' object has no attribute 'all'.

hpaulj

That ' super ugly and unfriendly' equation doesn't make sense. What kind of x value is it supposed to evaluate?

-np.cos(x.all()) * (0.5 < np.sin(x) < 2) + (np.sin(x) <= 0.5) + (x ** 2) * (np.sin(x) >= 2)

x.all() requires an array (with all method), and returns a boolean (scalar or array), which is a nonsense input for np.cos.

np.sin(x) is ok with a scalar or array, but the 0.5<...<2 only works for a scalar (it's Python that doesn't work for numpy).

The next np.sin(x)<=.5 will produces a boolean (scalar or array). x**2 will be a numeric value.

The + and * will sort of work, converting the boolean True/False to 1/0 integers. But logical operators are better.

If we knew what is was supposed to do, we could probably write it to work directly with a numeric array. np.vectorize is not a good substitute for writing proper array compatible code. As I commented, vectorize passes the array values to the function one by one, as scalars. That's why the all method produces an error (and doesn't make sense). On top of that vectorize is slow.

A straight forward list comprehension is faster:

np.array([your_function(i) for i in x])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

xtensor pass numpy array to function with xt::xtensor argument type

From Dev

How to pass whole numpy array as argument in python to a user defined function?

From Dev

Pass 2d array as argument to a function using numpy and swig

From Dev

How to pass an array as function argument?

From Dev

How to pass an array argument to a function

From Dev

Pass in part of an array as function argument

From Dev

Pass multidimensional array with variable size as argument of function

From Dev

Kotlin - Pass array of functions as argument to function

From Dev

How to properly pass an array as a function argument?

From Dev

Pass string array as argument with function pointer

From Dev

Unable to pass an array as function argument in Javascript

From Dev

How to pass in a dynamically allocated array as an argument to a function

From Dev

add two numpy arrays using lamdify - pass expression as function argument

From Dev

Is it possible to pass a numpy array by reference into a cpdef function?

From Dev

Tranformations of Numpy Array, Pass values to function

From Dev

numpy array passed as argument to function, and returned array are the same

From Dev

Pass argument and function as argument in python

From Dev

How to produce a character array in ctypes and pass its pointer as a function argument

From Dev

How to pass an array as argument (in Python) when solving ODE by odeint function

From Dev

How to pass a list, vector or array as argument in invokeMethod function

From Dev

Pass function with one argument to array.map using jsfuck

From

How to pass a 2 dimensional array as a function argument in Go?

From

How to pass multidimensional array in arbitrary size as function argument in GO

From Dev

How to pass array as an argument to a Ruby function from command line?

From Dev

pass array argument to main function index start at 1 c++

From Dev

how to pass a swift Array as UnsafePointer<T> argument in a function

From Dev

Node-Addon-Api Pass Array As Function Argument

From Dev

How to pass a 2 Dimensional array as an argument of function in C?

From Dev

How to pass a constant array as argument to a C++ function/method

Related Related

  1. 1

    xtensor pass numpy array to function with xt::xtensor argument type

  2. 2

    How to pass whole numpy array as argument in python to a user defined function?

  3. 3

    Pass 2d array as argument to a function using numpy and swig

  4. 4

    How to pass an array as function argument?

  5. 5

    How to pass an array argument to a function

  6. 6

    Pass in part of an array as function argument

  7. 7

    Pass multidimensional array with variable size as argument of function

  8. 8

    Kotlin - Pass array of functions as argument to function

  9. 9

    How to properly pass an array as a function argument?

  10. 10

    Pass string array as argument with function pointer

  11. 11

    Unable to pass an array as function argument in Javascript

  12. 12

    How to pass in a dynamically allocated array as an argument to a function

  13. 13

    add two numpy arrays using lamdify - pass expression as function argument

  14. 14

    Is it possible to pass a numpy array by reference into a cpdef function?

  15. 15

    Tranformations of Numpy Array, Pass values to function

  16. 16

    numpy array passed as argument to function, and returned array are the same

  17. 17

    Pass argument and function as argument in python

  18. 18

    How to produce a character array in ctypes and pass its pointer as a function argument

  19. 19

    How to pass an array as argument (in Python) when solving ODE by odeint function

  20. 20

    How to pass a list, vector or array as argument in invokeMethod function

  21. 21

    Pass function with one argument to array.map using jsfuck

  22. 22

    How to pass a 2 dimensional array as a function argument in Go?

  23. 23

    How to pass multidimensional array in arbitrary size as function argument in GO

  24. 24

    How to pass array as an argument to a Ruby function from command line?

  25. 25

    pass array argument to main function index start at 1 c++

  26. 26

    how to pass a swift Array as UnsafePointer<T> argument in a function

  27. 27

    Node-Addon-Api Pass Array As Function Argument

  28. 28

    How to pass a 2 Dimensional array as an argument of function in C?

  29. 29

    How to pass a constant array as argument to a C++ function/method

HotTag

Archive