Skip to content

Commit

Permalink
Add concat function for Eigen arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dean0x7d committed Jul 17, 2017
1 parent 0798131 commit a3bcf75
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cppcore/include/numeric/dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ class CartesianArray {
public:
CartesianArray() = default;
CartesianArray(idx_t size) : x(size), y(size), z(size) {}
CartesianArray(ArrayXf const& x, ArrayXf const& y, ArrayXf const& z) : x(x), y(y), z(z) {}
CartesianArray(ArrayXf x, ArrayXf y, ArrayXf z)
: x(std::move(x)), y(std::move(y)), z(std::move(z)) {}

CartesianRef operator[](idx_t i) { return {x[i], y[i], z[i]}; }
Cartesian operator[](idx_t i) const { return {x[i], y[i], z[i]}; }
Expand Down Expand Up @@ -333,4 +334,27 @@ Vector slice(Vector const& v, Bools const& keep) {
return result;
};

/// Concatenate two 1D arrays/vectors
template<class Vector>
Vector concat(Ref<Vector const> v1, Ref<Vector const> v2) {
using std::begin; using std::end;
auto result = Vector(v1.size() + v2.size());
std::copy(begin(v1), end(v1), begin(result));
std::copy(begin(v2), end(v2), begin(result) + v1.size());
return result;
}

template<class Vector, typename R = Ref<Vector const>>
Vector concat(Vector const& v1, Vector const& v2) {
return concat(R(v1), R(v2));
}

/// Concatenate two Cartesian arrays
inline CartesianArray concat(CartesianArrayConstRef const& ca1,
CartesianArrayConstRef const& ca2) {
return {concat(ca1.x(), ca2.x()),
concat(ca1.y(), ca2.y()),
concat(ca1.z(), ca2.z())};
}

} // namespace cpb
33 changes: 33 additions & 0 deletions cppcore/tests/test_numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,36 @@ TEST_CASE("Aligned size") {
REQUIRE((num::aligned_size<std::complex<double>, 16>(2) == 2));
REQUIRE((num::aligned_size<std::complex<float>, 32>(9) == 12));
}

TEST_CASE("concat") {
auto const x1 = ArrayXf::Constant(3, 1).eval();
auto const x2 = ArrayXf::LinSpaced(3, 2, 4).eval();
auto expected_x = ArrayXf(6);
expected_x << 1, 1, 1, 2, 3, 4;

auto const result_x = concat(x1, x2);
REQUIRE(result_x.isApprox(expected_x));

auto const y1 = ArrayXf::Constant(3, 2).eval();
auto const y2 = ArrayXf::LinSpaced(3, 3, 5).eval();
auto expected_y = ArrayXf(6);
expected_y << 2, 2, 2, 3, 4, 5;

auto const result_y = concat(y1, y2);
REQUIRE(result_y.isApprox(expected_y));

auto const z1 = ArrayXf::Constant(3, 0).eval();
auto const z2 = ArrayXf::Constant(3, -1).eval();
auto expected_z = ArrayXf(6);
expected_z << 0, 0, 0, -1, -1, -1;

auto const result_z = concat(z1, z2);
REQUIRE(result_z.isApprox(expected_z));

auto const r1 = CartesianArray(x1, y1, z1);
auto const r2 = CartesianArray(x2, y2, z2);
auto const result = concat(r1, r2);
REQUIRE(result.x.isApprox(expected_x));
REQUIRE(result.y.isApprox(expected_y));
REQUIRE(result.z.isApprox(expected_z));
}

0 comments on commit a3bcf75

Please sign in to comment.