Fill a symmetric matrix using an array

旧街凉风 提交于 2019-12-11 08:48:27

问题


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

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?


回答1:


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

Prints: Live On Coliru

I'd personally suggest creating a helper function like:

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";
}

Prints

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

Note: the size checks use the series expansion for 1 + ... + n and the inverse of that: n = 1/2 (sqrt(8 x + 1) - 1)



来源:https://stackoverflow.com/questions/48911543/fill-a-symmetric-matrix-using-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!