• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <linalg>

    std::linalg::symmetric_matrix_rank_2_update

    namespace std::linalg {
      template<in-vector InVec1,
               in-vector InVec2,
               possibly-packed-inout-matrix InOutMat,
               class Triangle>
      void symmetric_matrix_rank_2_update(
        InVec1 x,
        InVec2 y,
        InOutMat A,
        Triangle t); // (1)
    
      template<class ExecutionPolicy,
               in-vector InVec1,
               in-vector InVec2,
               possibly-packed-inout-matrix InOutMat,
               class Triangle>
      void symmetric_matrix_rank_2_update(
        ExecutionPolicy&& exec,
        InVec1 x,
        InVec2 y,
        InOutMat A,
        Triangle t); // (2)
    }
    

    概要

    対称かつ共役を取らないrank-2 updateを対称行列に行う。 引数tは対称行列の成分が上三角にあるのか、それとも下三角にあるのかを示す。

    • (1): AA+xyT+yxT
    • (2): (1)を指定された実行ポリシーで実行する。

    適格要件

    事前条件

    効果

    AA+xxT

    戻り値

    なし

    計算量

    O(x.extent(0)×y.extent(0))

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

    #include <array>
    #include <iostream>
    #include <linalg>
    #include <mdspan>
    #include <vector>
    
    template <class Matrix>
    void print_mat(const Matrix& A) {
      for(int i = 0; i < A.extent(0); ++i) {
        for(int j = 0; j < i; ++j) {
          std::cout << A[j, i] << ' ';
        }
        for(int j = i; j < A.extent(1) - 1; ++j) {
          std::cout << A[i, j] << ' ';
        }
        std::cout << A[i, A.extent(1) - 1] << '\n';
      }
    }
    
    template <class Matrix>
    void init_mat(Matrix& A) {
      for(int i = 0; i < A.extent(0); ++i) {
        for(int j = i; j < A.extent(1); ++j) {
          A[i,j] = A.extent(1) * i + j;
        }
      }
    }
    
    int main()
    {
      constexpr size_t N = 4;
    
      std::vector<double> A_vec(N * N);
      std::vector<double> x_vec(N);
      std::array<double, N> y_vec;
    
      std::mdspan<
        double,
        std::extents<size_t, N, N>,
        std::linalg::layout_blas_packed<
          std::linalg::upper_triangle_t,
          std::linalg::row_major_t>
      > A(A_vec.data());
      std::mdspan x(x_vec.data(), N);
      std::mdspan y(y_vec.data(), N);
    
      init_mat(A);
    
      for (int i = 0; i < x.extent(0); ++i) {
        x[i] = i;
      }
      for (int i = 0; i < y.extent(0); ++i) {
        y[i] = i + y.extent(0);
      }
    
      // (1)
      std::cout << "(1)\n";
      std::linalg::symmetric_matrix_rank_2_update(
        x,
        y,
        A,
        std::linalg::upper_triangle);
      print_mat(A);
    
      // (2)
      init_mat(A);
      std::cout << "(2)\n";
      std::linalg::symmetric_matrix_rank_2_update(
        std::execution::par,
        x,
        y,
        A,
        std::linalg::upper_triangle);
      print_mat(A);
    
      return 0;
    }
    

    出力

    (1)
    0 5 10 15
    5 15 22 29
    10 22 34 43
    15 29 43 57
    (2)
    0 5 10 15
    5 15 22 29
    10 22 34 43
    15 29 43 57
    

    バージョン

    言語

    • C++26

    処理系

    関連項目

    参照