@@ -8,128 +8,63 @@ to existing Arrays. Given below are few of such functions and their correspondin
8
8
[ Indexer::new] [ 2 ] to create an Indexer object and set either a ` Seq ` object or ` Array ` as indexing
9
9
object for a given dimension.
10
10
11
- ## Using Seq objects to index Array
11
+ ## Using Seq objects
12
12
13
- Create a view of an existing Array using Sequences and the function [ index] [ 3 ] .
13
+ ### Create a view of an existing Array
14
+
15
+ We will Sequences and the function [ index] [ 3 ] in this approach.
14
16
15
17
``` rust,noplaypen
16
- let dims = Dim4::new(&[5, 5, 1, 1]);
17
- let a = randu::<f32>(dims);
18
- af_print!("a", a);
19
- //a
20
- //[5 5 1 1]
21
- // 0.3990 0.5160 0.8831 0.9107 0.6688
22
- // 0.6720 0.3932 0.0621 0.9159 0.8434
23
- // 0.5339 0.2706 0.7089 0.0231 0.1328
24
- // 0.1386 0.9455 0.9434 0.2330 0.2657
25
- // 0.7353 0.1587 0.1227 0.2220 0.2299
26
-
27
- // Index array using sequences
28
- let seqs = &[Seq::new(1u32, 3, 1), Seq::default()];
29
- let sub = index(&a, seqs);
30
- af_print!("a(seq(1,3,1), span)", sub);
31
- // [3 5 1 1]
32
- // 0.6720 0.3932 0.0621 0.9159 0.8434
33
- // 0.5339 0.2706 0.7089 0.0231 0.1328
34
- // 0.1386 0.9455 0.9434 0.2330 0.2657
18
+ {{#include ../../src/core/index.rs:non_macro_seq_index}}
19
+ ```
20
+ However, the same above code can be condensed into a much terse syntax with the help of [ view] [ 11 ]
21
+ macro. Take a look at the above code modified to use view macro.
22
+ ``` rust, noplaypen
23
+ {{#include ../../src/core/index.rs:seq_index}}
35
24
```
36
25
37
- Set a sub-portion of an existing Array with a constant value using [ assign\_ seq] [ 4 ] .
26
+ ### Modify a sub region of an existing Array
27
+
28
+ Let us take a look at an example where a portion of an existing Array will be set to with another
29
+ Array. We will an constant value Array and the function [ assign\_ seq] [ 4 ] in the below example.
38
30
39
31
``` rust,noplaypen
40
- let a = constant(2.0 as f32, Dim4::new(&[5, 3, 1, 1]));
41
- let b = constant(1.0 as f32, Dim4::new(&[3, 3, 1, 1]));
42
- let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()];
43
- let sub = assign_seq(&a, seqs, &b);
44
- print(&a);
45
- // 2.0 2.0 2.0
46
- // 2.0 2.0 2.0
47
- // 2.0 2.0 2.0
48
- // 2.0 2.0 2.0
49
- // 2.0 2.0 2.0
50
-
51
- print(&sub);
52
- // 2.0 2.0 2.0
53
- // 1.0 1.0 1.0
54
- // 1.0 1.0 1.0
55
- // 1.0 1.0 1.0
56
- // 2.0 2.0 2.0
32
+ {{#include ../../src/core/index.rs:non_macro_seq_assign}}
57
33
```
58
34
59
35
> ** NOTE** Normally you want to avoid accessing individual elements of the array like this for performance reasons.
60
36
61
- ## Using Array and Seq combination to index Array
37
+ ## Using Array and Seq combination
62
38
63
- Create a view of an existing Array using another Array and Sequence.
39
+ ### Create a view of an existing Array
40
+
41
+ To use a combination of Array and Seq objects to index an existing Array, we will need a more
42
+ generalized function [ index\_ gen] [ 12 ] .
64
43
65
44
``` rust,noplaypen
66
- use arrayfire::{Array, Dim4, Seq, print, randu, index_gen, Indexer};
67
- let values: [f32; 3] = [1.0, 2.0, 3.0];
68
- let indices = Array::new(&values, Dim4::new(&[3, 1, 1, 1]));
69
- let seq4gen = Seq::new(0.0, 2.0, 1.0);
70
- let a = randu::<f32>(Dim4::new(&[5, 3, 1, 1]));
71
- // [5 3 1 1]
72
- // 0.0000 0.2190 0.3835
73
- // 0.1315 0.0470 0.5194
74
- // 0.7556 0.6789 0.8310
75
- // 0.4587 0.6793 0.0346
76
- // 0.5328 0.9347 0.0535
77
-
78
- let mut idxrs = Indexer::new();
79
- idxrs.set_index(&indices, 0, None); // 2nd parameter is indexing dimension
80
- idxrs.set_index(&seq4gen, 1, Some(false)); // 3rd parameter indicates batch operation
81
-
82
- let sub2 = index_gen(&a, idxrs);
83
- println!("a(indices, seq(0, 2, 1))"); print(&sub2);
84
- // [3 3 1 1]
85
- // 0.1315 0.0470 0.5194
86
- // 0.7556 0.6789 0.8310
87
- // 0.4587 0.6793 0.0346
45
+ {{#include ../../src/core/index.rs:non_macro_seq_array_index}}
46
+ ```
47
+ Similar to how, [ view] [ 11 ] macro helped with abreviating the syntax when indexing with just
48
+ sequences, it can also help when using a combination of Seq and Array.
49
+ ``` rust, noplaypen
50
+ {{#include ../../src/core/index.rs:seq_array_index}}
88
51
```
89
52
90
- Set a sub-portion of an existing Array with another Array using a combination
91
- of ` Seq ` and ` Array ` .
53
+ ### Modify a sub region of an existing Array
54
+
55
+ Set a portion of an existing Array with another Array using a combination of ` Seq ` and ` Array ` .
56
+ We will use [ assign\_ gen] [ 13 ] function to do it.
92
57
93
58
``` rust,noplaypen
94
- use arrayfire::{Array, Dim4, Seq, print, randu, constant, Indexer, assign_gen};
95
- let values: [f32; 3] = [1.0, 2.0, 3.0];
96
- let indices = Array::new(&values, Dim4::new(&[3, 1, 1, 1]));
97
- let seq4gen = Seq::new(0.0, 2.0, 1.0);
98
- let a = randu::<f32>(Dim4::new(&[5, 3, 1, 1]));
99
- // [5 3 1 1]
100
- // 0.0000 0.2190 0.3835
101
- // 0.1315 0.0470 0.5194
102
- // 0.7556 0.6789 0.8310
103
- // 0.4587 0.6793 0.0346
104
- // 0.5328 0.9347 0.0535
105
-
106
- let b = constant(2.0 as f32, Dim4::new(&[3, 3, 1, 1]));
107
-
108
- let mut idxrs = Indexer::new();
109
- idxrs.set_index(&indices, 0, None); // 2nd parameter is indexing dimension
110
- idxrs.set_index(&seq4gen, 1, Some(false)); // 3rd parameter indicates batch operation
111
-
112
- let sub2 = assign_gen(&a, &idxrs, &b);
113
- println!("a(indices, seq(0, 2, 1))"); print(&sub2);
114
- // [5 3 1 1]
115
- // 0.0000 0.2190 0.3835
116
- // 2.0000 2.0000 2.0000
117
- // 2.0000 2.0000 2.0000
118
- // 2.0000 2.0000 2.0000
119
- // 0.5328 0.9347 0.0535
59
+ {{#include ../../src/core/index.rs:non_macro_seq_array_assign}}
120
60
```
121
61
122
62
## Extract or Set rows/coloumns of an Array
123
63
124
64
Extract a specific set of rows/coloumns from an existing Array.
125
65
126
66
``` rust,noplaypen
127
- let dims = Dim4::new(&[5, 5, 1, 1]);
128
- let a = randu::<f32>(dims);
129
- println!("Grab last row of the random matrix");
130
- print(&a);
131
- print(&row(&a, 4));
132
- print(&col(&a, 4));
67
+ {{#include ../../src/core/index.rs:setrow}}
133
68
```
134
69
135
70
You can also use [ rows] [ 5 ] & [ cols] [ 6 ] to retrieve a subset of rows or coloumns respectively.
@@ -148,3 +83,6 @@ for coloumns.
148
83
[ 8 ] : http://arrayfire.org/arrayfire-rust/arrayfire/fn.set_col.html
149
84
[ 9 ] : http://arrayfire.org/arrayfire-rust/arrayfire/fn.set_rows.html
150
85
[ 10 ] : http://arrayfire.org/arrayfire-rust/arrayfire/fn.set_cols.html
86
+ [ 11 ] : http://arrayfire.org/arrayfire-rust/arrayfire/macro.view.html
87
+ [ 12 ] : http://arrayfire.org/arrayfire-rust/arrayfire/fn.index_gen.html
88
+ [ 13 ] : http://arrayfire.org/arrayfire-rust/arrayfire/fn.assign_gen.html
0 commit comments