From bug-request at octave dot org Mon Jan 17 08:28:34 2005 Subject: Bug concatenation of NDArray's From: David Bateman To: bug at octave dot org Date: Mon, 17 Jan 2005 15:28:42 +0100 This is a multi-part message in MIME format. --------------080504020501060904020508 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit There is a bug in the concatenation of NDArray's that can easily be seen with the example A = cat(3,[1,2;3,4],[1,2;3,4]); B = [A,A] That will seg-fault. The problem is in the function template Array& Array::insert (const Array& a, const Array& ra_idx) where the calculation of the number of elements to skip between copied blocks is incorrect. The problem only appears for concatenation of NDArray's where the dimension along which they are concatentated is not the last one. Attached there is a fix that should be applied to all branches. However, the fix assumes that only one element of ra_idx is non-zero and that only one element (the same as non-zero ra_idx element) of dims() and a.dims() differs. This is in fact the case everywhere that this function is currently. However, it doesn't cover the general case of insertion of one arbitrary matrix into another. Is this a problem? 2005-01-17 David Bateman * Array.cc (Array::insert (const Array& a, const Array& ra_idx)): Modify calculation of number elements to skip between copied blocks. Regards David -- David Bateman David dot Bateman at motorola dot com Motorola CRM +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax) 91193 Gif-Sur-Yvette FRANCE The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary --------------080504020501060904020508 Content-Type: text/plain; name="patch.concat-20050117" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="patch.concat-20050117" --- Array.cc~ 2004-12-28 04:36:13.000000000 +0100 +++ Array.cc 2005-01-17 15:10:35.088793700 +0100 at @ -1056,17 +1056,15 @@ if (dva.numel ()) { const T *a_data = a.data (); - int numel_to_move = dva (0); - int skip = dv (0); - for (int i = 0; i < len_a - 1; i++) + int numel_to_move = 1; + int skip = 0; + for (int i = 0; i < len_a; i++) if (ra_idx(i) == 0 && dva(i) == dv(i)) - { - numel_to_move *= dva(i+1); - skip *= dv(i+1); - } + numel_to_move *= dva(i); else { - skip -= dva(i); + skip = numel_to_move * (dv(i) - dva(i)); + numel_to_move *= dva(i); break; } --------------080504020501060904020508-- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------