4

I want to do something quite simple but I'm unable to find it in the depths of numpy. I want to numerically and continuously integrate a function given by its values (not by its formula!). That means I simply want an array which holds the sums of the beginning of the input array. Example:

Input:

[ 4, 3, 5, 8 ]

Output:

[ 4, 7, 12, 20 ]  # [ sum(i[0:1]), sum(i[0:2]), sum(i[0:3]), sum(i[0:4]) ]

Sounds pretty straight forward, so I'm hopeful this must be easy with some numpy functionality I'm currently unable to find.

I found stuff like scipy.integrate.quad() but that seems to integrate over a given range (from a to b) and the returns a single value. I need an array as output.

7
  • 6
    How is this integration? Commented Apr 6, 2017 at 23:21
  • The output are the values of the stemfunction of the function which produces the values of the input. Commented Apr 6, 2017 at 23:29
  • 1
    Ah, gotcha. For future readers: here's what a stem function looks like. Then we use Riemann sums. Commented Apr 6, 2017 at 23:32
  • 1
    Numerical integration is the process of approximating an integral, given a domain and a function. Since you already have the stemfunction available, this question is not about numerical integration. Commented Apr 10, 2017 at 15:07
  • 2
    @NicoSchlömer You're mistaken. I don't have the stemfunction available; I want to create it in the process. By that I mean its values, not its formula. Hence integration. But call it as you like. Commented Apr 10, 2017 at 22:55

4 Answers 4

11

You're looking for numpy.cumsum:

   >>> numpy.cumsum([ 4, 3, 5, 8 ])
   array([ 4,  7, 12, 20])
Sign up to request clarification or add additional context in comments.

Comments

7

You would simply need numpy.cumsum().

import numpy as np
a = np.array([ 4, 3, 5, 8 ])
print np.cumsum(a) # prints [ 4  7 12 20]

Comments

6

You can use quadpy (pip install quadpy), a project of mine, which as opposed to scipy.integrate.quad() does vectorized compution. Provide it with many intervals, and get all the integral values over these intervals back.

import numpy
import quadpy

a = 0.0
b = 3.0
h = 1.0e-2
n = int((b-a) / h)

x0 = numpy.linspace(a, b, num=n, endpoint=False)
x1 = x0 + h
intervals = numpy.stack([x0, x1])

vals = quadpy.line_segment.integrate(
        lambda x: numpy.sin(x),
        intervals,
        quadpy.line_segment.GaussLegendre(5)
        )

res = numpy.cumsum(vals)

import matplotlib.pyplot as plt
plt.plot(x1, numpy.sin(x1), label='f')
plt.plot(x1, res, label='F')
plt.legend()
plt.show()

enter image description here

3 Comments

Nice! But actually I just have a bunch of numbers I want to continually sum up (and by now I know that np.cumsum() does that. Someone, however, found my wording for this (numerical integration) so misleading that he rephrased my question so fundamentally that in the end it looked like I was searching for functions. I undid that (in my eyes way too rigorous) change. I guess your answer was given during that period.
@Nico You cannot just edit a question such that it allows you to write an answer, the main goal of which seems to be promoting your software.
@ImportanceOfBeingErnest Thanks for the reply. The question was very unclear, specifically its connection to numerical integration; see the discussion below the original post. From I don't have the stemfunction available; I want to create it in the process. [...], I gathered that the question is really about that. I proceeded to edit the post accordingly, and after some tinkering gave an answer, namely one that creates the antiderivative. I indeed use a package that I wrote, but I don't see what's wrong with that.
1

You don't need numpy to get the output. Using standard itertools we get the following:

from itertools import accumulate

a = [4, 3, 5, 8]
*b, = accumulate(a)
print(b) 

# [4, 7, 12, 20]

1 Comment

I am using numpy for performance reasons. I'm summing up millions of samples. Otherwise the itertools approach could of course be feasible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.