mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Clang-format tests, examples, libraries, benchmarks, etc.
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
3252ecc7a4
commit
46e9cdb7fe
@@ -12,21 +12,20 @@
|
||||
|
||||
#include <Eigen/CXX11/Tensor>
|
||||
|
||||
using Eigen::Tensor;
|
||||
using Eigen::array;
|
||||
using Eigen::Pair;
|
||||
using Eigen::Tensor;
|
||||
|
||||
template <int DataLayout>
|
||||
static void test_simple_index_pairs()
|
||||
{
|
||||
Tensor<float, 4, DataLayout> tensor(2,3,5,7);
|
||||
static void test_simple_index_pairs() {
|
||||
Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
|
||||
tensor.setRandom();
|
||||
tensor = (tensor + tensor.constant(0.5)).log();
|
||||
|
||||
Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2,3,5,7);
|
||||
Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2, 3, 5, 7);
|
||||
index_pairs = tensor.index_pairs();
|
||||
|
||||
for (DenseIndex n = 0; n < 2*3*5*7; ++n) {
|
||||
for (DenseIndex n = 0; n < 2 * 3 * 5 * 7; ++n) {
|
||||
const Pair<DenseIndex, float>& v = index_pairs.coeff(n);
|
||||
VERIFY_IS_EQUAL(v.first, n);
|
||||
VERIFY_IS_EQUAL(v.second, tensor.coeff(n));
|
||||
@@ -34,37 +33,34 @@ static void test_simple_index_pairs()
|
||||
}
|
||||
|
||||
template <int DataLayout>
|
||||
static void test_index_pairs_dim()
|
||||
{
|
||||
Tensor<float, 4, DataLayout> tensor(2,3,5,7);
|
||||
static void test_index_pairs_dim() {
|
||||
Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
|
||||
tensor.setRandom();
|
||||
tensor = (tensor + tensor.constant(0.5)).log();
|
||||
|
||||
Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2,3,5,7);
|
||||
Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2, 3, 5, 7);
|
||||
|
||||
index_pairs = tensor.index_pairs();
|
||||
|
||||
for (Eigen::DenseIndex n = 0; n < tensor.size(); ++n) {
|
||||
const Pair<DenseIndex, float>& v = index_pairs(n); //(i, j, k, l);
|
||||
const Pair<DenseIndex, float>& v = index_pairs(n); //(i, j, k, l);
|
||||
VERIFY_IS_EQUAL(v.first, n);
|
||||
VERIFY_IS_EQUAL(v.second, tensor(n));
|
||||
}
|
||||
}
|
||||
|
||||
template <int DataLayout>
|
||||
static void test_argmax_pair_reducer()
|
||||
{
|
||||
Tensor<float, 4, DataLayout> tensor(2,3,5,7);
|
||||
static void test_argmax_pair_reducer() {
|
||||
Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
|
||||
tensor.setRandom();
|
||||
tensor = (tensor + tensor.constant(0.5)).log();
|
||||
|
||||
Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2,3,5,7);
|
||||
Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2, 3, 5, 7);
|
||||
index_pairs = tensor.index_pairs();
|
||||
|
||||
Tensor<Pair<DenseIndex, float>, 0, DataLayout> reduced;
|
||||
DimensionList<DenseIndex, 4> dims;
|
||||
reduced = index_pairs.reduce(
|
||||
dims, internal::ArgMaxPairReducer<Pair<DenseIndex, float> >());
|
||||
reduced = index_pairs.reduce(dims, internal::ArgMaxPairReducer<Pair<DenseIndex, float> >());
|
||||
|
||||
Tensor<float, 0, DataLayout> maxi = tensor.maximum();
|
||||
|
||||
@@ -73,8 +69,7 @@ static void test_argmax_pair_reducer()
|
||||
array<DenseIndex, 3> reduce_dims;
|
||||
for (int d = 0; d < 3; ++d) reduce_dims[d] = d;
|
||||
Tensor<Pair<DenseIndex, float>, 1, DataLayout> reduced_by_dims(7);
|
||||
reduced_by_dims = index_pairs.reduce(
|
||||
reduce_dims, internal::ArgMaxPairReducer<Pair<DenseIndex, float> >());
|
||||
reduced_by_dims = index_pairs.reduce(reduce_dims, internal::ArgMaxPairReducer<Pair<DenseIndex, float> >());
|
||||
|
||||
Tensor<float, 1, DataLayout> max_by_dims = tensor.maximum(reduce_dims);
|
||||
|
||||
@@ -84,19 +79,17 @@ static void test_argmax_pair_reducer()
|
||||
}
|
||||
|
||||
template <int DataLayout>
|
||||
static void test_argmin_pair_reducer()
|
||||
{
|
||||
Tensor<float, 4, DataLayout> tensor(2,3,5,7);
|
||||
static void test_argmin_pair_reducer() {
|
||||
Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
|
||||
tensor.setRandom();
|
||||
tensor = (tensor + tensor.constant(0.5)).log();
|
||||
|
||||
Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2,3,5,7);
|
||||
Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2, 3, 5, 7);
|
||||
index_pairs = tensor.index_pairs();
|
||||
|
||||
Tensor<Pair<DenseIndex, float>, 0, DataLayout> reduced;
|
||||
DimensionList<DenseIndex, 4> dims;
|
||||
reduced = index_pairs.reduce(
|
||||
dims, internal::ArgMinPairReducer<Pair<DenseIndex, float> >());
|
||||
reduced = index_pairs.reduce(dims, internal::ArgMinPairReducer<Pair<DenseIndex, float> >());
|
||||
|
||||
Tensor<float, 0, DataLayout> mini = tensor.minimum();
|
||||
|
||||
@@ -105,8 +98,7 @@ static void test_argmin_pair_reducer()
|
||||
array<DenseIndex, 3> reduce_dims;
|
||||
for (int d = 0; d < 3; ++d) reduce_dims[d] = d;
|
||||
Tensor<Pair<DenseIndex, float>, 1, DataLayout> reduced_by_dims(7);
|
||||
reduced_by_dims = index_pairs.reduce(
|
||||
reduce_dims, internal::ArgMinPairReducer<Pair<DenseIndex, float> >());
|
||||
reduced_by_dims = index_pairs.reduce(reduce_dims, internal::ArgMinPairReducer<Pair<DenseIndex, float> >());
|
||||
|
||||
Tensor<float, 1, DataLayout> min_by_dims = tensor.minimum(reduce_dims);
|
||||
|
||||
@@ -116,12 +108,11 @@ static void test_argmin_pair_reducer()
|
||||
}
|
||||
|
||||
template <int DataLayout>
|
||||
static void test_simple_argmax()
|
||||
{
|
||||
Tensor<float, 4, DataLayout> tensor(2,3,5,7);
|
||||
static void test_simple_argmax() {
|
||||
Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
|
||||
tensor.setRandom();
|
||||
tensor = (tensor + tensor.constant(0.5)).log();
|
||||
tensor(0,0,0,0) = 10.0;
|
||||
tensor(0, 0, 0, 0) = 10.0;
|
||||
|
||||
Tensor<DenseIndex, 0, DataLayout> tensor_argmax;
|
||||
|
||||
@@ -129,20 +120,19 @@ static void test_simple_argmax()
|
||||
|
||||
VERIFY_IS_EQUAL(tensor_argmax(0), 0);
|
||||
|
||||
tensor(1,2,4,6) = 20.0;
|
||||
tensor(1, 2, 4, 6) = 20.0;
|
||||
|
||||
tensor_argmax = tensor.argmax();
|
||||
|
||||
VERIFY_IS_EQUAL(tensor_argmax(0), 2*3*5*7 - 1);
|
||||
VERIFY_IS_EQUAL(tensor_argmax(0), 2 * 3 * 5 * 7 - 1);
|
||||
}
|
||||
|
||||
template <int DataLayout>
|
||||
static void test_simple_argmin()
|
||||
{
|
||||
Tensor<float, 4, DataLayout> tensor(2,3,5,7);
|
||||
static void test_simple_argmin() {
|
||||
Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
|
||||
tensor.setRandom();
|
||||
tensor = (tensor + tensor.constant(0.5)).log();
|
||||
tensor(0,0,0,0) = -10.0;
|
||||
tensor(0, 0, 0, 0) = -10.0;
|
||||
|
||||
Tensor<DenseIndex, 0, DataLayout> tensor_argmin;
|
||||
|
||||
@@ -150,18 +140,17 @@ static void test_simple_argmin()
|
||||
|
||||
VERIFY_IS_EQUAL(tensor_argmin(0), 0);
|
||||
|
||||
tensor(1,2,4,6) = -20.0;
|
||||
tensor(1, 2, 4, 6) = -20.0;
|
||||
|
||||
tensor_argmin = tensor.argmin();
|
||||
|
||||
VERIFY_IS_EQUAL(tensor_argmin(0), 2*3*5*7 - 1);
|
||||
VERIFY_IS_EQUAL(tensor_argmin(0), 2 * 3 * 5 * 7 - 1);
|
||||
}
|
||||
|
||||
template <int DataLayout>
|
||||
static void test_argmax_dim()
|
||||
{
|
||||
Tensor<float, 4, DataLayout> tensor(2,3,5,7);
|
||||
std::vector<int> dims {2, 3, 5, 7};
|
||||
static void test_argmax_dim() {
|
||||
Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
|
||||
std::vector<int> dims{2, 3, 5, 7};
|
||||
|
||||
for (int dim = 0; dim < 4; ++dim) {
|
||||
tensor.setRandom();
|
||||
@@ -173,7 +162,10 @@ static void test_argmax_dim()
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
for (int k = 0; k < 5; ++k) {
|
||||
for (int l = 0; l < 7; ++l) {
|
||||
ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
|
||||
ix[0] = i;
|
||||
ix[1] = j;
|
||||
ix[2] = k;
|
||||
ix[3] = l;
|
||||
if (ix[dim] != 0) continue;
|
||||
// suppose dim == 1, then for all i, k, l, set tensor(i, 0, k, l) = 10.0
|
||||
tensor(ix) = 10.0;
|
||||
@@ -184,8 +176,7 @@ static void test_argmax_dim()
|
||||
|
||||
tensor_argmax = tensor.argmax(dim);
|
||||
|
||||
VERIFY_IS_EQUAL(tensor_argmax.size(),
|
||||
ptrdiff_t(2*3*5*7 / tensor.dimension(dim)));
|
||||
VERIFY_IS_EQUAL(tensor_argmax.size(), ptrdiff_t(2 * 3 * 5 * 7 / tensor.dimension(dim)));
|
||||
for (ptrdiff_t n = 0; n < tensor_argmax.size(); ++n) {
|
||||
// Expect max to be in the first index of the reduced dimension
|
||||
VERIFY_IS_EQUAL(tensor_argmax.data()[n], 0);
|
||||
@@ -195,7 +186,10 @@ static void test_argmax_dim()
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
for (int k = 0; k < 5; ++k) {
|
||||
for (int l = 0; l < 7; ++l) {
|
||||
ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
|
||||
ix[0] = i;
|
||||
ix[1] = j;
|
||||
ix[2] = k;
|
||||
ix[3] = l;
|
||||
if (ix[dim] != tensor.dimension(dim) - 1) continue;
|
||||
// suppose dim == 1, then for all i, k, l, set tensor(i, 2, k, l) = 20.0
|
||||
tensor(ix) = 20.0;
|
||||
@@ -206,8 +200,7 @@ static void test_argmax_dim()
|
||||
|
||||
tensor_argmax = tensor.argmax(dim);
|
||||
|
||||
VERIFY_IS_EQUAL(tensor_argmax.size(),
|
||||
ptrdiff_t(2*3*5*7 / tensor.dimension(dim)));
|
||||
VERIFY_IS_EQUAL(tensor_argmax.size(), ptrdiff_t(2 * 3 * 5 * 7 / tensor.dimension(dim)));
|
||||
for (ptrdiff_t n = 0; n < tensor_argmax.size(); ++n) {
|
||||
// Expect max to be in the last index of the reduced dimension
|
||||
VERIFY_IS_EQUAL(tensor_argmax.data()[n], tensor.dimension(dim) - 1);
|
||||
@@ -216,10 +209,9 @@ static void test_argmax_dim()
|
||||
}
|
||||
|
||||
template <int DataLayout>
|
||||
static void test_argmin_dim()
|
||||
{
|
||||
Tensor<float, 4, DataLayout> tensor(2,3,5,7);
|
||||
std::vector<int> dims {2, 3, 5, 7};
|
||||
static void test_argmin_dim() {
|
||||
Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
|
||||
std::vector<int> dims{2, 3, 5, 7};
|
||||
|
||||
for (int dim = 0; dim < 4; ++dim) {
|
||||
tensor.setRandom();
|
||||
@@ -231,7 +223,10 @@ static void test_argmin_dim()
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
for (int k = 0; k < 5; ++k) {
|
||||
for (int l = 0; l < 7; ++l) {
|
||||
ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
|
||||
ix[0] = i;
|
||||
ix[1] = j;
|
||||
ix[2] = k;
|
||||
ix[3] = l;
|
||||
if (ix[dim] != 0) continue;
|
||||
// suppose dim == 1, then for all i, k, l, set tensor(i, 0, k, l) = -10.0
|
||||
tensor(ix) = -10.0;
|
||||
@@ -242,8 +237,7 @@ static void test_argmin_dim()
|
||||
|
||||
tensor_argmin = tensor.argmin(dim);
|
||||
|
||||
VERIFY_IS_EQUAL(tensor_argmin.size(),
|
||||
ptrdiff_t(2*3*5*7 / tensor.dimension(dim)));
|
||||
VERIFY_IS_EQUAL(tensor_argmin.size(), ptrdiff_t(2 * 3 * 5 * 7 / tensor.dimension(dim)));
|
||||
for (ptrdiff_t n = 0; n < tensor_argmin.size(); ++n) {
|
||||
// Expect min to be in the first index of the reduced dimension
|
||||
VERIFY_IS_EQUAL(tensor_argmin.data()[n], 0);
|
||||
@@ -253,7 +247,10 @@ static void test_argmin_dim()
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
for (int k = 0; k < 5; ++k) {
|
||||
for (int l = 0; l < 7; ++l) {
|
||||
ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
|
||||
ix[0] = i;
|
||||
ix[1] = j;
|
||||
ix[2] = k;
|
||||
ix[3] = l;
|
||||
if (ix[dim] != tensor.dimension(dim) - 1) continue;
|
||||
// suppose dim == 1, then for all i, k, l, set tensor(i, 2, k, l) = -20.0
|
||||
tensor(ix) = -20.0;
|
||||
@@ -264,8 +261,7 @@ static void test_argmin_dim()
|
||||
|
||||
tensor_argmin = tensor.argmin(dim);
|
||||
|
||||
VERIFY_IS_EQUAL(tensor_argmin.size(),
|
||||
ptrdiff_t(2*3*5*7 / tensor.dimension(dim)));
|
||||
VERIFY_IS_EQUAL(tensor_argmin.size(), ptrdiff_t(2 * 3 * 5 * 7 / tensor.dimension(dim)));
|
||||
for (ptrdiff_t n = 0; n < tensor_argmin.size(); ++n) {
|
||||
// Expect min to be in the last index of the reduced dimension
|
||||
VERIFY_IS_EQUAL(tensor_argmin.data()[n], tensor.dimension(dim) - 1);
|
||||
@@ -273,8 +269,7 @@ static void test_argmin_dim()
|
||||
}
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(cxx11_tensor_argmax)
|
||||
{
|
||||
EIGEN_DECLARE_TEST(cxx11_tensor_argmax) {
|
||||
CALL_SUBTEST(test_simple_index_pairs<RowMajor>());
|
||||
CALL_SUBTEST(test_simple_index_pairs<ColMajor>());
|
||||
CALL_SUBTEST(test_index_pairs_dim<RowMajor>());
|
||||
|
||||
Reference in New Issue
Block a user