|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +####################################################### |
| 4 | +# Copyright (c) 2024, ArrayFire |
| 5 | +# All rights reserved. |
| 6 | +# |
| 7 | +# This file is distributed under 3-clause BSD license. |
| 8 | +# The complete license agreement can be obtained at: |
| 9 | +# http://arrayfire.com/licenses/BSD-3-Clause |
| 10 | +######################################################## |
| 11 | + |
| 12 | +import sys |
| 13 | +from array import array |
| 14 | + |
| 15 | +import arrayfire as af |
| 16 | +from arrayfire.array_object import _array_as_str |
| 17 | + |
| 18 | +if __name__ == "__main__": |
| 19 | + if len(sys.argv) > 1: |
| 20 | + af.set_device(int(sys.argv[1])) |
| 21 | + af.info() |
| 22 | + |
| 23 | + print("\n---- Intro to ArrayFire using unsigned(s32) arrays ----\n") |
| 24 | + |
| 25 | + h_A = array("i", (1, 2, 4, -1, 2, 0, 4, 2, 3)) |
| 26 | + h_B = array("i", (2, 3, 5, 6, 0, 10, -12, 0, 1)) |
| 27 | + |
| 28 | + A = af.Array(obj=h_A, shape=(3, 3), dtype=af.int32) |
| 29 | + B = af.Array(obj=h_B, shape=(3, 3), dtype=af.int32) |
| 30 | + |
| 31 | + print("\n---- Sub referencing and sub assignment\n") |
| 32 | + print(_array_as_str(A)) |
| 33 | + print(_array_as_str(A[0, :])) |
| 34 | + print(_array_as_str(A[:, 0])) |
| 35 | + A[0, 0] = 11 |
| 36 | + A[1] = 100 |
| 37 | + print(_array_as_str(A)) |
| 38 | + print(_array_as_str(B)) |
| 39 | + A[1, :] = B[2, :] |
| 40 | + print(_array_as_str(A)) |
| 41 | + |
| 42 | + b_A = array("I", (1, 1, 1, 0, 1, 1, 0, 0, 0)) |
| 43 | + b_B = array("I", (1, 0, 1, 0, 1, 0, 1, 0, 1)) |
| 44 | + |
| 45 | + C = af.Array(obj=b_A, shape=(3, 3), dtype=af.uint32) |
| 46 | + D = af.Array(obj=b_B, shape=(3, 3), dtype=af.uint32) |
| 47 | + print(_array_as_str(C)) |
| 48 | + print(_array_as_str(D)) |
| 49 | + |
| 50 | + print("\n---- Bitwise operations\n") |
| 51 | + print(_array_as_str(af.bitand(C, D))) |
| 52 | + print(_array_as_str(af.bitor(C, D))) |
| 53 | + print(_array_as_str(af.bitxor(C, D))) |
| 54 | + |
| 55 | + print("\n---- Transpose\n") |
| 56 | + print(_array_as_str(A)) |
| 57 | + print(_array_as_str(af.transpose(A))) |
| 58 | + |
| 59 | + print("\n---- Flip Vertically / Horizontally\n") |
| 60 | + print(_array_as_str(A)) |
| 61 | + print(_array_as_str(af.flip(A, axis=0))) |
| 62 | + print(_array_as_str(af.flip(A, axis=1))) |
| 63 | + |
| 64 | + print("\n---- Sum, Min, Max along row / columns\n") |
| 65 | + print(_array_as_str(A)) |
| 66 | + print(_array_as_str(af.min(A, axis=0))) # type: ignore[arg-type] |
| 67 | + print(_array_as_str(af.max(A, axis=0))) # type: ignore[arg-type] |
| 68 | + |
| 69 | + print(_array_as_str(af.min(A, axis=1))) # type: ignore[arg-type] |
| 70 | + print(_array_as_str(af.max(A, axis=1))) # type: ignore[arg-type] |
| 71 | + |
| 72 | + print(_array_as_str(af.sum(A, axis=0))) # type: ignore[arg-type] |
| 73 | + print(_array_as_str(af.sum(A, axis=1))) # type: ignore[arg-type] |
| 74 | + |
| 75 | + print("\n---- Get minimum with index\n") |
| 76 | + (min_val, min_idx) = af.imin(A, axis=0) |
| 77 | + print(_array_as_str(min_val)) # type: ignore[arg-type] |
| 78 | + print(_array_as_str(min_idx)) # type: ignore[arg-type] |
0 commit comments