use crate::prelude_dev::*;
use num::complex::ComplexFloat;
use num::Num;
pub trait ArangeAPI<Inp>: Sized {
type Out;
fn arange_f(self) -> Result<Self::Out>;
fn arange(self) -> Self::Out {
Self::arange_f(self).unwrap()
}
}
pub fn arange<Args, Inp>(param: Args) -> Args::Out
where
Args: ArangeAPI<Inp>,
{
return ArangeAPI::arange(param);
}
pub fn arange_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: ArangeAPI<Inp>,
{
return ArangeAPI::arange_f(param);
}
impl<T, B> ArangeAPI<(T, B)> for (T, T, T, &B)
where
T: Num + PartialOrd,
B: DeviceAPI<T> + DeviceCreationPartialOrdNumAPI<T>,
{
type Out = Tensor<T, B, IxD>;
fn arange_f(self) -> Result<Self::Out> {
let (start, stop, step, device) = self;
let data = device.arange_impl(start, stop, step)?;
let layout = vec![data.len()].into();
unsafe { Ok(Tensor::new_unchecked(data, layout)) }
}
}
impl<T, B> ArangeAPI<(T, B)> for (T, T, &B)
where
T: Num + PartialOrd,
B: DeviceAPI<T> + DeviceCreationPartialOrdNumAPI<T>,
{
type Out = Tensor<T, B, IxD>;
fn arange_f(self) -> Result<Self::Out> {
let (start, stop, device) = self;
let step = T::one();
arange_f((start, stop, step, device))
}
}
impl<T, B> ArangeAPI<(T, B)> for (T, &B)
where
T: Num + PartialOrd,
B: DeviceAPI<T> + DeviceCreationPartialOrdNumAPI<T>,
{
type Out = Tensor<T, B, IxD>;
fn arange_f(self) -> Result<Self::Out> {
let (stop, device) = self;
let start = T::zero();
let step = T::one();
arange_f((start, stop, step, device))
}
}
impl<T> ArangeAPI<T> for (T, T, T)
where
T: Num + PartialOrd + Clone + Send + Sync,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn arange_f(self) -> Result<Self::Out> {
let (start, stop, step) = self;
arange_f((start, stop, step, &DeviceCpu::default()))
}
}
impl<T> ArangeAPI<T> for (T, T)
where
T: Num + PartialOrd + Clone,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn arange_f(self) -> Result<Self::Out> {
let (start, stop) = self;
arange_f((start, stop, &DeviceCpu::default()))
}
}
impl<T> ArangeAPI<T> for T
where
T: Num + PartialOrd + Clone,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn arange_f(self) -> Result<Self::Out> {
arange_f((T::zero(), self, &DeviceCpu::default()))
}
}
pub trait EmptyAPI<Inp>: Sized {
type Out;
unsafe fn empty_f(self) -> Result<Self::Out>;
unsafe fn empty(self) -> Self::Out {
Self::empty_f(self).unwrap()
}
}
pub unsafe fn empty<Args, Inp>(param: Args) -> Args::Out
where
Args: EmptyAPI<Inp>,
{
return EmptyAPI::empty(param);
}
pub unsafe fn empty_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: EmptyAPI<Inp>,
{
return EmptyAPI::empty_f(param);
}
impl<T, D, B, L> EmptyAPI<(T, D)> for (L, &B)
where
L: Into<Layout<D>>,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, IxD>;
unsafe fn empty_f(self) -> Result<Self::Out> {
let (layout, device) = self;
let layout = layout.into();
let (_, idx_max) = layout.bounds_index()?;
let storage = B::empty_impl(device, idx_max)?;
unsafe { Ok(Tensor::new_unchecked(storage, layout.into_dim()?)) }
}
}
impl<T, D, L> EmptyAPI<(T, D)> for L
where
L: Into<Layout<D>>,
D: DimAPI,
T: Clone,
{
type Out = Tensor<T, DeviceCpu, IxD>;
unsafe fn empty_f(self) -> Result<Self::Out> {
empty_f((self, &DeviceCpu::default()))
}
}
pub trait EmptyLikeAPI<Inp>: Sized {
type Out;
unsafe fn empty_like_f(self) -> Result<Self::Out>;
unsafe fn empty_like(self) -> Self::Out {
Self::empty_like_f(self).unwrap()
}
}
pub unsafe fn empty_like<Args, Inp>(param: Args) -> Args::Out
where
Args: EmptyLikeAPI<Inp>,
{
return EmptyLikeAPI::empty_like(param);
}
pub unsafe fn empty_like_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: EmptyLikeAPI<Inp>,
{
return EmptyLikeAPI::empty_like_f(param);
}
impl<R, T, B, D> EmptyLikeAPI<()> for (&TensorAny<R, T, B, D>, TensorIterOrder, &B)
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, D>;
unsafe fn empty_like_f(self) -> Result<Self::Out> {
let (tensor, order, device) = self;
let layout = layout_for_array_copy(tensor.layout(), order)?;
let idx_max = layout.size();
let storage = device.empty_impl(idx_max)?;
unsafe { Ok(Tensor::new_unchecked(storage, layout)) }
}
}
impl<R, T, B, D> EmptyLikeAPI<()> for (&TensorAny<R, T, B, D>, &B)
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, D>;
unsafe fn empty_like_f(self) -> Result<Self::Out> {
let (tensor, device) = self;
empty_like_f((tensor, TensorIterOrder::default(), device))
}
}
impl<R, T, B, D> EmptyLikeAPI<()> for (&TensorAny<R, T, B, D>, TensorIterOrder)
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, D>;
unsafe fn empty_like_f(self) -> Result<Self::Out> {
let (tensor, order) = self;
let device = tensor.device();
empty_like_f((tensor, order, device))
}
}
impl<R, T, B, D> EmptyLikeAPI<()> for &TensorAny<R, T, B, D>
where
R: DataAPI<Data = B::Raw>,
T: Clone,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, D>;
unsafe fn empty_like_f(self) -> Result<Self::Out> {
let device = self.device();
empty_like_f((self, TensorIterOrder::default(), device))
}
}
pub trait EyeAPI<Inp>: Sized {
type Out;
fn eye_f(self) -> Result<Self::Out>;
fn eye(self) -> Self::Out {
Self::eye_f(self).unwrap()
}
}
pub fn eye<Args, Inp>(param: Args) -> Args::Out
where
Args: EyeAPI<Inp>,
{
return EyeAPI::eye(param);
}
pub fn eye_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: EyeAPI<Inp>,
{
return EyeAPI::eye_f(param);
}
impl<T, B> EyeAPI<(T, B)> for (usize, usize, isize, TensorOrder, &B)
where
T: Num,
B: DeviceAPI<T> + DeviceCreationNumAPI<T> + OpAssignAPI<T, Ix1>,
{
type Out = Tensor<T, B, IxD>;
fn eye_f(self) -> Result<Self::Out> {
let (n_rows, n_cols, k, order, device) = self;
let layout = match order {
TensorOrder::C => [n_rows, n_cols].c(),
TensorOrder::F => [n_cols, n_rows].f(),
};
let mut storage = device.zeros_impl(layout.size())?;
let layout_diag = layout.diagonal(Some(k), Some(0), Some(1))?;
device.fill(storage.raw_mut(), &layout_diag, T::one())?;
unsafe { Ok(Tensor::new_unchecked(storage, layout.into_dim()?)) }
}
}
impl<T, B> EyeAPI<(T, B)> for (usize, usize, isize, &B)
where
T: Num,
B: DeviceAPI<T> + DeviceCreationNumAPI<T> + OpAssignAPI<T, Ix1>,
{
type Out = Tensor<T, B, IxD>;
fn eye_f(self) -> Result<Self::Out> {
let (n_rows, n_cols, k, device) = self;
eye_f((n_rows, n_cols, k, TensorOrder::default(), device))
}
}
impl<T, B> EyeAPI<(T, B)> for (usize, &B)
where
T: Num,
B: DeviceAPI<T> + DeviceCreationNumAPI<T> + OpAssignAPI<T, Ix1>,
{
type Out = Tensor<T, B, IxD>;
fn eye_f(self) -> Result<Self::Out> {
let (n_rows, device) = self;
eye_f((n_rows, n_rows, 0, TensorOrder::default(), device))
}
}
impl<T> EyeAPI<T> for (usize, usize, isize, TensorOrder)
where
T: Num + Clone + Send + Sync,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn eye_f(self) -> Result<Self::Out> {
let (n_rows, n_cols, k, order) = self;
eye_f((n_rows, n_cols, k, order, &DeviceCpu::default()))
}
}
impl<T> EyeAPI<T> for (usize, usize, isize)
where
T: Num + Clone + Send + Sync,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn eye_f(self) -> Result<Self::Out> {
let (n_rows, n_cols, k) = self;
eye_f((n_rows, n_cols, k, TensorOrder::default(), &DeviceCpu::default()))
}
}
impl<T> EyeAPI<T> for usize
where
T: Num + Clone + Send + Sync,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn eye_f(self) -> Result<Self::Out> {
eye_f((self, self, 0, TensorOrder::default(), &DeviceCpu::default()))
}
}
pub trait FullAPI<Inp>: Sized {
type Out;
fn full_f(self) -> Result<Self::Out>;
fn full(self) -> Self::Out {
Self::full_f(self).unwrap()
}
}
pub fn full<Args, Inp>(param: Args) -> Args::Out
where
Args: FullAPI<Inp>,
{
return FullAPI::full(param);
}
pub fn full_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: FullAPI<Inp>,
{
return FullAPI::full_f(param);
}
impl<T, D, B, L> FullAPI<(T, D)> for (L, T, &B)
where
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
L: Into<Layout<D>>,
{
type Out = Tensor<T, B, IxD>;
fn full_f(self) -> Result<Self::Out> {
let (layout, fill, device) = self;
let layout = layout.into();
let idx_max = layout.size();
let storage = device.full_impl(idx_max, fill)?;
unsafe { Ok(Tensor::new_unchecked(storage, layout.into_dim()?)) }
}
}
impl<T, D, L> FullAPI<(T, D)> for (L, T)
where
D: DimAPI,
T: Clone,
L: Into<Layout<D>>,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn full_f(self) -> Result<Self::Out> {
let (layout, fill) = self;
full_f((layout, fill, &DeviceCpu::default()))
}
}
pub trait FullLikeAPI<Inp>: Sized {
type Out;
fn full_like_f(self) -> Result<Self::Out>;
fn full_like(self) -> Self::Out {
Self::full_like_f(self).unwrap()
}
}
pub fn full_like<Args, Inp>(param: Args) -> Args::Out
where
Args: FullLikeAPI<Inp>,
{
return FullLikeAPI::full_like(param);
}
pub fn full_like_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: FullLikeAPI<Inp>,
{
return FullLikeAPI::full_like_f(param);
}
impl<R, T, B, D> FullLikeAPI<()> for (&TensorAny<R, T, B, D>, T, TensorIterOrder, &B)
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, D>;
fn full_like_f(self) -> Result<Self::Out> {
let (tensor, fill, order, device) = self;
let layout = layout_for_array_copy(tensor.layout(), order)?;
let idx_max = layout.size();
let storage = device.full_impl(idx_max, fill)?;
unsafe { Ok(Tensor::new_unchecked(storage, layout)) }
}
}
impl<R, T, B, D> FullLikeAPI<()> for (&TensorAny<R, T, B, D>, T, &B)
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, D>;
fn full_like_f(self) -> Result<Self::Out> {
let (tensor, fill, device) = self;
full_like_f((tensor, fill, TensorIterOrder::default(), device))
}
}
impl<R, T, B, D> FullLikeAPI<()> for (&TensorAny<R, T, B, D>, T, TensorIterOrder)
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, D>;
fn full_like_f(self) -> Result<Self::Out> {
let (tensor, fill, order) = self;
let device = tensor.device();
full_like_f((tensor, fill, order, device))
}
}
impl<R, T, B, D> FullLikeAPI<()> for (&TensorAny<R, T, B, D>, T)
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
type Out = Tensor<T, B, D>;
fn full_like_f(self) -> Result<Self::Out> {
let (tensor, fill) = self;
let device = tensor.device();
full_like_f((tensor, fill, TensorIterOrder::default(), device))
}
}
impl<R, T, B, D> TensorAny<R, T, B, D>
where
R: DataAPI<Data = B::Raw>,
T: Clone,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationAnyAPI<T>,
{
pub fn full_like(&self, fill: T) -> Tensor<T, B, D> {
full_like((self, fill))
}
pub fn full_like_f(&self, fill: T) -> Result<Tensor<T, B, D>> {
full_like_f((self, fill))
}
}
pub trait LinspaceAPI<Inp>: Sized {
type Out;
fn linspace_f(self) -> Result<Self::Out>;
fn linspace(self) -> Self::Out {
Self::linspace_f(self).unwrap()
}
}
pub fn linspace<Args, Inp>(param: Args) -> Args::Out
where
Args: LinspaceAPI<Inp>,
{
return LinspaceAPI::linspace(param);
}
pub fn linspace_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: LinspaceAPI<Inp>,
{
return LinspaceAPI::linspace_f(param);
}
impl<T, B> LinspaceAPI<(T, B)> for (T, T, usize, bool, &B)
where
T: ComplexFloat,
B: DeviceAPI<T> + DeviceCreationComplexFloatAPI<T>,
{
type Out = Tensor<T, B, IxD>;
fn linspace_f(self) -> Result<Self::Out> {
let (start, end, n, endpoint, device) = self;
let data = B::linspace_impl(device, start, end, n, endpoint)?;
let layout = vec![data.len()].into();
unsafe { Ok(Tensor::new_unchecked(data, layout)) }
}
}
impl<T, B> LinspaceAPI<(T, B)> for (T, T, usize, &B)
where
T: ComplexFloat,
B: DeviceAPI<T> + DeviceCreationComplexFloatAPI<T>,
{
type Out = Tensor<T, B, IxD>;
fn linspace_f(self) -> Result<Self::Out> {
let (start, end, n, device) = self;
linspace_f((start, end, n, true, device))
}
}
impl<T> LinspaceAPI<T> for (T, T, usize, bool)
where
T: ComplexFloat + Send + Sync,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn linspace_f(self) -> Result<Self::Out> {
let (start, end, n, endpoint) = self;
linspace_f((start, end, n, endpoint, &DeviceCpu::default()))
}
}
impl<T> LinspaceAPI<T> for (T, T, usize)
where
T: ComplexFloat + Send + Sync,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn linspace_f(self) -> Result<Self::Out> {
let (start, end, n) = self;
linspace_f((start, end, n, true, &DeviceCpu::default()))
}
}
pub trait OnesAPI<Inp>: Sized {
type Out;
fn ones_f(self) -> Result<Self::Out>;
fn ones(self) -> Self::Out {
Self::ones_f(self).unwrap()
}
}
pub fn ones<Args, Inp>(param: Args) -> Args::Out
where
Args: OnesAPI<Inp>,
{
return OnesAPI::ones(param);
}
pub fn ones_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: OnesAPI<Inp>,
{
return OnesAPI::ones_f(param);
}
impl<T, D, B, L> OnesAPI<(T, D)> for (L, &B)
where
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
L: Into<Layout<D>>,
{
type Out = Tensor<T, B, IxD>;
fn ones_f(self) -> Result<Self::Out> {
let (layout, device) = self;
let layout = layout.into();
let (_, idx_max) = layout.bounds_index()?;
let storage = device.ones_impl(idx_max)?;
unsafe { Ok(Tensor::new_unchecked(storage, layout.into_dim()?)) }
}
}
impl<T, D, L> OnesAPI<(T, D)> for L
where
T: Num + Clone,
D: DimAPI,
L: Into<Layout<D>>,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn ones_f(self) -> Result<Self::Out> {
ones_f((self, &DeviceCpu::default()))
}
}
pub trait OnesLikeAPI<Inp>: Sized {
type Out;
fn ones_like_f(self) -> Result<Self::Out>;
fn ones_like(self) -> Self::Out {
Self::ones_like_f(self).unwrap()
}
}
pub fn ones_like<Args, Inp>(param: Args) -> Args::Out
where
Args: OnesLikeAPI<Inp>,
{
return OnesLikeAPI::ones_like(param);
}
pub fn ones_like_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: OnesLikeAPI<Inp>,
{
return OnesLikeAPI::ones_like_f(param);
}
impl<R, T, B, D> OnesLikeAPI<()> for (&TensorAny<R, T, B, D>, TensorIterOrder, &B)
where
R: DataAPI<Data = B::Raw>,
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
type Out = Tensor<T, B, D>;
fn ones_like_f(self) -> Result<Self::Out> {
let (tensor, order, device) = self;
let layout = layout_for_array_copy(tensor.layout(), order)?;
let idx_max = layout.size();
let storage = device.ones_impl(idx_max)?;
unsafe { Ok(Tensor::new_unchecked(storage, layout)) }
}
}
impl<R, T, B, D> OnesLikeAPI<()> for (&TensorAny<R, T, B, D>, &B)
where
R: DataAPI<Data = B::Raw>,
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
type Out = Tensor<T, B, D>;
fn ones_like_f(self) -> Result<Self::Out> {
let (tensor, device) = self;
ones_like_f((tensor, TensorIterOrder::default(), device))
}
}
impl<R, T, B, D> OnesLikeAPI<()> for (&TensorAny<R, T, B, D>, TensorIterOrder)
where
R: DataAPI<Data = B::Raw>,
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
type Out = Tensor<T, B, D>;
fn ones_like_f(self) -> Result<Self::Out> {
let (tensor, order) = self;
let device = tensor.device();
ones_like_f((tensor, order, device))
}
}
impl<R, T, B, D> OnesLikeAPI<()> for &TensorAny<R, T, B, D>
where
R: DataAPI<Data = B::Raw>,
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
type Out = Tensor<T, B, D>;
fn ones_like_f(self) -> Result<Self::Out> {
let device = self.device();
ones_like_f((self, TensorIterOrder::default(), device))
}
}
impl<R, T, B, D> TensorAny<R, T, B, D>
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
T: Num,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
pub fn ones_like(&self) -> Tensor<T, B, D> {
ones_like((self, TensorIterOrder::default(), self.device()))
}
pub fn ones_like_f(&self) -> Result<Tensor<T, B, D>> {
ones_like_f((self, TensorIterOrder::default(), self.device()))
}
}
pub trait ZerosAPI<Inp>: Sized {
type Out;
fn zeros_f(self) -> Result<Self::Out>;
fn zeros(self) -> Self::Out {
Self::zeros_f(self).unwrap()
}
}
pub fn zeros<Args, Inp>(param: Args) -> Args::Out
where
Args: ZerosAPI<Inp>,
{
return ZerosAPI::zeros(param);
}
pub fn zeros_f<Args, Inp>(param: Args) -> Result<Args::Out>
where
Args: ZerosAPI<Inp>,
{
return ZerosAPI::zeros_f(param);
}
impl<T, D, B, L> ZerosAPI<(T, D)> for (L, &B)
where
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
L: Into<Layout<D>>,
{
type Out = Tensor<T, B, IxD>;
fn zeros_f(self) -> Result<Self::Out> {
let (layout, device) = self;
let layout: Layout<D> = layout.into();
let (_, idx_max) = layout.bounds_index()?;
let storage = B::zeros_impl(device, idx_max)?;
unsafe { Ok(Tensor::new_unchecked(storage, layout.into_dim()?)) }
}
}
impl<T, D, L> ZerosAPI<(T, D)> for L
where
T: Num + Clone,
D: DimAPI,
L: Into<Layout<D>>,
{
type Out = Tensor<T, DeviceCpu, IxD>;
fn zeros_f(self) -> Result<Self::Out> {
zeros_f((self, &DeviceCpu::default()))
}
}
pub trait ZerosLikeAPI<Inp>: Sized {
type Out;
fn zeros_like_f(self) -> Result<Self::Out>;
fn zeros_like(self) -> Self::Out {
Self::zeros_like_f(self).unwrap()
}
}
pub fn zeros_like<Args, Inp, Rhs>(param: Args) -> Rhs
where
Args: ZerosLikeAPI<Inp, Out = Rhs>,
{
return ZerosLikeAPI::zeros_like(param);
}
pub fn zeros_like_f<Args, Inp, Rhs>(param: Args) -> Result<Rhs>
where
Args: ZerosLikeAPI<Inp, Out = Rhs>,
{
return ZerosLikeAPI::zeros_like_f(param);
}
impl<R, T, B, D> ZerosLikeAPI<()> for (&TensorAny<R, T, B, D>, TensorIterOrder, &B)
where
R: DataAPI<Data = B::Raw>,
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
type Out = Tensor<T, B, D>;
fn zeros_like_f(self) -> Result<Self::Out> {
let (tensor, order, device) = self;
let layout = layout_for_array_copy(tensor.layout(), order)?;
let idx_max = layout.size();
let storage = B::zeros_impl(device, idx_max)?;
unsafe { Ok(Tensor::new_unchecked(storage, layout)) }
}
}
impl<R, T, B, D> ZerosLikeAPI<()> for (&TensorAny<R, T, B, D>, &B)
where
R: DataAPI<Data = B::Raw>,
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
type Out = Tensor<T, B, D>;
fn zeros_like_f(self) -> Result<Self::Out> {
let (tensor, device) = self;
zeros_like_f((tensor, TensorIterOrder::default(), device))
}
}
impl<R, T, B, D> ZerosLikeAPI<()> for (&TensorAny<R, T, B, D>, TensorIterOrder)
where
R: DataAPI<Data = B::Raw>,
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
type Out = Tensor<T, B, D>;
fn zeros_like_f(self) -> Result<Self::Out> {
let (tensor, order) = self;
let device = tensor.device();
zeros_like_f((tensor, order, device))
}
}
impl<R, T, B, D> ZerosLikeAPI<()> for &TensorAny<R, T, B, D>
where
R: DataAPI<Data = B::Raw>,
T: Num,
D: DimAPI,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
type Out = Tensor<T, B, D>;
fn zeros_like_f(self) -> Result<Self::Out> {
zeros_like_f((self, TensorIterOrder::default(), self.device()))
}
}
impl<R, T, B, D> TensorAny<R, T, B, D>
where
R: DataAPI<Data = B::Raw>,
D: DimAPI,
T: Num,
B: DeviceAPI<T> + DeviceCreationNumAPI<T>,
{
pub fn zeros_like(&self) -> Tensor<T, B, D> {
zeros_like((self, TensorIterOrder::default(), self.device()))
}
pub fn zeros_like_f(&self) -> Result<Tensor<T, B, D>> {
zeros_like_f((self, TensorIterOrder::default(), self.device()))
}
}
#[cfg(test)]
mod test {
use super::*;
use num::complex::Complex32;
#[test]
fn playground() {
let a = arange((2.5, 3.2, 0.02));
println!("{a:6.3?}");
let a = arange(15.0);
println!("{a:6.3?}");
let a = arange((15.0, &DeviceCpu::default()));
println!("{a:6.3?}");
let a: Tensor<f64, _> = unsafe { empty(([15, 18].f(), &DeviceCpuSerial)) };
println!("{a:6.3?}");
let a = unsafe { a.empty_like() };
println!("{a:6.3?}");
let a = unsafe { empty_like((&a, TensorIterOrder::C)) };
println!("{a:6.3?}");
let a: Tensor<f64, _> = eye(3);
println!("{a:6.3?}");
let a = full(([2, 2].f(), 3.16));
println!("{a:6.3?}");
let a = full_like((&a, 2.71));
println!("{a:6.3?}");
let a = a.full_like(2.71);
println!("{a:6.3?}");
let a = linspace((3.2, 4.7, 12));
println!("{a:6.3?}");
let a = linspace((Complex32::new(1.8, 7.5), Complex32::new(-8.9, 1.6), 12));
println!("{a:6.3?}");
let a: Tensor<f64> = ones(vec![2, 2]);
println!("{a:6.3?}");
let a = a.ones_like();
println!("{a:6.3?}");
let a: Tensor<f64> = zeros([2, 2]);
println!("{a:6.3?}");
let a: Tensor<f64, _> = zeros(([2, 2], &DeviceCpuSerial));
println!("{a:6.3?}");
let a = a.zeros_like();
println!("{a:6.3?}");
}
}