From owner-bug-octave at bevo dot che dot wisc dot edu Fri Dec 13 02:43:56 1996 Subject: assignment expression From: "John W. Eaton" To: Andreas Weingessel Cc: bug-octave at bevo dot che dot wisc dot edu Date: Fri, 13 Dec 1996 02:41:32 -0600 On 12-Dec-1996, Andreas Weingessel wrote: : The following assignment works fine: : : octave:1> a(1,:)=rand(1,5) : a = : : 0.061189 0.593056 0.671360 0.300912 0.531030 : : The following assignment is obviously wrong, and octave responds with : the right error message: : : octave:2> b(1,:)=rand(5,1) : error: A(I, J) = X: X must be a scalar or the number of elements in I must : error: match the number of rows in X and the number of elements in J must : error: match the number of columns in X : error: evaluating assignment expression near line 2, column 8 : : If you now want to correct the previous assignment, this assignment : suddenly does not work, although it is the same as in the first line: : : octave:3> b(1,:)=rand(1,5) : error: A(I, J) = X: X must be a scalar or the number of elements in I must : error: match the number of rows in X and the number of elements in J must : error: match the number of columns in X : error: evaluating assignment expression near line 3, column 7 : : : Although the variable b has not been defined yet (there has always : been an error in the above expressions), it has a value : : octave:4> b : b = : : 0 0 0 0 0 : : The variable also gets a value after the first (wrong) assignment : : octave:5> c(1,:)=rand(5,1) : error: A(I, J) = X: X must be a scalar or the number of elements in I must : error: match the number of rows in X and the number of elements in J must : error: match the number of columns in X : error: evaluating assignment expression near line 5, column 7 : octave:6> c : c = 0 Please try the following patch. Thanks, jwe Fri Dec 13 02:01:32 1996 John W. Eaton * Array2-idx.h (assign): Delay resizing left hand side until we know if the assignment conforms. * ov.cc (octave_value::convert_and_assign): Preserve lhs value if assignment fails. Index: Array2-idx.h =================================================================== RCS file: /home/jwe/src/master/octave/liboctave/Array2-idx.h,v retrieving revision 1.8 diff -c -r1.8 Array2-idx.h *** Array2-idx.h 1996/12/09 18:05:38 1.8 --- Array2-idx.h 1996/12/13 08:01:24 *************** *** 334,339 **** --- 334,355 ---- } } + #define MAYBE_RESIZE_LHS \ + do \ + { \ + if (liboctave_rre_flag) \ + { \ + int max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ + int max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ + \ + int new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ + int new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ + \ + lhs.resize (new_nr, new_nc, 0.0); \ + } \ + } \ + while (0) + template int assign (Array2& lhs, const Array2& rhs) *************** *** 378,396 **** } else { - if (liboctave_rre_flag) - { - int max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; - int max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; - - int new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; - int new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; - - lhs.resize (new_nr, new_nc, 0.0); - } - if (rhs_nr == 1 && rhs_nc == 1 && n > 0 && m > 0) { RT scalar = rhs.elem (0, 0); for (int j = 0; j < m; j++) --- 394,403 ---- } else { if (rhs_nr == 1 && rhs_nc == 1 && n > 0 && m > 0) { + MAYBE_RESIZE_LHS; + RT scalar = rhs.elem (0, 0); for (int j = 0; j < m; j++) *************** *** 405,410 **** --- 412,419 ---- } else if (n == rhs_nr && m == rhs_nc) { + MAYBE_RESIZE_LHS; + for (int j = 0; j < m; j++) { int jj = idx_j.elem (j); Index: ov.cc =================================================================== RCS file: /home/jwe/src/master/octave/src/ov.cc,v retrieving revision 1.10 diff -c -r1.10 ov.cc *** ov.cc 1996/11/07 16:46:11 1.10 --- ov.cc 1996/12/13 08:34:36 *************** *** 590,607 **** if (tmp) { ! if (tmp != rep) { if (--rep->count == 0) delete rep; ! rep = tmp; ! rep->count = 1; } - else - delete tmp; ! assignment_ok = try_assignment (idx, rhs); } else gripe_conversion_failed (type_name (), rhs.type_name ()); --- 590,612 ---- if (tmp) { ! octave_value *old_rep = rep; ! rep = tmp; ! rep->count = 1; ! ! assignment_ok = try_assignment (idx, rhs); ! ! if (! assignment_ok && old_rep) { if (--rep->count == 0) delete rep; ! rep = old_rep; ! old_rep = 0; } ! if (old_rep && --old_rep->count == 0) ! delete old_rep; } else gripe_conversion_failed (type_name (), rhs.type_name ());