This file is indexed.

/usr/lib/python2.7/dist-packages/arrayfire/data.py is in python-arrayfire 3.3.20160624-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

"""
Functions to create and manipulate arrays.
"""

from sys import version_info
from .library import *
from .array import *
from .util import *
from .util import _is_number

def constant(val, d0, d1=None, d2=None, d3=None, dtype=Dtype.f32):
    """
    Create a multi dimensional array whose elements contain the same value.

    Parameters
    ----------
    val : scalar.
          Value of each element of the constant array.

    d0 : int.
         Length of first dimension.

    d1 : optional: int. default: None.
         Length of second dimension.

    d2 : optional: int. default: None.
         Length of third dimension.

    d3 : optional: int. default: None.
         Length of fourth dimension.

    dtype : optional: af.Dtype. default: af.Dtype.f32.
           Data type of the array.

    Returns
    -------

    out : af.Array
          Multi dimensional array whose elements are of value `val`.
          - If d1 is None, `out` is 1D of size (d0,).
          - If d1 is not None and d2 is None, `out` is 2D of size (d0, d1).
          - If d1 and d2 are not None and d3 is None, `out` is 3D of size (d0, d1, d2).
          - If d1, d2, d3 are all not None, `out` is 4D of size (d0, d1, d2, d3).
    """

    out = Array()
    out.arr = constant_array(val, d0, d1, d2, d3, dtype.value)
    return out

# Store builtin range function to be used later
_brange = range

def range(d0, d1=None, d2=None, d3=None, dim=0, dtype=Dtype.f32):
    """
    Create a multi dimensional array using length of a dimension as range.

    Parameters
    ----------
    val : scalar.
          Value of each element of the constant array.

    d0 : int.
         Length of first dimension.

    d1 : optional: int. default: None.
         Length of second dimension.

    d2 : optional: int. default: None.
         Length of third dimension.

    d3 : optional: int. default: None.
         Length of fourth dimension.

    dim : optional: int. default: 0.
         The dimension along which the range is calculated.

    dtype : optional: af.Dtype. default: af.Dtype.f32.
           Data type of the array.

    Returns
    -------

    out : af.Array
          Multi dimensional array whose elements are along `dim` fall between [0 - self.dims[dim]-1]
          - If d1 is None, `out` is 1D of size (d0,).
          - If d1 is not None and d2 is None, `out` is 2D of size (d0, d1).
          - If d1 and d2 are not None and d3 is None, `out` is 3D of size (d0, d1, d2).
          - If d1, d2, d3 are all not None, `out` is 4D of size (d0, d1, d2, d3).


    Examples
    --------
    >>> import arrayfire as af
    >>> a = af.range(3, 2) # dim is not specified, range is along first dimension.
    >>> af.display(a) # The data ranges from [0 - 2] (3 elements along first dimension)
    [3 2 1 1]
        0.0000     0.0000
        1.0000     1.0000
        2.0000     2.0000

    >>> a = af.range(3, 2, dim=1) # dim is 1, range is along second dimension.
    >>> af.display(a) # The data ranges from [0 - 1] (2 elements along second dimension)
    [3 2 1 1]
        0.0000     1.0000
        0.0000     1.0000
        0.0000     1.0000
    """
    out = Array()
    dims = dim4(d0, d1, d2, d3)

    safe_call(backend.get().af_range(ct.pointer(out.arr), 4, ct.pointer(dims), dim, dtype.value))
    return out


def iota(d0, d1=None, d2=None, d3=None, dim=-1, tile_dims=None, dtype=Dtype.f32):
    """
    Create a multi dimensional array using the number of elements in the array as the range.

    Parameters
    ----------
    val : scalar.
          Value of each element of the constant array.

    d0 : int.
         Length of first dimension.

    d1 : optional: int. default: None.
         Length of second dimension.

    d2 : optional: int. default: None.
         Length of third dimension.

    d3 : optional: int. default: None.
         Length of fourth dimension.

    tile_dims : optional: tuple of ints. default: None.
         The number of times the data is tiled.

    dtype : optional: af.Dtype. default: af.Dtype.f32.
           Data type of the array.

    Returns
    -------

    out : af.Array
          Multi dimensional array whose elements are along `dim` fall between [0 - self.elements() - 1].

    Examples
    --------
    >>> import arrayfire as af
    >>> import arrayfire as af
    >>> a = af.iota(3,3) # tile_dim is not specified, data is not tiled
    >>> af.display(a) # the elements range from [0 - 8] (9 elements)
    [3 3 1 1]
        0.0000     3.0000     6.0000
        1.0000     4.0000     7.0000
        2.0000     5.0000     8.0000

    >>> b = af.iota(3,3,tile_dims(1,2)) # Asking to tile along second dimension.
    >>> af.display(b)
    [3 6 1 1]
        0.0000     3.0000     6.0000     0.0000     3.0000     6.0000
        1.0000     4.0000     7.0000     1.0000     4.0000     7.0000
        2.0000     5.0000     8.0000     2.0000     5.0000     8.0000
    """
    out = Array()
    dims = dim4(d0, d1, d2, d3)
    td=[1]*4

    if tile_dims is not None:
        for i in _brange(len(tile_dims)):
            td[i] = tile_dims[i]

    tdims = dim4(td[0], td[1], td[2], td[3])

    safe_call(backend.get().af_iota(ct.pointer(out.arr), 4, ct.pointer(dims),
                                    4, ct.pointer(tdims), dtype.value))
    return out

def randu(d0, d1=None, d2=None, d3=None, dtype=Dtype.f32):
    """
    Create a multi dimensional array containing values from a uniform distribution.

    Parameters
    ----------
    d0 : int.
         Length of first dimension.

    d1 : optional: int. default: None.
         Length of second dimension.

    d2 : optional: int. default: None.
         Length of third dimension.

    d3 : optional: int. default: None.
         Length of fourth dimension.

    dtype : optional: af.Dtype. default: af.Dtype.f32.
           Data type of the array.

    Returns
    -------

    out : af.Array
          Multi dimensional array whose elements are sampled uniformly between [0, 1].
          - If d1 is None, `out` is 1D of size (d0,).
          - If d1 is not None and d2 is None, `out` is 2D of size (d0, d1).
          - If d1 and d2 are not None and d3 is None, `out` is 3D of size (d0, d1, d2).
          - If d1, d2, d3 are all not None, `out` is 4D of size (d0, d1, d2, d3).
    """
    out = Array()
    dims = dim4(d0, d1, d2, d3)

    safe_call(backend.get().af_randu(ct.pointer(out.arr), 4, ct.pointer(dims), dtype.value))
    return out

def randn(d0, d1=None, d2=None, d3=None, dtype=Dtype.f32):
    """
    Create a multi dimensional array containing values from a normal distribution.

    Parameters
    ----------
    d0 : int.
         Length of first dimension.

    d1 : optional: int. default: None.
         Length of second dimension.

    d2 : optional: int. default: None.
         Length of third dimension.

    d3 : optional: int. default: None.
         Length of fourth dimension.

    dtype : optional: af.Dtype. default: af.Dtype.f32.
           Data type of the array.

    Returns
    -------

    out : af.Array
          Multi dimensional array whose elements are sampled from a normal distribution with mean 0 and sigma of 1.
          - If d1 is None, `out` is 1D of size (d0,).
          - If d1 is not None and d2 is None, `out` is 2D of size (d0, d1).
          - If d1 and d2 are not None and d3 is None, `out` is 3D of size (d0, d1, d2).
          - If d1, d2, d3 are all not None, `out` is 4D of size (d0, d1, d2, d3).
    """

    out = Array()
    dims = dim4(d0, d1, d2, d3)

    safe_call(backend.get().af_randn(ct.pointer(out.arr), 4, ct.pointer(dims), dtype.value))
    return out

def set_seed(seed=0):
    """
    Set the seed for the random number generator.

    Parameters
    ----------
    seed: int.
          Seed for the random number generator
    """
    safe_call(backend.get().af_set_seed(ct.c_ulonglong(seed)))

def get_seed():
    """
    Get the seed for the random number generator.

    Returns
    ----------
    seed: int.
          Seed for the random number generator
    """
    seed = ct.c_ulonglong(0)
    safe_call(backend.get().af_get_seed(ct.pointer(seed)))
    return seed.value

def identity(d0, d1, d2=None, d3=None, dtype=Dtype.f32):
    """
    Create an identity matrix or batch of identity matrices.

    Parameters
    ----------
    d0 : int.
         Length of first dimension.

    d1 : int.
         Length of second dimension.

    d2 : optional: int. default: None.
         Length of third dimension.

    d3 : optional: int. default: None.
         Length of fourth dimension.

    dtype : optional: af.Dtype. default: af.Dtype.f32.
           Data type of the array.

    Returns
    -------

    out : af.Array
          Multi dimensional array whose first two dimensions form a identity matrix.
          - If d2 is  None, `out` is 2D of size (d0, d1).
          - If d2 is not None and d3 is None, `out` is 3D of size (d0, d1, d2).
          - If d2, d3 are not None, `out` is 4D of size (d0, d1, d2, d3).
    """

    out = Array()
    dims = dim4(d0, d1, d2, d3)

    safe_call(backend.get().af_identity(ct.pointer(out.arr), 4, ct.pointer(dims), dtype.value))
    return out

def diag(a, num=0, extract=True):
    """
    Create a diagonal matrix or Extract the diagonal from a matrix.

    Parameters
    ----------
    a : af.Array.
        1 dimensional or 2 dimensional arrayfire array.

    num : optional: int. default: 0.
        The index of the diagonal.
        - num == 0 signifies the diagonal.
        - num  > 0 signifies super diagonals.
        - num <  0 signifies sub diagonals.

    extract : optional: bool. default: True.
         - If True , diagonal is extracted. `a` has to be 2D.
         - If False, diagonal matrix is created. `a` has to be 1D.

    Returns
    -------

    out : af.Array
         - if extract is True, `out` contains the num'th diagonal from `a`.
         - if extract is False, `out` contains `a` as the num'th diagonal.
    """
    out = Array()
    if extract:
        safe_call(backend.get().af_diag_extract(ct.pointer(out.arr), a.arr, ct.c_int(num)))
    else:
        safe_call(backend.get().af_diag_create(ct.pointer(out.arr), a.arr, ct.c_int(num)))
    return out

def join(dim, first, second, third=None, fourth=None):
    """
    Join two or more arrayfire arrays along a specified dimension.

    Parameters
    ----------

    dim: int.
        Dimension along which the join occurs.

    first : af.Array.
        Multi dimensional arrayfire array.

    second : af.Array.
        Multi dimensional arrayfire array.

    third : optional: af.Array. default: None.
        Multi dimensional arrayfire array.

    fourth : optional: af.Array. default: None.
        Multi dimensional arrayfire array.

    Returns
    -------

    out : af.Array
          An array containing the input arrays joined along the specified dimension.

    Examples
    ---------

    >>> import arrayfire as af
    >>> a = af.randu(2, 3)
    >>> b = af.randu(2, 3)
    >>> c = af.join(0, a, b)
    >>> d = af.join(1, a, b)
    >>> af.display(a)
    [2 3 1 1]
        0.9508     0.2591     0.7928
        0.5367     0.8359     0.8719

    >>> af.display(b)
    [2 3 1 1]
        0.3266     0.6009     0.2442
        0.6275     0.0495     0.6591

    >>> af.display(c)
    [4 3 1 1]
        0.9508     0.2591     0.7928
        0.5367     0.8359     0.8719
        0.3266     0.6009     0.2442
        0.6275     0.0495     0.6591

    >>> af.display(d)
    [2 6 1 1]
        0.9508     0.2591     0.7928     0.3266     0.6009     0.2442
        0.5367     0.8359     0.8719     0.6275     0.0495     0.6591
    """
    out = Array()
    if (third is None and fourth is None):
        safe_call(backend.get().af_join(ct.pointer(out.arr), dim, first.arr, second.arr))
    else:
        c_void_p_4 = ct.c_void_p * 4
        c_array_vec = c_void_p_4(first.arr, second.arr, 0, 0)
        num = 2
        if third is not None:
            c_array_vec[num] = third.arr
            num+=1
        if fourth is not None:
            c_array_vec[num] = fourth.arr
            num+=1

        safe_call(backend.get().af_join_many(ct.pointer(out.arr), dim, num, ct.pointer(c_array_vec)))
    return out


def tile(a, d0, d1=1, d2=1, d3=1):
    """
    Tile an array along specified dimensions.

    Parameters
    ----------

    a : af.Array.
       Multi dimensional array.

    d0: int.
        The number of times `a` has to be tiled along first dimension.

    d1: optional: int. default: 1.
        The number of times `a` has to be tiled along second dimension.

    d2: optional: int. default: 1.
        The number of times `a` has to be tiled along third dimension.

    d3: optional: int. default: 1.
        The number of times `a` has to be tiled along fourth dimension.

    Returns
    -------

    out : af.Array
          An array containing the input after tiling the the specified number of times.

    Examples
    ---------

    >>> import arrayfire as af
    >>> a = af.randu(2, 3)
    >>> b = af.tile(a, 2)
    >>> c = af.tile(a, 1, 2)
    >>> d = af.tile(a, 2, 2)
    >>> af.display(a)
    [2 3 1 1]
        0.9508     0.2591     0.7928
        0.5367     0.8359     0.8719

    >>> af.display(b)
    [4 3 1 1]
        0.4107     0.9518     0.4198
        0.8224     0.1794     0.0081
        0.4107     0.9518     0.4198
        0.8224     0.1794     0.0081

    >>> af.display(c)
    [2 6 1 1]
        0.4107     0.9518     0.4198     0.4107     0.9518     0.4198
        0.8224     0.1794     0.0081     0.8224     0.1794     0.0081

    >>> af.display(d)
    [4 6 1 1]
        0.4107     0.9518     0.4198     0.4107     0.9518     0.4198
        0.8224     0.1794     0.0081     0.8224     0.1794     0.0081
        0.4107     0.9518     0.4198     0.4107     0.9518     0.4198
        0.8224     0.1794     0.0081     0.8224     0.1794     0.0081
    """
    out = Array()
    safe_call(backend.get().af_tile(ct.pointer(out.arr), a.arr, d0, d1, d2, d3))
    return out


def reorder(a, d0=1, d1=0, d2=2, d3=3):
    """
    Reorder the dimensions of the input.

    Parameters
    ----------

    a : af.Array.
       Multi dimensional array.

    d0: optional: int. default: 1.
        The location of the first dimension in the output.

    d1: optional: int. default: 0.
        The location of the second dimension in the output.

    d2: optional: int. default: 2.
        The location of the third dimension in the output.

    d3: optional: int. default: 3.
        The location of the fourth dimension in the output.

    Returns
    -------

    out : af.Array
          - An array containing the input aftern reordering its dimensions.

    Note
    ------
    - `af.reorder(a, 1, 0)` is the same as `transpose(a)`

    Examples
    --------
    >>> import arrayfire as af
    >>> a = af.randu(5, 5, 3)
    >>> af.display(a)
    [5 5 3 1]
        0.4107     0.0081     0.6600     0.1046     0.8395
        0.8224     0.3775     0.0764     0.8827     0.1933
        0.9518     0.3027     0.0901     0.1647     0.7270
        0.1794     0.6456     0.5933     0.8060     0.0322
        0.4198     0.5591     0.1098     0.5938     0.0012

        0.8703     0.9250     0.4387     0.6530     0.4224
        0.5259     0.3063     0.3784     0.5476     0.5293
        0.1443     0.9313     0.4002     0.8577     0.0212
        0.3253     0.8684     0.4390     0.8370     0.1103
        0.5081     0.6592     0.4718     0.0618     0.4420

        0.8355     0.6767     0.1033     0.9426     0.9276
        0.4878     0.6742     0.2119     0.4817     0.8662
        0.2055     0.4523     0.5955     0.9097     0.3578
        0.1794     0.1236     0.3745     0.6821     0.6263
        0.5606     0.7924     0.9165     0.6056     0.9747


    >>> b = af.reorder(a, 2, 0, 1)
    >>> af.display(b)
    [3 5 5 1]
        0.4107     0.8224     0.9518     0.1794     0.4198
        0.8703     0.5259     0.1443     0.3253     0.5081
        0.8355     0.4878     0.2055     0.1794     0.5606

        0.0081     0.3775     0.3027     0.6456     0.5591
        0.9250     0.3063     0.9313     0.8684     0.6592
        0.6767     0.6742     0.4523     0.1236     0.7924

        0.6600     0.0764     0.0901     0.5933     0.1098
        0.4387     0.3784     0.4002     0.4390     0.4718
        0.1033     0.2119     0.5955     0.3745     0.9165

        0.1046     0.8827     0.1647     0.8060     0.5938
        0.6530     0.5476     0.8577     0.8370     0.0618
        0.9426     0.4817     0.9097     0.6821     0.6056

        0.8395     0.1933     0.7270     0.0322     0.0012
        0.4224     0.5293     0.0212     0.1103     0.4420
        0.9276     0.8662     0.3578     0.6263     0.9747
    """
    out = Array()
    safe_call(backend.get().af_reorder(ct.pointer(out.arr), a.arr, d0, d1, d2, d3))
    return out

def shift(a, d0, d1=0, d2=0, d3=0):
    """
    Shift the input along each dimension.

    Parameters
    ----------

    a : af.Array.
       Multi dimensional array.

    d0: int.
        The amount of shift along first dimension.

    d1: optional: int. default: 0.
        The amount of shift along second dimension.

    d2: optional: int. default: 0.
        The amount of shift along third dimension.

    d3: optional: int. default: 0.
        The amount of shift along fourth dimension.

    Returns
    -------

    out : af.Array
          - An array the same shape as `a` after shifting it by the specified amounts.

    Examples
    --------
    >>> import arrayfire as af
    >>> a = af.randu(3, 3)
    >>> b = af.shift(a, 2)
    >>> c = af.shift(a, 1, -1)
    >>> af.display(a)
    [3 3 1 1]
        0.7269     0.3569     0.3341
        0.7104     0.1437     0.0899
        0.5201     0.4563     0.5363

    >>> af.display(b)
    [3 3 1 1]
        0.7104     0.1437     0.0899
        0.5201     0.4563     0.5363
        0.7269     0.3569     0.3341

    >>> af.display(c)
    [3 3 1 1]
        0.4563     0.5363     0.5201
        0.3569     0.3341     0.7269
        0.1437     0.0899     0.7104
    """
    out = Array()
    safe_call(backend.get().af_shift(ct.pointer(out.arr), a.arr, d0, d1, d2, d3))
    return out

def moddims(a, d0, d1=1, d2=1, d3=1):
    """
    Modify the shape of the array without changing the data layout.

    Parameters
    ----------

    a : af.Array.
       Multi dimensional array.

    d0: int.
        The first dimension of output.

    d1: optional: int. default: 1.
        The second dimension of output.

    d2: optional: int. default: 1.
        The third dimension of output.

    d3: optional: int. default: 1.
        The fourth dimension of output.

    Returns
    -------

    out : af.Array
          - An containing the same data as `a` with the specified shape.
          - The number of elements in `a` must match `d0 x d1 x d2 x d3`.
    """
    out = Array()
    dims = dim4(d0, d1, d2, d3)
    safe_call(backend.get().af_moddims(ct.pointer(out.arr), a.arr, 4, ct.pointer(dims)))
    return out

def flat(a):
    """
    Flatten the input array.

    Parameters
    ----------

    a : af.Array.
       Multi dimensional array.

    Returns
    -------

    out : af.Array
          - 1 dimensional array containing all the elements from `a`.
    """
    out = Array()
    safe_call(backend.get().af_flat(ct.pointer(out.arr), a.arr))
    return out

def flip(a, dim=0):
    """
    Flip an array along a dimension.

    Parameters
    ----------

    a : af.Array.
       Multi dimensional array.

    dim : optional: int. default: 0.
       The dimension along which the flip is performed.

    Returns
    -------

    out : af.Array
          The output after flipping `a` along `dim`.

    Examples
    ---------

    >>> import arrayfire as af
    >>> a = af.randu(3, 3)
    >>> af.display(a)
    [3 3 1 1]
        0.7269     0.3569     0.3341
        0.7104     0.1437     0.0899
        0.5201     0.4563     0.5363

    >>> af.display(b)
    [3 3 1 1]
        0.5201     0.4563     0.5363
        0.7104     0.1437     0.0899
        0.7269     0.3569     0.3341

    >>> af.display(c)
    [3 3 1 1]
        0.3341     0.3569     0.7269
        0.0899     0.1437     0.7104
        0.5363     0.4563     0.5201

    """
    out = Array()
    safe_call(backend.get().af_flip(ct.pointer(out.arr), a.arr, ct.c_int(dim)))
    return out

def lower(a, is_unit_diag=False):
    """
    Extract the lower triangular matrix from the input.

    Parameters
    ----------

    a : af.Array.
       Multi dimensional array.

    is_unit_diag: optional: bool. default: False.
       Flag specifying if the diagonal elements are 1.

    Returns
    -------

    out : af.Array
          An array containing the lower triangular elements from `a`.
    """
    out = Array()
    safe_call(backend.get().af_lower(ct.pointer(out.arr), a.arr, is_unit_diag))
    return out

def upper(a, is_unit_diag=False):
    """
    Extract the upper triangular matrix from the input.

    Parameters
    ----------

    a : af.Array.
       Multi dimensional array.

    is_unit_diag: optional: bool. default: False.
       Flag specifying if the diagonal elements are 1.

    Returns
    -------

    out : af.Array
          An array containing the upper triangular elements from `a`.
    """
    out = Array()
    safe_call(backend.get().af_upper(ct.pointer(out.arr), a.arr, is_unit_diag))
    return out

def select(cond, lhs, rhs):
    """
    Select elements from one of two arrays based on condition.

    Parameters
    ----------

    cond : af.Array
           Conditional array

    lhs  : af.Array or scalar
           numerical array whose elements are picked when conditional element is True

    rhs  : af.Array or scalar
           numerical array whose elements are picked when conditional element is False

    Returns
    --------

    out: af.Array
         An array containing elements from `lhs` when `cond` is True and `rhs` when False.

    Examples
    ---------

    >>> import arrayfire as af
    >>> a = af.randu(3,3)
    >>> b = af.randu(3,3)
    >>> cond = a > b
    >>> res = af.select(cond, a, b)

    >>> af.display(a)
    [3 3 1 1]
        0.4107     0.1794     0.3775
        0.8224     0.4198     0.3027
        0.9518     0.0081     0.6456

    >>> af.display(b)
    [3 3 1 1]
        0.7269     0.3569     0.3341
        0.7104     0.1437     0.0899
        0.5201     0.4563     0.5363

    >>> af.display(res)
    [3 3 1 1]
        0.7269     0.3569     0.3775
        0.8224     0.4198     0.3027
        0.9518     0.4563     0.6456
    """
    out = Array()

    is_left_array = isinstance(lhs, Array)
    is_right_array = isinstance(rhs, Array)

    if not (is_left_array or is_right_array):
        raise TypeError("Atleast one input needs to be of type arrayfire.array")

    elif (is_left_array and is_right_array):
        safe_call(backend.get().af_select(ct.pointer(out.arr), cond.arr, lhs.arr, rhs.arr))

    elif (_is_number(rhs)):
        safe_call(backend.get().af_select_scalar_r(ct.pointer(out.arr), cond.arr, lhs.arr, ct.c_double(rhs)))
    else:
        safe_call(backend.get().af_select_scalar_l(ct.pointer(out.arr), cond.arr, ct.c_double(lhs), rhs.arr))

    return out

def replace(lhs, cond, rhs):
    """
    Select elements from one of two arrays based on condition.

    Parameters
    ----------

    lhs  : af.Array or scalar
           numerical array whose elements are replaced with `rhs` when conditional element is False

    cond : af.Array
           Conditional array

    rhs  : af.Array or scalar
           numerical array whose elements are picked when conditional element is False

    Examples
    ---------
    >>> import arrayfire as af
    >>> a = af.randu(3,3)
    >>> af.display(a)
    [3 3 1 1]
        0.4107     0.1794     0.3775
        0.8224     0.4198     0.3027
        0.9518     0.0081     0.6456

    >>> cond = (a >= 0.25) & (a <= 0.75)
    >>> af.display(cond)
    [3 3 1 1]
             1          0          1
             0          1          1
             0          0          1

    >>> af.replace(a, cond, 0.3333)
    >>> af.display(a)
    [3 3 1 1]
        0.3333     0.1794     0.3333
        0.8224     0.3333     0.3333
        0.9518     0.0081     0.3333

    """
    is_right_array = isinstance(rhs, Array)

    if (is_right_array):
        safe_call(backend.get().af_replace(lhs.arr, cond.arr, rhs.arr))
    else:
        safe_call(backend.get().af_replace_scalar(lhs.arr, cond.arr, ct.c_double(rhs)))