codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Dynamic allocation of sparse matrix using uBLAS


By : , Category : c++

The memory seems to be allocated dynamically anyway, so I can just do:

int n;
n = 10;

compressed_matrix<double> m (n,n,n);
ReLated :

As you have noticed yourself, boost::numeric::ublas::matrix doesn't resize itself automatically, like std::vector. You have to do the resizing manually before calling operator(), or write a function template that does the resizing for you as shown here:

namespace ublas = boost::numeric::ublas; //just a friendly alias!

template<typename T, typename U>
void assign(ublas::matrix<T>& m,std::size_t r,std::size_t c,U const& data)
{
    m.resize(std::max(m.size1(), r+1), std::max(m.size2(), c+1));
    m(r, c) = data;
}

Now you can do this:

int main () 
{
    ublas::matrix<double> m;

    assign(m, 0, 0, 9);  //m(0,0) = 9; 
    assign(m, 3, 2, 20); //m(3,2) = 20

    std::cout << m << std::endl; //prints: [4,3]((9,0,0),(0,0,0),(0,0,0),(0,0,20))
    return 0;
}

Online demo

Hope that helps.

I wonder at which part of the documentation you looked, because the official documentation contains a lot about the sparse matrix types. For example about batch insertion:

Batch insertion constructors:

form 1: sp_mat(locations, values, sort_locations = true)
form 2: sp_mat(locations, values, n_rows, n_cols, sort_locations = true, check_for_zeros = true)
form 3: sp_mat(add_values, locations, values, n_rows, n_cols, sort_locations = true, check_for_zeros = true)
form 4: sp_mat(rowind, colptr, values, n_rows, n_cols)

EDIT: Ah, I understand your question now; you want to preallocate the space necessary for element storage; I'm afraid there's no possibility to do so with the underlying sparse matrix storage format...

You must use at least 1 in your shape.

N = scipy.sparse.csc_matrix((4,1))

Which you can stack:

print scipy.sparse.hstack( (N,N) )
#<4x2 sparse matrix of type '<type 'numpy.float64'>'
#    with 0 stored elements in COOrdinate format>
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .