Boost logo

Boost :

From: Joerg Walter (jhr.walter_at_[hidden])
Date: 2002-12-02 03:57:44


----- Original Message -----
From: "Stas Fomin" <stas_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Saturday, November 30, 2002 6:57 PM
Subject: [boost] UBLAS: preserving matrix resize.

> I have to make matrix resize with preserving of the content of the matrix.
> (add rows, columns...)
>
>
> I try to do this using matrix ranges:

[snip sample]

> Questions:
> 1. Why this do not work? (see examples from
> http://lists.boost.org/MailArchives/boost/msg31729.php)

I've just tested the following program under GCC 3.1:

----------
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

namespace ublas = boost::numeric::ublas;

typedef ublas::matrix<double> Mat;

int main() {
  Mat A(2,2);
  A(0,0)=11;
  A(0,1)=12;
  A(1,0)=21;
  A(1,1)=22;

  Mat _A(4,4);
  _A.project(ublas::range(0,A.size1()),
ublas::range(0,A.size2())).assign(A);
  /* or ublas::project(_A, ublas::range(0,A.size1()),
ublas::range(0,A.size2()))).assign(A); */
  std::cout << A << std::endl;
  std::cout << _A << std::endl;
  A.swap(_A);
  std::cout << A << std::endl;
  std::cout << _A << std::endl;

  Mat B(2,2);
  B(0,0)=11;
  B(0,1)=12;
  B(1,0)=21;
  B(1,1)=22;

  Mat _B(4,4);
  ublas::matrix_range<Mat> _Br(_B, ublas::range(0,B.size1()),
ublas::range(0,B.size2()));
  _Br.assign(B);
  std::cout << B << std::endl;
  std::cout << _B << std::endl;
  B.swap(_B);
  std::cout << B << std::endl;
  std::cout << _B << std::endl;
  return 0;
}

----------

Output as I'd expect:

----------
[2,2]((11,12),(21,22))
[4,4]((11,12,0,0),(21,22,0,0),(0,0,0,0),(0,0,0,0))
[4,4]((11,12,0,0),(21,22,0,0),(0,0,0,0),(0,0,0,0))
[2,2]((11,12),(21,22))
[2,2]((11,12),(21,22))
[4,4]((11,12,0,0),(21,22,0,0),(0,0,0,0),(0,0,0,0))
[4,4]((11,12,0,0),(21,22,0,0),(0,0,0,0),(0,0,0,0))
[2,2]((11,12),(21,22))
----------

I have to admit that I've checked against the current CVS version only.
We've recently fixed some temporary lifetime issues w.r.t. project()
functions. If these cause your problem and you can't afford to upgrade to
the CVS version, maybe the second implementation in the test program can
help.

> 2. How to do "preserving matrix resize" efficiently?

I.e. without copying/swapping? Why don't you start with your final matrix
referencing the smaller sub matrices via matrix_range<>?

Regards

Joerg


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk