Re: Loop-free submatrix extraction

Peter Gottschling <[email protected]> Sat, 22 Dec 2007 17:24:03 +0100
Newsgroups gmane.comp.lib.mtl.devel
Message-ID <[email protected]>
--===============0342701520==
Content-Type: multipart/alternative; boundary=Apple-Mail-2-990111799


--Apple-Mail-2-990111799
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed

Hi again, Michael,

For dense2D and morton_dense there is a function sub_matrix (shame on  
me that there is no documentation yet).  You can use it like:
b= sub_matrix(a, begin_row, end_row, begin_column, end_column);

As you see it only supports intervals of rows and columns not  
arbitrary sets.  The reason is that this can be realized efficiently  
with a view.

If you are going to write a function I would do something like (only  
sketch not tested code):

template <typename Matrix, typename RowVector, typename ColVector>
Matrix list_sub_matrix(const Matrix& matrix, const RowVector&  
row_vector, const ColVector& col_vector)
{
   Matrix s(size(row_vector), size(col_vector));
   {
     matrix::inserter<Matrix>   ins(s);
     for (size_t r= 0; r < size(row_vector); r++)
        for (size_t c= 0; c < size(col_vector); c++)
             ins(r, c) << matrix[row_vector[r]][col_vector[c]];
   }
   return s;
}

Returning a matrix is quite expensive at the moment but will be okay  
once I've implemented move semantics.  I hope I haven't overseen a  
detail otherwise you can asked again.  Once you have it done and  
tested I'd like to put it in the repository.

Good luck and Merry Christmas,
Peter


On Dec 22, 2007, at 8:24 AM, Michael Smolsky wrote:

> Peter,
>
> Thank you for your detailed response.
>
> I'm exploring MTL further. I hope you don't mind if I ask you some  
> questions, the answers to which I couldn't find in the documentation.
>
> I need to extract a submatrix of a matrix, in a way similar to:
>
> octave:4> a=rand(4),b=a([1,2],[2,4])
> a =
>
>    0.622575   0.844744   0.424293   0.207615
>    0.642011   0.102342   0.011919   0.168107
>    0.774028   0.446422   0.076552   0.759136
>    0.633285   0.792054   0.738877   0.982645
>
> b =
>
>    0.84474   0.20762
>    0.10234   0.16811
>
> Is there an elegant way of doing this, or I need to write loops? I  
> feel optimistic having read about inserters in your documentation...
>
> My matrix is dense for now, but I will probably need to apply this  
> to a sparse matrix as well.
>
> Thanks you in advance,
>
> Michael.
>
>> ----- Original Message -----
>> From: "Peter Gottschling" <[email protected]>
>> To: "General Matrix Template Library (MTL) list" <mtl- 
>> [email protected]>, "Matrix Template Library 4" <[email protected]>
>> Subject: Re: MTL: MTL development road
>> Date: Mon, 03 Dec 2007 10:53:38 +0100
>>
>>
>
> -- 
> Want an e-mail address like mine?
> Get a free e-mail account today at www.mail.com!
>
>
> _______________________________________________
> This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/

------------
Peter Gottschling
Research Associate
Open Systems Laboratory
Indiana University
135 Lindley Hall
Bloomington, IN 47405
Tel.: +1-812-855-3608   Fax: +1-812-856-0853
http://www.osl.iu.edu/~pgottsch



--Apple-Mail-2-990111799
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=ISO-8859-1

<html><body style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; ">
Hi again, Michael,<div><br =
class=3D"webkit-block-placeholder"></div><div>For dense2D and =
morton_dense there is a function sub_matrix (shame on me that there is =
no documentation yet). =A0You can use it like:</div><div>b=3D =
sub_matrix(a, begin_row, end_row, begin_column, =
end_column);</div><div><br =
class=3D"webkit-block-placeholder"></div><div>As you see it only =
supports intervals of rows and columns not arbitrary sets. =A0The reason =
is that this can be realized efficiently with a view.</div><div><br =
class=3D"webkit-block-placeholder"></div><div>If you are going to write =
a function I would do something like (only sketch not tested =
code):</div><div><br =
class=3D"webkit-block-placeholder"></div><div>template &lt;typename =
Matrix, typename RowVector, typename ColVector&gt;</div><div>Matrix =
list_sub_matrix(const Matrix&amp; matrix, const RowVector&amp; =
row_vector, const ColVector&amp; =
col_vector)</div><div>{</div><div>=A0=A0Matrix s(size(row_vector), =
size(col_vector));</div><div>=A0=A0{</div><div>=A0=A0 =
=A0matrix::inserter&lt;Matrix&gt; =A0 ins(s);</div><div>=A0=A0 =A0for =
(size_t r=3D 0; r &lt; size(row_vector); r++)</div><div>=A0=A0 =A0 =A0 =
for (size_t c=3D 0; c &lt; size(col_vector); c++)</div><div>=A0=A0 =A0 =A0=
 =A0 =A0 =A0ins(r, c) &lt;&lt; =
matrix[row_vector[r]][col_vector[c]];</div><div>=A0=A0}</div><div>=A0=A0re=
turn s;</div><div>}</div><div><br =
class=3D"webkit-block-placeholder"></div><div>Returning a matrix is =
quite expensive at the moment but will be okay once I've implemented =
move semantics. =A0I hope I haven't overseen a detail otherwise you can =
asked again. =A0Once you have it done and tested I'd like to put it in =
the repository.</div><div><br =
class=3D"webkit-block-placeholder"></div><div>Good luck and Merry =
Christmas,</div><div>Peter</div><div><br =
class=3D"webkit-block-placeholder"></div><div><br><div><div>On Dec 22, =
2007, at 8:24 AM, Michael Smolsky wrote:</div><br =
class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">Peter,</div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: =
14px; "><br></div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; ">Thank you for your detailed =
response.</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">I'm exploring MTL further. I hope you don't mind if =
I ask you some questions, the answers to which I couldn't find in the =
documentation.</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">I need to extract a submatrix of a matrix, in a way =
similar to:</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">octave:4&gt; a=3Drand(4),b=3Da([1,2],[2,4])</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">a =3D</div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: =
14px; "><br></div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; "><span =
class=3D"Apple-converted-space">=A0=A0 </span>0.622575 <span =
class=3D"Apple-converted-space">=A0 </span>0.844744 <span =
class=3D"Apple-converted-space">=A0 </span>0.424293 <span =
class=3D"Apple-converted-space">=A0 </span>0.207615</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; "><span class=3D"Apple-converted-space">=A0=A0 =
</span>0.642011 <span class=3D"Apple-converted-space">=A0 =
</span>0.102342 <span class=3D"Apple-converted-space">=A0 =
</span>0.011919 <span class=3D"Apple-converted-space">=A0 =
</span>0.168107</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; "><span =
class=3D"Apple-converted-space">=A0=A0 </span>0.774028 <span =
class=3D"Apple-converted-space">=A0 </span>0.446422 <span =
class=3D"Apple-converted-space">=A0 </span>0.076552 <span =
class=3D"Apple-converted-space">=A0 </span>0.759136</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; "><span class=3D"Apple-converted-space">=A0=A0 =
</span>0.633285 <span class=3D"Apple-converted-space">=A0 =
</span>0.792054 <span class=3D"Apple-converted-space">=A0 =
</span>0.738877 <span class=3D"Apple-converted-space">=A0 =
</span>0.982645</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">b =3D</div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: =
14px; "><br></div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; "><span =
class=3D"Apple-converted-space">=A0=A0 </span>0.84474 <span =
class=3D"Apple-converted-space">=A0 </span>0.20762</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; "><span class=3D"Apple-converted-space">=A0=A0 =
</span>0.10234 <span class=3D"Apple-converted-space">=A0 =
</span>0.16811</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">Is there an elegant way of doing this, or I need to =
write loops? I feel optimistic having read about inserters in your =
documentation...</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">My matrix is dense for now, but I will probably need =
to apply this to a sparse matrix as well.</div><div style=3D"margin-top: =
0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; =
min-height: 14px; "><br></div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Thanks you in =
advance,</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">Michael.</div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: =
14px; "><br></div> <blockquote type=3D"cite"><div style=3D"margin-top: =
0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">----- =
Original Message -----</div><div style=3D"margin-top: 0px; margin-right: =
0px; margin-bottom: 0px; margin-left: 0px; ">From: "Peter Gottschling" =
&lt;<a =
href=3D"mailto:[email protected]">[email protected]</a>&gt;</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">To: "General Matrix Template Library (MTL) list" =
&lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt;, =
"Matrix Template Library 4" &lt;<a =
href=3D"mailto:[email protected]">[email protected]</a>&gt;</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">Subject: Re: MTL: MTL development road</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">Date: Mon, 03 Dec 2007 10:53:38 +0100</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; min-height: 14px; "><br></div><div style=3D"margin-top: =
0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; =
min-height: 14px; "><br></div> </blockquote><div style=3D"margin-top: =
0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; =
min-height: 14px; "><br></div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">--<span =
class=3D"Apple-converted-space">=A0</span></div><div style=3D"margin-top: =
0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Want an =
e-mail address like mine?</div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Get a free =
e-mail account today at <a =
href=3D"http://www.mail.com">www.mail.com</a>!</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; min-height: 14px; "><br></div><div style=3D"margin-top: =
0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; =
min-height: 14px; "><br></div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; =
">_______________________________________________</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; ">This list is archived at <a =
href=3D"http://www.osl.iu.edu/MailArchives/mtl-devel/">http://www.osl.iu.e=
du/MailArchives/mtl-devel/</a></div> </blockquote></div><br><div> <span =
class=3D"Apple-style-span" style=3D"border-collapse: separate; =
border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; =
font-size: 12px; font-style: normal; font-variant: normal; font-weight: =
normal; letter-spacing: normal; line-height: normal; text-align: auto; =
-khtml-text-decorations-in-effect: none; text-indent: 0px; =
-apple-text-size-adjust: auto; text-transform: none; orphans: 2; =
white-space: normal; widows: 2; word-spacing: 0px; "><p style=3D"margin: =
0.0px 0.0px 0.0px 0.0px"><font face=3D"Helvetica" size=3D"3" =
style=3D"font: 12.0px Helvetica">------------</font></p><p =
style=3D"margin: 0.0px 0.0px 0.0px 0.0px"><font face=3D"Helvetica" =
size=3D"3" style=3D"font: 12.0px Helvetica">Peter =
Gottschling</font></p><p style=3D"margin: 0.0px 0.0px 0.0px 0.0px"><font =
face=3D"Helvetica" size=3D"3" style=3D"font: 12.0px Helvetica">Research =
Associate</font></p><p style=3D"margin: 0.0px 0.0px 0.0px 0.0px"><font =
face=3D"Helvetica" size=3D"3" style=3D"font: 12.0px Helvetica">Open =
Systems Laboratory</font></p><p style=3D"margin: 0.0px 0.0px 0.0px =
0.0px"><font face=3D"Helvetica" size=3D"3" style=3D"font: 12.0px =
Helvetica">Indiana University</font></p><p style=3D"margin: 0.0px 0.0px =
0.0px 0.0px"><font face=3D"Helvetica" size=3D"3" style=3D"font: 12.0px =
Helvetica">135 Lindley Hall</font></p><p style=3D"margin: 0.0px 0.0px =
0.0px 0.0px"><font face=3D"Helvetica" size=3D"3" style=3D"font: 12.0px =
Helvetica">Bloomington, IN 47405</font></p><p style=3D"margin: 0.0px =
0.0px 0.0px 0.0px"><font face=3D"Helvetica" size=3D"3" style=3D"font: =
12.0px Helvetica">Tel.: +1-812-855-3608<span =
class=3D"Apple-converted-space">=A0</span><span =
class=3D"Apple-converted-space">=A0<span =
class=3D"Apple-converted-space">=A0</span></span>Fax: =
+1-812-856-0853</font></p><p style=3D"margin: 0.0px 0.0px 0.0px =
0.0px"><font face=3D"Helvetica" size=3D"3" style=3D"font: 12.0px =
Helvetica"><a =
href=3D"http://www.osl.iu.edu/~pgottsch">http://www.osl.iu.edu/~pgottsch</=
a></font></p><br class=3D"Apple-interchange-newline"></span> =
</div><br></div></body></html>=

--Apple-Mail-2-990111799--

--===============0342701520==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/
--===============0342701520==--