Difference between revisions of "cpp/numeric/linalg"
From cppreference.com
m (→Example: restore to non-runnable state.) |
m (+enwiki.) |
||
(2 intermediate revisions by one user not shown) | |||
Line 2: | Line 2: | ||
{{cpp/numeric/linalg/navbar}} | {{cpp/numeric/linalg/navbar}} | ||
− | Basic linear algebra algorithms are based on the dense Basic Linear Algebra Subroutines (BLAS) which corresponds to a subset of the [http://www.netlib.org/blas/blast-forum/blas-report.pdf BLAS Standard]. These algorithms that access the elements of arrays view those elements through {{lc|std::mdspan}} representing vector or matrix. | + | Basic linear algebra algorithms are based on the dense Basic Linear Algebra Subroutines ({{enwiki|Basic Linear Algebra Subprograms|BLAS}}) which corresponds to a subset of the [http://www.netlib.org/blas/blast-forum/blas-report.pdf BLAS Standard]. These algorithms that access the elements of arrays view those elements through {{lc|std::mdspan}} representing vector or matrix. |
The BLAS algorithms are categorized into three sets of operations called ''levels'', which generally correspond to the degree of the polynomial in the complexities of algorithms: | The BLAS algorithms are categorized into three sets of operations called ''levels'', which generally correspond to the degree of the polynomial in the complexities of algorithms: | ||
− | * BLAS 1: All algorithms with {{lc|std::mdspan}} parameters perform a count of {{lc|std::mdspan}} array accesses and arithmetic operations that are linear in the maximum product of extents of any {{lc|std::mdspan}} parameter. These algorithms contain ''vector'' operations such as dot products, norms, and vector addition. | + | * {{enwiki|Basic Linear Algebra Subprograms#Level 1|BLAS 1}}: All algorithms with {{lc|std::mdspan}} parameters perform a count of {{lc|std::mdspan}} array accesses and arithmetic operations that are linear in the maximum product of extents of any {{lc|std::mdspan}} parameter. These algorithms contain ''vector'' operations such as dot products, norms, and vector addition. |
− | * BLAS 2: All algorithms have general complexity in quadratic time. These algorithms contain ''matrix-vector'' operations such as matrix-vector multiplications and a solver of the triangular linear system. | + | * {{enwiki|Basic Linear Algebra Subprograms#Level 2|BLAS 2}}: All algorithms have general complexity in quadratic time. These algorithms contain ''matrix-vector'' operations such as matrix-vector multiplications and a solver of the triangular linear system. |
− | * BLAS 3: All algorithms have general complexity in cubic time. These algorithms contain ''matrix-matrix'' operations such as matrix-matrix multiplications and a solver of the multiple triangular linear systems. | + | * {{enwiki|Basic Linear Algebra Subprograms#Level 3|BLAS 3}}: All algorithms have general complexity in cubic time. These algorithms contain ''matrix-matrix'' operations such as matrix-matrix multiplications and a solver of the multiple triangular linear systems. |
{{dsc begin}} | {{dsc begin}} | ||
Line 88: | Line 88: | ||
#include <linalg> | #include <linalg> | ||
#include <mdspan> | #include <mdspan> | ||
− | |||
#include <numeric> | #include <numeric> | ||
+ | #include <vector> | ||
int main() | int main() | ||
Line 95: | Line 95: | ||
constexpr std::size_t N = 40; | constexpr std::size_t N = 40; | ||
std::vector<double> x_vec(N); | std::vector<double> x_vec(N); | ||
− | std::ranges::iota(x_vec | + | std::ranges::iota(x_vec, 0); |
std::mdspan x(x_vec.data(), N); | std::mdspan x(x_vec.data(), N); |
Latest revision as of 13:00, 22 August 2024
Basic linear algebra algorithms are based on the dense Basic Linear Algebra Subroutines (BLAS) which corresponds to a subset of the BLAS Standard. These algorithms that access the elements of arrays view those elements through std::mdspan representing vector or matrix.
The BLAS algorithms are categorized into three sets of operations called levels, which generally correspond to the degree of the polynomial in the complexities of algorithms:
- BLAS 1: All algorithms with std::mdspan parameters perform a count of std::mdspan array accesses and arithmetic operations that are linear in the maximum product of extents of any std::mdspan parameter. These algorithms contain vector operations such as dot products, norms, and vector addition.
- BLAS 2: All algorithms have general complexity in quadratic time. These algorithms contain matrix-vector operations such as matrix-vector multiplications and a solver of the triangular linear system.
- BLAS 3: All algorithms have general complexity in cubic time. These algorithms contain matrix-matrix operations such as matrix-matrix multiplications and a solver of the multiple triangular linear systems.
In-place transformations | ||
Defined in header
<linalg> | ||
Defined in namespace
std::linalg | ||
(C++26) |
std::mdspan accessor policy whose reference represents the product of a scaling factor that is fixed and its nested std::mdspan accessor's reference (class template) | |
(C++26) |
std::mdspan accessor policy whose reference represents the complex conjugate of its nested std::mdspan accessor's reference (class template) | |
(C++26) |
std::mdspan layout mapping policy that swaps the rightmost two indices, extents, and strides of any unique layout mapping policy (class template) | |
(C++26) |
returns a new read-only std::mdspan computed by the elementwise product of the scaling factor and the corresponding elements of the given std::mdspan (function template) | |
(C++26) |
returns a new read-only std::mdspan whose elements are the complex conjugates of the corresponding elements of the given std::mdspan (function template) | |
(C++26) |
returns a new std::mdspan representing the transpose of the input matrix by the given std::mdspan (function template) | |
(C++26) |
returns a conjugate transpose view of an object (function template) | |
BLAS 1 functions | ||
Defined in header
<linalg> | ||
Defined in namespace
std::linalg | ||
(C++26) |
generates plane rotation (function template) | |
(C++26) |
applies plane rotation to vectors (function template) | |
(C++26) |
swaps all corresponding elements of matrix or vector (function template) | |
(C++26) |
overwrites matrix or vector with the result of computing the elementwise multiplication by a scalar (function template) | |
(C++26) |
copies elements of one matrix or vector into another (function template) | |
(C++26) |
adds vectors or matrices elementwise (function template) | |
(C++26) |
returns nonconjugated dot product of two vectors (function template) | |
(C++26) |
returns conjugated dot product of two vectors (function template) | |
(C++26) |
returns scaled sum of squares of the vector elements (function template) | |
(C++26) |
returns Euclidean norm of a vector (function template) | |
(C++26) |
returns sum of absolute values of the vector elements (function template) | |
(C++26) |
returns index of maximum absolute value of vector elements (function template) | |
(C++26) |
returns Frobenius norm of a matrix (function template) | |
(C++26) |
returns one norm of a matrix (function template) | |
(C++26) |
returns infinity norm of a matrix (function template) | |
BLAS 2 functions | ||
Defined in header
<linalg> | ||
Defined in namespace
std::linalg | ||
(C++26) |
computes matrix-vector product (function template) | |
computes symmetric matrix-vector product (function template) | ||
computes Hermitian matrix-vector product (function template) | ||
computes triangular matrix-vector product (function template) | ||
solves a triangular linear system (function template) | ||
(C++26) |
performs nonsymmetric nonconjugated rank-1 update of a matrix (function template) | |
(C++26) |
performs nonsymmetric conjugated rank-1 update of a matrix (function template) | |
performs rank-1 update of a symmetric matrix (function template) | ||
performs rank-1 update of a Hermitian matrix (function template) | ||
performs rank-2 update of a symmetric matrix (function template) | ||
performs rank-2 update of a Hermitian matrix (function template) | ||
BLAS 3 functions | ||
Defined in header
<linalg> | ||
Defined in namespace
std::linalg | ||
(C++26) |
computes matrix-matrix product (function template) | |
(C++26) |
computes symmetric matrix-matrix product (function template) | |
(C++26) |
computes Hermitian matrix-matrix product (function template) | |
computes triangular matrix-matrix product (function template) | ||
performs rank-k update of a symmetric matrix (function template) | ||
performs rank-k update of a Hermitian matrix (function template) | ||
performs rank-2k update of a symmetric matrix (function template) | ||
performs rank-2k update of a Hermitian matrix (function template) | ||
solves multiple triangular linear systems (function template) | ||
Helper items | ||
Defined in header
<linalg> | ||
Defined in namespace
std::linalg | ||
describe the order of elements in an std::mdspan with linalg::layout_blas_packed layout (tag) | ||
specify whether algorithms and other users of a matrix should access the upper triangle or lower triangle of the matrix (tag) | ||
specify whether algorithms should access diagonal entries of the matrix (tag) | ||
(C++26) |
std::mdspan layout mapping policy that represents a square matrix that stores only the entries in one triangle, in a packed contiguous format (class template) |
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_linalg |
202311L | (C++26) | Basic linear algebra algorithms |
[edit] Example
Run this code
#include <cassert> #include <cstddef> #include <execution> #include <linalg> #include <mdspan> #include <numeric> #include <vector> int main() { constexpr std::size_t N = 40; std::vector<double> x_vec(N); std::ranges::iota(x_vec, 0); std::mdspan x(x_vec.data(), N); // x[i] *= 2.0, executed sequentially std::linalg::scale(2.0, x); // x[i] *= 3.0, executed in parallel std::linalg::scale(std::execution::par_unseq, 3.0, x); for (std::size_t i{}; i != N; ++i) assert(x[i] == 6.0 * static_cast<double>(i)); }