最終更新日時(UTC):
が更新

履歴 編集

function template
<linalg>

std::linalg::add(C++26)

namespace std::linalg {
  template<in-object InObj1,
           in-object InObj2,
           out-object OutObj>
  void add(InObj1 x,
           InObj2 y,
           OutObj z);               // (1)

  template<class ExecutionPolicy,
           in-object InObj1,
           in-object InObj2,
           out-object OutObj>
  void add(ExecutionPolicy&& exec,
           InObj1 x,
           InObj2 y,
           OutObj z);               // (2)
}

概要

同じサイズの行列またはベクトルのxyzに対して、$x + y$ をzに代入する:

$$ z \leftarrow x + y $$

テンプレートパラメータ制約

  • xyzの次元が全て等しくなければならない。
    • x.rank() == y.rank() && y.rank() == z.rank()

適格要件

  • (1), (2): possibly-addable<InObj1, InObj2, OutObj>()true (行列またはベクトルのxyzの各次元の静的要素数が同じ)
  • (2): is_execution_policy<ExecutionPolicy>::valuetrue

事前条件

行列またはベクトルのxyzの各次元が同じであること。

効果

$x + y$ をzに代入する。

  • (1): 逐次実行する。
  • (2): 指定された実行ポリシーに応じて実行する。

戻り値

なし

備考

zxまたはyとしてもよい。

[注意] 処理系にあるコンパイラで確認していないため、間違っているかもしれません。

#include <cmath>
#include <execution>
#include <iostream>
#include <linalg>
#include <mdspan>
#include <vector>

template <class Vector>
void print(Vector v) {
  for (int i = 0; i < v.extent(0) - 1; ++i) {
    std::cout << v[i] << ", ";
  }
  std::cout << v[v.extent(0) - 1] << std::endl;
}

int main()
{
  constexpr size_t N = 3;

  std::vector<double> a_vec({1, 2, 3});
  std::mdspan a(a_vec.data(), N);

  std::vector<double> b_vec({4, 5, 6});
  std::mdspan b(b_vec.data(), N);

  std::vector<double> c_vec(N);
  std::mdspan c(c_vec.data(), N);

  // (1)
  std::linalg::add(a, b, c);
  print(c);

  // (2)
  std::linalg::add(std::execution::par, a, b, c);
  print(c);

  return 0;
}

出力

5, 7, 9
5, 7, 9

バージョン

言語

  • C++26

処理系

関連項目

参照