Skip to content

Commit

Permalink
empty_like, eye, from_dlpack, full, full_like, linspace, meshgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Dec 27, 2023
1 parent efb61c9 commit a416042
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 4 deletions.
20 changes: 19 additions & 1 deletion src/ragged/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,32 @@

from __future__ import annotations

from ._creation import arange, asarray, empty
from ._creation import (
arange,
asarray,
empty,
empty_like,
eye,
from_dlpack,
full,
full_like,
linspace,
meshgrid,
)
from ._obj import array

__all__ = [
# _creation
"arange",
"asarray",
"empty",
"empty_like",
"eye",
"from_dlpack",
"full",
"full_like",
"linspace",
"meshgrid",
# _obj
"array",
]
257 changes: 257 additions & 0 deletions src/ragged/common/_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def asarray(
function reuses the existing memory buffer if possible and copies
otherwise.
Returns:
An array containing the data from `obj`.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.asarray.html
"""
return array(obj, dtype=dtype, device=device, copy=copy)
Expand All @@ -127,10 +130,264 @@ def empty(
data type is `np.float64`.
device: Device on which to place the created array.
Returns:
An array containing uninitialized data.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.empty.html
"""

assert shape, "TODO"
assert dtype, "TODO"
assert device, "TODO"
assert False, "TODO"


def empty_like(
x: array, /, *, dtype: None | Dtype = None, device: None | Device = None
) -> array:
"""
Returns an uninitialized array with the same shape as an input array x.
Args:
x: Input array from which to derive the output array shape.
dtype: Output array data type. If `dtype` is `None`, the output array
data type is inferred from `x`.
device: Device on which to place the created array. If `device` is
`None`, output array device is inferred from `x`.
Returns:
An array having the same shape as `x` and containing uninitialized data.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.empty_like.html
"""

assert x, "TODO"
assert dtype, "TODO"
assert device, "TODO"
assert False, "TODO"


def eye(
n_rows: int,
n_cols: None | int = None,
/,
*,
k: int = 0,
dtype: None | Dtype = None,
device: None | Device = None,
) -> array:
"""
Returns a two-dimensional array with ones on the kth diagonal and zeros elsewhere.
Args:
n_rows: Number of rows in the output array.
n_cols: Number of columns in the output array. If `None`, the default
number of columns in the output array is equal to `n_rows`.
k: Index of the diagonal. A positive value refers to an upper diagonal,
a negative value to a lower diagonal, and 0 to the main diagonal.
dtype: Output array data type. If `dtype` is `None`, the output array
data type is `np.float64`.
device: Device on which to place the created array.
Returns:
An array where all elements are equal to zero, except for the kth
diagonal, whose values are equal to one.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.eye.html
"""

assert n_rows, "TODO"
assert n_cols, "TODO"
assert k, "TODO"
assert dtype, "TODO"
assert device, "TODO"
assert False, "TODO"


def from_dlpack(x: object, /) -> array:
"""
Returns a new array containing the data from another (array) object with a `__dlpack__` method.
Args:
x: Input (array) object.
Returns:
An array containing the data in `x`.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.from_dlpack.html
"""

assert x, "TODO"
assert False, "TODO"


def full(
shape: int | tuple[int, ...],
fill_value: bool | int | float | complex,
*,
dtype: None | Dtype = None,
device: None | Device = None,
) -> array:
"""
Returns a new array having a specified shape and filled with fill_value.
Args:
shape: Output array shape.
fill_value: Fill value.
dtype: Output array data type. If `dtype` is `None`, the output array
data type is inferred from `fill_value` according to the following
rules:
- if the fill value is an `int`, the output array data type is
`np.int64`.
- if the fill value is a `float`, the output array data type
is `np.float64`.
- if the fill value is a `complex` number, the output array
data type is `np.complex128`.
- if the fill value is a `bool`, the output array is
`np.bool_`.
device: Device on which to place the created array.
Returns:
An array where every element is equal to fill_value.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.full.html
"""

assert shape, "TODO"
assert fill_value, "TODO"
assert dtype, "TODO"
assert device, "TODO"
assert False, "TODO"


def full_like(
x: array,
/,
fill_value: bool | int | float | complex,
*,
dtype: None | Dtype = None,
device: None | Device = None,
) -> array:
"""
Returns a new array filled with fill_value and having the same shape as an input array x.
Args:
x: Input array from which to derive the output array shape.
fill_value: Fill value.
dtype: Output array data type. If `dtype` is `None`, the output array
data type is inferred from `x`.
device: Device on which to place the created array. If `device` is
`None`, the output array device is inferred from `x`.
Returns:
An array having the same shape as `x` and where every element is equal
to `fill_value`.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.full_like.html
"""

assert x, "TODO"
assert fill_value, "TODO"
assert dtype, "TODO"
assert device, "TODO"
assert False, "TODO"


def linspace(
start: int | float | complex,
stop: int | float | complex,
/,
num: int,
*,
dtype: None | Dtype = None,
device: None | Device = None,
endpoint: bool = True,
) -> array:
r"""
Returns evenly spaced numbers over a specified interval.
Let `N` be the number of generated values (which is either `num` or `num+1`
depending on whether `endpoint` is `True` or `False`, respectively). For
real-valued output arrays, the spacing between values is given by
$$\Delta_{\textrm{real}} = \frac{\textrm{stop} - \textrm{start}}{N - 1}$$
For complex output arrays, let `a = real(start)`, `b = imag(start)`,
`c = real(stop)`, and `d = imag(stop)`. The spacing between complex values
is given by
$$\Delta_{\textrm{complex}} = \frac{c-a}{N-1} + \frac{d-b}{N-1} j$$
Args:
start: The start of the interval.
stop: The end of the interval. If `endpoint` is `False`, the function
generates a sequence of `num+1` evenly spaced numbers starting with
`start` and ending with `stop` and exclude the `stop` from the
returned array such that the returned array consists of evenly
spaced numbers over the half-open interval `[start, stop)`. If
endpoint is `True`, the output array consists of evenly spaced
numbers over the closed interval `[start, stop]`.
num: Number of samples. Must be a nonnegative integer value.
dtype: Output array data type. Should be a floating-point data type.
If `dtype` is `None`,
- if either `start` or `stop` is a `complex` number, the
output data type is `np.complex128`.
- if both `start` and `stop` are real-valued, the output data
type is `np.float64`.
device: Device on which to place the created array.
endpoint: Boolean indicating whether to include `stop` in the interval.
Returns:
A one-dimensional array containing evenly spaced values.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.linspace.html
"""

assert start, "TODO"
assert stop, "TODO"
assert num, "TODO"
assert dtype, "TODO"
assert device, "TODO"
assert endpoint, "TODO"
assert False, "TODO"


def meshgrid(*arrays: array, indexing: str = "xy") -> list[array]:
"""
Returns coordinate matrices from coordinate vectors.
Args:
arrays: An arbitrary number of one-dimensional arrays representing
grid coordinates. Each array should have the same numeric data type.
indexing: Cartesian `"xy"` or matrix `"ij"` indexing of output. If
provided zero or one one-dimensional vector(s) (i.e., the zero- and
one-dimensional cases, respectively), the `indexing` keyword has no
effect and should be ignored.
Returns:
List of `N` arrays, where `N` is the number of provided one-dimensional
input arrays. Each returned array must have rank `N`. For `N`
one-dimensional arrays having lengths `Ni = len(xi)`,
- if matrix indexing `"ij"`, then each returned array must have the
shape `(N1, N2, N3, ..., Nn)`.
- if Cartesian indexing `"xy"`, then each returned array must have
shape `(N2, N1, N3, ..., Nn)`.
Accordingly, for the two-dimensional case with input one-dimensional
arrays of length `M` and `N`, if matrix indexing `"ij"`, then each
returned array must have shape `(M, N)`, and, if Cartesian indexing
`"xy"`, then each returned array must have shape `(N, M)`.
Similarly, for the three-dimensional case with input one-dimensional
arrays of length `M`, `N`, and `P`, if matrix indexing `"ij"`, then
each returned array must have shape `(M, N, P)`, and, if Cartesian
indexing `"xy"`, then each returned array must have shape `(N, M, P)`.
Each returned array should have the same data type as the input arrays.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.meshgrid.html
"""

assert arrays, "TODO"
assert indexing, "TODO"
assert False, "TODO"
20 changes: 19 additions & 1 deletion src/ragged/v202212/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,32 @@

from __future__ import annotations

from ._creation import arange, asarray, empty
from ._creation import (
arange,
asarray,
empty,
empty_like,
eye,
from_dlpack,
full,
full_like,
linspace,
meshgrid,
)
from ._obj import array

__all__ = [
# _creation
"arange",
"asarray",
"empty",
"empty_like",
"eye",
"from_dlpack",
"full",
"full_like",
"linspace",
"meshgrid",
# _obj
"array",
]
26 changes: 24 additions & 2 deletions src/ragged/v202212/_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@

from __future__ import annotations

from ..common._creation import arange, asarray, empty
from ..common._creation import (
arange,
asarray,
empty,
empty_like,
eye,
from_dlpack,
full,
full_like,
linspace,
meshgrid,
)

__all__ = ["arange", "asarray", "empty"]
__all__ = [
"arange",
"asarray",
"empty",
"empty_like",
"eye",
"from_dlpack",
"full",
"full_like",
"linspace",
"meshgrid",
]

0 comments on commit a416042

Please sign in to comment.