Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
186 lines (174 loc) · 6.83 KB

File metadata and controls

186 lines (174 loc) · 6.83 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// -*- c++ -*-
/*
* Copyright (c) 2010-2012, Jim Bosch
* All rights reserved.
*
* ndarray is distributed under a simple BSD-like license;
* see the LICENSE file that should be present in the root
* of the source distribution, or alternately available at:
* https://github.com/ndarray/ndarray
*/
#include "ndarray/eigen.h"
#include "Eigen/SVD"
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ndarray-eigen
#include "boost/test/unit_test.hpp"
template <typename T, typename U>
void testElements2(T const & a, U const & b) {
BOOST_CHECK( a.template getSize<0>() == b.rows() );
BOOST_CHECK( a.template getSize<1>() == b.cols() );
BOOST_CHECK( a.template getStride<0>() == b.rowStride() );
BOOST_CHECK( a.template getStride<1>() == b.colStride() );
for (int i = 0; i < b.rows(); ++i) {
for (int j = 0; j < b.cols(); ++j) {
BOOST_CHECK(&a[i][j] == &b(i,j));
}
}
}
template <typename T, typename U>
void testElements1(T const & a, U const & b) {
BOOST_CHECK( a.template getSize<0>() == b.size() );
BOOST_CHECK( a.template getStride<0>() == b.innerStride() );
for (int i = 0; i < b.size(); ++i) {
BOOST_CHECK(&a[i] == &b[i]);
}
}
template <int C, int Rows, int Cols>
void testEigenView(ndarray::EigenView<double,2,C,Eigen::ArrayXpr,Rows,Cols> b) {
ndarray::Array<double,2,C> a(b.shallow());
b.setRandom();
testElements2(a, b);
Eigen::Matrix<double,Rows,Eigen::Dynamic> m1(b.rows(), 6);
m1.setRandom(b.rows(), 6);
Eigen::Matrix<double,Eigen::Dynamic,Cols> m2(6, b.cols());
m2.setRandom(6, b.cols());
b.matrix() = m1 * m2;
Eigen::Matrix<double,Rows,Cols> m3 = m1 * m2;
for (int i = 0; i < b.rows(); ++i) {
for (int j = 0; j < b.cols(); ++j) {
BOOST_CHECK(a[i][j] == m3(i,j));
}
}
Eigen::Array<double,Rows,Cols> m4(b.rows(), b.cols());
m4.setRandom();
Eigen::Array<double,Rows,Cols> m5 = m4 * b;
Eigen::Array<double,Rows,Cols> m6 = m4 * m3.array();
BOOST_CHECK( (m5 == m6).all() );
}
template <int C, int Rows, int Cols>
void testEigenView(ndarray::EigenView<double,1,C,Eigen::ArrayXpr,Rows,Cols> b) {
ndarray::Array<double,1,C> a(b.shallow());
b.setRandom();
testElements1(a, b);
Eigen::Matrix<double,Rows,Eigen::Dynamic> m1(b.rows(), 6);
m1.setRandom(b.rows(), 6);
Eigen::Matrix<double,Eigen::Dynamic,Cols> m2(6, b.cols());
m2.setRandom(6, b.cols());
b.matrix() = m1 * m2;
Eigen::Matrix<double,Rows,Cols> m3 = m1 * m2;
for (int i = 0; i < b.rows(); ++i) {
BOOST_CHECK(a[i] == m3[i]);
}
Eigen::Array<double,Rows,Cols> m4(b.rows(), b.cols());
m4.setRandom();
Eigen::Array<double,Rows,Cols> m5 = m4 * b;
Eigen::Array<double,Rows,Cols> m6 = m4 * m3.array();
BOOST_CHECK( (m5 == m6).all() );
}
template <int C, int Rows, int Cols>
void testEigenView(ndarray::EigenView<double,2,C,Eigen::MatrixXpr,Rows,Cols> b) {
ndarray::Array<double,2,C> a(b.shallow());
b.setRandom();
testElements2(a, b);
Eigen::Matrix<double,Rows,Eigen::Dynamic> m1(b.rows(), 6);
m1.setRandom(b.rows(), 6);
Eigen::Matrix<double,Eigen::Dynamic,Cols> m2(6, b.cols());
m2.setRandom(6, b.cols());
b = m1 * m2;
Eigen::Matrix<double,Rows,Cols> m3 = m1 * m2;
for (int i = 0; i < b.rows(); ++i) {
for (int j = 0; j < b.cols(); ++j) {
BOOST_CHECK(a[i][j] == m3(i,j));
}
}
Eigen::Array<double,Rows,Cols> m4(b.rows(), b.cols());
m4.setRandom();
Eigen::Array<double,Rows,Cols> m5 = m4 * b.array();
Eigen::Array<double,Rows,Cols> m6 = m4 * m3.array();
BOOST_CHECK( (m5 == m6).all() );
}
template <int C, int Rows, int Cols>
void testEigenView(ndarray::EigenView<double,1,C,Eigen::MatrixXpr,Rows,Cols> b) {
ndarray::Array<double,1,C> a(b.shallow());
b.setRandom();
testElements1(a, b);
Eigen::Matrix<double,Rows,Eigen::Dynamic> m1(b.rows(), 6);
m1.setRandom(b.rows(), 6);
Eigen::Matrix<double,Eigen::Dynamic,Cols> m2(6, b.cols());
m2.setRandom(6, b.cols());
b = m1 * m2;
Eigen::Matrix<double,Rows,Cols> m3 = m1 * m2;
for (int i = 0; i < b.rows(); ++i) {
BOOST_CHECK(a[i] == m3[i]);
}
Eigen::Array<double,Rows,Cols> m4(b.rows(), b.cols());
m4.setRandom();
Eigen::Array<double,Rows,Cols> m5 = m4 * b.array();
Eigen::Array<double,Rows,Cols> m6 = m4 * m3.array();
BOOST_CHECK( (m5 == m6).all() );
}
template <typename XprKind>
void invokeEigenViewTests() {
ndarray::Array<double,2,2> a22(ndarray::allocate(5,4));
testEigenView(a22.asEigen<XprKind>());
testEigenView(a22.asEigen<XprKind,5,4>());
testEigenView(a22.transpose().asEigen<XprKind>());
testEigenView(a22.transpose().asEigen<XprKind,4,5>());
ndarray::Array<double,2,1> a21(a22[ndarray::view()(0,3)]);
testEigenView(a21.asEigen<XprKind>());
testEigenView(a21.asEigen<XprKind,5,3>());
testEigenView(a21.transpose().asEigen<XprKind>());
testEigenView(a21.transpose().asEigen<XprKind,3,5>());
ndarray::Array<double,2,0> a20(a22[ndarray::view()(0,4,2)]);
testEigenView(a20.asEigen<XprKind>());
testEigenView(a20.asEigen<XprKind,5,2>());
testEigenView(a20.transpose().asEigen<XprKind>());
testEigenView(a20.transpose().asEigen<XprKind,2,5>());
ndarray::Array<double,1,1> a11(ndarray::allocate(4));
testEigenView(a11.asEigen<XprKind>());
testEigenView(a11.asEigen<XprKind,4,1>());
testEigenView(a11.asEigen<XprKind,1,4>());
testEigenView(a11.transpose().asEigen<XprKind>());
testEigenView(a11.transpose().asEigen<XprKind,4,1>());
testEigenView(a11.transpose().asEigen<XprKind,1,4>());
ndarray::Array<double,1,0> a10(a11[ndarray::view(0,4,2)]);
testEigenView(a10.asEigen<XprKind>());
testEigenView(a10.asEigen<XprKind,2,1>());
testEigenView(a10.asEigen<XprKind,1,2>());
testEigenView(a10.transpose().asEigen<XprKind>());
testEigenView(a10.transpose().asEigen<XprKind,2,1>());
testEigenView(a10.transpose().asEigen<XprKind,1,2>());
}
BOOST_AUTO_TEST_CASE(EigenView) {
invokeEigenViewTests<Eigen::ArrayXpr>();
invokeEigenViewTests<Eigen::MatrixXpr>();
Eigen::MatrixXd m(Eigen::MatrixXd::Random(5,6));
ndarray::SelectEigenView<Eigen::MatrixXd>::Type v(ndarray::copy(m));
BOOST_CHECK( (v.array() == m.array()).all() );
}
template <typename SVD, typename Matrix, typename Vector>
void testSVD(Matrix const & a, Vector const & b, Vector & x) {
SVD svd(a, Eigen::ComputeThinU | Eigen::ComputeThinV);
x = svd.solve(b);
BOOST_CHECK((a.transpose() * a * x).isApprox(a.transpose() * b));
}
BOOST_AUTO_TEST_CASE(SVD) {
typedef ndarray::EigenView<double,2,2> Matrix;
typedef ndarray::EigenView<double,1,1> Vector;
Matrix a(ndarray::allocate(8,5));
Vector b(ndarray::allocate(8));
Vector x(ndarray::allocate(5));
a.setRandom();
b.setRandom();
testSVD< Eigen::JacobiSVD<Matrix::PlainEigenType> >(a, b, x);
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.