Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the sum of the absolute values of np.sin(x) and np.cos(x) from 0 to 2*pi not the same?

I am trying to compute the sum of the absolute values of these two expressions, and I am somehow confused, since the sum should be the same, right?

Consider the integrals:

Integral of abs(sin(x))dx

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Integral of abs(cos(x))dx

It’s easy to see, that the area underneath them is the same, and indeed both integrals return 4.

I wrote a simple script to evenly sample those functions and add all the sampled values together. Scaling aside, both expressions should yield the same result, but they dont. Here is the code:

import numpy as np
angles = np.linspace(0, 2*np.pi, 1000)
line1, line2= [], []

for n in angles:
    line1.append(np.abs(np.cos(n)))
    line2.append(np.abs(np.sin(n)))

print(sum(line1), sum(line2))

The result is the following:

636.983414656738 635.9826284722284

The sums are off by almost exactly 1. I know that they are not 4, because there is some constant factor missing, but the point is, that the values should be the same. Am I completly missing something here or is this a bug?

>Solution :

Consider the extreme case of reducing the samples to 3:

In [93]: angles = np.linspace(0, 2*np.pi, 3)
In [94]: angles
Out[94]: array([0.        , 3.14159265, 6.28318531])
In [95]: np.cos(angles)
Out[95]: array([ 1., -1.,  1.])
In [96]: np.sin(angles)
Out[96]: array([ 0.0000000e+00,  1.2246468e-16, -2.4492936e-16])

The extra 1 for cos persists for larger samples.

In [97]: angles = np.linspace(0, 2*np.pi, 1001)
In [98]: np.sum(np.abs(np.cos(angles)))
Out[98]: 637.6176779711009
In [99]: np.sum(np.abs(np.sin(angles)))
Out[99]: 636.6176779711009

But if we tell it to skip the 2*np.pi end point, the values match:

In [100]: angles = np.linspace(0, 2*np.pi, 1001, endpoint=False)
In [101]: np.sum(np.abs(np.cos(angles)))
Out[101]: 637.256653677874
In [102]: np.sum(np.abs(np.sin(angles)))
Out[102]: 637.2558690641631
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading