131 enum DenseAssignment { SetAssignment, AddAssignment, SubAssignment };
134 using Scalar = Scalar_;
136 using StorageIndex = int;
137 using Complex = std::complex<RealScalar>;
144 using GeneratorType =
145 Matrix<Scalar, BlockSize_, NumBlocks_, (BlockSize_ == 1 && NumBlocks_ != 1) ?
int(
RowMajor) : int(
ColMajor)>;
152 static constexpr int RowsAtCompileTime = internal::bccb_dim(BlockSize_, NumBlocks_);
153 static constexpr int ColsAtCompileTime = RowsAtCompileTime;
154 static constexpr int MaxRowsAtCompileTime = RowsAtCompileTime;
155 static constexpr int MaxColsAtCompileTime = RowsAtCompileTime;
156 static constexpr int SizeAtCompileTime = internal::size_at_compile_time(RowsAtCompileTime, ColsAtCompileTime);
157 static constexpr int MaxSizeAtCompileTime = SizeAtCompileTime;
158 static constexpr bool IsRowMajor =
false;
170 template <
typename Derived>
172 EIGEN_INTERNAL_ASSERT_NONEMPTY_MSG(m_g,
"Bccb generator must be non-empty");
173 if (m_g.size() > internal::structured_direct_threshold()) {
174 m_symbol = computeSymbol();
175 m_prodSymbol = computeProdSymbol(m_g);
179 EIGEN_DEVICE_FUNC Index rows()
const {
return m_g.size(); }
180 EIGEN_DEVICE_FUNC Index cols()
const {
return m_g.size(); }
194 ComplexArray
symbol()
const {
return m_symbol.size() > 0 ? m_symbol : computeSymbol(); }
199 Index k2 = row % n2 - col % n2;
200 if (k2 < 0) k2 += n2;
201 Index k1 = row / n2 - col / n2;
203 return m_g.coeff(k2, k1);
212 Bccb transpose()
const {
return Bccb(reverse2(m_g), reverse2(m_symbol), reverse2(m_prodSymbol)); }
235 template <
typename Rhs>
237 Rhs::ColsAtCompileTime>
240 EIGEN_INTERNAL_ASSERT_SOLVER_RHS_MSG(*
this, b,
false,
"right-hand side has the wrong number of rows");
241 const Index N = rows();
242 const ComplexArray s =
symbol();
243 const RealArray mods = s.cwiseAbs();
244 const ComplexArray sinv = internal::structured_pinv_symbol(s, mods, internal::structured_rank_threshold(s, mods));
246 applySymbol(x, sinv, b.derived(), ResultScalar(1),
false);
257 const ComplexArray s =
symbol();
258 const RealArray mods = s.cwiseAbs();
259 return (!(mods.array() < internal::structured_rank_threshold(s, mods))).count();
267 ComplexArray sinv =
symbol().cwiseInverse();
268 const GeneratorType ginv = generatorFromSymbol(sinv);
269 const bool cache = rows() > internal::structured_direct_threshold();
270 return Bccb(ginv, cache ? sinv : ComplexArray(), cache ? computeProdSymbol(ginv) : ComplexArray());
282 const ComplexArray s =
symbol();
285 for (
Index k1 = 0; k1 < s.cols(); ++k1)
286 for (
Index k2 = 0; k2 < s.rows(); ++k2)
287 det = internal::balance_exponent(Complex(det * internal::balance_exponent(s(k2, k1), exponent)), exponent);
288 return toScalar(internal::apply_exponent(det, exponent), std::is_same<RealScalar, Scalar>());
304 ComplexMatrix F(N, N);
305 for (
Index f1 = 0; f1 < n1; ++f1)
306 for (
Index f2 = 0; f2 < n2; ++f2) fourierColumn(F, f1, f2, f1 * n2 + f2);
315 const RealVector mods = s.cwiseAbs();
316 const std::vector<Index> perm = internal::structured_svd_permutation(mods);
317 RealVector sv(s.size());
318 for (
Index t = 0; t < s.size(); ++t) sv[t] = mods[perm[t]];
328 const RealVector mods = s.cwiseAbs();
329 const std::vector<Index> perm = internal::structured_svd_permutation(mods);
330 ComplexMatrix U(N, N);
331 for (
Index t = 0; t < N; ++t) {
332 fourierColumn(U, perm[t] / n2, perm[t] % n2, t);
333 const RealScalar a = mods[perm[t]];
334 if (a > RealScalar(0)) U.col(t) *= s[perm[t]] / a;
345 const std::vector<Index> perm = internal::structured_svd_permutation(RealVector(s.cwiseAbs()));
346 ComplexMatrix V(N, N);
347 for (
Index t = 0; t < N; ++t) fourierColumn(V, perm[t] / n2, perm[t] % n2, t);
355 template <
typename Dest>
356 void evalTo(Dest& dst)
const {
357 assignTo<SetAssignment>(dst);
361 template <
typename Dest>
362 void addTo(Dest& dst)
const {
363 assignTo<AddAssignment>(dst);
367 template <
typename Dest>
368 void subTo(Dest& dst)
const {
369 assignTo<SubAssignment>(dst);
377 template <
typename Rhs>
385 template <
typename Dest,
typename Rhs,
typename ProductScalar>
386 void addProduct(Dest& dst,
const Rhs& rhs,
const ProductScalar& alpha)
const {
387 const Index N = rows();
388 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE_MSG(*
this, rhs,
"invalid product: dimensions do not match");
389 if (N <= internal::structured_direct_threshold()) {
390 directProduct(dst, rhs, alpha);
396 const ComplexArray& s = m_prodSymbol.size() > 0 ? m_prodSymbol : m_symbol;
397 applySymbol(dst, s, rhs, alpha,
true);
403 template <DenseAssignment Assignment,
typename Dest>
404 void assignTo(Dest& dst)
const {
406 for (
Index c1 = 0; c1 < n1; ++c1)
407 for (
Index j2 = 0; j2 < n2; ++j2) {
408 auto col = dst.col(c1 * n2 + j2);
409 for (
Index b1 = 0; b1 < n1; ++b1) {
411 if (k1 < 0) k1 += n1;
412 EIGEN_IF_CONSTEXPR (Assignment == SetAssignment) {
413 col.segment(b1 * n2, j2) = m_g.col(k1).tail(j2);
414 col.segment(b1 * n2 + j2, n2 - j2) = m_g.col(k1).head(n2 - j2);
415 }
else EIGEN_IF_CONSTEXPR (Assignment == AddAssignment) {
416 col.segment(b1 * n2, j2) += m_g.col(k1).tail(j2);
417 col.segment(b1 * n2 + j2, n2 - j2) += m_g.col(k1).head(n2 - j2);
419 col.segment(b1 * n2, j2) -= m_g.col(k1).tail(j2);
420 col.segment(b1 * n2 + j2, n2 - j2) -= m_g.col(k1).head(n2 - j2);
432 Bccb(
const GeneratorType& g,
const ComplexArray&
symbol,
const ComplexArray& prodSymbol)
433 : m_g(g), m_symbol(
symbol), m_prodSymbol(prodSymbol) {}
453 ComplexArray computeProdSymbol(
const GeneratorType& g)
const {
454 const Index n2 = g.rows(), n1 = g.cols();
455 const Index p2 = internal::fft_next_good_size(n2) == n2 ? n2 : internal::fft_next_good_size(2 * n2 - 1);
456 const Index p1 = internal::fft_next_good_size(n1) == n1 ? n1 : internal::fft_next_good_size(2 * n1 - 1);
457 if (p2 == n2 && p1 == n1)
return ComplexArray();
458 ComplexArray Ge = ComplexArray::Zero(p2, p1);
459 Ge.topLeftCorner(n2, n1) = g.template cast<Complex>();
460 if (p2 != n2) Ge.bottomLeftCorner(n2 - 1, n1) = g.bottomRows(n2 - 1).template cast<Complex>();
461 if (p1 != n1) Ge.topRightCorner(n2, n1 - 1) = g.rightCols(n1 - 1).template cast<Complex>();
462 if (p2 != n2 && p1 != n1)
463 Ge.bottomRightCorner(n2 - 1, n1 - 1) = g.bottomRightCorner(n2 - 1, n1 - 1).template cast<Complex>();
473 template <
typename Dest,
typename Rhs,
typename ProductScalar>
474 void directProductColumn(Dest& dst,
const Rhs& rhs,
Index k,
const ProductScalar& alpha)
const {
475 const Index N = rows();
478 const bool unitAlpha = alpha == ProductScalar(1);
479 for (
Index i = 0; i < N; ++i) {
480 ProductScalar acc(0);
481 for (
Index j = 0; j < N; ++j) acc +=
coeff(i, j) * rhs.coeff(j, k);
482 dst.coeffRef(i, k) += unitAlpha ? acc : ProductScalar(alpha * acc);
488 template <
typename Dest,
typename Rhs,
typename ProductScalar>
489 void directProduct(Dest& dst,
const Rhs& rhs,
const ProductScalar& alpha)
const {
490 for (
Index k = 0; k < rhs.cols(); ++k) directProductColumn(dst, rhs, k, alpha);
502 template <
typename Dest,
typename Rhs,
typename ProductScalar>
503 void applySymbol(Dest& dst,
const ComplexArray& s,
const Rhs& rhs,
const ProductScalar& alpha,
504 bool accumulate)
const {
506 const Index p2 = s.rows(), p1 = s.cols();
507 const bool padded = p2 != n2 || p1 != n1;
508 ComplexVector xc(rows());
509 ComplexArray X(p2, p1), Xn;
510 for (
Index k = 0; k < rhs.cols(); ++k) {
514 xc = rhs.col(k).template cast<Complex>();
517 X.topLeftCorner(n2, n1) = xc.reshaped(n2, n1);
519 X = xc.reshaped(n2, n1);
522 X.array() *= s.array();
524 if (padded) Xn = X.topLeftCorner(n2, n1);
525 const auto out = internal::structured_scalar_part_impl<ProductScalar>::run((padded ? Xn : X).reshaped());
527 dst.col(k) += alpha * out;
529 dst.col(k) = alpha * out;
537 template <
bool Inverse>
538 void transform2(ComplexArray& X)
const {
539 auto&& fft = internal::structured_fft_engine<RealScalar>();
540 const Index n2 = X.rows(), n1 = X.cols();
545 ComplexVector tmp, rowv;
547 for (
Index k1 = 0; k1 < n1; ++k1) {
548 EIGEN_IF_CONSTEXPR (Inverse) {
549 fft.inv(tmp, X.col(k1), n2);
551 fft.fwd(tmp, X.col(k1), n2);
557 for (
Index k2 = 0; k2 < n2; ++k2) {
558 rowv = X.row(k2).transpose();
559 EIGEN_IF_CONSTEXPR (Inverse) {
560 fft.inv(tmp, rowv, n1);
562 fft.fwd(tmp, rowv, n1);
564 X.row(k2) = tmp.transpose();
570 void fft2(ComplexArray& X)
const { transform2<false>(X); }
573 void ifft2(ComplexArray& X)
const { transform2<true>(X); }
576 GeneratorType generatorFromSymbol(ComplexArray
symbol)
const {
578 GeneratorType
generator = internal::structured_scalar_part_impl<Scalar>::run(
symbol);
583 ComplexArray computeSymbol()
const {
584 ComplexArray s = m_g.template cast<Complex>();
592 template <
typename MatType>
593 static MatType reverse2(
const MatType& M) {
594 const Index r = M.rows(), c = M.cols();
596 if (M.size() == 0)
return R;
598 if (c > 1) R.row(0).tail(c - 1) = M.row(0).tail(c - 1).reverse();
599 if (r > 1) R.col(0).tail(r - 1) = M.col(0).tail(r - 1).reverse();
600 if (r > 1 && c > 1) R.bottomRightCorner(r - 1, c - 1) = M.bottomRightCorner(r - 1, c - 1).reverse();
609 void fourierColumn(ComplexMatrix& F,
Index f1,
Index f2,
Index dstCol)
const {
611 const RealScalar scale = RealScalar(1) / numext::sqrt(RealScalar(rows()));
612 ComplexVector w2(n2);
614 for (
Index i2 = 0; i2 < n2; ++i2) {
615 w2[i2] = std::polar(scale, RealScalar(2 * EIGEN_PI) * RealScalar(jf) / RealScalar(n2));
617 if (jf >= n2) jf -= n2;
620 for (
Index b1 = 0; b1 < n1; ++b1) {
621 const Complex w1 = std::polar(RealScalar(1), RealScalar(2 * EIGEN_PI) * RealScalar(bf) / RealScalar(n1));
622 F.col(dstCol).segment(b1 * n2, n2) = w1 * w2;
624 if (bf >= n1) bf -= n1;
629 static Scalar toScalar(
const Complex& z, std::true_type ) {
return numext::real(z); }
630 static Scalar toScalar(
const Complex& z, std::false_type ) {
return z; }
633 ComplexArray m_symbol;
636 ComplexArray m_prodSymbol;