pub trait Constructors<T: RawDataType>: StridedMemory {
// Required method
unsafe fn from_contiguous_owned_buffer(
shape: Vec<usize>,
data: Vec<T>,
) -> Self;
// Provided methods
fn new<const D: usize>(data: impl Flatten<T> + Shape + Nested<D>) -> Self { ... }
fn full(n: T, shape: impl ToVec<usize>) -> Self { ... }
fn zeros(shape: impl ToVec<usize>) -> Self
where T: From<bool> { ... }
fn ones(shape: impl ToVec<usize>) -> Self
where T: From<bool> { ... }
fn scalar(n: T) -> Self { ... }
fn arange(start: T, stop: T) -> Self
where T: NumericDataType { ... }
fn arange_with_step(start: T, stop: T, step: T) -> Self
where T: NumericDataType { ... }
fn linspace(start: T, stop: T, num: usize) -> Self
where T: FloatDataType { ... }
fn linspace_exclusive(start: T, stop: T, num: usize) -> Self
where T: FloatDataType { ... }
}Required Methods§
Sourceunsafe fn from_contiguous_owned_buffer(shape: Vec<usize>, data: Vec<T>) -> Self
unsafe fn from_contiguous_owned_buffer(shape: Vec<usize>, data: Vec<T>) -> Self
Constructs a new ndarray from the given data buffer and shape assuming a contiguous layout
§Parameters
shape: A vector that defines the dimensions of the ndarray.data: The underlying buffer that holds the ndarray’s elements.requires_grad: If gradients need to be computed for this ndarray.
§Safety
datamust remain valid and not be used elsewhere after being passed to this function.shape.iter().product()must equaldata.len()
Provided Methods§
Sourcefn new<const D: usize>(data: impl Flatten<T> + Shape + Nested<D>) -> Self
fn new<const D: usize>(data: impl Flatten<T> + Shape + Nested<D>) -> Self
Constructs an n-dimensional NdArray from input data such as a vector or array.
§Parameters
data: a nested array or vector of valid data types (floats, integers, bools)
§Panics
- If the input data has inhomogeneous dimensions, i.e., nested arrays do not have consistent sizes.
- If the input data is empty (cannot create a zero-length ndarray)
§Example
let ndarray : NdArray<i32> = NdArray::new([[1, 2], [3, 4]]);
assert_eq!(ndarray.shape(), &[2, 2]);
let ndarray = NdArray::new(vec![1f32, 2.0, 3.0, 4.0, 5.0]);
assert_eq!(ndarray.ndims(), 1);Sourcefn full(n: T, shape: impl ToVec<usize>) -> Self
fn full(n: T, shape: impl ToVec<usize>) -> Self
Creates an ndarray filled with a specified value and given shape.
§Parameters
n- The value to fill the ndarray with (can be any valid data type like float, integer, or bool).shape- An array or vector representing the shape of the ndarray (e.g.[2, 3, 5]).
§Panics
This function panics if the provided shape is empty.
§Examples
let ndarray = NdArray::full(5i32, [2, 3]); // creates a 2x3 ndarray filled with the value 5.
let ndarray = NdArray::full(true, [2, 3, 5]); // creates a 2x3x5 ndarray filled with 'true'Sourcefn zeros(shape: impl ToVec<usize>) -> Self
fn zeros(shape: impl ToVec<usize>) -> Self
Creates a new ndarray filled with zeros with the given shape.
§Parameters
shape: An array or vector representing the shape of the ndarray (e.g.[2, 3, 5]).
§Panics
This function panics if the provided shape is empty.
§Examples
let ndarray = NdArray::<i32>::zeros([2, 3]);
let ndarray = NdArray::<bool>::zeros([2, 3]); // creates an ndarray filled with 'false'Sourcefn ones(shape: impl ToVec<usize>) -> Self
fn ones(shape: impl ToVec<usize>) -> Self
Creates a new ndarray filled with ones with the given shape.
§Parameters
shape: An array or vector representing the shape of the ndarray (e.g.[2, 3, 5]).
§Panics
This function panics if the provided shape is empty.
§Examples
let ndarray = NdArray::<i32>::ones([2, 3]);
let ndarray = NdArray::<bool>::ones([2, 3]); // creates an ndarray filled with 'true'Sourcefn arange(start: T, stop: T) -> Selfwhere
T: NumericDataType,
fn arange(start: T, stop: T) -> Selfwhere
T: NumericDataType,
Generates a 1D ndarray with evenly spaced values within a specified range.
§Arguments
start- The starting value of the sequence, inclusive.stop- The ending value of the sequence, exclusive.
§Returns
An NdArray containing values starting from start and ending before stop,
with a step-size of 1.
§Examples
let ndarray = NdArray::arange(0i32, 5); // [0, 1, 2, 3, 4].Sourcefn arange_with_step(start: T, stop: T, step: T) -> Selfwhere
T: NumericDataType,
fn arange_with_step(start: T, stop: T, step: T) -> Selfwhere
T: NumericDataType,
Generates a 1D ndarray with evenly spaced values within a specified range.
§Arguments
start- The starting value of the sequence, inclusive.stop- The ending value of the sequence, exclusive.step- The interval between each consecutive value
§Examples
let ndarray = NdArray::arange_with_step(0i32, 5, 2); // [0, 2, 4].Sourcefn linspace(start: T, stop: T, num: usize) -> Selfwhere
T: FloatDataType,
fn linspace(start: T, stop: T, num: usize) -> Selfwhere
T: FloatDataType,
Generates a 1-dimensional ndarray with num evenly spaced values between start and stop
(inclusive).
§Arguments
start- The starting value of the sequence.stop- The ending value of the sequence. The value is inclusive in the range.num- The number of evenly spaced values to generate. Must be greater than 0.
§Panic
Panics if num is 0.
§Example
let result = NdArray::linspace(0f32, 1.0, 5); // [0.0, 0.25, 0.5, 0.75, 1.0]
assert_eq!(result, NdArray::new([0f32, 0.25, 0.5, 0.75, 1.0]));Sourcefn linspace_exclusive(start: T, stop: T, num: usize) -> Selfwhere
T: FloatDataType,
fn linspace_exclusive(start: T, stop: T, num: usize) -> Selfwhere
T: FloatDataType,
Generates a 1-dimensional ndarray with num evenly spaced values between start and stop
(exclusive).
§Arguments
start- The starting value of the sequence.stop- The ending value of the sequence. The value is exclusive in the range.num- The number of evenly spaced values to generate. Must be greater than 0.
§Panic
Panics if num is 0.
§Example
let result = NdArray::linspace_exclusive(0.0f32, 1.0, 5);
assert_eq!(result, NdArray::new([0f32, 0.2, 0.4, 0.6, 0.8]));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.