basis.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2020 Northwestern University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *********************************************************************/
39 #include <cmath>
40 
43 
44 namespace ergodic_exploration
45 {
46 using arma::span;
47 
48 Basis::Basis(double lx, double ly, unsigned int num_basis)
49  : lx_(lx)
50  , ly_(ly)
51  , total_basis_(num_basis * num_basis)
52  , lamdak_(total_basis_)
53  , k_(2, total_basis_)
54 {
55  // TODO: add hk
56 
57  // basis numbers
58  auto col = 0;
59  for (unsigned int i = 0; i < num_basis; i++)
60  {
61  for (unsigned int j = 0; j < num_basis; j++)
62  {
63  k_(0, col) = j;
64  k_(1, col) = i;
65  col++;
66  }
67  }
68  // k_.t().print("k");
69 
70  // TODO: verify power on demoninator
71  // frequency coefficients weights
72  for (unsigned int i = 0; i < total_basis_; i++)
73  {
74  lamdak_(i) = 1.0 / std::pow((1.0 + std::sqrt(sum(square(k_.col(i))))), 1.5);
75  }
76  // lamdak_.print("lamdak");
77 }
78 
79 vec Basis::fourierBasis(const vec& x) const
80 {
81  vec fk(total_basis_);
82  for (unsigned int i = 0; i < total_basis_; i++)
83  {
84  fk(i) =
85  std::cos(k_(0, i) * (PI / lx_) * x(0)) * std::cos(k_(1, i) * (PI / ly_) * x(1));
86  }
87 
88  return fk;
89 }
90 
91 mat Basis::gradFourierBasis(const vec& x) const
92 {
93  mat dfk(2, total_basis_);
94  auto k1 = 0.0;
95  auto k2 = 0.0;
96 
97  for (unsigned int i = 0; i < total_basis_; i++)
98  {
99  k1 = k_(0, i) * (PI / lx_);
100  k2 = k_(1, i) * (PI / ly_);
101 
102  dfk(0, i) = -k1 * std::sin(k1 * x(0)) * std::cos(k2 * x(1));
103  dfk(1, i) = -k2 * std::cos(k1 * x(0)) * std::sin(k2 * x(1));
104  }
105 
106  return dfk;
107 }
108 
109 vec Basis::trajCoeff(const mat& xt) const
110 {
111  mat fk_mat(total_basis_, xt.n_cols);
112 
113  for (unsigned int i = 0; i < xt.n_cols; i++)
114  {
115  fk_mat.col(i) = fourierBasis(xt(span(0, 1), span(i, i)));
116  }
117 
118  // sum of each row
119  return (1.0 / xt.n_cols) * sum(fk_mat, 1);
120 }
121 
122 vec Basis::spatialCoeff(const vec& phi_vals, const mat& phi_grid) const
123 {
124  mat fk_mat(total_basis_, phi_grid.n_cols);
125 
126  for (unsigned int i = 0; i < phi_grid.n_cols; i++)
127  {
128  fk_mat.col(i) = fourierBasis(phi_grid.col(i)) * phi_vals(i);
129  }
130 
131  // sum of each row
132  return sum(fk_mat, 1);
133 }
134 } // namespace ergodic_exploration
ergodic_exploration::Basis::gradFourierBasis
mat gradFourierBasis(const vec &x) const
Compose gradient cosine basis functions.
Definition: basis.cpp:91
ergodic_exploration::Basis::trajCoeff
vec trajCoeff(const mat &xt) const
Compose trajectory fourier coefficients.
Definition: basis.cpp:109
ergodic_exploration
Definition: basis.hpp:43
ergodic_exploration::Basis::Basis
Basis(double lx, double ly, unsigned int num_basis)
Constructor.
Definition: basis.cpp:48
numerics.hpp
Useful numerical utilities.
ergodic_exploration::Basis::fourierBasis
vec fourierBasis(const vec &x) const
Compose cosine basis functions given positon.
Definition: basis.cpp:79
ergodic_exploration::Basis::lx_
double lx_
Definition: basis.hpp:102
basis.hpp
Fourier cosine basis.
ergodic_exploration::Basis::k_
imat k_
Definition: basis.hpp:105
ergodic_exploration::Basis::lamdak_
vec lamdak_
Definition: basis.hpp:104
ergodic_exploration::Basis::spatialCoeff
vec spatialCoeff(const vec &phi_vals, const mat &phi_grid) const
Compose spatial fourier coefficients.
Definition: basis.cpp:122
ergodic_exploration::Basis::total_basis_
unsigned int total_basis_
Definition: basis.hpp:103
ergodic_exploration::PI
constexpr double PI
Definition: numerics.hpp:58
ergodic_exploration::Basis::ly_
double ly_
Definition: basis.hpp:102


ergodic_exploration
Author(s): bostoncleek
autogenerated on Wed Mar 2 2022 00:17:13