1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
extern crate libc;

use array::Array;
use defines::{AfError, BinaryOp};
use error::HANDLE_ERROR;
use self::libc::{c_int, uint8_t, c_uint, c_double};
use util::{AfArray, MutAfArray, MutDouble, MutUint};

#[allow(dead_code)]
extern {
    fn af_sum(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_sum_nan(out: MutAfArray, input: AfArray, dim: c_int, nanval: c_double) -> c_int;
    fn af_product(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_product_nan(out: MutAfArray, input: AfArray, dim: c_int, val: c_double) -> c_int;
    fn af_min(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_max(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_all_true(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_any_true(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_count(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_sum_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_sum_nan_all(r: MutDouble, i: MutDouble, input: AfArray, val: c_double) -> c_int;
    fn af_product_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_product_nan_all(r: MutDouble, i: MutDouble, input: AfArray, val: c_double) -> c_int;
    fn af_min_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_max_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_all_true_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_any_true_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_count_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_imin(out: MutAfArray, idx: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_imax(out: MutAfArray, idx: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_imin_all(r: MutDouble, i: MutDouble, idx: MutUint, input: AfArray) -> c_int;
    fn af_imax_all(r: MutDouble, i: MutDouble, idx: MutUint, input: AfArray) -> c_int;
    fn af_accum(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_where(out: MutAfArray, input: AfArray) -> c_int;
    fn af_diff1(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_diff2(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_sort(out: MutAfArray, input: AfArray, dim: c_uint, ascend: c_int) -> c_int;
    fn af_sort_index(o: MutAfArray, i: MutAfArray, inp: AfArray, d: c_uint, a: c_int) -> c_int;
    fn af_set_unique(out: MutAfArray, input: AfArray, is_sorted: c_int) -> c_int;
    fn af_set_union(out: MutAfArray, first: AfArray, second: AfArray, is_unq: c_int) -> c_int;
    fn af_set_intersect(out: MutAfArray, one: AfArray, two: AfArray, is_unq: c_int) -> c_int;

    fn af_sort_by_key(out_keys: MutAfArray, out_vals: MutAfArray,
                      in_keys: AfArray, in_vals: AfArray, dim: c_uint, ascend: c_int) -> c_int;

    fn af_scan(out: MutAfArray, inp: AfArray, dim: c_int, op: uint8_t, inclusive: c_int) -> c_int;
    fn af_scan_by_key(out: MutAfArray, key: AfArray, inp: AfArray,
                      dim: c_int, op: uint8_t, inclusive: c_int) -> c_int;
}

macro_rules! dim_reduce_func_def {
    ($doc_str: expr, $fn_name: ident, $ffi_name: ident) => (
        #[doc=$doc_str]
        #[allow(unused_mut)]
        pub fn $fn_name(input: &Array, dim: i32) -> Array {
            unsafe {
                let mut temp: i64 = 0;
                let err_val = $ffi_name(&mut temp as MutAfArray,
                                        input.get() as AfArray, dim as c_int);
                HANDLE_ERROR(AfError::from(err_val));
                Array::from(temp)
            }
        }
    )
}

dim_reduce_func_def!("
    Sum elements along a given dimension

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which the input Array will be reduced

    # Return Values

    Result Array after summing all elements along given dimension

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, sum};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = sum(&a, 0);
    print(&b);
    let c = sum(&a, 1);
    print(&c);
    ```
    ",
    sum, af_sum);

dim_reduce_func_def!("
    Compute product of elements along a given dimension

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which the input Array will be reduced

    # Return Values

    Result Array after multiplying all elements along given dimension

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, product};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = product(&a, 0);
    print(&b);
    let c = product(&a, 1);
    print(&c);
    ```
    ", product, af_product);

dim_reduce_func_def!("
    Find minimum among elements of given dimension

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which the input Array will be reduced

    # Return Values

    Result Array after finding minimum among elements along a given dimension

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, min};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = min(&a, 0);
    print(&b);
    let c = min(&a, 1);
    print(&c);
    ```
    ", min, af_min);

dim_reduce_func_def!("
    Find maximum among elements of given dimension

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which the input Array will be reduced

    # Return Values

    Result Array after finding maximum among elements along a given dimension

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, max};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = max(&a, 0);
    print(&b);
    let c = max(&a, 1);
    print(&c);
    ```
    ", max, af_max);

dim_reduce_func_def!("
    Find if all of the values along a given dimension in the Array are true

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which the predicate is evaluated

    # Return Values

    Result Array that contains the result of `AND` operation of all elements along given dimension

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, all_true};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = all_true(&a, 0);
    print(&b);
    let c = all_true(&a, 1);
    print(&c);
    ```
    ", all_true, af_all_true);

dim_reduce_func_def!("
    Find if any of the values along a given dimension in the Array are true

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which the predicate is evaluated

    # Return Values

    Result Array that contains the result of `OR` operation of all elements along given dimension

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, any_true};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = any_true(&a, 0);
    print(&b);
    let c = any_true(&a, 1);
    print(&c);
    ```
    ", any_true, af_any_true);

dim_reduce_func_def!("
    Count number of non-zero elements along a given dimension

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which the non-zero elements are counted

    # Return Values

    Result Array with number of non-zero elements along a given dimension

    # Examples

    ```rust
    use arrayfire::{Dim4, gt, print, randu, count};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let cnst: f32 = 0.5;
    let a = gt(&randu::<f32>(dims), &cnst, false);
    print(&a);
    let b = count(&a, 0);
    print(&b);
    let c = count(&a, 1);
    print(&c);
    ```
    ", count, af_count);

dim_reduce_func_def!("
    Perform exclusive sum of elements along a given dimension

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which the exclusive scan operation is carried out

    # Return Values

    Result Array with exclusive sums of input Array elements along a given dimension

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, accum};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = accum(&a, 0);
    print(&b);
    let c = accum(&a, 1);
    print(&c);
    ```
    ", accum, af_accum);

dim_reduce_func_def!("
    Calculate first order numerical difference along a given dimension

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which first order difference is calculated

    # Return Values

    Result Array with first order difference values

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, diff1};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = diff1(&a, 0);
    print(&b);
    let c = diff1(&a, 1);
    print(&c);
    ```
    ", diff1, af_diff1);

dim_reduce_func_def!("
    Calculate second order numerical difference along a given dimension

    # Parameters

    - `input` - Input Array
    - `dim`   - Dimension along which second order difference is calculated

    # Return Values

    Result Array with second order difference values

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, diff2};
    let dims = Dim4::new(&[5, 3, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    let b = diff2(&a, 0);
    print(&b);
    let c = diff2(&a, 1);
    print(&c);
    ```
    ", diff2, af_diff2);

/// Sum along specific dimension using user specified value instead of `NAN` values
///
/// Sum values of the `input` Array along `dim` dimension after replacing any `NAN` values in the
/// Array with the value of the parameter `nanval`.
///
/// # Parameters
///
/// - `input` is the input Array
/// - `dim` is reduction dimension
/// - `nanval` is value with which all the `NAN` values of Array are replaced with
///
/// # Return Values
///
/// Array that is reduced along given dimension via addition operation
pub fn sum_nan(input: &Array, dim: i32, nanval: f64) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_sum_nan(&mut temp as MutAfArray, input.get() as AfArray,
                                 dim as c_int, nanval as c_double);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Product of elements along specific dimension using user specified value instead of `NAN` values
///
/// Compute product of the values of the `input` Array along `dim` dimension after replacing any `NAN` values in the Array with `nanval` value.
///
/// # Parameters
///
/// - `input` is the input Array
/// - `dim` is reduction dimension
/// - `nanval` is value with which all the `NAN` values of Array are replaced with
///
/// # Return Values
///
/// Array that is reduced along given dimension via multiplication operation
pub fn product_nan(input: &Array, dim: i32, nanval: f64) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_product_nan(&mut temp as MutAfArray, input.get() as AfArray,
                                     dim as c_int, nanval as c_double);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

macro_rules! all_reduce_func_def {
    ($doc_str: expr, $fn_name: ident, $ffi_name: ident) => (
        #[doc=$doc_str]
        #[allow(unused_mut)]
        pub fn $fn_name(input: &Array) -> (f64, f64) {
            unsafe {
                let mut real: f64 = 0.0;
                let mut imag: f64 = 0.0;
                let err_val = $ffi_name(&mut real as MutDouble, &mut imag as MutDouble,
                                        input.get() as AfArray);
                HANDLE_ERROR(AfError::from(err_val));
                (real, imag)
            }
        }
    )
}

all_reduce_func_def!("
    Sum all values of the Array

    # Parameters

    - `input` is the input Array

    # Return Values

    A tuple containing the summation result.

    Note: For non-complex data type Arrays, second value of tuple is zero.

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, sum_all};
    let dims = Dim4::new(&[5, 5, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    println!(\"Result : {:?}\", sum_all(&a));
    ```
    ", sum_all, af_sum_all);

all_reduce_func_def!("
    Product of all values of the Array

    # Parameters

    - `input` is the input Array

    # Return Values

    A tuple containing the product result.

    Note: For non-complex data type Arrays, second value of tuple is zero.

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, product_all};
    let dims = Dim4::new(&[5, 5, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    println!(\"Result : {:?}\", product_all(&a));
    ```
    ", product_all, af_product_all);

all_reduce_func_def!("
    Find minimum among all values of the Array

    # Parameters

    - `input` is the input Array

    # Return Values

    A tuple containing the minimum value.

    Note: For non-complex data type Arrays, second value of tuple is zero.

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, min_all};
    let dims = Dim4::new(&[5, 5, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    println!(\"Result : {:?}\", min_all(&a));
    ```
    ", min_all, af_min_all);

all_reduce_func_def!("
    Find maximum among all values of the Array

    # Parameters

    - `input` is the input Array

    # Return Values

    A tuple containing the maximum value.

    Note: For non-complex data type Arrays, second value of tuple is zero.

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, max_all};
    let dims = Dim4::new(&[5, 5, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    println!(\"Result : {:?}\", max_all(&a));
    ```
    ", max_all, af_max_all);

all_reduce_func_def!("
    Find if all values of Array are non-zero

    # Parameters

    - `input` is the input Array

    # Return Values

    A tuple containing the result of `AND` operation on all values of Array.

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, all_true_all};
    let dims = Dim4::new(&[5, 5, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    println!(\"Result : {:?}\", all_true_all(&a));
    ```
    ", all_true_all, af_all_true_all);

all_reduce_func_def!("
    Find if any value of Array is non-zero

    # Parameters

    - `input` is the input Array

    # Return Values

    A tuple containing the result of `OR` operation on all values of Array.

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, any_true_all};
    let dims = Dim4::new(&[5, 5, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    println!(\"Result : {:?}\", any_true_all(&a));
    ```
    ", any_true_all, af_any_true_all);

all_reduce_func_def!("
    Count number of non-zero values in the Array

    # Parameters

    - `input` is the input Array

    # Return Values

    A tuple containing the count of non-zero values in the Array.

    # Examples

    ```rust
    use arrayfire::{Dim4, print, randu, count_all};
    let dims = Dim4::new(&[5, 5, 1, 1]);
    let a = randu::<f32>(dims);
    print(&a);
    println!(\"Result : {:?}\", count_all(&a));
    ```
    ", count_all, af_count_all);

/// Sum all values using user provided value for `NAN`
///
/// Sum all the values of the `input` Array after replacing any `NAN` values with `val`.
///
/// # Parameters
///
/// - `input` is the input Array
/// - `val` is the val that replaces all `NAN` values of the Array before reduction operation is
/// performed.
///
/// # Return Values
///
/// A tuple of summation result.
///
/// Note: For non-complex data type Arrays, second value of tuple is zero.
pub fn sum_nan_all(input: &Array, val: f64) -> (f64, f64) {
    unsafe {
        let mut real: f64 = 0.0;
        let mut imag: f64 = 0.0;
        let err_val = af_sum_nan_all(&mut real as MutDouble, &mut imag as MutDouble,
                                     input.get() as AfArray, val as c_double);
        HANDLE_ERROR(AfError::from(err_val));
        (real, imag)
    }
}

/// Product of all values using user provided value for `NAN`
///
/// Compute the product of all the values of the `input` Array after replacing any `NAN` values with `val`
///
/// # Parameters
///
/// - `input` is the input Array
/// - `val` is the val that replaces all `NAN` values of the Array before reduction operation is
/// performed.
///
/// # Return Values
///
/// A tuple of product result.
///
/// Note: For non-complex data type Arrays, second value of tuple is zero.
pub fn product_nan_all(input: &Array, val: f64) -> (f64, f64) {
    unsafe {
        let mut real: f64 = 0.0;
        let mut imag: f64 = 0.0;
        let err_val = af_product_nan_all(&mut real as MutDouble, &mut imag as MutDouble,
                                         input.get() as AfArray, val as c_double);
        HANDLE_ERROR(AfError::from(err_val));
        (real, imag)
    }
}

macro_rules! dim_ireduce_func_def {
    ($doc_str: expr, $fn_name: ident, $ffi_name: ident) => (
        #[doc=$doc_str]
        #[allow(unused_mut)]
        pub fn $fn_name(input: &Array, dim: i32) -> (Array, Array) {
            unsafe {
                let mut temp: i64 = 0;
                let mut idx: i64 = 0;
                let err_val = $ffi_name(&mut temp as MutAfArray, &mut idx as MutAfArray,
                                        input.get() as AfArray, dim as c_int);
                HANDLE_ERROR(AfError::from(err_val));
                (Array::from(temp), Array::from(idx))
            }
        }
    )
}

dim_ireduce_func_def!("
    Find minimum value along given dimension and their corresponding indices

    # Parameters

    - `input` - Input Array
    - `dim` - Dimension along which the input Array will be reduced

    # Return Values

    A tuple of Arrays: Array minimum values and Array containing their index along the reduced dimension.
    ", imin, af_imin);

dim_ireduce_func_def!("
    Find maximum value along given dimension and their corresponding indices

    # Parameters

    - `input` - Input Array
    - `dim` - Dimension along which the input Array will be reduced

    # Return Values

    A tuple of Arrays: Array maximum values and Array containing their index along the reduced dimension.
    ", imax, af_imax);

macro_rules! all_ireduce_func_def {
    ($doc_str: expr, $fn_name: ident, $ffi_name: ident) => (
        #[doc=$doc_str]
        #[allow(unused_mut)]
        pub fn $fn_name(input: &Array) -> (f64, f64, u32) {
            unsafe {
                let mut real: f64 = 0.0;
                let mut imag: f64 = 0.0;
                let mut temp: u32 = 0;
                let err_val = $ffi_name(&mut real as MutDouble, &mut imag as MutDouble,
                                        &mut temp as MutUint, input.get() as AfArray);
                HANDLE_ERROR(AfError::from(err_val));
                (real, imag, temp)
            }
        }
    )
}

all_ireduce_func_def!("
    Find minimum and it's index in the whole Array

    # Parameters

    `input` - Input Array

    # Return Values

    A triplet with

      * minimum element of Array in the first component.
      * second component of value zero if Array is of non-complex type.
      * index of minimum element in the third component.
    ", imin_all, af_imin_all);
all_ireduce_func_def!("
    Find maximum and it's index in the whole Array

    # Parameters

    `input` - Input Array

    # Return Values

    A triplet with

      - maximum element of Array in the first component.
      - second component of value zero if Array is of non-complex type.
      - index of maximum element in the third component.
    ", imax_all, af_imax_all);

/// Locate the indices of non-zero elements.
///
/// The locations are provided by flattening the input into a linear array.
///
/// # Parameters
///
/// - `input` - Input Array
///
/// # Return Values
///
/// Array of indices where the input Array has non-zero values.
#[allow(unused_mut)]
pub fn locate(input: &Array) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_where(&mut temp as MutAfArray, input.get() as AfArray);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Sort the values in input Arrays
///
/// Sort an multidimensional Array along a given dimension
///
/// # Parameters
///
/// - `input` - Input Array
/// - `dim` - Dimension along which to sort
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
///
/// # Return Values
///
/// Sorted Array.
#[allow(unused_mut)]
pub fn sort(input: &Array, dim: u32, ascending: bool) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_sort(&mut temp as MutAfArray, input.get() as AfArray,
                              dim as c_uint, ascending as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Sort the values in input Arrays
///
/// # Parameters
///
/// - `input` - Input Array
/// - `dim` - Dimension along which to sort
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
///
/// # Return Values
///
/// A tuple of Arrays.
///
/// The first Array contains the keys based on sorted values.
///
/// The second Array contains the original indices of the sorted values.
#[allow(unused_mut)]
pub fn sort_index(input: &Array, dim: u32, ascending: bool) -> (Array, Array) {
    unsafe {
        let mut temp: i64 = 0;
        let mut idx: i64 = 0;
        let err_val = af_sort_index(&mut temp as MutAfArray, &mut idx as MutAfArray,
                                    input.get() as AfArray,
                                    dim as c_uint, ascending as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        (Array::from(temp), Array::from(idx))
    }
}

/// Sort the values in input Arrays
///
/// Sort an multidimensional Array based on keys
///
/// # Parameters
///
/// - `keys` - Array with key values
/// - `vals` - Array with input values
/// - `dim` - Dimension along which to sort
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
///
/// # Return Values
///
/// A tuple of Arrays.
///
/// The first Array contains the keys based on sorted values.
///
/// The second Array contains the sorted values.
#[allow(unused_mut)]
pub fn sort_by_key(keys: &Array, vals: &Array, dim: u32,
                   ascending: bool) -> (Array, Array) {
    unsafe {
        let mut temp: i64 = 0;
        let mut temp2: i64 = 0;
        let err_val = af_sort_by_key(&mut temp as MutAfArray, &mut temp2 as MutAfArray,
                                     keys.get() as AfArray, vals.get() as AfArray,
                                     dim as c_uint, ascending as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        (Array::from(temp), Array::from(temp2))
    }
}

/// Find unique values from a Set
///
/// # Parameters
///
/// - `input` - Input Array
/// - `is_sorted` - is a boolean variable. If ```True`` indicates, the `input` Array is sorted.
///
/// # Return Values
///
/// An Array of unique values from the input Array.
#[allow(unused_mut)]
pub fn set_unique(input: &Array, is_sorted: bool) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_set_unique(&mut temp as MutAfArray, input.get() as AfArray,
                                    is_sorted as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Find union of two sets
///
/// # Parameters
///
/// - `first` is one of the input sets
/// - `second` is the other of the input sets
/// - `is_unique` is a boolean value indicates if the input sets are unique
///
/// # Return Values
///
/// An Array with union of the input sets
#[allow(unused_mut)]
pub fn set_union(first: &Array, second: &Array, is_unique: bool) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_set_union(&mut temp as MutAfArray, first.get() as AfArray,
                                   second.get() as AfArray, is_unique as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Find intersection of two sets
///
/// # Parameters
///
/// - `first` is one of the input sets
/// - `second` is the other of the input sets
/// - `is_unique` is a boolean value indicates if the input sets are unique
///
/// # Return Values
///
/// An Array with intersection of the input sets
#[allow(unused_mut)]
pub fn set_intersect(first: &Array, second: &Array, is_unique: bool) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_set_intersect(&mut temp as MutAfArray, first.get() as AfArray,
                                       second.get() as AfArray, is_unique as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Generalized scan
///
/// # Parameters
///
/// - `input` is the data on which scan is to be performed
/// - `dim` is the dimension along which scan operation is to be performed
/// - `op` takes value of [BinaryOp](./enum.BinaryOp.html) enum indicating
///    the type of scan operation
/// - `inclusive` says if inclusive/exclusive scan is to be performed
///
/// # Return Values
///
/// Output Array of scanned input
pub fn scan(input: &Array, dim: i32, op: BinaryOp, inclusive: bool) -> Array {
    unsafe {
        let mut temp : i64 = 0;
        let err_val = af_scan(&mut temp as MutAfArray, input.get() as AfArray, dim as c_int,
                              op as uint8_t, inclusive as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Generalized scan by key
///
/// # Parameters
///
/// - `key` is the key Array
/// - `input` is the data on which scan is to be performed
/// - `dim` is the dimension along which scan operation is to be performed
/// - `op` takes value of [BinaryOp](./enum.BinaryOp.html) enum indicating
///    the type of scan operation
/// - `inclusive` says if inclusive/exclusive scan is to be performed
///
/// # Return Values
///
/// Output Array of scanned input
pub fn scan_by_key(key: &Array, input: &Array, dim: i32, op: BinaryOp, inclusive: bool) -> Array {
    unsafe {
        let mut temp : i64 = 0;
        let err_val = af_scan_by_key(&mut temp as MutAfArray,
                                     key.get() as AfArray, input.get() as AfArray, dim as c_int,
                                     op as uint8_t, inclusive as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}