TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
det.hpp
Go to the documentation of this file.
1// Copyright (c) 2019--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "../basic_array.hpp"
15#include "../concepts.hpp"
16#include "../exceptions.hpp"
17#include "../lapack/getrf.hpp"
20#include "../mem/policies.hpp"
21#include "../traits.hpp"
22
23namespace nda::linalg {
24
29
46 template <Matrix M>
48 auto det_in_place(M &&m) { // NOLINT (temporary views are allowed here)
49 EXPECTS(is_matrix_square(m));
50
51 // use optimized routines for small matrices, otherwise use LAPACK routine
52 auto const dim = m.shape()[0];
53 if (dim == 1) {
54 return m(0, 0);
55 } else if (dim == 2) {
56 return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0);
57 } else if (dim == 3) {
58 return m(0, 0) * (m(1, 1) * m(2, 2) - m(1, 2) * m(2, 1)) //
59 - m(0, 1) * (m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0)) //
60 + m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0)); //
61 } else if (dim > 3) {
62 // LU factorization with getrf
63 auto ipiv = vector<int, sso<100>>(dim);
64 int info = nda::lapack::getrf(m, ipiv);
65 if (info < 0) NDA_RUNTIME_ERROR << "Error in nda::linalg::det_in_place: getrf routine failed: info = " << info;
66
67 // calculate the determinant from the LU decomposition
68 auto det = get_value_t<M>{1};
69 int n_flips = 0;
70 for (int i = 0; i < dim; i++) {
71 det *= m(i, i);
72 // count the number of column interchanges performed by getrf
73 if (ipiv(i) != i + 1) ++n_flips;
74 }
75
76 return ((n_flips % 2 == 1) ? -det : det);
77 } else {
78 // empty matrix
79 return get_value_t<M>{1};
80 }
81 }
82
93 template <Matrix M>
95 auto det(M const &m) {
96 EXPECTS(is_matrix_square(m));
97 auto m_copy = make_regular(m);
98 return det_in_place(m_copy);
99 }
100
102
103} // namespace nda::linalg
104
105namespace nda::clef {
106
107 // Make nda::linalg::det visible to lazy function calls.
108 using nda::linalg::det;
109
115
116} // namespace nda::clef
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides the generic class for arrays.
Provides basic functions to create and manipulate arrays and views.
Provides concepts for the nda library.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
Provides a generic interface to the LAPACK getrf routine.
decltype(auto) make_regular(A &&a)
Make a given object regular.
bool is_matrix_square(A const &a, bool print_error=false)
Check if a given matrix is square, i.e. if the first dimension has the same extent as the second dime...
basic_array< ValueType, 1, C_layout, 'V', ContainerPolicy > vector
Alias template of an nda::basic_array with rank 1 and a 'V' algebra.
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:116
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition traits.hpp:182
#define CLEF_MAKE_FNT_LAZY(name)
Macro to make any function lazy, i.e. accept lazy arguments and return a function call expression nod...
Definition make_lazy.hpp:89
auto det(A &&...__a)
Lazy version of nda::linalg::det.
Definition det.hpp:114
int getrf(A &&a, IPIV &&ipiv)
Interface to the LAPACK getrf routine.
Definition getrf.hpp:58
auto det(M const &m)
Compute the determinant of an matrix .
Definition det.hpp:95
auto det_in_place(M &&m)
Compute the determinant of an matrix .
Definition det.hpp:48
static constexpr bool have_host_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Host.
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:92
Provides functions to create and manipulate matrices, i.e. arrays/view with 'M' algebra.
Defines various memory handling policies.
Provides type traits for the nda library.