목차
머리말
"Playing with ArrayFire : 06 Vectorization Introduction"에서는 ArrayFire를 사용하여 코드를 벡터화하는 몇 가지 방법에 대해 배웠으며이 기사에서는 ArrayFire의 어레이 및 매트릭스 작업을 계속 학습합니다.
1. 어레이 및 매트릭스 작동 기능
ArrayFire는 배열과 행렬을 조작하는 여러 가지 방법을 제공합니다. 이러한 방법 또는 기능에는 다음이 포함됩니다.
기능 명 | 의미 |
---|---|
moddims () | 데이터를 변경하지 않고 배열의 차원 변경 |
정렬() | 서로 다른 차원의 배열 복사본 생성 (얕은) |
플랫() | 배열을 1 차원으로 평평하게 |
튀기다() | 차원을 따라 배열 뒤집기 |
어울리다() | 최대 4 개의 어레이 연결 |
재 주문() | 배열의 차원 순서 변경 |
시프트() | 차원을 따라 데이터 이동 |
타일() | 차원을 따라 배열 반복 |
바꾸어 놓다() | 행렬 전치 |
티() | 행렬 또는 벡터 전치 (속기) |
H () | 에르 미트 행렬의 전치 (공액 전치) |
아래에서는 이러한 기능과 그 사용법에 대한 몇 가지 예를 제공합니다.
1. 플랫 ()
이 함수의 기능은 배열을 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. 플립 ()
이 함수의 기능은 선택한 차원을 따라 배열의 내용을 뒤집는 것입니다. 다음 예에서는 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. 조인 ()
이 함수의 기능은 특정 차원을 따라 배열을 연결하는 것입니다. 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. 재정렬 ()
이 함수의 기능은 차원의 변화에 따라 데이터를 교환하여 배열의 데이터 순서를 수정하는 것입니다. 배열의 데이터는 선형 순서를 유지합니다.
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. 시프트 ()
이 기능의 기능은 선택한 차원을 따라 원형 버퍼의 데이터를 이동하는 것입니다. 다음 예를 고려하십시오.
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. 타일 ()
이 함수의 기능은 지정된 차원을 따라 배열을 반복하는 것입니다. 다음 예제는 배열의 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. 배열 ()
이 함수는 크기가 다른 행렬의 (얕은) 복사본을 만드는 데 사용할 수 있습니다. 총 요소 수는 동일해야합니다. 이 함수는 앞에서 논의한 moddim () 함수의 래퍼입니다.
2. 재정렬 함수를 결합하여 그리드 좌표 열거
배열 재구성 기능의 조합을 사용하면 몇 줄의 코드로 복잡한 작동 모드를 빠르게 작성할 수 있습니다. 예를 들어 각 축이 1에서 n으로 이동하는 그리드에 대한 (x, y) 좌표를 생성하는 것을 고려하십시오. 배열을 채우기 위해 여러 개의 루프를 사용하는 대신 위 함수의 작은 조합을 사용할 수 있습니다.
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