Tensor

Struct Tensor 

Source
pub struct Tensor<T> { /* private fields */ }
Expand description

The N Dimensional Array

Implementations§

Source§

impl<T: Copy + PartialEq + NumCast> Tensor<T>

Source

pub fn argwhere(&self) -> Result<Tensor<usize>, Errors>

Returns a new tensor with indices for non zero elements in self

§Example
let indices = t.argwhere();
Source

pub fn nonzero(&self) -> Result<Tensor<usize>, Errors>

Alias to argwhere

Source§

impl<T: Copy> Tensor<T>

Source

pub fn cat(&self, other: &Tensor<T>, dim: usize) -> Result<Tensor<T>, Errors>

Returns a Tensor which is the concatenation of self and other along dimension dim

The 2 tensors must have equal number of dimensions and the dimensions can differ only at concatenation dimension

§Arguments
  • other - The other tensor to concatenate with self
  • dim - The dimension index to concatenate on
§Examples
let t1 =
    Tensor::from_slice_and_dims(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], &[3, 4]).unwrap();
let t2 = Tensor::from_slice_and_dims(&[13, 14, 15, 16, 17, 18], &[3, 2]).unwrap();
let res = t1.cat(&t2, 1).unwrap();
// res = [[1, 2, 3, 4, 13, 14], [5, 6, 7, 8, 15, 16], [9, 10, 11, 12, 17, 18]]
Source

pub fn concat(&self, other: &Tensor<T>, dim: usize) -> Result<Tensor<T>, Errors>

Alias to cat

Source

pub fn concatenate( &self, other: &Tensor<T>, dim: usize, ) -> Result<Tensor<T>, Errors>

Alias to cat

Source

pub fn chunk(&self, cnt: usize, dim: usize) -> Result<Vec<Tensor<T>>, Errors>

Split tensors into cnt number of pieces along dimension dim and return the collection of tensors

§Arguments
  • cnt - The number of chunks
  • dim - The dimension to chunk on
§Examples
let t = Tensor::arange(0, 11, 1).unwrap();
let res = t.chunk(6, 0).unwrap();
// res: Vec<Tensor<i32>> = vec![[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10]]
Source

pub fn gather( &self, dim: usize, indices: &Tensor<usize>, ) -> Result<Tensor<T>, Errors>

Return tensor with values according to the index in the corresponding element in indices along dimension dim

§Arguments
  • dim - The dimension to gather on
  • indices - The indices tensor
§Examples
let t = Tensor::arange(1, 5, 1).unwrap().reshape(&[2, 2]).unwrap();
let ind = Tensor::from_slice_and_dims(&[0, 0, 1, 0], &[2, 2]).unwrap();
let gather = t.gather(1, ind);
// gather = [[1, 1], [4, 3]]
Source

pub fn masked_select(&self, pick: &Tensor<bool>) -> Result<Tensor<T>, Errors>

Returns a 1D Tensor which picks elements from self if the corresponding element in mask in true

The dimensions of the specified tensor must match the dimensions of self

§Arguments
  • pick - The mask tensor
§Examples
let t = Tensor::from_slice_and_dims(
    &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
    &[3, 5],
)
.unwrap();
let pick = Tensor::from_slice_and_dims(
    &[
        true, true, false, true, false, false, false, true, false, false, false, false,
        false, false, false,
    ],
    &[3, 5],
)
.unwrap();
let res = t.masked_select(&pick).unwrap();
// res = [1, 2, 4, 8]
Source

pub fn fn_select<F: Fn(T, &[usize]) -> bool>( &self, f: F, ) -> Result<Tensor<T>, Errors>

Returns a 1D Tensor which picks elements from self if the function f returns true given the element

§Arguments
  • f - The selector function
§Examples
let t = Tensor::arange(1, 11, 1).unwrap();
let res = t.fn_select(|val, _| val % 2 == 0).unwrap();
// res = [2, 4, 6, 8, 10]
Source

pub fn narrow( &self, dim: usize, st: usize, len: usize, ) -> Result<Tensor<T>, Errors>

Returns a slice of self along dimension dim from st of length len

§Arguments
  • dim - Dimension to narrow
  • st - Start index
  • len - Length of slice
§Examples
let t =
    Tensor::arange(1, 13, 1).unwrap().reshape(&[3, 4]);
let res = t.narrow(1, 1, 3).unwrap();
// res = [[2, 3, 4], [6, 7, 8], [10, 11, 12]]
Source

pub fn reshape(&self, new_dims: &[usize]) -> Result<Tensor<T>, Errors>

Return tensor with same data as self but with dimensions new_dims. The output tensor can possibly be a view of self

§Arguments
  • new_dims - The new dimensions
§Examples
let t = Tensor::arange(1, 13).unwrap();
let res = t.reshape(&[3, 4]);
// res = [[1, 2, 3, 4], [5, 6, 7, 8], [11, 12, 13, 14]]
Source

pub fn permute(&self, permutation: &[usize]) -> Result<Tensor<T>, Errors>

Returns a tensor with same data as self but with it’s dimension permuted by the permutation permutation

§Arguments
  • permutation - The permutation to permute the dimensions to. Must be a permutation from 0 to number of dimensions - 1
§Examples
let t = Tensor::arange(1, 11, 1).unwrap().reshape(&[5, 2]).unwrap();
let res = t.permute(&[1, 0]).unwrap();
// res = [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]
Source

pub fn transpose(&self, dim1: usize, dim2: usize) -> Result<Tensor<T>, Errors>

Return the tensor obtained by swapping dimensions dim1 and dim2 of self

§Arguments
  • dim1 - Dimension 1
  • dim2 - Dimension 2
§Examples
let t = Tensor::arange(1, 11, 1)
    .unwrap()
    .reshape(&[5, 2])
    .unwrap()
let res = t.transpose(0, 1).unwrap();
// res = [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]
Source§

impl<T: Copy> Tensor<T>

Source

pub fn from_slice(arr: &[T]) -> Result<Tensor<T>, Errors>

Create a one dimensional tensor from a specified slice

§Arguments
  • arr - The slice containing the data
§Examples
let t = Tensor::from_slice(&[1, 2, 3, 4, 5, 6]).unwrap();
// t = [1, 2, 3, 4, 5, 6]
Source

pub fn from_slice_and_dims( arr: &[T], dims: &[usize], ) -> Result<Tensor<T>, Errors>

Create a new tensor with specified data in a slice, with specified dimensions

§Arguments
  • arr - The slice containing the data
  • dims - The required dimensions
§Examples
let t = Tensor::from_slice_and_dims(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], &[2, 5]).unwrap();
// t = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
Source

pub fn from_val(dims: &[usize], val: T) -> Result<Tensor<T>, Errors>

Return a new tensor with all elements equal to a specified value, with specified dimensions

§Arguments
  • dims - The required dimensions
  • val - The value
§Examples
let t = Tensor::from_val(&[2, 2], 1729).unwrap();
// t = [[1729, 1729], [1729, 1729]]
Source§

impl<T: Default + Copy> Tensor<T>

Source

pub fn from_default(dims: &[usize]) -> Result<Tensor<T>, Errors>

Return a tensor with all elements equal to the type’s default value, with specified dimensions

§Arguments
  • dims - The required dimensions
§Examples
let t = Tensor::<i32>::from_default(&[2, 3]).unwrap();
// t = [[0, 0, 0], [0, 0, 0]]
Source§

impl<T: Copy + NumCast> Tensor<T>

Source

pub fn zeros(dims: &[usize]) -> Result<Tensor<T>, Errors>

Return a tensor with all elements equal to 0, with specified dimensions

§Arguments
  • dims - The required dimensions
§Examples
let t = Tensor::<i32>::zeros(&[2, 3]).unwrap();
// t = [[0, 0, 0], [0, 0, 0]]
Source

pub fn ones(dims: &[usize]) -> Result<Tensor<T>, Errors>

Return a tensor with all elements equal to 1, with specified dimensions

§Arguments
  • dims - The required dimensions
§Examples
let t = Tensor::<i32>::ones(&[2, 3]).unwrap();
// t = [[1, 1, 1], [1, 1, 1]]
Source

pub fn eye(n: usize, m: usize) -> Result<Tensor<T>, Errors>

Return a 2D tensor with elements equal to 0 except the main diagonal (when row = column) which is equal to 1

§Arguments
  • n - Number of rows
  • m - Number of columns
§Examples
let t = Tensor::<i32>::eye(2, 3).unwrap();
// t = [[1, 0, 0], [0, 1, 0]]
Source§

impl<T: Copy + Add<Output = T> + Mul<Output = T> + NumCast + PartialOrd> Tensor<T>

Source

pub fn arange(st: T, en: T, step: T) -> Result<Tensor<T>, Errors>

Return a 1D tensor where the i th element is st + i * step such that each element is in the range from st to en (exclusive)

§Arguments
  • st - The start of the range
  • en - The end of the range
  • step - The step size
§Examples
let t = Tensor::arange(1, 11, 1).unwrap();
// t = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Source§

impl<T: Copy + Float + Display> Tensor<T>

Source

pub fn linspace(st: T, en: T, cnt: usize) -> Result<Tensor<T>, Errors>

Returns a 1D tensor with cnt elements equally spaced in the range from st to en

§Arguments
  • st - Start of the range
  • en - End of the range
  • cnt - The number of elements in the resultant tensor
§Examples
let t = Tensor::linspace(0.0, 1.0, 11).unwrap();
// t = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
Source

pub fn logspace(base: T, st: T, en: T, cnt: usize) -> Result<Tensor<T>, Errors>

Return a 1D Tensor of size cnt with elements evenly spaced from base^st to base^en on a logarithmic scale with base base

§Arguments
  • base - Base for the logarithmic scale
  • st - Start of the range
  • en - End of the range
§Examples
let t = Tensor::logspace(10.0, -10.0, 10.0, 5).unwrap();
// t = [1e-10, 1e-5, 1, 1e5, 1e10]
Source§

impl<T: Copy> Tensor<T>

Source

pub fn map<R: Copy, F: Fn(T) -> R>(&self, f: F) -> Tensor<R>

Apply any function to every element of self and create a new tensor with the results This is similar to rust’s .map() in iterators

§Arguments
  • f - Function that takes type T and returns a new value R
§Examples
// t is some tensor
let some_complicated_function_values_as_tensor = t.map(|x| some_complicated_function(x));
Source

pub fn map_with<O: Copy, R: Copy, F: Fn(T, O) -> R>( &self, other: &Tensor<O>, f: F, ) -> Tensor<R>

elements) and create a new tensor with the results This is similar to rust’s .zip().map() in iterators

§Arguments
  • other - The other tensor
  • f - Function that takes type T and returns a new value R
§Examples
// t is some tensor
let dist_from_origin_tensor = x_coords.map_with(&y_coords, |(x, y)| (x * x + y * y).sqrt());
Source§

impl<T: Copy + Signed> Tensor<T>

Source

pub fn abs(&self) -> Tensor<T>

Calculate the absolute function for every element of self and returns a new tensor of the results

§Examples
let abs_t = t.abs();
Source

pub fn absolute(&self) -> Tensor<T>

Calculate the absolute function for every element of self and returns a new tensor of the results

§Examples
let absolute_t = t.absolute();
Source§

impl<T: Copy + Float> Tensor<T>

Source

pub fn asin(&self) -> Tensor<T>

Calculate the inverse sine function for every element of self and returns a new tensor of the results

§Examples
let asin_t = t.asin();
Source

pub fn acos(&self) -> Tensor<T>

Calculate the inverse cosine function for every element of self and returns a new tensor of the results

§Examples
let acos_t = t.acos();
Source

pub fn atan(&self) -> Tensor<T>

Calculate the inverse tangent function for every element of self and returns a new tensor of the results

§Examples
let atan_t = t.atan();
Source

pub fn atan2(&self, other: &Tensor<T>) -> Tensor<T>

Calculate the inverse tangent 2 function for every element of self and returns a new tensor of the results

§Arguments
  • other - The other tensor with which the atan2 function is to be calculated
§Examples
let atan2_t = rise_t.atan2(run_t);
Source

pub fn asinh(&self) -> Tensor<T>

Calculate the inverse hyperbolic sine function for every element of self and returns a new tensor of the results

§Examples
let asinh_t = t.asinh();
Source

pub fn acosh(&self) -> Tensor<T>

Calculate the inverse hyperbolic cosine function for every element of self and returns a new tensor of the results

§Examples
let acosh_t = t.acosh();
Source

pub fn atanh(&self) -> Tensor<T>

Calculate the inverse hyperbolic tangent function for every element of self and returns a new tensor of the results

§Examples
let atanh_t = t.atanh();
Source

pub fn arcsin(&self) -> Tensor<T>

Calculate the inverse sine function for every element of self and returns a new tensor of the results

§Examples
let arcsin_t = t.arcsin();
Source

pub fn arccos(&self) -> Tensor<T>

Calculate the inverse cosine function for every element of self and returns a new tensor of the results

§Examples
let arccos_t = t.arccos();
Source

pub fn arctan(&self) -> Tensor<T>

Calculate the inverse tangent function for every element of self and returns a new tensor of the results

§Examples
let arctan_t = t.arctan();
Source

pub fn arctan2(&self, other: &Tensor<T>) -> Tensor<T>

Calculate the inverse tangent 2 function for every element of self and returns a new tensor of the results

§Arguments
  • other - The other tensor with which the atan2 function is to be calculated
§Examples
let arctan2_t = t.arctan2();
Source

pub fn arcsinh(&self) -> Tensor<T>

Calculate the inverse hyperbolic sine function for every element of self and returns a new tensor of the results

§Examples
let arcsinh_t = t.arcsinh();
Source

pub fn arccosh(&self) -> Tensor<T>

Calculate the inverse hyperbolic cosine function for every element of self and returns a new tensor of the results

§Examples
let arccosh_t = t.arccosh();
Source

pub fn arctanh(&self) -> Tensor<T>

Calculate the inverse hyperbolic tangent function for every element of self and returns a new tensor of the results

§Examples
let arctanh_t = t.arctanh();
Source

pub fn ceil(&self) -> Tensor<T>

Calculate the ceiling function for every element of self and returns a new tensor of the results

§Examples
let ceil_t = t.ceil();
Source

pub fn cos(&self) -> Tensor<T>

Calculate the cosine function for every element of self and returns a new tensor of the results

§Examples
let cos_t = t.cos();
Source

pub fn cosh(&self) -> Tensor<T>

Calculate the hyperbolic cosine function for every element of self and returns a new tensor of the results

§Examples
let cosh_t = t.cosh();
Source

pub fn degrees_to_radians(&self) -> Tensor<T>

Convert Degrees to Radians for every element of self and returns a new tensor of the results

§Examples
let degrees_to_radians_t = t.degrees_to_radians();
Source

pub fn radians_to_degrees(&self) -> Tensor<T>

Convert radians to degrees for every element of self and returns a new tensor of the results

§Examples
let radians_to_degrees_t = t.radians_to_degrees();
Source

pub fn exp(&self) -> Tensor<T>

Calculate the exponential function for every element of self and returns a new tensor of the results

§Examples
let exp_t = t.exp();
Source

pub fn exp_2(&self) -> Tensor<T>

Calculate the exponential function with base 2 for every element of self and returns a new tensor of the results

§Examples
let exp_2_t = t.exp_2();
Source

pub fn trunc(&self) -> Tensor<T>

Truncate every element of self and returns a new tensor of the results

§Examples
let trunc_t = t.trunc();
Source

pub fn log(&self) -> Tensor<T>

Calculate the natura logarithm function for every element of self and returns a new tensor of the results

§Examples
let log_t = t.log();
Source

pub fn log_10(&self) -> Tensor<T>

Calculate the logarithm function with base 10 for every element of self and returns a new tensor of the results

§Examples
let log_10_t = t.log_10();
Source

pub fn log_2(&self) -> Tensor<T>

Calculate the logarithm function with base 2 for every element of self and returns a new tensor of the results

§Examples
let log_2_t = t.log_2();
Source

pub fn round(&self) -> Tensor<T>

Round every element of self and returns a new tensor of the results

§Examples
let round_t = t.round();
Source

pub fn floor(&self) -> Tensor<T>

Calculate the floor function for every element of self and returns a new tensor of the results

§Examples
let floor_t = t.floor();
Source

pub fn frac(&self) -> Tensor<T>

Get fractional complement for every element of self and returns a new tensor of the results

§Examples
let frac_t = t.frac();
Source

pub fn sqrt(&self) -> Tensor<T>

Calculate the square root function for every element of self and returns a new tensor of the results

§Examples
let sqrt_t = t.sqrt();
Source

pub fn hypot(&self, other: &Tensor<T>) -> Tensor<T>

Finds length of hypotenuse of right triangle with sides lengths as elements in self and returns a new tensor of the results results

§Examples
let hypot_t = t.hypot();
Source

pub fn rsqrt(&self) -> Tensor<T>

Calculate the reciprocal of the square root function for every element of self and returns a new tensor of the results

§Examples
let rsqrt_t = t.rsqrt();
Source

pub fn expit(&self) -> Tensor<T>

Here we calculate expit(x) for every element in self

The Expit Function, also known as the logistic function, or the sigmoid function is a S shaped curve that appears frequently in many branches of science.

Formally it is defined as: sigmoid(x) = 1 / (1 + e^(-x))

§Examples
// t is some tensor of floats
let expit_1 = t.expit();
Source

pub fn logit(&self) -> Tensor<T>

Here we calculate logit(x) for every element in self

The Logit function is the quantile function associated with the standard logistic distribution.

Formally it is defined as: logit(p) = ln(p / (1 - p))

§Examples
// t is some tensor of floats
let logit_1 = t.logit();
Source

pub fn xlogy(&self, other: &Tensor<T>) -> Tensor<T>

Here we calculate xlogy(x) for every element in self

The xlogy function is formally defined as: xlog(y) = x * log(y)

§Examples
// t is some tensor of floats
let xlogy_1 = t.xlogy();
Source§

impl<T: Copy + Float + NumCast> Tensor<T>

Source

pub fn erf(&self) -> Tensor<T>

Here we calculate erf(x) for every element in self

The error function, denoted by erf, is encountered when integrating the normal distribution.

It is formally defined as a complex valued function: erf(z) = 2 / sqrt(pi) * integrate(e^-(t^2) dt) from 0 to z.

Here we approximate erf(z) for every element in self only for real z with maximum error upto 1.5 * 10^(-7).

The approximation method can be found on the wikipedia article of the erf function. This approximation method is credited to Abramowitz and Stegun.

See the wikipedia article for more information

§Examples
// t is some tensor of floats
let t_erf = t.erf();
Source

pub fn erfc(&self) -> Tensor<T>

Here we calculate erfc(x) for every element in self

The error function complement, denoted by erfc, is encountered when integrating the normal distribution.

It is formally defined as a complex valued function: erfc(z) = 1 - erf(z) = 1 - 2 / sqrt(pi) * integrate(e^-(t^2) dt) from 0 to z.

Here we approximate erfc(z) for every element in self only for real z with maximum error upto 1.5 * 10^(-7).

The approximation method can be found on the wikipedia article of the erfc function. This approximation method is credited to Abramowitz and Stegun.

See the wikipedia article for more information

§Examples
// t is some tensor of floats
let t_erfc = t.erfc();
Source§

impl<T: Copy + Float + FloatConst> Tensor<T>

Source

pub fn sinc(&self) -> Tensor<T>

Here we calculate the normalized sinc function for every element in self

The normalized sinc function is defined as the Fourier transform of the rectangular function with no scaling.

It is formally defined as: sinc(x) = sin(pi * x) / (pi * x) where x != 0, and sinc(0) = 1

§Examples
// t is some tensor of floats
let t_sinc = t.sinc();
Source

pub fn log_gammaf(&self) -> Tensor<T>

Here we calculate log_gamma(x) for every element in self for real x

The log gamma function is defined as the natural log of the gamma function, which is the most used extension to the factorial function into the complex plane.

It is formally defined as a complex valued function: log_gamma(x) = ln(gamma(x)) = ln( integrate(t^(z - 1) * e^(-t) * dt) from 0 to +inf )

Here we approximate log_gamma(x) with accuracy > 10 d.p

The approximation is based on the implementation of SciPy. For small values (< 0.2), this returns the Taylor series expansion of log gamma around 0 with 23 terms. For slightly larger values, this returns the result using a recursive formula and the Taylor series expansion like before. For big values (> 7), this returns the famous stirling approximation for log gamma For values in between, this returns the result by repeatedly applying another recursive. formula, and finally using the stirling approximation. For negative values, this returns the result using a reflection formula.

See scipy’s implementation and Hare “Computing the Principal Branch of log-Gamma” on Journal of Algorithms 1997 for more details

§Examples
// t is some tensor of floats
let t_gamma = t.log_gammaf();
Source

pub fn gammaf(&self) -> Tensor<T>

Calculate Gamma Function (for reals)

The gamma function is the most used extension to the factorial function into the complex plane.

It is formally defined as a complex valued function: gamma(x) = integrate(t^(z - 1) * e^(-t) * dt) from 0 to +inf

Here we approximate gamma(x) with accuracy > 10 d.p

Under the hood, this exponentiates log_gamma(x) See Tensor::log_gamma for details of the approximation.

§Examples
// t is some tensor of floats
let t_gamma = t.gammaf();
Source§

impl<T: Copy + Float + FloatConst> Tensor<Complex<T>>

Source

pub fn log_gamma(&self) -> Tensor<Complex<T>>

Here we calculate log_gamma(x) for every element in self for complex x

The log gamma function is defined as the natural log of the gamma function, which is the most used extension to the factorial function into the complex plane.

It is formally defined as a complex valued function: log_gamma(x) = ln(gamma(x)) = ln( integrate(t^(z - 1) * e^(-t) * dt) from 0 to +inf )

Here we approximate log_gamma(x) with accuracy > 10 d.p

The approximation is based on the implementation of SciPy. For small values (< 0.2), this returns the Taylor series expansion of log gamma around 0 with 23 terms. For slightly larger values, this returns the result using a recursive formula and the Taylor series expansion like before. For big values (> 7), this returns the famous stirling approximation for log gamma For values in between, this returns the result by repeatedly applying another recursive. formula, and finally using the stirling approximation. For negative values, this returns the result using a reflection formula.

See scipy’s implementation and Hare “Computing the Principal Branch of log-Gamma” on Journal of Algorithms 1997 for more details

§Examples
// t is some tensor of floats
let t_gamma = t.log_gamma();
Source

pub fn gamma(&self) -> Tensor<Complex<T>>

Calculate Gamma Function (for complex) The gamma function is the most used extension to the factorial function into the complex plane. It is formally defined as a complex valued function: gamma(x) = integrate(t^(z - 1) * e^(-t) * dt) from 0 to +inf

Here we approximate gamma(x) with accuracy > 10 d.p

Under the hood, this exponentiates log_gamma(x) See [log_gamma] for details of the approximation.

§Examples
// t is some tensor of floats
let t_gamma = t.gammaf();
Source§

impl<R: Copy, T: Copy + Mul<Output = R>> Tensor<T>

Source

pub fn square(&self) -> Tensor<R>

Calculate the square function for every element of self and returns a new tensor of the results

§Examples
let square_t = t.square();
Source§

impl<T: Copy> Tensor<T>

Source

pub fn element_add<O: Copy, R: Copy>(&self, other: &Tensor<O>) -> Tensor<R>
where T: Add<O, Output = R>,

Pairwise add elements from self and other and return tensor with results

§Examples
let a_plus_b = a.element_add(b);
Source

pub fn element_sub<O: Copy, R: Copy>(&self, other: &Tensor<O>) -> Tensor<R>
where T: Sub<O, Output = R>,

Pairwise subtract elements from self and other and return tensor with results

§Examples
let a_plus_b = a.element_add(b);
Source

pub fn element_mul<O: Copy, R: Copy>(&self, other: &Tensor<O>) -> Tensor<R>
where T: Mul<O, Output = R>,

Pairwise multiply elements from self and other and return tensor with results

§Examples
let a_plus_b = a.element_add(b);
Source

pub fn element_div<O: Copy, R: Copy>(&self, other: &Tensor<O>) -> Tensor<R>
where T: Div<O, Output = R>,

Pairwise divide elements from self and other and return tensor with results

§Examples
let a_plus_b = a.element_add(b);
Source§

impl<T: Copy + PartialOrd + Display> Tensor<T>

Source

pub fn clamp(&self, mn: T, mx: T) -> Result<Tensor<T>, Errors>

Clamp every element in self between mn, and mx and returns tensor of the results

clamp(x) = max(mn, min(mx, x))

§Arguments
  • mn - Minimum for clamp
  • mx - Maximum for clamp
§Examples
let clamp_t = t.clamp();
Source

pub fn clip(&self, mn: T, mx: T) -> Result<Tensor<T>, Errors>

Alias to Tensor::clamp

Source§

impl<T: Copy + Pow<T, Output = T>> Tensor<T>

Source

pub fn pow(&self, other: &Tensor<T>) -> Tensor<T>

Calculate the exponential function for every element of self with exponent as the corresponding element in other and returns a new tensor of the results

§Examples
let pow_t = t.pow();
Source§

impl<T: Copy + Neg<Output = T>> Tensor<T>

Source

pub fn neg(&self) -> Tensor<T>

Calculate the Negative function for every element of self and returns a new tensor of the results

§Examples
let neg_t = t.neg();
Source

pub fn negative(&self) -> Tensor<T>

Calculate the Negative function for every element of self and returns a new tensor of the results

§Examples
let negative_t = t.negative();
Source§

impl<T: Copy + Inv<Output = T>> Tensor<T>

Source

pub fn inv(&self) -> Tensor<T>

Calculate the reciprocal function for every element of self and returns a new tensor of the results

§Examples
let inv_t = t.inv();
Source

pub fn reciprocal(&self) -> Tensor<T>

Calculate the reciprocal function for every element of self and returns a new tensor of the results

§Examples
let reciprocal_t = t.reciprocal();
Source§

impl<T: Copy> Tensor<T>

Source

pub fn new( storage: Rc<RefCell<TensorStorage<T>>>, offset: usize, dims: &[usize], strides: &[usize], ) -> Result<Tensor<T>, Errors>

Create new tensor with custom TensorStorage, offset, dimensions, and strides. Not recommended for use

Source

pub fn new_unchecked( storage: Rc<RefCell<TensorStorage<T>>>, offset: usize, dims: &[usize], strides: &[usize], ) -> Tensor<T>

Create new tensor with custom TensorStorage, offset, dimensions, and strides, without any checks Not recommended for use

Source

pub fn get_storage_ptr(&self) -> Rc<RefCell<TensorStorage<T>>>

Get pointer to self.storage

Source

pub fn no_dim(&self) -> usize

Get number of dimensions

Source

pub fn len(&self) -> usize

Get number of elements

Source

pub fn is_view(&self) -> bool

Does self borrow memory from other tensors

Source

pub fn make_contiguous(&self) -> Tensor<T>

Make self own it’s own data

Source

pub fn slice(&self, rngs: &[Range<usize>]) -> Result<Tensor<T>, Errors>

Index self with rngs

Source

pub fn slice_unchecked(&self, rngs: &[Range<usize>]) -> Tensor<T>

Index self with rngs without checks

Source

pub fn at(&self, index: &[usize]) -> Result<T, Errors>

Get value in self at index index

Source

pub fn at_unchecked(&self, index: &[usize]) -> T

Get value in self at index index without checks

Source

pub fn upd(&self, index: &[usize], new_val: T) -> Result<(), Errors>

Update value in self at index index to new_val

Source

pub fn upd_unchecked(&self, index: &[usize], new_val: T)

Update value in self at index index to new_val without checks

Trait Implementations§

Source§

impl<T: Clone> Clone for Tensor<T>

Source§

fn clone(&self) -> Tensor<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Tensor<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: Copy> IntoIterator for &'a Tensor<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = TensorIterator<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Tensor<T>

§

impl<T> !RefUnwindSafe for Tensor<T>

§

impl<T> !Send for Tensor<T>

§

impl<T> !Sync for Tensor<T>

§

impl<T> Unpin for Tensor<T>

§

impl<T> !UnwindSafe for Tensor<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.