SOPT
Sparse OPTimisation
Functions
wrapper.cc File Reference
#include <catch2/catch_all.hpp>
#include <random>
#include "sopt/wrapper.h"
+ Include dependency graph for wrapper.cc:

Go to the source code of this file.

Functions

 TEST_CASE ("Function wrappers", "[utility]")
 

Function Documentation

◆ TEST_CASE()

TEST_CASE ( "Function wrappers"  ,
""  [utility] 
)

Definition at line 6 of file wrapper.cc.

6  {
7  using namespace sopt;
8  using t_Array = Array<int>;
9  using t_RefArray = t_Array&;
10  using t_ConstRefArray = t_Array const;
11 
12  SECTION("Square function") {
13  auto func = [](t_RefArray output, t_ConstRefArray const &input) { output = input * 2 + 1; };
14 
15  t_Array const x = t_Array::Random(5);
16  auto const A = details::wrap<t_Array>(func);
17  // Expected result
18  t_Array const expected = (x * 2 + 1).eval();
19 
20  CHECK((A * x).matrix() == expected.matrix());
21  CHECK(A(x).matrix() == expected.matrix());
22  }
23 
24  SECTION("Rectangular function") {
25  auto func = [](t_RefArray output, t_ConstRefArray const &input) {
26  output = input.head(input.size() / 2) * 2 + 1;
27  };
28 
29  t_Array const x = t_Array::Random(5);
30  auto const A = details::wrap<t_Array>(func, {{1, 2, 0}});
31  // Expected result
32  t_Array const expected = (x.head(x.size() / 2) * 2 + 1).eval();
33 
34  CHECK((A * x).cols() == 1);
35  CHECK((A * x).rows() == 2);
36  CHECK((A * x).matrix() == expected.matrix());
37  CHECK(A(x).matrix() == expected.matrix());
38  }
39 
40  SECTION("Fixed output-size functions") {
41  auto func = [](t_RefArray output, t_ConstRefArray const &input) {
42  output = input.head(3) * 2 + 1;
43  };
44 
45  t_Array const x = t_Array::Random(5);
46  auto const A = details::wrap<t_Array>(func, {{0, 1, 3}});
47  // Expected result
48  t_Array const expected = (x.head(3) * 2 + 1).eval();
49 
50  CHECK((A * x).cols() == 1);
51  CHECK((A * x).rows() == 3);
52  CHECK((A * x).matrix() == expected.matrix());
53  CHECK(A(x).matrix() == expected.matrix());
54  }
55 }
t_uint rows
t_uint cols
Eigen::Array< T, Eigen::Dynamic, 1 > Array
A 1-dimensional list of elements of given type.
Definition: types.h:34

References cols, and rows.