Creation#
- class ivy.data_classes.array.creation._ArrayWithCreation[source]#
Bases:
ABC
- _abc_impl = <_abc._abc_data object>#
- asarray(*, copy=None, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.asarray. This method simply wraps the function, and so the docstring for ivy.asarray also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.copy (
Optional
[bool
], default:None
) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy and must raise a ValueError in case a copy would be necessary. If None, the function must reuse existing memory buffer if possible and copy otherwise. Default:None
.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – datatype, optional. Datatype is inferred from the input data.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. Default:None
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – An array interpretation of
self
.
Examples
With list of lists as input:
>>> ivy.asarray([[1,2],[3,4]]) ivy.array([[1, 2], [3, 4]])
With tuple of lists as input:
>>> ivy.asarray(([1.4,5.6,5.5],[3.1,9.1,7.5])) ivy.array([[1.39999998, 5.5999999 , 5.5 ], [3.0999999 , 9.10000038, 7.5 ]])
With ndarray as input:
>>> x = ivy.np.ndarray(shape=(2,2), order='C') >>> x array([[6.90786433e-310, 6.90786433e-310], [6.90786433e-310, 6.90786433e-310]]) >>> ivy.asarray(x) ivy.array([[6.90786433e-310, 6.90786433e-310], [6.90786433e-310, 6.90786433e-310]])
- copy_array(*, to_ivy_array=True, out=None)[source]#
ivy.Array instance method variant of ivy.copy_array. This method simply wraps the function, and so the docstring for ivy.copy_array also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input arrayto_ivy_array (
bool
, default:True
) – boolean, if True the returned array will be an ivy.Array object otherwise returns an ivy.NativeArray object (i.e. a torch.tensor, np.array, etc., depending on the backend), defaults to True.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – a copy of the input array
x
.
- empty_like(*, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.empty_like. This method simply wraps the function, and so the docstring for ivy.empty_like also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array from which to derive the output array shape.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. If dtype is None, the output array data type must be inferred fromself
. Default:None
.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. If device is None, the output array device must be inferred fromself
. Default:None
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array having the same shape as
self
and containing uninitialized data.
- from_dlpack(*, out=None)[source]#
ivy.Array instance method variant of ivy.from_dlpack. This method simply wraps the function, and so the docstring for ivy.from_dlpack also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array containing the data in
self
.
- full_like(fill_value, *, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.full_like. This method simply wraps the function, and so the docstring for ivy.full_like also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array from which to derive the output array shape.fill_value (
float
) – Scalar fill valuedtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. Ifdtype
is None, the output array data type must be inferred fromself
. Default:None
.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. Ifdevice
isNone
, the output array device must be inferred fromself
. Default:None
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array having the same shape as
self
and where every element is equal tofill_value
.
Examples
With
int
datatype:>>> x = ivy.array([1,2,3]) >>> fill_value = 0 >>> x.full_like(fill_value) ivy.array([0, 0, 0])
With float datatype:
>>> fill_value = 0.000123 >>> x = ivy.array(ivy.ones(5)) >>> y = x.full_like(fill_value) >>> print(y) ivy.array([0.000123, 0.000123, 0.000123, 0.000123, 0.000123])
With
ivy.Array
input:>>> x = ivy.array([1, 2, 3, 4, 5, 6]) >>> fill_value = 1 >>> y = x.full_like(fill_value) >>> print(y) ivy.array([1, 1, 1, 1, 1, 1])
- linspace(stop, /, num, *, axis=None, endpoint=True, dtype=None, device=None, out=None)[source]#
- Return type:
Array
- logspace(stop, /, num, *, base=10.0, axis=0, endpoint=True, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.logspace. This method simply wraps the function, and so the docstring for ivy.logspace also applies to this method with minimal changes.
- Parameters:
self (
Array
) – First value in the range in log space. base ** start is the starting value in the sequence. Can be an array or a float.stop (
Union
[Array
,NativeArray
,float
]) – Last value in the range in log space. base ** stop is the final value in the sequence. Can be an array or a float.num (
int
) – Number of values to generate.base (
float
, default:10.0
) – The base of the log space. Default is 10.0axis (
int
, default:0
) – Axis along which the operation is performed. Relevant only if start or stop are array-like. Default is 0.endpoint (
bool
, default:True
) – If True, stop is the last sample. Otherwise, it is not included. Default is True.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – The data type of the output tensor. If None, the dtype of on_value is used or if that is None, the dtype of off_value is used, or if that is None, defaults to float32. Default is None.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Default is None.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to. Default is None.
- Return type:
Array
- Returns:
ret – Tensor of evenly-spaced values in log space.
Both the description and the type hints above assumes an array input for
simplicity, but this function is *nestable, and therefore also accepts*
ivy.Container
instances in place of any of the arguments.
Examples
With float input:
>>> x = ivy.array([1, 2]) >>> y = ivy.array([4, 5]) >>> x.logspace(y, 4) ivy.array([[1.e+01, 1.e+02], [1.e+02, 1.e+03], [1.e+03, 1.e+04], [1.e+04, 1.e+05])
>>> x.logspace(y, 4, axis = 1) ivy.array([[[1.e+01, 1.e+02, 1.e+03, 1.e+04], [1.e+02, 1.e+03, 1.e+04, 1.e+05]]])
>>> x = ivy.array([1, 2]) >>> y = ivy.array([4]) # Broadcasting example >>> x.logspace(y, 4) ivy.array([[10., 100.] [100., 464.15888336] [1000., 2154.43469003] [10000., 10000.]])
- meshgrid(*arrays, sparse=False, indexing='xy')[source]#
ivy.Array instance method variant of ivy.meshgrid. This method simply wraps the function, and so the docstring for ivy.meshgrid also applies to this method with minimal changes.
- Parameters:
self (
Array
) – one-dimensional input array.arrays (
Union
[Array
,NativeArray
]) – an arbitrary number of one-dimensional arrays representing grid coordinates. Each array should have the same numeric data type.sparse (
bool
, default:False
) – if True, a sparse grid is returned in order to conserve memory. Default:False
.indexing (
str
, default:'xy'
) – 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), theindexing
keyword has no effect and should be ignored. Default:'xy'
.
- Return type:
List
[Array
]- Returns:
ret – list of N arrays, where
N
is the number of provided one-dimensional input arrays. Each returned array must have rankN
. ForN
one-dimensional arrays having lengthsNi = len(xi)
.
- native_array(*, dtype=None, device=None)[source]#
ivy.Array instance method variant of ivy.native_array. This method simply wraps the function, and so the docstring for ivy.native_array also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – datatype, optional. Datatype is inferred from the input data.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. Default:None
.
- Return type:
NativeArray
- Returns:
ret – A native array interpretation of
self
.
- one_hot(depth, /, *, on_value=None, off_value=None, axis=None, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.one_hot. This method simply wraps the function, and so the docstring for ivy.one_hot also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array containing the indices for which the ones should be scattereddepth (
int
) – Scalar defining the depth of the one-hot dimension.on_value (
Optional
[Number
], default:None
) – Value to fill in output whenindices[j] == i
. Default 1.off_value (
Optional
[Number
], default:None
) – Value to fill in output whenindices[j] != i
. Default 0.axis (
Optional
[int
], default:None
) – The axis to scatter on. The default is-1
which is the last axis.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – The data type of the output array. If None, the data type of the on_value is used, or if that is None, the data type of the off_value is used. Default float32.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Same as x if None.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – Tensor of zeros with the same shape and type as a, unless dtype provided which overrides.
Examples
With
ivy.Array
inputs:>>> x = ivy.array([3, 1]) >>> y = 5 >>> z = x.one_hot(5) >>> print(z) ivy.array([[0., 0., 0., 1., 0.], ... [0., 1., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, y) ivy.array([[1., 0., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, 5, out=z) ivy.array([[1., 0., 0., 0., 0.]]) >>> print(z) ivy.array([[1., 0., 0., 0., 0.]])
- ones_like(*, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.ones_like. This method simply wraps the function, and so the docstring for ivy.ones_like also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array from which to derive the output array shape.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. Ifdtype
isNone
, the output array data type must be inferred fromself
. DefaultNone
.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. If device isNone
, the output array device must be inferred fromself
. Default:None
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array having the same shape as
self
and filled with ones.
- tril(*, k=0, out=None)[source]#
ivy.Array instance method variant of ivy.tril. This method simply wraps the function, and so the docstring for ivy.tril also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array having shape (…, M, N) and whose innermost two dimensions form MxN matrices.k (
int
, default:0
) – diagonal above which to zero elements. If k = 0, the diagonal is the main diagonal. If k < 0, the diagonal is below the main diagonal. If k > 0, the diagonal is above the main diagonal. Default:0
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array containing the lower triangular part(s). The returned array must have the same shape and data type as
self
. All elements above the specified diagonal k must be zeroed. The returned array should be allocated on the same device asself
.
- triu(*, k=0, out=None)[source]#
ivy.Array instance method variant of ivy.triu. This method simply wraps the function, and so the docstring for ivy.triu also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array having shape (…, M, N) and whose innermost two dimensions form MxN matrices. *,k (
int
, default:0
) – diagonal below which to zero elements. If k = 0, the diagonal is the main diagonal. If k < 0, the diagonal is below the main diagonal. If k > 0, the diagonal is above the main diagonal. Default:0
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array containing the upper triangular part(s). The returned array must have the same shape and data type as
self
. All elements below the specified diagonal k must be zeroed. The returned array should be allocated on the same device asself
.
- zeros_like(*, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.zeros_like. This method simply wraps the function, and so the docstring for ivy.zeros_like also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array from which to derive the output array shape.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. Ifdtype
isNone
, the output array data type must be inferred fromself
. Default:None
.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. Ifdevice
isNone
, the output array device must be inferred fromself
. Default:None
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array having the same shape as
self
and filled withzeros
.
This should have hopefully given you an overview of the creation submodule, if you have any questions, please feel free to reach out on our discord!