使用数组填充对称矩阵

时间:2018-02-21 17:03:17

标签: c++ arrays matrix boost symmetric

我正在尝试使用n x n中的n*(n+1)/2库,使用boost维度数组创建对称矩阵c++矩阵。

到目前为止,我能够创建矩阵,并使用以下代码用随机值填充

#include <iostream>
#include <fstream>    
#include </usr/include/boost/numeric/ublas/matrix.hpp>
#include </usr/include/boost/numeric/ublas/matrix_sparse.hpp>
#include </usr/include/boost/numeric/ublas/symmetric.hpp>
#include </usr/include/boost/numeric/ublas/io.hpp>

using namespace std;



int test_boost () {
    using namespace boost::numeric::ublas;
    symmetric_matrix<double, upper> m_sym (3, 3);

    double filler[6] = {0, 1, 2, 3, 4, 5};        

    for (unsigned i = 0; i < m_sym.size1 (); ++ i)
        for (unsigned j = i; j < m_sym.size2 (); ++ j)
            m_sym (i, j) = filler[i+j*m_sym.size1()];

    std::cout << m_sym << std::endl;
    return 0;
}

我要做的是使用数组filler中的值填充对称矩阵的上部(或下部)。所以输出上对称矩阵应该是

         |      0    |      1    |      2    |
------------------------------------------------
   0 |          0           1           3    
   1 |          1           2           4
   2 |          3           4           5

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

我通过保持一个从开始到结束遍历填充的迭代器来简化这一点:

symmetric_matrix<double, upper> m_sym (3, 3);

double filler[6] = {0, 1, 2, 3, 4, 5};        

assert(m_sym.size1() == m_sym.size2());

double const* in = std::begin(filler);
for (size_t i = 0; i < m_sym.size1(); ++ i)
    for (size_t j = 0; j <= i && in != std::end(filler); ++ j)
        m_sym (i, j) = *in++;

打印: Live On Coliru

我个人建议创建一个帮助函数,如:

<强> Live On Wandbox

#include <iostream>
#include <fstream>    
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/symmetric.hpp>
#include <boost/numeric/ublas/io.hpp>

namespace bnu = boost::numeric::ublas;

template <typename T = double>
bnu::symmetric_matrix<T, bnu::upper> make_symmetric(std::initializer_list<T> filler) {
    size_t n = (sqrt(8*filler.size() + 1) - 1)/2;
    assert((n*(n+1))/2 == filler.size());

    bnu::symmetric_matrix<T, bnu::upper> result(n, n);

    auto in = std::begin(filler);
    for (size_t i = 0; i < result.size1(); ++ i)
        for (size_t j = 0; j <= i && in != std::end(filler); ++ j)
            result (i, j) = *in++;

    return result;
}

int main() {
    std::cout << make_symmetric({0,1,2}) << "\n";
    std::cout << make_symmetric({0,1,2,3,4,5}) << "\n";
    std::cout << make_symmetric({0,1,2,3,4,5,6,7,8,9}) << "\n";
}

打印

[2,2]((0,1),(1,2))
[3,3]((0,1,3),(1,2,4),(3,4,5))
[4,4]((0,1,3,6),(1,2,4,7),(3,4,5,8),(6,7,8,9))
  

注意:尺寸检查使用series expansion for 1 + ... + n及其相反的结果:n = 1/2 (sqrt(8 x + 1) - 1)