- [mathjax enable]
- linalg[meta header]
- function template[meta id-type]
- std::linalg[meta namespace]
- cpp26[meta cpp]
namespace std::linalg {
template<in-vector InVec>
typename InVec::size_type
vector_idx_abs_max(InVec v); // (1)
template<class ExecutionPolicy, in-vector InVec>
typename InVec::size_type
vector_idx_abs_max(ExecutionPolicy&& exec,
InVec v); // (2)
}- in-vector[link inout-vector.md]
ベクトルの各成分の絶対値が最大となるインデックスを計算する。
- (1): 逐次実行する。
- (2): 指定された実行ポリシーに応じて実行する。
- (1), (2):
Tをdecltype(abs-if-needed(real-if-needed(declval<typename InVec::value_type>())) + abs-if-needed(imag-if-needed(declval<typename InVec::value_type>())))とすると、declval<T>() < declval<T>()が有効な式であること。 - (2):
is_execution_policy<ExecutionPolicy>::valueがtrue
-
(1), (2):
Nをvの次元v.extent(0)とすると、以下を返す。- もし
Nが0ならstd::numeric_limits<typename InVec::size_type>::max() - そうでない場合、もし
InVec::value_typeが算術型なら絶対値が最大の最初のvの成分のインデックス。つまり、
$$ \mathop{\mathrm{argmax}}\limits_{i = 0, \dots, N - 1} |\verb|v[|i\verb|]|| $$ - そうでない場合、
$$ \mathop{\mathrm{argmax}}\limits_{i = 0, \dots, N - 1}\left{|\mathrm{Re}(\verb|v[|i\verb|]|)| + |\mathrm{Im}(\verb|v[|i\verb|]|)|\right} $$ - もし
[注意] 処理系にあるコンパイラで確認していないため、間違っているかもしれません。
#include <array>
#include <cmath>
#include <execution>
#include <iostream>
#include <linalg>
#include <mdspan>
int main()
{
constexpr size_t N = 4;
std::array<double, N> vec;
std::mdspan v(vec.data(), N);
for(int i = 0; i < v.extent(0); ++i) {
v[i] = std::pow(-1.0, i) / (i + 1);
}
std::cout << std::linalg::vector_idx_abs_max(v) << '\n' // (1)
<< std::linalg::vector_idx_abs_max(std::execution::par, v) << '\n'; // (2)
return 0;
}- std::linalg::vector_idx_abs_max[color ff0000]
0
0
- C++26
- Clang: ??
- GCC: ??
- ICC: ??
- Visual C++: ??