Filling a numpy array using an arbitrary function

Samaursa

Normally, the summation of a function would be done like this in python

fsum = 0
for k in range(1, N+1):
    fsum += f(k)

Where f is some function.

The whole point of using numpy is to vectorize everything. In this case, the loop is going to be slow for very large N. How do I go about using numpy to fill a numpy array using a function f (that may take multiple arguments)?

ZSG

First I've expanded your code to the minimum required to actually execute it, including choose a specific f(k):

import numpy as np

def f(k):
    return(np.log(k**2))

N=8
fsum = 0
for k in range(1, N+1):
    fsum += f(k)

print fsum

Which gives and answer of 21.209. Now let's do the same thing but vectorized. Note the function f(k) is the same.

import numpy as np

def f(k):
    return(np.log(k**2))

N=8
k = np.array(range(1, N+1))
print np.sum(f(k))

And this gives the same answer. The key difference is that I have defined a numpy array to contain the inputs you iterate over in your for loop. That is:

k = np.array(range(1, N+1))

Which, if you want to be even more efficient, can be simplified to:

k = np.arange(1, N+1)

Since f(k) was already written to use numpy math functions it was already vectorized. Then instead of using a loop containing a += operation, I instead used the numpy.sum vectorized function on f(k):

print np.sum(f(k))

As you can see the result is more concise, and for large arrays it will be significantly faster too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Efficiently filling NumPy array using lists of indices

From Dev

numpy: filling a subset of array

From Dev

Filling array with zeros in numpy

From Dev

Filling an array of pointers in a function

From Dev

Filling ListView using an array

From Dev

Why is using a generator function slower than filling and iterating an array in this example?

From Dev

Filling array with map function issue

From Dev

Filling a Multidimensional Array using a Stream

From Dev

numpy array multiplication with arrays of arbitrary dimensions

From Dev

How to resample a Numpy array of arbitrary dimensions?

From Dev

numpy reshaping multdimensional array through arbitrary axis

From Dev

Subsampling numpy array in arbitrary number of dimensions

From Dev

How to resample a Numpy array of arbitrary dimensions?

From Dev

filling gaps on an image using numpy and scipy

From Dev

Filling empty list with zero vector using numpy

From Dev

Function called before filling $scope array

From Dev

Filling in missing objects in an array using Underscore

From Dev

React Filling ButtonGroup with Buttons using an array

From Dev

Numpy: Fix array with rows of different lengths by filling the empty elements with zeros

From Dev

Convert Python sequence to NumPy array, filling missing values

From Dev

numpy filling the diagonal 0 for 3D array

From Dev

Weird behaviour when filling a nested numpy array with `np.nan`

From Dev

Problems interpolating and evaluating numpy array at arbitrary points with Scipy

From Dev

numpy structured array from arbitrary-level nested dictionary

From Dev

Quick way to access first element in Numpy array with arbitrary number of dimensions?

From Dev

Cropping a NumPy array of an arbitrary dimension given two corners

From Dev

Evaluating function using numpy array returns inf and nan

From Dev

numpy: evaluating function in matrix, using previous array as argument in calculating the next

From Dev

How to interface a NumPy complex array with C function using ctypes?

Related Related

  1. 1

    Efficiently filling NumPy array using lists of indices

  2. 2

    numpy: filling a subset of array

  3. 3

    Filling array with zeros in numpy

  4. 4

    Filling an array of pointers in a function

  5. 5

    Filling ListView using an array

  6. 6

    Why is using a generator function slower than filling and iterating an array in this example?

  7. 7

    Filling array with map function issue

  8. 8

    Filling a Multidimensional Array using a Stream

  9. 9

    numpy array multiplication with arrays of arbitrary dimensions

  10. 10

    How to resample a Numpy array of arbitrary dimensions?

  11. 11

    numpy reshaping multdimensional array through arbitrary axis

  12. 12

    Subsampling numpy array in arbitrary number of dimensions

  13. 13

    How to resample a Numpy array of arbitrary dimensions?

  14. 14

    filling gaps on an image using numpy and scipy

  15. 15

    Filling empty list with zero vector using numpy

  16. 16

    Function called before filling $scope array

  17. 17

    Filling in missing objects in an array using Underscore

  18. 18

    React Filling ButtonGroup with Buttons using an array

  19. 19

    Numpy: Fix array with rows of different lengths by filling the empty elements with zeros

  20. 20

    Convert Python sequence to NumPy array, filling missing values

  21. 21

    numpy filling the diagonal 0 for 3D array

  22. 22

    Weird behaviour when filling a nested numpy array with `np.nan`

  23. 23

    Problems interpolating and evaluating numpy array at arbitrary points with Scipy

  24. 24

    numpy structured array from arbitrary-level nested dictionary

  25. 25

    Quick way to access first element in Numpy array with arbitrary number of dimensions?

  26. 26

    Cropping a NumPy array of an arbitrary dimension given two corners

  27. 27

    Evaluating function using numpy array returns inf and nan

  28. 28

    numpy: evaluating function in matrix, using previous array as argument in calculating the next

  29. 29

    How to interface a NumPy complex array with C function using ctypes?

HotTag

Archive