Mercurial > octave-nkf
comparison src/data.cc @ 14567:5bb5fcffa29d stable
correctly fill cell arrays for three-arg diag function calls
* data.cc (Fdiag): Special case for cell arrays so that off-diagonal
elements are filled with [].
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 17 Apr 2012 11:53:39 -0400 |
parents | 3959f3f81e33 |
children | 3a9a56999ce5 fd5c0159b588 |
comparison
equal
deleted
inserted
replaced
14566:aa491bd9e19b | 14567:5bb5fcffa29d |
---|---|
1292 octave_value arg0 = args(0); | 1292 octave_value arg0 = args(0); |
1293 if (arg0.ndims () == 2 && (args(0).rows () == 1 || args(0).columns () == 1)) | 1293 if (arg0.ndims () == 2 && (args(0).rows () == 1 || args(0).columns () == 1)) |
1294 { | 1294 { |
1295 octave_idx_type m = args(1).int_value (), n = args(2).int_value (); | 1295 octave_idx_type m = args(1).int_value (), n = args(2).int_value (); |
1296 if (! error_state) | 1296 if (! error_state) |
1297 retval = arg0.diag ().resize (dim_vector (m, n), true); | 1297 { |
1298 if (arg0.is_cell ()) | |
1299 { | |
1300 Cell rhs = arg0.cell_value (); | |
1301 Cell tmp (m, n); | |
1302 | |
1303 for (octave_idx_type i = 0; i < rhs.numel (); i++) | |
1304 tmp.xelem (i, i) = rhs.xelem (i); | |
1305 | |
1306 retval = tmp; | |
1307 } | |
1308 else | |
1309 retval = arg0.diag ().resize (dim_vector (m, n), true); | |
1310 } | |
1298 else | 1311 else |
1299 error ("diag: invalid dimensions"); | 1312 error ("diag: invalid dimensions"); |
1300 } | 1313 } |
1301 else | 1314 else |
1302 error ("diag: V must be a vector"); | 1315 error ("diag: V must be a vector"); |
1341 %!assert(diag (int8([0, 1, 0, 0; 0, 0, 2, 0; 0, 0, 0, 3; 0, 0, 0, 0]), 1), int8([1; 2; 3])); | 1354 %!assert(diag (int8([0, 1, 0, 0; 0, 0, 2, 0; 0, 0, 0, 3; 0, 0, 0, 0]), 1), int8([1; 2; 3])); |
1342 %!assert(diag (int8([0, 0, 0, 0; 1, 0, 0, 0; 0, 2, 0, 0; 0, 0, 3, 0]), -1), int8([1; 2; 3])); | 1355 %!assert(diag (int8([0, 0, 0, 0; 1, 0, 0, 0; 0, 2, 0, 0; 0, 0, 3, 0]), -1), int8([1; 2; 3])); |
1343 | 1356 |
1344 ## Test non-square size | 1357 ## Test non-square size |
1345 %!assert(diag ([1,2,3], 6, 3), [1 0 0; 0 2 0; 0 0 3; 0 0 0; 0 0 0; 0 0 0]) | 1358 %!assert(diag ([1,2,3], 6, 3), [1 0 0; 0 2 0; 0 0 3; 0 0 0; 0 0 0; 0 0 0]) |
1359 %!assert (diag (1, 2, 3), [1,0,0; 0,0,0]); | |
1360 %!assert (diag ({1}, 2, 3), {1,[],[]; [],[],[]}); | |
1361 %!assert (diag ({1,2}, 3, 4), {1,[],[],[]; [],2,[],[]; [],[],[],[]}); | |
1346 | 1362 |
1347 %% Test input validation | 1363 %% Test input validation |
1348 %!error <Invalid call to diag> diag (); | 1364 %!error <Invalid call to diag> diag (); |
1349 %!error <Invalid call to diag> diag (1,2,3,4); | 1365 %!error <Invalid call to diag> diag (1,2,3,4); |
1350 %!error diag (ones (2), 3, 3); | 1366 %!error diag (ones (2), 3, 3); |