uBlas - diagonal_matrix
Dear all, I can use a banded_matrix to represent a diagonal matrix, but while some math for a diagonal matrix is a little bit easier than for a rectangular matrix, it would be nice to have such a type. In my case: I use a SVD A = U * S * V.T to calculate the moore-penrose-inverse A+. Therfore I use A+ = V * S+ * U.T (i do not remember exact, but focus on S+) with S+ as the moore-penrose-inverse of S which is very easy to calculate. but to use something like pinv(S) , do I need a symmetric_matrix type? Or can I implement a pinv() for a banded_matrix wich must fulfil the constraint of being diagonal? Thanks a lot Patrick
Answering myself,
without a symmetric type I can just check for a diagonal matrix inside the
pinv function and branch, see code below.
Are there some recommendations for namespaces for functions like "pinv" ?
Inside the loop, shall I use size_type?
Thx for your patience,
Patrick
template <class T>
boost::numeric::ublas::banded_matrix<T> pinv(const
boost::numeric::ublas::banded_matrix<T>& m)
{
if (m.lower()==0 && m.upper()==0) // just for reading
{
boost::numeric::ublas::banded_matrix<T> rm(m.size2(),m.size1());
for (size_t i=0;i
Dear all,
I can use a banded_matrix to represent a diagonal matrix, but while some math for a diagonal matrix is a little bit easier than for a rectangular matrix, it would be nice to have such a type.
In my case:
I use a SVD A = U * S * V.T to calculate the moore-penrose-inverse A+.
Therfore I use A+ = V * S+ * U.T (i do not remember exact, but focus on S+) with S+ as the moore-penrose-inverse of S which is very easy to calculate.
but to use something like pinv(S) , do I need a symmetric_matrix type? Or can I implement a pinv() for a banded_matrix wich must fulfil the constraint of being diagonal?
Thanks a lot Patrick
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Hi Patrick, you wrote:
Answering myself,
That happens sometimes ;-)
without a symmetric type I can just check for a diagonal matrix inside the pinv function and branch, see code below.
As far as I've understood you're looking for a type safe diagonal variant of
banded_matrix. Would it be helpful, if we'd go to add something like
// Diagonal matrix class
template
Are there some recommendations for namespaces for functions like "pinv" ? Inside the loop, shall I use size_type?
I currently haven't enough time to look into a SVD implementation. So I'll cc this reply to http://groups.yahoo.com/group/ublas-dev. HTH, Joerg [snip]
Hello Joerg,
Hi Patrick,
without a symmetric type I can just check for a diagonal matrix inside the pinv function and branch, see code below.
That in fact is not true. Defining the interface for a moore-penrose-inverse function could look more like this (answering myself again ;-) ): diagonal_matrix<T> pinv(const diagonal_matrix<T>&); matrix<T> pinv(const matrix<T>&); but banded_matrix<T> pinv(const banded_matrix<T>&) is not ok, because the pinv of a banded_matrix<T> is a rectangular matrix<T>. So branching inside the function ist not nice. At all I think it is a nice example, that in a mathematical kind of view, there could be a huge difference between banded and diagonal.
As far as I've understood you're looking for a type safe diagonal variant of banded_matrix. Would it be helpful, if we'd go to add something like
it would be very nice to have something like that. But I think it is only
useful if there are more applications with a semantic mathematical meaning.
In my case the difference in the code is not huge:
1.) without diagonal_matrix:
boost::numeric::ublas::matrix<double> pinv(const
boost::numeric::ublas::matrix<double>& m)
{
using namespace boost::numeric::ublas;
matrix<double> U,V;
banded_matrix<double> S;
svd(m,U,S,V); // assuming we have one
for (size_t i=0;i // Diagonal matrix class
template // Construction and destruction
BOOST_UBLAS_INLINE
diagonal_matrix ():
matrix_type () {}
BOOST_UBLAS_INLINE
diagonal_matrix (std::size_t size1, std::size_t size2):
matrix_type (size1, size2) {}
BOOST_UBLAS_INLINE
~diagonal_matrix () {} // Assignment
BOOST_UBLAS_INLINE
diagonal_matrix &operator = (const diagonal_matrix &m) {
matrix_type::operator = (m);
return *this;
}
}; and the corresponding diagonal_adaptor to the code base? YES !!!! JUHU !!!
...but I think BOOST_UBLAS_INLINE
diagonal_matrix (std::size_t size1, std::size_t size2):
matrix_type (size1, size2) {} could be:
BOOST_UBLAS_INLINE
diagonal_matrix (std::size_t size):
matrix_type (size,size) {}
or both. Are there some recommendations for namespaces for functions like "pinv"
?
Inside the loop, shall I use size_type? I currently haven't enough time to look into a SVD implementation. So I'll
cc this reply to http://groups.yahoo.com/group/ublas-dev. The SVD is not important (i do not know what is inside either). Just keep in
mind that a SVD for a matrix<T>A produces 3 matrices:
matrix<T>U,diagonal_matrix<T>S, matrix<T>V. I should have pointed that out
more clearly. Sorry.
Aeh, is this thread OT here? Shall I just send these topics to the mailing
list uBlas-dev? I will make a reply to the forward as well.
Thank you, greetings
Patrick
participants (3)
-
jhr.walter@t-online.de
-
Patrick Kowalzick
-
Patrick Kowalzick