diff options
author | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2014-10-13 17:02:09 -0700 |
---|---|---|
committer | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2014-10-13 17:02:09 -0700 |
commit | 99d75235a9567865d2c070a2840d54c8a5ad0f43 (patch) | |
tree | 8ef64899252a8be7b6a868bd64bd167063ea4b2d /unsupported/test/cxx11_tensor_of_complex.cpp | |
parent | 4c70b0a7627d45286ecbb3c73d2d774412168205 (diff) |
Misc improvements and cleanups
Diffstat (limited to 'unsupported/test/cxx11_tensor_of_complex.cpp')
-rw-r--r-- | unsupported/test/cxx11_tensor_of_complex.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/unsupported/test/cxx11_tensor_of_complex.cpp b/unsupported/test/cxx11_tensor_of_complex.cpp new file mode 100644 index 000000000..b5044b962 --- /dev/null +++ b/unsupported/test/cxx11_tensor_of_complex.cpp @@ -0,0 +1,64 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com> +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "main.h" + +#include <Eigen/CXX11/Tensor> + +using Eigen::Tensor; +using Eigen::TensorMap; + + + +static void test_additions() +{ + Tensor<std::complex<float>, 1> data1(3); + Tensor<std::complex<float>, 1> data2(3); + for (int i = 0; i < 3; ++i) { + data1(i) = std::complex<float>(i, -i); + data2(i) = std::complex<float>(i, 7 * i); + } + + Tensor<std::complex<float>, 1> sum = data1 + data2; + for (int i = 0; i < 3; ++i) { + VERIFY_IS_EQUAL(sum(i), std::complex<float>(2*i, 6*i)); + } +} + + +static void test_contractions() +{ + Tensor<std::complex<float>, 4> t_left(30, 50, 8, 31); + Tensor<std::complex<float>, 5> t_right(8, 31, 7, 20, 10); + Tensor<std::complex<float>, 5> t_result(30, 50, 7, 20, 10); + + t_left.setRandom(); + t_right.setRandom(); + + typedef Map<Matrix<std::complex<float>, Dynamic, Dynamic>> MapXcf; + MapXcf m_left(t_left.data(), 1500, 248); + MapXcf m_right(t_right.data(), 248, 1400); + Matrix<std::complex<float>, Dynamic, Dynamic> m_result(1500, 1400); + + // This contraction should be equivalent to a regular matrix multiplication + typedef Tensor<float, 1>::DimensionPair DimPair; + Eigen::array<DimPair, 2> dims({{DimPair(2, 0), DimPair(3, 1)}}); + t_result = t_left.contract(t_right, dims); + m_result = m_left * m_right; + for (size_t i = 0; i < t_result.dimensions().TotalSize(); i++) { + VERIFY_IS_APPROX(t_result.data()[i], m_result.data()[i]); + } +} + + +void test_cxx11_tensor_of_complex() +{ + CALL_SUBTEST(test_additions()); + CALL_SUBTEST(test_contractions()); +} |