Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is numpy.prod() incorrectly returning negative results, or 0, for my long lists of natural numbers?

I'm just working on Project Euler problem 12, so I need to do some testing against numbers that are multiples of over 500 unique factors.

I figured that the array [1, 2, 3... 500] would be a good starting point, since the product of that array is the lowest possible such number. However, numpy.prod() returns zero for this array. I'm sure I'm missing something obvious, but what the hell is it?

>>> import numpy as np
>>> array = []
>>> for i in range(1,100):
...   array.append(i)
... 
>>> np.prod(array)
0
>>> array.append(501)
>>> np.prod(array)
0
>>> array.append(5320934)
>>> np.prod(array)
0
like image 545
Ben Quigley Avatar asked Aug 22 '16 22:08

Ben Quigley


People also ask

How do you calculate numerical negative value for all elements in a given NumPy array?

Now suppose we have an array of numbers: A = [1,2,3,-1,-2,-3,0] So, the negative value of A is A'=[-1,-2,-3,1,2,3,0]. So, for finding the numerical negative value of an element we have to use numpy. negative() function of NumPy library.

What is a correct syntax to create a NumPy array?

The array object in NumPy is called ndarray . We can create a NumPy ndarray object by using the array() function.

When using NumPy in python how do you check the dimensionality number and length of dimensions of an object called my object?

Alternatively, we can use the shape attribute to get the size of each dimension and then use len() function for the number of dimensions. Use numpy. array() function to convert a list to a NumPy array and use one of the above two ways to get the number of dimensions.

How do you negative an array in Python?

negative() function is used when we want to compute the negative of array elements. It returns element-wise negative value of an array or negative value of a scalar. Parameters : arr : [array_like or scalar] Input array.


1 Answers

Note that Python uses "unlimited" integers, but in numpy everything is typed, and so it is a "C"-style (probably 64-bit) integer here. You're probably experiencing an overflow.

If you look at the documentation for numpy.prod, you can see the dtype parameter:

The type of the returned array, as well as of the accumulator in which the elements are multiplied.

There are a few things you can do:

  1. Drop back to Python, and multiply using its "unlimited integers" (see this question for how to do so).

  2. Consider whether you actually need to find the product of such huge numbers. Often, when you're working with the product of very small or very large numbers, you switch to sums of logarithms. As @WarrenWeckesser notes, this is obviously imprecise (it's not like taking the exponent at the end will give you the exact solution) - rather, it's used to gauge whether one product is growing faster than another.

like image 94
Ami Tavory Avatar answered Nov 10 '22 01:11

Ami Tavory