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
|
/*******************************************************
* Copyright (c) 2014, 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
********************************************************/
#include <gtest/gtest.h>
#include <arrayfire.h>
#include <vector>
using namespace af;
using std::vector;
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_tile)
{
//! [ex_matrix_manipulation_tile]
float h[] = {1, 2, 3, 4};
array small_arr = array(2, 2, h); // 2x2 matrix
af_print(small_arr);
array large_arr = tile(small_arr, 2, 3); // produces 4x6 matrix: (2*2)x(2*3)
af_print(large_arr);
//! [ex_matrix_manipulation_tile]
ASSERT_EQ(4, large_arr.dims(0));
ASSERT_EQ(6, large_arr.dims(1));
vector<float> h_large_arr(large_arr.elements());
large_arr.host(&h_large_arr.front());
unsigned fdim = large_arr.dims(0);
unsigned sdim = large_arr.dims(1);
for(unsigned i = 0; i < sdim; i++) {
for(unsigned j = 0; j < fdim; j++) {
ASSERT_FLOAT_EQ(h[(i%2) * 2 + (j%2)], h_large_arr[i * fdim + j] );
}
}
}
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_join)
{
//! [ex_matrix_manipulation_join]
float hA[] = { 1, 2, 3, 4, 5, 6 };
float hB[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
array A = array(3, 2, hA);
array B = array(3, 3, hB);
af_print(join(1, A, B)); // 3x5 matrix
// array result = join(0, A, B); // fail: dimension mismatch
//! [ex_matrix_manipulation_join]
array out = join(1, A, B);
vector<float> h_out(out.elements());
out.host(&h_out.front());
af_print(out);
ASSERT_EQ(3, out.dims(0));
ASSERT_EQ(5, out.dims(1));
unsigned fdim = out.dims(0);
unsigned sdim = out.dims(1);
for(unsigned i = 0; i < sdim; i++) {
for(unsigned j = 0; j < fdim; j++) {
if( i < 2 ) {
ASSERT_FLOAT_EQ(hA[i * fdim + j], h_out[i * fdim + j]) << "At [" << i << ", " << j << "]";
}
else {
ASSERT_FLOAT_EQ(hB[(i - 2) * fdim + j], h_out[i * fdim + j]) << "At [" << i << ", " << j << "]";
}
}
}
}
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_mesh)
{
//! [ex_matrix_manipulation_mesh]
float hx[] = {1, 2, 3, 4};
float hy[] = {5, 6};
array x = array(4, hx);
array y = array(2, hy);
af_print(tile(x, 1, 2));
af_print(tile(y.T(), 4, 1));
//! [ex_matrix_manipulation_mesh]
array outx = tile(x, 1, 2);
array outy = tile(y.T(), 4, 1);
ASSERT_EQ(4, outx.dims(0));
ASSERT_EQ(4, outy.dims(0));
ASSERT_EQ(2, outx.dims(1));
ASSERT_EQ(2, outy.dims(1));
vector<float> houtx(outx.elements());
outx.host(&houtx.front());
vector<float> houty(outy.elements());
outy.host(&houty.front());
for(unsigned i = 0; i < houtx.size(); i++) ASSERT_EQ(hx[i%4], houtx[i]) << "At [" << i << "]";
for(unsigned i = 0; i < houty.size(); i++) ASSERT_EQ(hy[i>3], houty[i]) << "At [" << i << "]";
}
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_moddims)
{
//! [ex_matrix_manipulation_moddims]
int hA[] = {1, 2, 3, 4, 5, 6};
array A = array(3, 2, hA);
af_print(A); // 2x3 matrix
af_print(moddims(A, 2, 3)); // 2x3 matrix
af_print(moddims(A, 6, 1)); // 6x1 column vector
// moddims(A, 2, 2); // fail: wrong number of elements
// moddims(A, 8, 8); // fail: wrong number of elements
//! [ex_matrix_manipulation_moddims]
}
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_transpose)
{
//! [ex_matrix_manipulation_transpose]
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
//! [ex_matrix_manipulation_transpose]
}
|