| 2344 | |
| 2345 | |
| 2346 | shared_ptr<BaseMatrix> CreateSparseMatrixInverse(shared_ptr<const BaseSparseMatrix> baseA, |
| 2347 | shared_ptr<BitArray> subset, |
| 2348 | shared_ptr<const Array<int>> clusters) |
| 2349 | { |
| 2350 | if (baseA->invcreator) |
| 2351 | return baseA->invcreator(const_pointer_cast<BaseSparseMatrix>(baseA), subset, clusters); |
| 2352 | |
| 2353 | auto index = BaseMatrix::invcreators.CheckIndex(baseA->GetInverseType()); |
| 2354 | if(index != -1) |
| 2355 | return BaseMatrix::invcreators[index](const_pointer_cast<BaseSparseMatrix>(baseA), subset, clusters); |
| 2356 | |
| 2357 | auto [dynH, dynW] = baseA->EntrySizes(); |
| 2358 | |
| 2359 | shared_ptr<BaseMatrix> inv = nullptr; |
| 2360 | |
| 2361 | if ( dynH == dynW ) |
| 2362 | { |
| 2363 | Switch<MAX_SYS_DIM>(dynH - 1, [&](auto HM) |
| 2364 | { |
| 2365 | constexpr int H = HM + 1; |
| 2366 | |
| 2367 | if constexpr(H == 1) |
| 2368 | { |
| 2369 | if ( auto A = dynamic_pointer_cast<const SparseMatrix<double>>(baseA) ) |
| 2370 | { |
| 2371 | inv = InverseSparseMatrixTM<double>(A, subset, clusters); |
| 2372 | } |
| 2373 | else |
| 2374 | { |
| 2375 | inv = InverseSparseMatrixTM<Complex>(dynamic_pointer_cast<const SparseMatrix<Complex>>(baseA), subset, clusters); |
| 2376 | } |
| 2377 | } |
| 2378 | else |
| 2379 | { |
| 2380 | if (auto A = dynamic_pointer_cast<const SparseMatrix<Mat<H, H, double>>>(baseA)) |
| 2381 | { |
| 2382 | inv = InverseSparseMatrixTM(A, subset, clusters); |
| 2383 | } |
| 2384 | else |
| 2385 | { |
| 2386 | inv = InverseSparseMatrixTM(dynamic_pointer_cast<const SparseMatrix<Mat<H, H, Complex>>>(baseA), subset, clusters); |
| 2387 | } |
| 2388 | } |
| 2389 | }); |
| 2390 | |
| 2391 | if ( inv == nullptr ) |
| 2392 | { |
| 2393 | throw Exception("SparseMatrix inverse not available for block-size " + std::to_string(dynH) + " matrix"); |
| 2394 | } |
| 2395 | } |
| 2396 | else |
| 2397 | { |
| 2398 | throw Exception("SparseMatrix inverse not available for non-square " + std::to_string(dynH) + "x" + std::to_string(dynW) + " matrix"); |
| 2399 | } |
| 2400 | return inv; |
| 2401 | } |
| 2402 | |
| 2403 |
no test coverage detected