pub struct Ndarr<T: Clone, R: Unsigned> {
pub data: Vec<T>,
pub dim: Dim<R>,
}Expand description
Main struct of N Dimensional generic array. The shape is denoted by the shape array where the length is the Rank of the Ndarray the actual values are stored in a flattened state in a rank 1 array.
Fields§
§data: Vec<T>§dim: Dim<R>Implementations§
Source§impl<T1: Clone + Debug, R1: Unsigned> Ndarr<T1, R1>
impl<T1: Clone + Debug, R1: Unsigned> Ndarr<T1, R1>
pub fn poly_dyadic<F, T2, T3, R2: Unsigned>( &self, other: &Ndarr<T2, R2>, f: F, ) -> Result<Ndarr<T3, Maximum<R1, R2>>, DimError>
pub fn mat_mul<R2: Unsigned>( &self, other: &Ndarr<T1, R2>, ) -> Ndarr<T1, Sub1<Sub1<Sum<R1, R2>>>>
pub fn inner_product<F, G, T2, T3, R2: Unsigned>(
&self,
other: &Ndarr<T2, R2>,
f: F,
g: G,
) -> Ndarr<T3, Sub1<Sub1<Sum<R1, R2>>>>where
R1: Max<R2> + Add<R2>,
<R1 as Max<R2>>::Output: Unsigned,
<R1 as Add<R2>>::Output: Sub<B1>,
<<R1 as Add<R2>>::Output as Sub<B1>>::Output: Sub<B1>,
<<<R1 as Add<R2>>::Output as Sub<B1>>::Output as Sub<B1>>::Output: Unsigned,
T1: Clone + Debug,
T2: Clone + Debug,
T3: Clone + Debug,
F: Fn(T1, T2) -> T3,
G: Fn(T3, T3) -> T3,
Sourcepub fn outer_product<F, T2, T3, R2: Unsigned>(
&self,
other: &Ndarr<T2, R2>,
f: F,
) -> Ndarr<T3, Sum<R1, R2>>
pub fn outer_product<F, T2, T3, R2: Unsigned>( &self, other: &Ndarr<T2, R2>, f: F, ) -> Ndarr<T3, Sum<R1, R2>>
Examples found in repository?
11fn update(mat: &mut Ndarr<i8, U2>) {
12 let rolls = Ndarr::from([1, 0, -1]);
13 let out = rolls
14 .map(|r| mat.roll(*r, 0))
15 .outer_product(&rolls, |a, r| a.roll(r, 1))
16 .sum();
17 mat.bimap_in_place(&out, |prev, new| {
18 if new == 3 || (prev == 1 && (new == 4)) {
19 1
20 } else {
21 0
22 }
23 })
24}Source§impl<T, R: Unsigned> Ndarr<T, R>
impl<T, R: Unsigned> Ndarr<T, R>
pub fn abs(&self) -> Self
pub fn is_positive(&self) -> Ndarr<bool, R>
pub fn is_negative(&self) -> Ndarr<bool, R>
Source§impl<T, R: Unsigned> Ndarr<T, R>
impl<T, R: Unsigned> Ndarr<T, R>
Sourcepub fn sum(&self) -> Twhere
T: Add<Output = T>,
pub fn sum(&self) -> Twhere
T: Add<Output = T>,
Examples found in repository?
11fn update(mat: &mut Ndarr<i8, U2>) {
12 let rolls = Ndarr::from([1, 0, -1]);
13 let out = rolls
14 .map(|r| mat.roll(*r, 0))
15 .outer_product(&rolls, |a, r| a.roll(r, 1))
16 .sum();
17 mat.bimap_in_place(&out, |prev, new| {
18 if new == 3 || (prev == 1 && (new == 4)) {
19 1
20 } else {
21 0
22 }
23 })
24}pub fn max(&self) -> Option<&T>where
T: Ord,
Source§impl<T, R: Unsigned> Ndarr<T, R>
impl<T, R: Unsigned> Ndarr<T, R>
pub fn sin(&self) -> Self
pub fn cos(&self) -> Self
pub fn tan(&self) -> Self
pub fn sinh(&self) -> Self
pub fn cosh(&self) -> Self
pub fn tanh(&self) -> Self
Sourcepub fn exp(&self) -> Self
pub fn exp(&self) -> Self
Examples found in repository?
14fn metropolis(spin_arr: &mut Ndarr<f32, U2>) {
15 let energy: Ndarr<f32, U2> = 2.
16 * spin_arr.clone()
17 * (spin_arr.roll(1, 0) + spin_arr.roll(-1, 0) + spin_arr.roll(1, 1) + spin_arr.roll(-1, 1));
18 let temp_exp = (-&energy / T).exp();
19 let indexes: Vec<usize> = (0..N).collect();
20 let i_s = NdarrRand::choose(&indexes, [M], None); //random i indexes
21 let j_s = NdarrRand::choose(&indexes, [M], None); //random j indexes
22 let p_swich: f32 = NdarrRand::uniform(0.0, 1.0, [1], None)[0]; // selection probability of random spin switch
23 for (i, j) in i_s.data.iter().zip(j_s.data.iter()) {
24 if energy[[*i, *j]] < 0.0 || p_swich < temp_exp[[*i, *j]] {
25 spin_arr[[*i, *j]] *= &-1.0;
26 }
27 }
28}pub fn log(&self, base: T) -> Self
pub fn ln(&self) -> Self
pub fn log2(&self) -> Self
pub fn is_infinite(&self) -> Ndarr<bool, R>
pub fn is_finite(&self) -> Ndarr<bool, R>
pub fn is_normal(&self) -> Ndarr<bool, R>
pub fn is_nan(&self) -> Ndarr<bool, R>
Source§impl<T1: Clone, R: Unsigned> Ndarr<T1, R>
impl<T1: Clone, R: Unsigned> Ndarr<T1, R>
pub fn map_in_place<F1: Fn(&T1) -> T1>(&mut self, f: F1)
Sourcepub fn map<T2: Clone + Debug, F2: Fn(&T1) -> T2>(&self, f: F2) -> Ndarr<T2, R>
pub fn map<T2: Clone + Debug, F2: Fn(&T1) -> T2>(&self, f: F2) -> Ndarr<T2, R>
Examples found in repository?
11fn update(mat: &mut Ndarr<i8, U2>) {
12 let rolls = Ndarr::from([1, 0, -1]);
13 let out = rolls
14 .map(|r| mat.roll(*r, 0))
15 .outer_product(&rolls, |a, r| a.roll(r, 1))
16 .sum();
17 mat.bimap_in_place(&out, |prev, new| {
18 if new == 3 || (prev == 1 && (new == 4)) {
19 1
20 } else {
21 0
22 }
23 })
24}
25
26fn main() {
27 //initialize game matrix with random 0 or 1
28 let mut x = NdarrRand::choose(&[0, 1], [N, N], None);
29 let mut stdout = stdout();
30 stdout.flush().unwrap();
31 stdout.write_all(b"\x1B[2J\x1B[1;1H").unwrap();
32
33 for i in 0..STEPS {
34 update(&mut x); //call update function
35 let vis = x.map(|x| {
36 if *x == 0 {
37 "░".to_string()
38 } else {
39 "█".to_string()
40 }
41 }); //make it pretty
42 println!("{}", vis);
43 println!(
44 "\n Conway's Game of Life using rapl: [Step {} out of {}] \n",
45 i + 1,
46 STEPS
47 );
48 sleep(Duration::from_millis(50));
49 stdout.write_all(b"\x1B[1;1H").unwrap();
50 stdout.flush().unwrap();
51 }
52 stdout.write_all(b"\x1B[2J\x1B[1;1H").unwrap();
53}More examples
30fn main() {
31 let mut stdout = stdout();
32 let mut spin_arr = NdarrRand::choose(&[-1.0, 1.0], [N, N], None);
33 stdout.flush().unwrap();
34 stdout.write_all(b"\x1B[2J\x1B[1;1H").unwrap();
35 for i in 0..STEPS {
36 metropolis(&mut spin_arr); //updates array with metropolis algorithm
37 let vis = spin_arr.map(|x| {
38 if *x < 0.0 {
39 "░".to_string()
40 } else {
41 "█".to_string()
42 }
43 }); //make it pretty
44
45 println!("{}", vis);
46
47 println!(
48 "\n The Ising Model using rapl: [Step {} out of {}] \n \n for more info: https://en.wikipedia.org/wiki/Ising_model"
49 , i + 1, STEPS);
50 sleep(Duration::from_millis(5));
51 stdout.write_all(b"\x1B[1;1H").unwrap();
52 stdout.flush().unwrap();
53 }
54 stdout.write_all(b"\x1B[2J\x1B[1;1H").unwrap();
55 stdout.flush().unwrap();
56}pub fn bimap<F: Fn(T1, T1) -> T1>(&self, other: &Self, f: F) -> Selfwhere
T1: Default,
Sourcepub fn bimap_in_place<F: Fn(T1, T1) -> T1>(&mut self, other: &Self, f: F)
pub fn bimap_in_place<F: Fn(T1, T1) -> T1>(&mut self, other: &Self, f: F)
Examples found in repository?
11fn update(mat: &mut Ndarr<i8, U2>) {
12 let rolls = Ndarr::from([1, 0, -1]);
13 let out = rolls
14 .map(|r| mat.roll(*r, 0))
15 .outer_product(&rolls, |a, r| a.roll(r, 1))
16 .sum();
17 mat.bimap_in_place(&out, |prev, new| {
18 if new == 3 || (prev == 1 && (new == 4)) {
19 1
20 } else {
21 0
22 }
23 })
24}pub fn scanr<F: Fn(T1, T1) -> T1>(&self, axis: usize, f: F) -> Self
pub fn scanl<F: Fn(T1, T1) -> T1>(&self, axis: usize, f: F) -> Self
Source§impl<T: Float + Default + Clone + Debug, R: Unsigned> Ndarr<T, R>
impl<T: Float + Default + Clone + Debug, R: Unsigned> Ndarr<T, R>
pub fn threshold(&self, threshold: &T, value: &T) -> Self
pub fn hard_tanh(&self, min_val: &T, max_val: &T) -> Self
pub fn elu(&self, alpha: &T) -> Self
pub fn hard_shrink(&self, lambda: &T) -> Self
pub fn hard_sigmoid(&self) -> Self
pub fn hard_swish(&self) -> Self
pub fn log_sigmoid(&self) -> Self
pub fn relu_6(&self) -> Self
pub fn selu(&self) -> Self
pub fn celu(&self, alpha: &T) -> Self
pub fn silu(&self) -> Self
pub fn softplus(&self, beta: &T) -> Self
pub fn mish(&self) -> Self
pub fn softshrink(&self, lambda: &T) -> Self
pub fn softsign(&self) -> Self
pub fn tanhshrink(&self) -> Self
pub fn sigmoid(&self) -> Self
pub fn relu(&self) -> Self
pub fn leaky_relu(&self, a: T) -> Self
pub fn softmax(&self) -> Self
Source§impl<T, R: Unsigned> Ndarr<T, R>
impl<T, R: Unsigned> Ndarr<T, R>
Sourcepub fn zeros<D: Into<Dim<R>>>(shape: D) -> Selfwhere
T: Zero,
pub fn zeros<D: Into<Dim<R>>>(shape: D) -> Selfwhere
T: Zero,
Examples found in repository?
5fn main() {
6 //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7 let img = open_lumaf32("graphics/peppers.png").unwrap();
8 //transform to complex and take the 2D FFT
9 let fft = img.to_complex().fft2d();
10
11 //initialize kernel for convolution
12 let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13 let (m, n) = (img.shape()[0], img.shape()[1]);
14 let mid_x = m / 2;
15 let mid_y = n / 2;
16 kernel[[mid_x, mid_y]] = 4.;
17 kernel[[mid_x + 1, mid_y]] = -1.;
18 kernel[[mid_x - 1, mid_y]] = -1.;
19 kernel[[mid_x, mid_y + 1]] = -1.;
20 kernel[[mid_x, mid_y - 1]] = -1.;
21 //FFT the kernell
22 let kernell = kernel.to_complex().fft2d();
23 //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24 //image and the kernel
25 let out = (fft * kernell).ifft2().fftshif().re();
26 //save output image
27 out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}pub fn ones<D: Into<Dim<R>>>(shape: D) -> Selfwhere
T: One,
pub fn fill<D: Into<Dim<R>>>(with: T, shape: D) -> Self
Source§impl<T: Debug + Clone + Add<Output = T> + Div<Output = T> + Sub<Output = T>> Ndarr<T, U1>
impl<T: Debug + Clone + Add<Output = T> + Div<Output = T> + Sub<Output = T>> Ndarr<T, U1>
Source§impl Ndarr<u8, U3>
impl Ndarr<u8, U3>
Sourcepub fn save_as_rgb<P: AsRef<Path>>(&self, path: P, fmt: ImageFormat)
pub fn save_as_rgb<P: AsRef<Path>>(&self, path: P, fmt: ImageFormat)
Saves a Ndarr<u8,3> with shape (with, heighth, 3) as RGB Image. Takes
path and format, where format is enum: ImageFormat.
Source§impl Ndarr<f32, U3>
impl Ndarr<f32, U3>
Sourcepub fn save_as_rgb<P: AsRef<Path>>(&self, path: P, fmt: ImageFormat)
pub fn save_as_rgb<P: AsRef<Path>>(&self, path: P, fmt: ImageFormat)
Saves a Ndarr<f32,3> with shape (with, heighth, 3) as RGB Image. Takes
path and format, where format is enum: ImageFormat.
Source§impl Ndarr<u8, U2>
impl Ndarr<u8, U2>
Sourcepub fn save_as_luma<P: AsRef<Path>>(&self, path: P, fmt: ImageFormat)
pub fn save_as_luma<P: AsRef<Path>>(&self, path: P, fmt: ImageFormat)
Saves a Ndarr<u8,2> with shape (with, heighth) as Luma (Black and white)
Image. Takes path and format, where format is enum: ImageFormat.
Source§impl Ndarr<f32, U2>
impl Ndarr<f32, U2>
Sourcepub fn save_as_luma<P: AsRef<Path>>(&self, path: P, fmt: ImageFormat)
pub fn save_as_luma<P: AsRef<Path>>(&self, path: P, fmt: ImageFormat)
Normalize a Ndarr<f32,2> to values form 0.0 to 1.0 and saves it as
Luma (Black and white) Image. Takes path and format, where format
is enum: ImageFormat.
Examples found in repository?
5fn main() {
6 //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7 let img = open_lumaf32("graphics/peppers.png").unwrap();
8 //transform to complex and take the 2D FFT
9 let fft = img.to_complex().fft2d();
10
11 //initialize kernel for convolution
12 let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13 let (m, n) = (img.shape()[0], img.shape()[1]);
14 let mid_x = m / 2;
15 let mid_y = n / 2;
16 kernel[[mid_x, mid_y]] = 4.;
17 kernel[[mid_x + 1, mid_y]] = -1.;
18 kernel[[mid_x - 1, mid_y]] = -1.;
19 kernel[[mid_x, mid_y + 1]] = -1.;
20 kernel[[mid_x, mid_y - 1]] = -1.;
21 //FFT the kernell
22 let kernell = kernel.to_complex().fft2d();
23 //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24 //image and the kernel
25 let out = (fft * kernell).ifft2().fftshif().re();
26 //save output image
27 out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}Source§impl<T: Clone + Debug + Copy + PartialEq> Ndarr<C<T>, U1>
impl<T: Clone + Debug + Copy + PartialEq> Ndarr<C<T>, U1>
Source§impl<T: Clone + Debug + Copy + PartialEq> Ndarr<C<T>, U2>
impl<T: Clone + Debug + Copy + PartialEq> Ndarr<C<T>, U2>
Sourcepub fn fft2d(&self) -> Ndarr<C<T>, U2>
pub fn fft2d(&self) -> Ndarr<C<T>, U2>
Performs the two dimensional Fourier Transform to a rank two rapl array: Ndarr<C<T>,U2>.
Examples found in repository?
5fn main() {
6 //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7 let img = open_lumaf32("graphics/peppers.png").unwrap();
8 //transform to complex and take the 2D FFT
9 let fft = img.to_complex().fft2d();
10
11 //initialize kernel for convolution
12 let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13 let (m, n) = (img.shape()[0], img.shape()[1]);
14 let mid_x = m / 2;
15 let mid_y = n / 2;
16 kernel[[mid_x, mid_y]] = 4.;
17 kernel[[mid_x + 1, mid_y]] = -1.;
18 kernel[[mid_x - 1, mid_y]] = -1.;
19 kernel[[mid_x, mid_y + 1]] = -1.;
20 kernel[[mid_x, mid_y - 1]] = -1.;
21 //FFT the kernell
22 let kernell = kernel.to_complex().fft2d();
23 //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24 //image and the kernel
25 let out = (fft * kernell).ifft2().fftshif().re();
26 //save output image
27 out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}Sourcepub fn ifft2(&self) -> Ndarr<C<T>, U2>
pub fn ifft2(&self) -> Ndarr<C<T>, U2>
Examples found in repository?
5fn main() {
6 //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7 let img = open_lumaf32("graphics/peppers.png").unwrap();
8 //transform to complex and take the 2D FFT
9 let fft = img.to_complex().fft2d();
10
11 //initialize kernel for convolution
12 let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13 let (m, n) = (img.shape()[0], img.shape()[1]);
14 let mid_x = m / 2;
15 let mid_y = n / 2;
16 kernel[[mid_x, mid_y]] = 4.;
17 kernel[[mid_x + 1, mid_y]] = -1.;
18 kernel[[mid_x - 1, mid_y]] = -1.;
19 kernel[[mid_x, mid_y + 1]] = -1.;
20 kernel[[mid_x, mid_y - 1]] = -1.;
21 //FFT the kernell
22 let kernell = kernel.to_complex().fft2d();
23 //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24 //image and the kernel
25 let out = (fft * kernell).ifft2().fftshif().re();
26 //save output image
27 out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}Source§impl<T: Clone + Debug> Ndarr<T, U2>
impl<T: Clone + Debug> Ndarr<T, U2>
Sourcepub fn fftshif(&self) -> Self
pub fn fftshif(&self) -> Self
Examples found in repository?
5fn main() {
6 //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7 let img = open_lumaf32("graphics/peppers.png").unwrap();
8 //transform to complex and take the 2D FFT
9 let fft = img.to_complex().fft2d();
10
11 //initialize kernel for convolution
12 let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13 let (m, n) = (img.shape()[0], img.shape()[1]);
14 let mid_x = m / 2;
15 let mid_y = n / 2;
16 kernel[[mid_x, mid_y]] = 4.;
17 kernel[[mid_x + 1, mid_y]] = -1.;
18 kernel[[mid_x - 1, mid_y]] = -1.;
19 kernel[[mid_x, mid_y + 1]] = -1.;
20 kernel[[mid_x, mid_y - 1]] = -1.;
21 //FFT the kernell
22 let kernell = kernel.to_complex().fft2d();
23 //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24 //image and the kernel
25 let out = (fft * kernell).ifft2().fftshif().re();
26 //save output image
27 out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}Source§impl<T: Copy + PartialEq + Clone + Debug + Default, R: Unsigned> Ndarr<C<T>, R>
impl<T: Copy + PartialEq + Clone + Debug + Default, R: Unsigned> Ndarr<C<T>, R>
Sourcepub fn re(&self) -> Ndarr<T, R>
pub fn re(&self) -> Ndarr<T, R>
Examples found in repository?
5fn main() {
6 //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7 let img = open_lumaf32("graphics/peppers.png").unwrap();
8 //transform to complex and take the 2D FFT
9 let fft = img.to_complex().fft2d();
10
11 //initialize kernel for convolution
12 let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13 let (m, n) = (img.shape()[0], img.shape()[1]);
14 let mid_x = m / 2;
15 let mid_y = n / 2;
16 kernel[[mid_x, mid_y]] = 4.;
17 kernel[[mid_x + 1, mid_y]] = -1.;
18 kernel[[mid_x - 1, mid_y]] = -1.;
19 kernel[[mid_x, mid_y + 1]] = -1.;
20 kernel[[mid_x, mid_y - 1]] = -1.;
21 //FFT the kernell
22 let kernell = kernel.to_complex().fft2d();
23 //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24 //image and the kernel
25 let out = (fft * kernell).ifft2().fftshif().re();
26 //save output image
27 out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}pub fn im(&self) -> Ndarr<T, R>
Source§impl<T: Copy + PartialEq + Num + Debug, R: Unsigned> Ndarr<T, R>
impl<T: Copy + PartialEq + Num + Debug, R: Unsigned> Ndarr<T, R>
Sourcepub fn to_complex(&self) -> Ndarr<C<T>, R>
pub fn to_complex(&self) -> Ndarr<C<T>, R>
Examples found in repository?
5fn main() {
6 //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7 let img = open_lumaf32("graphics/peppers.png").unwrap();
8 //transform to complex and take the 2D FFT
9 let fft = img.to_complex().fft2d();
10
11 //initialize kernel for convolution
12 let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13 let (m, n) = (img.shape()[0], img.shape()[1]);
14 let mid_x = m / 2;
15 let mid_y = n / 2;
16 kernel[[mid_x, mid_y]] = 4.;
17 kernel[[mid_x + 1, mid_y]] = -1.;
18 kernel[[mid_x - 1, mid_y]] = -1.;
19 kernel[[mid_x, mid_y + 1]] = -1.;
20 kernel[[mid_x, mid_y - 1]] = -1.;
21 //FFT the kernell
22 let kernell = kernel.to_complex().fft2d();
23 //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24 //image and the kernel
25 let out = (fft * kernell).ifft2().fftshif().re();
26 //save output image
27 out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}Source§impl<T: Copy + PartialEq + Neg<Output = T> + Clone + Debug + Default, R: Unsigned> Ndarr<C<T>, R>
impl<T: Copy + PartialEq + Neg<Output = T> + Clone + Debug + Default, R: Unsigned> Ndarr<C<T>, R>
Source§impl<T: Copy + PartialEq + Add<Output = T> + Mul<Output = T> + Clone + Debug + Default, R: Unsigned> Ndarr<C<T>, R>
impl<T: Copy + PartialEq + Add<Output = T> + Mul<Output = T> + Clone + Debug + Default, R: Unsigned> Ndarr<C<T>, R>
Source§impl<T, R: Unsigned> Ndarr<C<T>, R>
impl<T, R: Unsigned> Ndarr<C<T>, R>
pub fn abs(&self) -> Ndarr<T, R>
pub fn exp(&self) -> Self
pub fn arg(&self) -> Ndarr<T, R>
pub fn ln(&self) -> Self
pub fn sqrt(&self) -> Self
pub fn powf(&self, n: T) -> Self
pub fn powc(&self, _z: C<T>) -> Self
pub fn sin(&self) -> Self
pub fn cos(&self) -> Self
pub fn tan(&self) -> Self
pub fn csc(&self) -> Self
pub fn sec(&self) -> Self
pub fn cot(&self) -> Self
pub fn to_polar(&self) -> Ndarr<(T, T), R>
pub fn is_infinite(&self) -> Ndarr<bool, R>
pub fn is_finite(&self) -> Ndarr<bool, R>
pub fn is_normal(&self) -> Ndarr<bool, R>
pub fn is_nan(&self) -> Ndarr<bool, R>
Source§impl<T: Clone, R: Unsigned> Ndarr<T, R>
impl<T: Clone, R: Unsigned> Ndarr<T, R>
pub fn new<D: Into<Dim<R>>>(data: &[T], shape: D) -> Result<Self, DimError>
pub fn rank(&self) -> usize
Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Examples found in repository?
5fn main() {
6 //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7 let img = open_lumaf32("graphics/peppers.png").unwrap();
8 //transform to complex and take the 2D FFT
9 let fft = img.to_complex().fft2d();
10
11 //initialize kernel for convolution
12 let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13 let (m, n) = (img.shape()[0], img.shape()[1]);
14 let mid_x = m / 2;
15 let mid_y = n / 2;
16 kernel[[mid_x, mid_y]] = 4.;
17 kernel[[mid_x + 1, mid_y]] = -1.;
18 kernel[[mid_x - 1, mid_y]] = -1.;
19 kernel[[mid_x, mid_y + 1]] = -1.;
20 kernel[[mid_x, mid_y - 1]] = -1.;
21 //FFT the kernell
22 let kernell = kernel.to_complex().fft2d();
23 //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24 //image and the kernel
25 let out = (fft * kernell).ifft2().fftshif().re();
26 //save output image
27 out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}pub fn flatten(self) -> Vec<T>
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn from<P: Into<Self>>(p: P) -> Self
pub fn from<P: Into<Self>>(p: P) -> Self
Examples found in repository?
11fn update(mat: &mut Ndarr<i8, U2>) {
12 let rolls = Ndarr::from([1, 0, -1]);
13 let out = rolls
14 .map(|r| mat.roll(*r, 0))
15 .outer_product(&rolls, |a, r| a.roll(r, 1))
16 .sum();
17 mat.bimap_in_place(&out, |prev, new| {
18 if new == 3 || (prev == 1 && (new == 4)) {
19 1
20 } else {
21 0
22 }
23 })
24}Sourcepub fn reshape<R2: Unsigned, D: Into<Dim<R2>>>(
&self,
shape: D,
) -> Result<Ndarr<T, R2>, DimError>
pub fn reshape<R2: Unsigned, D: Into<Dim<R2>>>( &self, shape: D, ) -> Result<Ndarr<T, R2>, DimError>
Reshapes an Ndarr into an specified Shape. Returns Error if the shape is not compatible.
pub fn slice_at(&self, axis: usize) -> Vec<Ndarr<T, Sub1<R>>>
pub fn slice_at_notyped(&self, axis: usize) -> Vec<Ndarr<T, UTerm>>
Sourcepub fn reduce<F: Fn(T, T) -> T + Clone>(
&self,
axis: usize,
f: F,
) -> Result<Ndarr<T, Sub1<R>>, DimError>
pub fn reduce<F: Fn(T, T) -> T + Clone>( &self, axis: usize, f: F, ) -> Result<Ndarr<T, Sub1<R>>, DimError>
Takes a function `F(T,T)-T and an axis, evaluates the function by inserting it between the elements along the specified axis in right-to-left.
pub fn reduce_notyped<F: Fn(T, T) -> T + Clone>( &self, axis: usize, f: F, ) -> Result<Ndarr<T, UTerm>, DimError>
pub fn broadcast_to<R2: Unsigned, D: Into<Dim<R2>>>( &self, shape: D, ) -> Result<Ndarr<T, Maximum<R, R2>>, DimError>
pub fn broadcast<R2: Unsigned, D: Into<Dim<R2>>>( &self, shape: D, ) -> Result<Ndarr<T, Maximum<R, R2>>, DimError>
pub fn broadcast_data<R2: Unsigned, D: Into<Dim<R2>>>( &self, shape: D, ) -> Result<Vec<T>, DimError>
Sourcepub fn roll(&self, shift: isize, axis: usize) -> Self
pub fn roll(&self, shift: isize, axis: usize) -> Self
Roll array elements along a given axis, by shift isize.
Elements that roll beyond the last position are re-introduced at the first.
Examples found in repository?
11fn update(mat: &mut Ndarr<i8, U2>) {
12 let rolls = Ndarr::from([1, 0, -1]);
13 let out = rolls
14 .map(|r| mat.roll(*r, 0))
15 .outer_product(&rolls, |a, r| a.roll(r, 1))
16 .sum();
17 mat.bimap_in_place(&out, |prev, new| {
18 if new == 3 || (prev == 1 && (new == 4)) {
19 1
20 } else {
21 0
22 }
23 })
24}More examples
14fn metropolis(spin_arr: &mut Ndarr<f32, U2>) {
15 let energy: Ndarr<f32, U2> = 2.
16 * spin_arr.clone()
17 * (spin_arr.roll(1, 0) + spin_arr.roll(-1, 0) + spin_arr.roll(1, 1) + spin_arr.roll(-1, 1));
18 let temp_exp = (-&energy / T).exp();
19 let indexes: Vec<usize> = (0..N).collect();
20 let i_s = NdarrRand::choose(&indexes, [M], None); //random i indexes
21 let j_s = NdarrRand::choose(&indexes, [M], None); //random j indexes
22 let p_swich: f32 = NdarrRand::uniform(0.0, 1.0, [1], None)[0]; // selection probability of random spin switch
23 for (i, j) in i_s.data.iter().zip(j_s.data.iter()) {
24 if energy[[*i, *j]] < 0.0 || p_swich < temp_exp[[*i, *j]] {
25 spin_arr[[*i, *j]] *= &-1.0;
26 }
27 }
28}Trait Implementations§
Source§impl<P, T, R: Unsigned> AddAssign<&P> for Ndarr<T, R>
impl<P, T, R: Unsigned> AddAssign<&P> for Ndarr<T, R>
Source§fn add_assign(&mut self, other: &P)
fn add_assign(&mut self, other: &P)
+= operation. Read moreSource§impl<P, T, R: Unsigned> DivAssign<&P> for Ndarr<T, R>
impl<P, T, R: Unsigned> DivAssign<&P> for Ndarr<T, R>
Source§fn div_assign(&mut self, other: &P)
fn div_assign(&mut self, other: &P)
/= operation. Read moreSource§impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> From<[[[[T; N1]; N2]; N3]; N4]> for Ndarr<T, U4>
impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> From<[[[[T; N1]; N2]; N3]; N4]> for Ndarr<T, U4>
Source§impl<T, const N1: usize, const N2: usize, const N3: usize> From<[[[T; N1]; N2]; N3]> for Ndarr<T, U3>
impl<T, const N1: usize, const N2: usize, const N3: usize> From<[[[T; N1]; N2]; N3]> for Ndarr<T, U3>
Source§impl<P, T, R: Unsigned> MulAssign<&P> for Ndarr<T, R>
impl<P, T, R: Unsigned> MulAssign<&P> for Ndarr<T, R>
Source§fn mul_assign(&mut self, other: &P)
fn mul_assign(&mut self, other: &P)
*= operation. Read moreSource§impl<P, T, R: Unsigned> RemAssign<&P> for Ndarr<T, R>
impl<P, T, R: Unsigned> RemAssign<&P> for Ndarr<T, R>
Source§fn rem_assign(&mut self, other: &P)
fn rem_assign(&mut self, other: &P)
%= operation. Read moreSource§impl<P, T, R: Unsigned> SubAssign<&P> for Ndarr<T, R>
impl<P, T, R: Unsigned> SubAssign<&P> for Ndarr<T, R>
Source§fn sub_assign(&mut self, other: &P)
fn sub_assign(&mut self, other: &P)
-= operation. Read moreimpl<T: Eq + Clone, R: Eq + Unsigned> Eq for Ndarr<T, R>
impl<T: Clone, R: Unsigned> StructuralPartialEq for Ndarr<T, R>
Auto Trait Implementations§
impl<T, R> Freeze for Ndarr<T, R>
impl<T, R> RefUnwindSafe for Ndarr<T, R>where
R: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, R> Send for Ndarr<T, R>
impl<T, R> Sync for Ndarr<T, R>
impl<T, R> Unpin for Ndarr<T, R>
impl<T, R> UnwindSafe for Ndarr<T, R>where
R: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more