使用数组填充对称矩阵 [英] Fill a symmetric matrix using an array

查看:167
本文介绍了使用数组填充对称矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a symmetric matrix n x n matrix and fill it using a n*(n+1)/2 dimension array using the boost library in c++.

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

So far, I am able to create the matrix, and fill it with random values using the following code

#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中的值填充对称矩阵的上部(或下部).所以输出的上对称矩阵应该是

What I am trying to do is fill the upper (or lower) part of the symmetric matrix using the values from the array filler. So the output upper symmetric matrix should be

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

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

Any idea on how to do that?

推荐答案

我只是通过保留一个从头到尾遍历填充符的迭代器来对此进行了简化:

I'd simplify this a bit by just keeping an iterator that traverses filler from start to end:

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++;

打印: 在Coliru上直播

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

I'd personally suggest creating a helper function like:

在魔盒上直播 >

#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))

注意:大小检查使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆