Skip to content

Use math.prod #308

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

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions array_api_tests/hypothesis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import re
from contextlib import contextmanager
from functools import reduce, wraps
from functools import wraps
import math
from operator import mul
import struct
from typing import Any, List, Mapping, NamedTuple, Optional, Sequence, Tuple, Union

Expand Down Expand Up @@ -217,9 +216,6 @@ def all_floating_dtypes() -> SearchStrategy[DataType]:
# Size to use for 2-dim arrays
SQRT_MAX_ARRAY_SIZE = int(math.sqrt(MAX_ARRAY_SIZE))

# np.prod and others have overflow and math.prod is Python 3.8+ only
def prod(seq):
return reduce(mul, seq, 1)

# hypotheses.strategies.tuples only generates tuples of a fixed size
def tuples(elements, *, min_size=0, max_size=None, unique_by=None, unique=False):
Expand All @@ -233,7 +229,7 @@ def shapes(**kw):
kw.setdefault('min_dims', 0)
kw.setdefault('min_side', 0)
return xps.array_shapes(**kw).filter(
lambda shape: prod(i for i in shape if i) < MAX_ARRAY_SIZE
lambda shape: math.prod(i for i in shape if i) < MAX_ARRAY_SIZE
)


Expand All @@ -245,7 +241,7 @@ def matrix_shapes(draw, stack_shapes=shapes()):
stack_shape = draw(stack_shapes)
mat_shape = draw(xps.array_shapes(max_dims=2, min_dims=2))
shape = stack_shape + mat_shape
assume(prod(i for i in shape if i) < MAX_ARRAY_SIZE)
assume(math.prod(i for i in shape if i) < MAX_ARRAY_SIZE)
return shape

square_matrix_shapes = matrix_shapes().filter(lambda shape: shape[-1] == shape[-2])
Expand Down Expand Up @@ -290,7 +286,7 @@ def mutually_broadcastable_shapes(
)
.map(lambda BS: BS.input_shapes)
.filter(lambda shapes: all(
prod(i for i in s if i > 0) < MAX_ARRAY_SIZE for s in shapes
math.prod(i for i in s if i > 0) < MAX_ARRAY_SIZE for s in shapes
))
)

Expand Down Expand Up @@ -321,7 +317,7 @@ def positive_definite_matrices(draw, dtypes=floating_dtypes):
base_shape = draw(shapes())
n = draw(integers(0, 8)) # 8 is an arbitrary small but interesting-enough value
shape = base_shape + (n, n)
assume(prod(i for i in shape if i) < MAX_ARRAY_SIZE)
assume(math.prod(i for i in shape if i) < MAX_ARRAY_SIZE)
dtype = draw(dtypes)
return broadcast_to(eye(n, dtype=dtype), shape)

Expand Down
1 change: 1 addition & 0 deletions array_api_tests/test_statistical_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def test_std(x, data):
)
# We can't easily test the result(s) as standard deviation methods vary a lot


def _sum_condition_number(elements):
sum_abs = sum([abs(i) for i in elements])
abs_sum = abs(sum(elements))
Expand Down
Loading