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)); }
233 template <
typename Rhs>
235 EIGEN_INTERNAL_ASSERT_SOLVER_RHS_MSG(*
this, b,
false,
"right-hand side has the wrong number of rows");
236 const Index N = rows();
237 const ComplexArray s =
symbol();
238 const RealArray mods = s.cwiseAbs();
239 const ComplexArray sinv = internal::structured_pinv_symbol(s, mods, internal::structured_rank_threshold(s, mods));
241 applySymbol(x, sinv, b.derived(), Scalar(1),
false);
252 const ComplexArray s =
symbol();
253 const RealArray mods = s.cwiseAbs();
254 return (!(mods.array() < internal::structured_rank_threshold(s, mods))).count();
262 ComplexArray sinv =
symbol().cwiseInverse();
263 const GeneratorType ginv = generatorFromSymbol(sinv);
264 const bool cache = rows() > internal::structured_direct_threshold();
265 return Bccb(ginv, cache ? sinv : ComplexArray(), cache ? computeProdSymbol(ginv) : ComplexArray());
277 const ComplexArray s =
symbol();
280 for (
Index k1 = 0; k1 < s.cols(); ++k1)
281 for (
Index k2 = 0; k2 < s.rows(); ++k2)
282 det = internal::balance_exponent(Complex(det * internal::balance_exponent(s(k2, k1), exponent)), exponent);
283 return toScalar(internal::apply_exponent(det, exponent), std::is_same<RealScalar, Scalar>());
299 ComplexMatrix F(N, N);
300 for (
Index f1 = 0; f1 < n1; ++f1)
301 for (
Index f2 = 0; f2 < n2; ++f2) fourierColumn(F, f1, f2, f1 * n2 + f2);
310 const RealVector mods = s.cwiseAbs();
311 const std::vector<Index> perm = internal::structured_svd_permutation(mods);
312 RealVector sv(s.size());
313 for (
Index t = 0; t < s.size(); ++t) sv[t] = mods[perm[t]];
323 const RealVector mods = s.cwiseAbs();
324 const std::vector<Index> perm = internal::structured_svd_permutation(mods);
325 ComplexMatrix U(N, N);
326 for (
Index t = 0; t < N; ++t) {
327 fourierColumn(U, perm[t] / n2, perm[t] % n2, t);
328 const RealScalar a = mods[perm[t]];
329 if (a > RealScalar(0)) U.col(t) *= s[perm[t]] / a;
340 const std::vector<Index> perm = internal::structured_svd_permutation(RealVector(s.cwiseAbs()));
341 ComplexMatrix V(N, N);
342 for (
Index t = 0; t < N; ++t) fourierColumn(V, perm[t] / n2, perm[t] % n2, t);
350 template <
typename Dest>
351 void evalTo(Dest& dst)
const {
352 assignTo<SetAssignment>(dst);
356 template <
typename Dest>
357 void addTo(Dest& dst)
const {
358 assignTo<AddAssignment>(dst);
362 template <
typename Dest>
363 void subTo(Dest& dst)
const {
364 assignTo<SubAssignment>(dst);
372 template <
typename Rhs>
380 template <
typename Dest,
typename Rhs,
typename ProductScalar>
381 void addProduct(Dest& dst,
const Rhs& rhs,
const ProductScalar& alpha)
const {
382 const Index N = rows();
383 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE_MSG(*
this, rhs,
"invalid product: dimensions do not match");
384 if (N <= internal::structured_direct_threshold()) {
385 directProduct(dst, rhs, alpha);
391 const ComplexArray& s = m_prodSymbol.size() > 0 ? m_prodSymbol : m_symbol;
392 applySymbol(dst, s, rhs, alpha,
true);
398 template <DenseAssignment Assignment,
typename Dest>
399 void assignTo(Dest& dst)
const {
401 for (
Index c1 = 0; c1 < n1; ++c1)
402 for (
Index j2 = 0; j2 < n2; ++j2) {
403 auto col = dst.col(c1 * n2 + j2);
404 for (
Index b1 = 0; b1 < n1; ++b1) {
406 if (k1 < 0) k1 += n1;
407 EIGEN_IF_CONSTEXPR (Assignment == SetAssignment) {
408 col.segment(b1 * n2, j2) = m_g.col(k1).tail(j2);
409 col.segment(b1 * n2 + j2, n2 - j2) = m_g.col(k1).head(n2 - j2);
410 }
else EIGEN_IF_CONSTEXPR (Assignment == AddAssignment) {
411 col.segment(b1 * n2, j2) += m_g.col(k1).tail(j2);
412 col.segment(b1 * n2 + j2, n2 - j2) += m_g.col(k1).head(n2 - j2);
414 col.segment(b1 * n2, j2) -= m_g.col(k1).tail(j2);
415 col.segment(b1 * n2 + j2, n2 - j2) -= m_g.col(k1).head(n2 - j2);
427 Bccb(
const GeneratorType& g,
const ComplexArray&
symbol,
const ComplexArray& prodSymbol)
428 : m_g(g), m_symbol(
symbol), m_prodSymbol(prodSymbol) {}
448 ComplexArray computeProdSymbol(
const GeneratorType& g)
const {
449 const Index n2 = g.rows(), n1 = g.cols();
450 const Index p2 = internal::fft_next_good_size(n2) == n2 ? n2 : internal::fft_next_good_size(2 * n2 - 1);
451 const Index p1 = internal::fft_next_good_size(n1) == n1 ? n1 : internal::fft_next_good_size(2 * n1 - 1);
452 if (p2 == n2 && p1 == n1)
return ComplexArray();
453 ComplexArray Ge = ComplexArray::Zero(p2, p1);
454 Ge.topLeftCorner(n2, n1) = g.template cast<Complex>();
455 if (p2 != n2) Ge.bottomLeftCorner(n2 - 1, n1) = g.bottomRows(n2 - 1).template cast<Complex>();
456 if (p1 != n1) Ge.topRightCorner(n2, n1 - 1) = g.rightCols(n1 - 1).template cast<Complex>();
457 if (p2 != n2 && p1 != n1)
458 Ge.bottomRightCorner(n2 - 1, n1 - 1) = g.bottomRightCorner(n2 - 1, n1 - 1).template cast<Complex>();
468 template <
typename Dest,
typename Rhs,
typename ProductScalar>
469 void directProductColumn(Dest& dst,
const Rhs& rhs,
Index k,
const ProductScalar& alpha)
const {
470 const Index N = rows();
473 const bool unitAlpha = alpha == ProductScalar(1);
474 for (
Index i = 0; i < N; ++i) {
475 ProductScalar acc(0);
476 for (
Index j = 0; j < N; ++j) acc +=
coeff(i, j) * rhs.coeff(j, k);
477 dst.coeffRef(i, k) += unitAlpha ? acc : ProductScalar(alpha * acc);
483 template <
typename Dest,
typename Rhs,
typename ProductScalar>
484 void directProduct(Dest& dst,
const Rhs& rhs,
const ProductScalar& alpha)
const {
485 for (
Index k = 0; k < rhs.cols(); ++k) directProductColumn(dst, rhs, k, alpha);
497 template <
typename Dest,
typename Rhs,
typename ProductScalar>
498 void applySymbol(Dest& dst,
const ComplexArray& s,
const Rhs& rhs,
const ProductScalar& alpha,
499 bool accumulate)
const {
501 const Index p2 = s.rows(), p1 = s.cols();
502 const bool padded = p2 != n2 || p1 != n1;
503 ComplexVector xc(rows());
504 ComplexArray X(p2, p1), Xn;
505 for (
Index k = 0; k < rhs.cols(); ++k) {
509 xc = rhs.col(k).template cast<Complex>();
512 X.topLeftCorner(n2, n1) = xc.reshaped(n2, n1);
514 X = xc.reshaped(n2, n1);
517 X.array() *= s.array();
519 if (padded) Xn = X.topLeftCorner(n2, n1);
520 const auto out = internal::structured_scalar_part_impl<ProductScalar>::run((padded ? Xn : X).reshaped());
522 dst.col(k) += alpha * out;
524 dst.col(k) = alpha * out;
532 template <
bool Inverse>
533 void transform2(ComplexArray& X)
const {
534 auto&& fft = internal::structured_fft_engine<RealScalar>();
535 const Index n2 = X.rows(), n1 = X.cols();
540 ComplexVector tmp, rowv;
542 for (
Index k1 = 0; k1 < n1; ++k1) {
543 EIGEN_IF_CONSTEXPR (Inverse) {
544 fft.inv(tmp, X.col(k1), n2);
546 fft.fwd(tmp, X.col(k1), n2);
552 for (
Index k2 = 0; k2 < n2; ++k2) {
553 rowv = X.row(k2).transpose();
554 EIGEN_IF_CONSTEXPR (Inverse) {
555 fft.inv(tmp, rowv, n1);
557 fft.fwd(tmp, rowv, n1);
559 X.row(k2) = tmp.transpose();
565 void fft2(ComplexArray& X)
const { transform2<false>(X); }
568 void ifft2(ComplexArray& X)
const { transform2<true>(X); }
571 GeneratorType generatorFromSymbol(ComplexArray
symbol)
const {
573 GeneratorType
generator = internal::structured_scalar_part_impl<Scalar>::run(
symbol);
578 ComplexArray computeSymbol()
const {
579 ComplexArray s = m_g.template cast<Complex>();
587 template <
typename MatType>
588 static MatType reverse2(
const MatType& M) {
589 const Index r = M.rows(), c = M.cols();
591 if (M.size() == 0)
return R;
593 if (c > 1) R.row(0).tail(c - 1) = M.row(0).tail(c - 1).reverse();
594 if (r > 1) R.col(0).tail(r - 1) = M.col(0).tail(r - 1).reverse();
595 if (r > 1 && c > 1) R.bottomRightCorner(r - 1, c - 1) = M.bottomRightCorner(r - 1, c - 1).reverse();
604 void fourierColumn(ComplexMatrix& F,
Index f1,
Index f2,
Index dstCol)
const {
606 const RealScalar scale = RealScalar(1) / numext::sqrt(RealScalar(rows()));
607 ComplexVector w2(n2);
609 for (
Index i2 = 0; i2 < n2; ++i2) {
610 w2[i2] = std::polar(scale, RealScalar(2 * EIGEN_PI) * RealScalar(jf) / RealScalar(n2));
612 if (jf >= n2) jf -= n2;
615 for (
Index b1 = 0; b1 < n1; ++b1) {
616 const Complex w1 = std::polar(RealScalar(1), RealScalar(2 * EIGEN_PI) * RealScalar(bf) / RealScalar(n1));
617 F.col(dstCol).segment(b1 * n2, n2) = w1 * w2;
619 if (bf >= n1) bf -= n1;
624 static Scalar toScalar(
const Complex& z, std::true_type ) {
return numext::real(z); }
625 static Scalar toScalar(
const Complex& z, std::false_type ) {
return z; }
628 ComplexArray m_symbol;
631 ComplexArray m_prodSymbol;