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

Implement functions from np.random #46

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions unumpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
True
"""
from ._multimethods import *
from .random import *

from ._version import get_versions

Expand Down
10 changes: 6 additions & 4 deletions unumpy/numpy_backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
from uarray import Dispatchable, wrap_single_convertor
from unumpy import ufunc, ufunc_list, ndarray, dtype
from unumpy import ufunc, ufunc_list, ndarray, dtype, random
import unumpy
import functools

Expand All @@ -24,11 +24,13 @@ def __ua_function__(method, args, kwargs):
if method in _implementations:
return _implementations[method](*args, **kwargs)

if not hasattr(np, method.__name__):
if hasattr(np, method.__name__):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be replaced by checking the __module__ of the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this :/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to check method.__module__. It will be different depending on the submodule that the method is in.

For simplicity, you can also move everything in _multimethods.py to __init__.py.

return getattr(np, method.__name__)(*args, **kwargs)
elif hasattr(np.random, method.__name__):
return getattr(np.random, method.__name__)(*args, **kwargs)
else:
return NotImplemented

return getattr(np, method.__name__)(*args, **kwargs)


@wrap_single_convertor
def __ua_convert__(value, dispatch_type, coerce):
Expand Down
1 change: 1 addition & 0 deletions unumpy/random/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._multimethods import *
prasunanand marked this conversation as resolved.
Show resolved Hide resolved
306 changes: 306 additions & 0 deletions unumpy/random/_multimethods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
import functools
import operator
from uarray import create_multimethod, mark_as, all_of_type, Dispatchable
import builtins

create_numpy = functools.partial(create_multimethod, domain="numpy")

from .._multimethods import ndarray, _self_argreplacer


@create_numpy(_self_argreplacer)
prasunanand marked this conversation as resolved.
Show resolved Hide resolved
@all_of_type(ndarray)
def rand(*tup):
return tup
prasunanand marked this conversation as resolved.
Show resolved Hide resolved


@create_numpy(_self_argreplacer)
prasunanand marked this conversation as resolved.
Show resolved Hide resolved
@all_of_type(ndarray)
def randn(*tup):
return tup
prasunanand marked this conversation as resolved.
Show resolved Hide resolved


@create_numpy(_self_argreplacer)
prasunanand marked this conversation as resolved.
Show resolved Hide resolved
@all_of_type(ndarray)
def randint(low, high=None, size=None, dtype="l"):
return low
prasunanand marked this conversation as resolved.
Show resolved Hide resolved


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def random_integers(low, high=None, size=None):
return low


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def random_sample(size=None):
return size


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def random(size=None):
return size


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def ranf(size=None):
return size


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def sample(size=None):
return size


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def choice(a, size=None, replace=True, p=None):
return (a, size, replace, p)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def bytes(length):
return length


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def shuffle(x):
return x


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def permutation(x):
return x


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def beta(a, b, size=None):
return (a, b, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def binomial(n, p, size=None):
return (n, p, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def dirichlet(alpha, size=None):
return (alpha, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def exponential(scale, size=None):
return (scale, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def f(dfnum, dfden, size=None):
return (dfnum, dfden, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def gamma(shape, scale=1.0, size=None):
return (shape, scale, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def geometric(p, size=None):
return (p, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def gumbel(loc=0.0, scale=1.0, size=None):
return (loc, scale, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def hypergeometric(ngood, nbad, nsample, size=None):
return (ngood, nbad, nsample, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def laplace(loc=0.0, scale=1.0, size=None):
return (loc, scale, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def logistic(loc=0.0, scale=1.0, size=None):
return (loc, scale, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def lognormal(mean=0.0, sigma=1.0, size=None):
return (mean, sigma, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def logseries(p, size=None):
return (p, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def multinomial(n, pvals, size=None):
return (n, pvals, size)


# check
@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def multivariate_normal(mean, cov, size=None, check_valid=None, tol=None):
return (mean, cov, size, check_valid, tol)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def negative_binomial(n, p, size=None):
return (n, p, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def noncentral_chisquare(df, nonc, size=None):
return (df, nonc, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def noncentral_f(dfnum, dfden, nonc, size=None):
return (dfnum, dfden, nonc, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def normal(loc=0.0, scale=1.0, size=None):
return (loc, scale, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def pareto(a, size=None):
return (a, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def poisson(lam=1.0, size=None):
return (lam, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def power(a, size=None):
return (a, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def rayleigh(scale=1.0, size=None):
return (scale, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def standard_cauchy(size=None):
return size


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def standard_exponential(size=None):
return size


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def standard_gamma(shape, size=None):
return (shape, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def standard_normal(size=None):
return size


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def standard_t(df, size=None):
return (df, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def triangular(left, mode, right, size=None):
return (left, mode, right, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def uniform(low=0.0, high=1.0, size=None):
return (low, high, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def vonmises(mu, kappa, size=None):
return (mu, kappa, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def wald(mean, scale, size=None):
return (mean, scale, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def weibull(a, size=None):
return (a, size)


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def zipf(a, size=None):
return (a, size)


# discuss: RandomState


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def seed(seed=None):
return seed


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def get_state():
return


@create_numpy(_self_argreplacer)
@all_of_type(ndarray)
def set_state(state):
return state