WaveBlocksND
homogeneous_inner_product.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cmath>
4 #include <functional>
5 #include <iostream>
6 #include <tuple>
7 #include <vector>
8 
9 #include <Eigen/Core>
10 #include <Eigen/Dense>
11 #include <unsupported/Eigen/MatrixFunctions>
12 
13 #include "../types.hpp"
14 #include "../wavepackets/hawp_commons.hpp"
15 
16 
17 namespace waveblocks {
18  namespace innerproducts {
19  using wavepackets::AbstractScalarHaWp;
20 
29  template<dim_t D, class MultiIndex, class QR>
31  {
32  public:
40  using CDiagonalXX = Eigen::DiagonalMatrix<complex_t, Eigen::Dynamic>;
41  using NodeMatrix = typename QR::NodeMatrix;
42  using WeightVector = typename QR::WeightVector;
43  using op_t = std::function<CMatrix1X(CMatrixDX,RMatrixD1)>;
44 
58  static CMatrixXX build_matrix(const AbstractScalarHaWp<D, MultiIndex>& packet,
59  const op_t& op=default_op) {
60  const dim_t n_nodes = QR::number_nodes();
61  const CMatrixD1& q = packet.parameters().q().template cast<complex_t>();
62  const CMatrixDD& Q = packet.parameters().Q();
63  NodeMatrix nodes;
64  WeightVector weights;
65  std::tie(nodes, weights) = QR::nodes_and_weights();
66 
67  // Compute affine transformation
68  const CMatrixDD Qs = (Q * Q.adjoint()).sqrt();
69 
70  // Transform nodes
71  const CMatrixDX transformed_nodes = q.replicate(1, n_nodes) + packet.eps() * (Qs * nodes);
72 
73  // Apply operator
74  const CMatrix1X values = op(transformed_nodes, packet.parameters().q());
75 
76  // Prefactor
77  const CMatrix1X factor =
78  // std::conj(packet.prefactor()) * packet.prefactor() * Qs.determinant() = 1
79  std::pow(packet.eps(), D) * weights.array() * values.array();
80 
81  // Evaluate basis
82  const CMatrixXX basis = packet.evaluate_basis(transformed_nodes);
83 
84  // Build matrix
85  const CDiagonalXX Dfactor(factor);
86  const CMatrixXX result = basis.matrix().conjugate() * Dfactor * basis.matrix().transpose();
87 
88  // Global phase cancels out
89  return result;
90  }
91 
98  static complex_t quadrature(const AbstractScalarHaWp<D, MultiIndex>& packet,
99  const op_t& op=default_op) {
100  const auto M = build_matrix(packet, op);
101  // Quadrature with wavepacket coefficients, c^H M c.
102  return packet.coefficients().adjoint() * M * packet.coefficients();
103  }
104 
105  private:
106  static CMatrix1X default_op(const CMatrixDX& nodes, const RMatrixD1& pos)
107  {
108  (void)pos;
109  return CMatrix1X::Ones(1, nodes.cols());
110  }
111  };
112  }
113 }
static complex_t quadrature(const AbstractScalarHaWp< D, MultiIndex > &packet, const op_t &op=default_op)
Perform quadrature.
Definition: homogeneous_inner_product.hpp:98
Definition: coefficients_file_parser.cpp:10
Eigen::Matrix< real_t, R, C > RMatrix
Definition: types.hpp:22
std::complex< real_t > complex_t
Definition: types.hpp:15
typename QR::WeightVector WeightVector
Definition: homogeneous_inner_product.hpp:42
Class providing homogeneous inner product calculation of scalar wavepackets.
Definition: homogeneous_inner_product.hpp:30
CMatrix< Eigen::Dynamic, Eigen::Dynamic > CMatrixXX
Definition: homogeneous_inner_product.hpp:33
CMatrix< D, D > CMatrixDD
Definition: homogeneous_inner_product.hpp:37
CMatrix< Eigen::Dynamic, 1 > CMatrixX1
Definition: homogeneous_inner_product.hpp:35
static CMatrixXX build_matrix(const AbstractScalarHaWp< D, MultiIndex > &packet, const op_t &op=default_op)
Calculate the matrix of the inner product.
Definition: homogeneous_inner_product.hpp:58
CMatrix< D, Eigen::Dynamic > CMatrixDX
Definition: homogeneous_inner_product.hpp:38
RMatrix< D, 1 > RMatrixD1
Definition: homogeneous_inner_product.hpp:39
CMatrix< D, 1 > CMatrixD1
Definition: homogeneous_inner_product.hpp:36
Eigen::Matrix< complex_t, R, C > CMatrix
Definition: types.hpp:19
std::function< CMatrix1X(CMatrixDX, RMatrixD1)> op_t
Definition: homogeneous_inner_product.hpp:43
typename QR::NodeMatrix NodeMatrix
Definition: homogeneous_inner_product.hpp:41
int dim_t
Definition: types.hpp:16
static CMatrix1X default_op(const CMatrixDX &nodes, const RMatrixD1 &pos)
Definition: homogeneous_inner_product.hpp:106
Eigen::DiagonalMatrix< complex_t, Eigen::Dynamic > CDiagonalXX
Definition: homogeneous_inner_product.hpp:40
CMatrix< 1, Eigen::Dynamic > CMatrix1X
Definition: homogeneous_inner_product.hpp:34