Discussion:
[Pyublas] numpy_array<T> to bounded_array<T> conversion
Garth Coghlan
2009-07-02 05:07:14 UTC
Permalink
Hi all,

I have some functions that I want to wrap that look like this:
int foo(Matrix& A, Matrix& B);
where
typedef boost::numeric::ublas::matrix<double, column,
bounded_array<T> > Matrix;

I would like to wrap this function like so:
int fooWrap(numpy_matrix<double> A, numpy_matrix<double> B)
{
return foo(A, B);
}

However, the compiler doesn't allow that! I think the problem boils
down to the fact that my Matrix type uses a bounded_array<> Storage
type, but the numpy_matrix<> uses a numpy_array<> Storage type. So a
numpy_matrix<> is not substitutable for a Matrix.

One solution would be to template foo() by Storage type. But that means
I have to do through and change the existing code.

Currently I am simply copying the data into and out of a local Matrix
variable, but I'd like to avoid that if possible.

Another solution might be to somehow make a bounded_array<> that uses
the data in the numpy_array. But I can't see any way to do that.

Any suggestions?

Cheers,
Garth
Andreas Klöckner
2009-07-10 00:26:30 UTC
Permalink
Hi Garth,
Post by Garth Coghlan
int foo(Matrix& A, Matrix& B);
where
typedef boost::numeric::ublas::matrix<double, column,
bounded_array<T> > Matrix;
int fooWrap(numpy_matrix<double> A, numpy_matrix<double> B)
{
return foo(A, B);
}
However, the compiler doesn't allow that! I think the problem boils
down to the fact that my Matrix type uses a bounded_array<> Storage
type, but the numpy_matrix<> uses a numpy_array<> Storage type. So a
numpy_matrix<> is not substitutable for a Matrix.
One solution would be to template foo() by Storage type. But that means
I have to do through and change the existing code.
Currently I am simply copying the data into and out of a local Matrix
variable, but I'd like to avoid that if possible.
Another solution might be to somehow make a bounded_array<> that uses
the data in the numpy_array. But I can't see any way to do that.
Any suggestions?
As far as I can see, there is no easy way around copying in your case. The
zero-copy magic of PyUblas explicitly depends on having a numpy_array<> manage
the storage.

You can get an automatic copy-on-input by replacing
int foo(Matrix& A, Matrix& B);
with
int foo(Matrix const & A, Matrix const & B);

If foo() modifies A and B, you're a bit out of luck unfortunately. A strategy
that I've had good success with would in your case boil down to templating
foo() on Matrix, thereby allowing it to take a numpy_matrix if that is
desired.

Andreas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.tiker.net/pipermail/pyublas/attachments/20090709/18892cc9/attachment.pgp>
Garth Coghlan
2009-07-10 00:42:59 UTC
Permalink
Hi Andreas,

Thanks for that. I'm afraid my foo() does need to modify the input
matrices. Also, I need to avoid changing the functions I am wrapping,
because we want to leave the library as is. Most of the functions I had
previously wrapped were in fact templated on Matrix, which is why I'd
had no problem.

Ah well, I'll just have to do the copy!

Thanks for your help,
Garth
Post by Andreas Klöckner
Hi Garth,
Post by Garth Coghlan
int foo(Matrix& A, Matrix& B);
where
typedef boost::numeric::ublas::matrix<double, column,
bounded_array<T> > Matrix;
int fooWrap(numpy_matrix<double> A, numpy_matrix<double> B)
{
return foo(A, B);
}
However, the compiler doesn't allow that! I think the problem boils
down to the fact that my Matrix type uses a bounded_array<> Storage
type, but the numpy_matrix<> uses a numpy_array<> Storage type. So a
numpy_matrix<> is not substitutable for a Matrix.
One solution would be to template foo() by Storage type. But that means
I have to do through and change the existing code.
Currently I am simply copying the data into and out of a local Matrix
variable, but I'd like to avoid that if possible.
Another solution might be to somehow make a bounded_array<> that uses
the data in the numpy_array. But I can't see any way to do that.
Any suggestions?
As far as I can see, there is no easy way around copying in your case. The
zero-copy magic of PyUblas explicitly depends on having a numpy_array<> manage
the storage.
You can get an automatic copy-on-input by replacing
int foo(Matrix& A, Matrix& B);
with
int foo(Matrix const & A, Matrix const & B);
If foo() modifies A and B, you're a bit out of luck unfortunately. A strategy
that I've had good success with would in your case boil down to templating
foo() on Matrix, thereby allowing it to take a numpy_matrix if that is
desired.
Andreas
--

Loading...