From 456b4b4d684033cec73bc113f0ea77c8e8c115e9 Mon Sep 17 00:00:00 2001 From: Shah Tanzeel Ahmed Date: Tue, 15 Aug 2023 18:56:47 +0500 Subject: [PATCH 1/5] adding logseries function to numpy frontend --- .../frontends/numpy/random/functions.py | 9 +++++ .../test_numpy/test_random/test_functions.py | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/ivy/functional/frontends/numpy/random/functions.py b/ivy/functional/frontends/numpy/random/functions.py index fc86b3b5e103b..75324e30f1367 100644 --- a/ivy/functional/frontends/numpy/random/functions.py +++ b/ivy/functional/frontends/numpy/random/functions.py @@ -233,3 +233,12 @@ def triangular(left, mode, right, size=None): right - (right - mode) * ((1 - u) * (right - mode) / (right - left)) ** 0.5 ) return ivy.where(condition, values1, values2) + +@to_ivy_arrays_and_back +@from_zero_dim_arrays_to_scalar +def logseries(p=0, size=None): + if p <= 0 or p >= 1: + raise ValueError("p value must be in the open interval (0, 1)") + x = ivy.random_uniform(low=0.0, high=1.0, shape=size, dtype="float64") + ret = (ivy.log(1-x) / (ivy.log(1-p)+1)) + return ret \ No newline at end of file diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py index b2e8d1a2d5e00..b2d4cd183143e 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py @@ -972,3 +972,41 @@ def test_numpy_triangular( right=right, size=size, ) + + +@handle_frontend_test( + fn_tree="numpy.random.logseries", + input_dtypes=helpers.get_dtypes("float", index=2), + p=st.floats( + allow_nan=False, + allow_infinity=False, + width=32, + min_value=0, + exclude_min=True, + max_value=1, + exclude_max=True, + ), + size=helpers.get_shape(allow_none=True), + test_with_out=st.just(False), +) +def test_numpy_logseries( + input_dtypes, + frontend, + test_flags, + fn_tree, + backend_fw, + on_device, + p, + size, +): + helpers.test_frontend_function( + input_dtypes=input_dtypes, + backend_to_test=backend_fw, + test_flags=test_flags, + frontend=frontend, + fn_tree=fn_tree, + on_device=on_device, + test_values=False, + p=p, + size=size, + ) From d927384c74e737384eda0ec2ddb4af0568b96098 Mon Sep 17 00:00:00 2001 From: Shah Tanzeel Ahmed Date: Tue, 15 Aug 2023 19:47:56 +0500 Subject: [PATCH 2/5] adding logseries function to numpy frontend --- ivy/functional/frontends/numpy/random/functions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ivy/functional/frontends/numpy/random/functions.py b/ivy/functional/frontends/numpy/random/functions.py index 75324e30f1367..19a70462674cb 100644 --- a/ivy/functional/frontends/numpy/random/functions.py +++ b/ivy/functional/frontends/numpy/random/functions.py @@ -239,6 +239,5 @@ def triangular(left, mode, right, size=None): def logseries(p=0, size=None): if p <= 0 or p >= 1: raise ValueError("p value must be in the open interval (0, 1)") - x = ivy.random_uniform(low=0.0, high=1.0, shape=size, dtype="float64") - ret = (ivy.log(1-x) / (ivy.log(1-p)+1)) + ret = -ivy.pow(p,size) / ivy.multiply(ivy.log(1-p),size) return ret \ No newline at end of file From 9ddd87509b73f52edcc529a3951946a8dc7ef0cc Mon Sep 17 00:00:00 2001 From: Shah Tanzeel Ahmed Date: Fri, 25 Aug 2023 15:27:00 +0500 Subject: [PATCH 3/5] adding logseries function to numpy frontend --- ivy/functional/frontends/numpy/random/functions.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ivy/functional/frontends/numpy/random/functions.py b/ivy/functional/frontends/numpy/random/functions.py index 19a70462674cb..8c972909c9807 100644 --- a/ivy/functional/frontends/numpy/random/functions.py +++ b/ivy/functional/frontends/numpy/random/functions.py @@ -239,5 +239,9 @@ def triangular(left, mode, right, size=None): def logseries(p=0, size=None): if p <= 0 or p >= 1: raise ValueError("p value must be in the open interval (0, 1)") - ret = -ivy.pow(p,size) / ivy.multiply(ivy.log(1-p),size) + r = ivy.log(1 - p) + u= ivy.random_uniform(low=0.0,high=1.0,shape=size) + v = ivy.random_uniform(low=0.0, high=1.0, shape=size) + q= 1 - ivy.exp(r * u) + ret = 1 + ivy.log(v) / ivy.log(q) return ret \ No newline at end of file From 6c09f1a5b664d4a160d8bb1a0dbde2eea7bf594d Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Wed, 30 Aug 2023 09:06:26 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frontends/numpy/random/functions.py | 27 ++++--- .../test_numpy/test_random/test_functions.py | 76 +++++++++---------- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/ivy/functional/frontends/numpy/random/functions.py b/ivy/functional/frontends/numpy/random/functions.py index 72d35f31925a1..a6f0a419df42e 100644 --- a/ivy/functional/frontends/numpy/random/functions.py +++ b/ivy/functional/frontends/numpy/random/functions.py @@ -101,6 +101,19 @@ def lognormal(mean=0.0, sigma=1.0, size=None): return ret +@to_ivy_arrays_and_back +@from_zero_dim_arrays_to_scalar +def logseries(p=0, size=None): + if p <= 0 or p >= 1: + raise ValueError("p value must be in the open interval (0, 1)") + r = ivy.log(1 - p) + u = ivy.random_uniform(low=0.0, high=1.0, shape=size) + v = ivy.random_uniform(low=0.0, high=1.0, shape=size) + q = 1 - ivy.exp(r * u) + ret = 1 + ivy.log(v) / ivy.log(q) + return ret + + @to_ivy_arrays_and_back @from_zero_dim_arrays_to_scalar def multinomial(n, pvals, size=None): @@ -219,19 +232,6 @@ def triangular(left, mode, right, size=None): ) return ivy.where(condition, values1, values2) - -@to_ivy_arrays_and_back -@from_zero_dim_arrays_to_scalar -def logseries(p=0, size=None): - if p <= 0 or p >= 1: - raise ValueError("p value must be in the open interval (0, 1)") - r = ivy.log(1 - p) - u= ivy.random_uniform(low=0.0,high=1.0,shape=size) - v = ivy.random_uniform(low=0.0, high=1.0, shape=size) - q= 1 - ivy.exp(r * u) - ret = 1 + ivy.log(v) / ivy.log(q) - return ret - @to_ivy_arrays_and_back @from_zero_dim_arrays_to_scalar @@ -265,4 +265,3 @@ def weibull(a, size=None): return 0 u = ivy.random_uniform(low=0.0, high=1.0, shape=size, dtype="float64") return ivy.pow(-ivy.log(1 - u), 1 / a) - diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py index 530dd198a3382..65e24eefbd4f7 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py @@ -439,6 +439,44 @@ def test_numpy_lognormal( ) +@handle_frontend_test( + fn_tree="numpy.random.logseries", + input_dtypes=helpers.get_dtypes("float", index=2), + p=st.floats( + allow_nan=False, + allow_infinity=False, + width=32, + min_value=0, + exclude_min=True, + max_value=1, + exclude_max=True, + ), + size=helpers.get_shape(allow_none=True), + test_with_out=st.just(False), +) +def test_numpy_logseries( + input_dtypes, + frontend, + test_flags, + fn_tree, + backend_fw, + on_device, + p, + size, +): + helpers.test_frontend_function( + input_dtypes=input_dtypes, + backend_to_test=backend_fw, + test_flags=test_flags, + frontend=frontend, + fn_tree=fn_tree, + on_device=on_device, + test_values=False, + p=p, + size=size, + ) + + # multinomial @handle_frontend_test( fn_tree="numpy.random.multinomial", @@ -1019,41 +1057,3 @@ def test_numpy_weibull( a=a, size=size, ) - - -@handle_frontend_test( - fn_tree="numpy.random.logseries", - input_dtypes=helpers.get_dtypes("float", index=2), - p=st.floats( - allow_nan=False, - allow_infinity=False, - width=32, - min_value=0, - exclude_min=True, - max_value=1, - exclude_max=True, - ), - size=helpers.get_shape(allow_none=True), - test_with_out=st.just(False), -) -def test_numpy_logseries( - input_dtypes, - frontend, - test_flags, - fn_tree, - backend_fw, - on_device, - p, - size, -): - helpers.test_frontend_function( - input_dtypes=input_dtypes, - backend_to_test=backend_fw, - test_flags=test_flags, - frontend=frontend, - fn_tree=fn_tree, - on_device=on_device, - test_values=False, - p=p, - size=size, - ) From c31b448ad4621181735685ff7d350ab7abe11eaa Mon Sep 17 00:00:00 2001 From: Shah Tanzeel Ahmed Date: Wed, 30 Aug 2023 21:36:41 +0500 Subject: [PATCH 5/5] adding logseries function to numpy frontend --- ivy/functional/frontends/numpy/random/functions.py | 2 +- .../test_frontends/test_numpy/test_random/test_functions.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/ivy/functional/frontends/numpy/random/functions.py b/ivy/functional/frontends/numpy/random/functions.py index a6f0a419df42e..8c794d902a945 100644 --- a/ivy/functional/frontends/numpy/random/functions.py +++ b/ivy/functional/frontends/numpy/random/functions.py @@ -104,7 +104,7 @@ def lognormal(mean=0.0, sigma=1.0, size=None): @to_ivy_arrays_and_back @from_zero_dim_arrays_to_scalar def logseries(p=0, size=None): - if p <= 0 or p >= 1: + if p < 0 or p >= 1: raise ValueError("p value must be in the open interval (0, 1)") r = ivy.log(1 - p) u = ivy.random_uniform(low=0.0, high=1.0, shape=size) diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py index 65e24eefbd4f7..240c61bd6063f 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py @@ -445,9 +445,7 @@ def test_numpy_lognormal( p=st.floats( allow_nan=False, allow_infinity=False, - width=32, min_value=0, - exclude_min=True, max_value=1, exclude_max=True, ),