目次
序文
「ArrayFireで遊ぶ:06ベクトル化の概要」では、ArrayFireを使用してコードをベクトル化するいくつかの方法について学習しました。この記事では、ArrayFireの配列および行列演算について引き続き学習します。
1.配列および行列演算関数
ArrayFireは、配列と行列を操作するためのいくつかの異なる方法を提供します。これらのメソッドまたは関数には、次のものが含まれます。
関数名 | 意味 |
---|---|
moddims() | データを変更せずに配列の次元を変更する |
アレイ() | 異なる次元の配列の(浅い)コピーを作成する |
平らな() | 配列を1次元に平坦化します |
flip() | 次元に沿って配列を反転します |
join() | 最大4つのアレイを接続します |
reorder() | 配列の次元の順序を変更します |
シフト() | ディメンションに沿ってデータを移動する |
タイル() | 次元に沿って配列を繰り返す |
transpose() | 行列の転置 |
T() | 行列またはベクトルの転置(省略形) |
H() | エルミート行列の転置(共役転置) |
以下に、これらの関数とその使用法のいくつかの例を示します。
1. flat()
この関数の機能は、配列を1次元に縮小することです。
a [3 3 1 1]
1.0000 4.0000 7.0000
2.0000 5.0000 8.0000
3.0000 6.0000 9.0000
flat(a) [9 1 1 1]
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000
flat()関数は、C ++で次のように呼び出すことができます。
array af::flat(const array& in) – C++ interface for flat() function
2. flip()
この関数の機能は、選択した次元に沿って配列の内容を反転することです。次の例では、5x2配列が0番目と1番目の軸に沿って反転していることを示しています。
a [5 2 1 1]
1.0000 6.0000
2.0000 7.0000
3.0000 8.0000
4.0000 9.0000
5.0000 10.0000
flip(a, 0) [5 2 1 1]
5.0000 10.0000
4.0000 9.0000
3.0000 8.0000
2.0000 7.0000
1.0000 6.0000
flip(a, 1) [5 2 1 1]
6.0000 1.0000
7.0000 2.0000
8.0000 3.0000
9.0000 4.0000
10.0000 5.0000
flip()関数は、C ++で次のように呼び出すことができます。
array af::flip(const array &in, const unsigned dim) – C++ interface for flip()
3. join()
この関数の機能は、特定の次元に沿って配列を接続することです。C ++は最大4つのアレイを接続できますが、Cは最大10のアレイをサポートします。配列をそれ自体に接続する方法の例を次に示します。
a [5 1 1 1]
1.0000
2.0000
3.0000
4.0000
5.0000
join(0, a, a) [10 1 1 1]
1.0000
2.0000
3.0000
4.0000
5.0000
1.0000
2.0000
3.0000
4.0000
5.0000
join(1, a, a) [5 2 1 1]
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
4.0000 4.0000
5.0000 5.0000
join()関数には、C ++言語のいくつかの候補関数があります。
array af::join(const int dim, const array &first, const array &second) – Joins 2 arrays along a dimension
array af::join(const int dim, const array &first, const array &second, const array &third) – Joins 3 arrays along a dimension.
array af::join(const int dim, const array &first, const array &second, const array &third, const array &fourth) – Joins 4 arrays along a dimension
4. moddims()
この関数の機能は、配列の次元を変更することですが、配列のデータや順序は変更しません。この関数は、配列に関連付けられたメタデータのみを変更することに注意してください。配列の内容は変更されません。これは、8x1配列を2x4に変換してから、8x1に戻す例です。
a [8 1 1 1]
1.0000
2.0000
1.0000
2.0000
1.0000
2.0000
1.0000
2.0000
af::dim4 new_dims(2, 4);
moddims(a, new_dims) [2 4 1 1]
1.0000 1.0000 1.0000 1.0000
2.0000 2.0000 2.0000 2.0000
moddims(a, a.elements(), 1, 1, 1) [8 1 1 1]
1.0000
2.0000
1.0000
2.0000
1.0000
2.0000
1.0000
2.0000
moddims()関数には、C ++ APIにいくつかの候補関数があります。
array af::moddims(const array &in, const unsigned ndims, const dim_t *const dims) – mods number of dimensions to match ndims as specidied in the array dims
array af::moddims(const array &in, const dim4 &dims) – mods dimensions as specified by dims
array af::moddims(const array &in, const dim_t d0, const dim_t d1=1, const dim_t d2=1, const dim_t d3=1) – mods dimensions of an array
5. reorder()
この関数の機能は、次元の変化に応じてデータを交換し、それによって配列内のデータの順序を変更することです。配列内のデータは線形の順序を維持します。
a [2 2 3 1]
1.0000 3.0000
2.0000 4.0000
1.0000 3.0000
2.0000 4.0000
1.0000 3.0000
2.0000 4.0000
reorder(a, 1, 0, 2) [2 2 3 1] //equivalent to a transpose
1.0000 2.0000
3.0000 4.0000
1.0000 2.0000
3.0000 4.0000
1.0000 2.0000
3.0000 4.0000
reorder(a, 2, 0, 1) [3 2 2 1]
1.0000 2.0000
1.0000 2.0000
1.0000 2.0000
3.0000 4.0000
3.0000 4.0000
3.0000 4.0000
reorder()関数には、C ++ APIにいくつかの候補関数があります。
array af::reorder(const array &in, const unsigned x, const unsigned y=1, const unsigned z=2, const unsigned w=3) – Reorders dimensions of an array
6. shift()
この関数の機能は、選択したディメンションに沿って循環バッファ内のデータを移動することです。次の例を考えてみましょう。
a [3 5 1 1]
0.0000 0.0000 0.0000 0.0000 0.0000
3.0000 4.0000 5.0000 1.0000 2.0000
3.0000 4.0000 5.0000 1.0000 2.0000
shift(a, 0, 2 ) [3 5 1 1]
0.0000 0.0000 0.0000 0.0000 0.0000
1.0000 2.0000 3.0000 4.0000 5.0000
1.0000 2.0000 3.0000 4.0000 5.0000
shift(a, -1, 2 ) [3 5 1 1]
1.0000 2.0000 3.0000 4.0000 5.0000
1.0000 2.0000 3.0000 4.0000 5.0000
0.0000 0.0000 0.0000 0.0000 0.0000
shift()関数は、C ++で次のように呼び出すことができます。
array af::shift(const array &in, const int x, const int y=0, const int z=0, const int w=0) – Shifts array along specified dimensions
7. tile()
この関数の機能は、指定された次元に沿って配列を繰り返すことです。次の例は、配列の0次元と1次元を繰り返す方法を示しています。
a [3 1 1 1]
1.0000
2.0000
3.0000
// Repeat array a twice in the zeroth dimension
tile(a, 2) [6 1 1 1]
1.0000
2.0000
3.0000
1.0000
2.0000
3.0000
// Repeat array a twice along both the zeroth and first dimensions
tile(a, 2, 2) [6 2 1 1]
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
// Repeat array a twice along the first and three times along the second
// dimension.
af::dim4 tile_dims(1, 2, 3);
tile(a, tile_dims) [3 2 3 1]
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
tile()関数は、C ++で次のように呼び出すことができます。
array af::tile(const array &in, const unsigned x, const unsigned y=1, const unsigned z=1, const unsigned w=1) – Tiles array along specified dimensions
array af::tile(const array &in, const dim4 &dims) – Tile an array according to a dim4 object
8.転置()
この関数の機能は、標準の行列転置を実行することです。入力配列は、2D行列の次元である必要があります。
a [3 3 1 1]
1.0000 3.0000 3.0000
2.0000 1.0000 3.0000
2.0000 2.0000 1.0000
transpose(a) [3 3 1 1]
1.0000 2.0000 2.0000
3.0000 1.0000 2.0000
3.0000 3.0000 1.0000
transpose()関数は、C ++で次のように呼び出すことができます。
array af::transpose(const array &in, const bool conjugate=false) – Transposes a matrix.
void af::transposeInPlace(array &in, const bool conjugate=false) – Transposes a matrix in-place.
__array af::T() – Transpose a matrix
__array af::H() – Conjugate Transpose (Hermitian transpose) of a matrix
短縮バージョンの使用方法の例を次に示します。
array x = randu(2, 2, f32);
af_print(x.T()); // transpose (real)
array c = randu(2, 2, c32);
af_print(c.T()); // transpose (complex)
af_print(c.H()); // Hermitian (conjugate) transpose
9. array()
この関数を使用して、さまざまなサイズの行列の(浅い)コピーを作成できます。要素の総数は同じでなければなりません。この関数は、前述のmoddim()関数のラッパーです。
2.並べ替え関数を組み合わせてグリッド座標を列挙します
アレイ再編成機能を組み合わせて使用することにより、複雑な動作モードを数行のコードですばやく記述できます。たとえば、グリッドの(x、y)座標を生成することを検討してください。ここで、各軸は1からnになります。配列を埋めるためにいくつかのループを使用する代わりに、上記の関数の小さな組み合わせを使用できます。
unsigned n=3;
af::array xy = join(1,
tile(seq(1, n), n),
flat( transpose(tile(seq(1, n), 1, n)) )
);
xy [9 2 1 1]
1.0000 1.0000
2.0000 1.0000
3.0000 1.0000
1.0000 2.0000
2.0000 2.0000
3.0000 2.0000
1.0000 3.0000
2.0000 3.0000
3.0000 3.0000