Skip to content

Commit ea57594

Browse files
committed
Change Array to generic to support type checks
All functions in all modules have been changed to accept the new generic Array objects. * Fixed typos in ifft2 and iff3 documentation * Fixed/Added `mut` qualifier for inplace fft transform arguments * Minor modifications to examples to use generic Array types
1 parent a79a929 commit ea57594

File tree

19 files changed

+1997
-1043
lines changed

19 files changed

+1997
-1043
lines changed

examples/acoustic_wave.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ use std::f64::consts::*;
66
fn main() {
77
set_device(0);
88
info();
9-
109
acoustic_wave_simulation();
1110
}
1211

13-
fn normalise(a: &Array) -> Array {
12+
fn normalise(a: &Array<f32>) -> Array<f32> {
1413
(a/(max_all(&abs(a)).0 as f32 * 2.0f32)) + 0.5f32
1514
}
15+
1616
fn acoustic_wave_simulation() {
1717
// Speed of sound
18-
let c = 0.1;
18+
let c :f32 = 0.1;
1919
// Distance step
20-
let dx = 0.5;
20+
let dx :f32 = 0.5;
2121
// Time step
22-
let dt = 1.0;
22+
let dt :f32 = 1.0;
2323

2424
// Grid size.
25-
let nx = 1500;
26-
let ny = 1500;
25+
let nx : u64 = 1500;
26+
let ny : u64 = 1500;
2727

2828
// Grid dimensions.
2929
let dims = Dim4::new(&[nx, ny, 1, 1]);
@@ -34,26 +34,27 @@ fn acoustic_wave_simulation() {
3434
let mut p_dot = p.clone();
3535

3636
// Laplacian (Del^2) convolution kernel.
37-
let laplacian_values = [0.0f32, 1.0, 0.0,
38-
1.0, -4.0, 1.0,
39-
0.0, 1.0, 0.0];
37+
let laplacian_values: [f32; 9] = [0.0, 1.0, 0.0,
38+
1.0, -4.0, 1.0,
39+
0.0, 1.0, 0.0];
4040
let laplacian_kernel = Array::new(&laplacian_values, Dim4::new(&[3, 3, 1, 1])) / (dx * dx);
4141

4242
// Create a window to show the waves.
4343
let mut win = Window::new(1000, 1000, "Waves".to_string());
4444

4545
// Hann windowed pulse.
46-
let pulse_time = 100.0f64;
47-
let centre_freq = 0.05;
46+
let pulse_time :f32 = 100.0;
47+
let centre_freq :f32 = 0.05;
48+
let twopi = PI as f32 * 2.0;
4849

4950
// Number of samples in pulse.
5051
let pulse_n = (pulse_time/dt).floor() as u64;
5152

52-
let i = range::<f32>(Dim4::new(&[pulse_n, 1, 1, 1]), 0);
53-
let t = i.clone() * dt;
54-
let hamming_window = cos(&(i * (2.0 * PI / pulse_n as f64))) * -0.46 + 0.54;
55-
let wave = sin(&(&t * centre_freq * 2.0 * PI));
56-
let pulse = wave * hamming_window;
53+
let i = range::<f32>(Dim4::new(&[pulse_n, 1, 1, 1]), 0);
54+
let t = i.clone() * dt;
55+
let hmg_wnd = cos(&(i * (twopi / pulse_n as f32))) * -0.46f32 + 0.54f32;
56+
let wave = sin(&(&t * centre_freq * twopi));
57+
let pulse = wave * hmg_wnd;
5758

5859
// Iteration count.
5960
let mut it = 0;

examples/helloworld.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[macro_use(af_print)]
22
extern crate arrayfire as af;
3+
extern crate num;
34

45
use af::*;
56

@@ -18,24 +19,29 @@ fn main() {
1819
let values: [f32; 3] = [1.0, 2.0, 3.0];
1920
let indices = Array::new(&values, Dim4::new(&[3, 1, 1, 1]));
2021

22+
af_print!("Indices ", indices);
23+
2124
let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);
2225

2326
let a = randu::<f32>(dims);
24-
af_print!("Create a 5-by-3 matrix of random floats on the GPU", a);
27+
af_print!("Create a 5-by-3 float matrix on the GPU", a);
2528

2629
println!("Element-wise arithmetic");
2730
let b = add(&sin(&a), &1.5f32, false);
2831

2932
let b2 = add(&sin(&a), &cos(&a), false);
3033

3134
let b3 = ! &a;
32-
af_print!("sin(a) + 1.5 => ", b);
35+
af_print!("sin(a) + 1.5 a.k.a b => ", b);
3336
af_print!("sin(a) + cos(a) => ", b2);
3437
af_print!("!a => ", b3);
3538

3639
let test = a.clone() + b.clone();
3740
af_print!("a + b", test);
3841

42+
let negation = -(a.clone());
43+
af_print!("-a ", negation);
44+
3945
// Index array using sequences
4046
let seqs = &[Seq::new(1u32, 3, 1), Seq::default()];
4147
let sub = index(&a, seqs);
@@ -51,10 +57,6 @@ fn main() {
5157
let sub2 = index_gen(&a, idxrs);
5258
af_print!("a(indices, seq(0, 2, 1))", sub2);
5359

54-
// printf("Negate the first three elements of second column\n");
55-
// B(seq(0, 2), 1) = B(seq(0, 2), 1) * -1;
56-
// af_print(B);
57-
5860
println!("Fourier transform the result");
5961
print(&fft(&b, 1.0, 0));
6062

@@ -74,15 +76,11 @@ fn main() {
7476
let d = Array::new(&d_input, d_dims);
7577
af_print!("Create 2-by-3 matrix from host data", d);
7678

77-
// printf("Copy last column onto first\n");
78-
// D.col(0) = D.col(end);
79-
// af_print(D);
80-
81-
// // Sort A
82-
println!("Sort A and print sorted array and corresponding indices");
83-
let x = sort_index(&a, 0, true);
84-
print(&x.0);
85-
print(&x.1);
79+
//// // Sort A
80+
//println!("Sort A and print sorted array and corresponding indices");
81+
//let x = sort_index(&a, 0, true);
82+
//print(&x.0);
83+
//print(&x.1);
8684

8785
let u8_cnst = &constant(1 as u8, dims);
8886
af_print!("u8 constant array", u8_cnst);

examples/histogram.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
let hst_wnd = Window::new(512, 512, String::from("Input Image Histogram"));
2020
hst_wnd.set_position(600, 100);
2121

22-
let man = load_image(format!("{}/man.jpg", assets_dir.display()), false);
22+
let man = load_image::<f32>(format!("{}/man.jpg", assets_dir.display()), false);
2323
let hst = histogram(&man, 256, 0.0, 255.0);
2424

2525
let disp_img = div(&man, &constant(255 as f32, man.dims()), false);

0 commit comments

Comments
 (0)