# Ublas :

From: pinacle (pinacle2000_at_[hidden])
Date: 2006-12-06 16:56:08

Hi Gunter,

Is there any way to perform this kind of MATLAB operation:

indices = [2 1 3];
X(1:3) = A(indices, indices) * y(1:3);

What I need is a more convenient way to obtain a sub-matrix or sub-vector as
implemented in MATLAB.

Thanks!
Feng

-----Original Message-----
From: ublas-bounces_at_[hidden] [mailto:ublas-bounces_at_[hidden]]
On Behalf Of Gunter Winkler
Sent: Wednesday, December 06, 2006 12:50 AM
To: ublas mailing list
Subject: Re: [ublas] prod using part of matrix

On Tuesday 05 December 2006 19:08, michel schonewille wrote:
> As I am quite new to Boost, please bear with me if this is a "newby
> question"...
>
> I would like to use prod for a matrix vector multiplication, but using
> only a part of the matrix and the vector, so e.g. if the matrix is 10
> rows and columns and the vector 10 rows, I would like to do a matrix
> vector multiplication on the "submatrix" consisting of the first 5
> rows and columns, and the "subvector" consisting of the first 5 rows.

You can use project (from matrix_proxy.hpp and vector_proxy.hpp)

MATLAB: x(1:5) = A(1:5,1:5)*y(1:5)

project( x, range(0,5) )
= prod( project( A, range(0,5), range(0,5) ),
project( y, range(0,5) );

note that range specifies a half open interval (as usual in STL). Thus
range(a,b) means index j with a <= j < b

if you further know that the left hand side of the assignment has different
storage location that the right hand side (=lhs does not alias rhs) you can
add the noalias() function which gives ublas the chance to avoid
temporaries.

noalias( project( x, range(0,5) ) )
= prod( project( A, range(0,5), range(0,5) ),
project( y, range(0,5) );

You find more details in the "overview" sections of the documentation.

mfg
Gunter