Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy.random.uniform has incorrect default arguments #9153

Closed
MatheusCPatrocinio opened this issue Aug 22, 2023 · 2 comments
Closed

numpy.random.uniform has incorrect default arguments #9153

MatheusCPatrocinio opened this issue Aug 22, 2023 · 2 comments

Comments

@MatheusCPatrocinio
Copy link

MatheusCPatrocinio commented Aug 22, 2023

I've been learning how to use Numba and I was trying to make an approximation to pi using Monte Carlo method, as it follows:

#Compute an aproximation to pi using Monte Carlo:
import numpy as np
from numba import njit,jit,float64


@njit
#Area of a circle: A = pi*radius**2 => pi= A/(radius**2)
#Let's consider a quadrant [0,1]x[0,1]
#And our circle has radius=1
#Thus the Area of the circle that is present in the quadrant will be A/4

def find_pi(n:float64,radius=1.0):
  x,y = np.random.uniform(high=radius,size=n),np.random.uniform(high=radius,size=n) #here we are generating uniformly random points in the quadrant with x and y coordinates
  h = np.sqrt(x**2+y**2) #we need to find the distance between to origin of the quadrant and the point, i.e., the hypotenuse
  #now, every point that has hypotenuse smaller or equal than the radius 1 is part of the 1/4 of area of the circle
  #we are going to divide the number of points inside the radius over the total of points generated in the quadrant
  points_inside = np.where(h<=radius,1,0)
  fourth_area = points_inside.sum()/n #now I compute the aproximation of 1/4 of the circle's area
  pi = (fourth_area*4)/(radius**2)
  return pi

And I get this error message:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
[<ipython-input-15-33f07fb29275>](https://localhost:8080/#) in <cell line: 1>()
----> 1 find_pi(10_000_000.0)

1 frames
[/usr/local/lib/python3.10/dist-packages/numba/core/dispatcher.py](https://localhost:8080/#) in _compile_for_args(self, *args, **kws)
    466                 e.patch_message(msg)
    467 
--> 468             error_rewrite(e, 'typing')
    469         except errors.UnsupportedError as e:
    470             # Something unsupported is present in the user code, add help info

[/usr/local/lib/python3.10/dist-packages/numba/core/dispatcher.py](https://localhost:8080/#) in error_rewrite(e, issue_type)
    407                 raise e
    408             else:
--> 409                 raise e.with_traceback(None)
    410 
    411         argtypes = []

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in method uniform of numpy.random.mtrand.RandomState object at 0x7e9d9658b840>) found for signature:
 
 >>> uniform(high=float64, size=float64)
 
There are 4 candidate implementations:
      - Of which 4 did not match due to:
      Overload in function '_OverloadWrapper._build.<locals>.ol_generated': File: numba/core/overload_glue.py: Line 129.
        With argument(s): '(high=float64, size=float64)':
       Rejected as the implementation raised a specific error:
         TypingError: missing a required argument: 'a'
  raised from /usr/local/lib/python3.10/dist-packages/numba/core/typing/templates.py:412

During: resolving callee type: Function(<built-in method uniform of numpy.random.mtrand.RandomState object at 0x7e9d9658b840>)
During: typing of call at <ipython-input-13-ec36feddfd93> (14)


File "<ipython-input-13-ec36feddfd93>", line 14:
def find_pi(n:float64,radius=1.0):
  x,y = np.random.uniform(high=radius,size=n),np.random.uniform(high=radius,size=n) #here we are generating uniformly random points in the quadrant with x and y coordinates
  ^

Could someone help me? I've tried to set all the variables to float64 type, but haven't been successfull.

-->

  • [x ] I have tried using the latest released version of Numba (most recent is
    visible in the change log (https://github.com/numba/numba/blob/main/CHANGE_LOG).
  • [ x] I have included a self contained code sample to reproduce the problem.
    i.e. it's possible to run as 'python bug.py'.
@sklam
Copy link
Member

sklam commented Aug 23, 2023

The problem:

 Rejected as the implementation raised a specific error:
         TypingError: missing a required argument: 'a'

is caused by a resolved issue #8161 that the argument names in Numba's implementation does not match NumPy's.

However, once you use 0.58.0rc1 or the latest main (bd7ebcf), the error becomes:

   Rejected as the implementation raised a specific error:
     TypingError: missing a required argument: 'low'

There's another issue that the default argument is not matching NumPy's.

For now, you can workaround this by defining low as well; For example

x,y = np.random.uniform(low=0.0, high=radius,size=n),np.random.uniform(low=0.0, high=radius,size=n)

@sklam sklam changed the title Numba Error - Failed in nopython mode pipeline (step: nopython frontend) numpy.random.uniform has incorrect default arguments Aug 23, 2023
@sklam
Copy link
Member

sklam commented Aug 23, 2023

(renamed issue to provide more detail of the problem)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants