Mercurial > hg > octave-nkf
changeset 4316:236c10efcde2
[project @ 2003-02-10 21:57:15 by jwe]
author | jwe |
---|---|
date | Mon, 10 Feb 2003 21:57:15 +0000 |
parents | 0a056052bc90 |
children | 1ced186d4b6b |
files | liboctave/CColVector.cc liboctave/CColVector.h liboctave/CMatrix.cc liboctave/CMatrix.h liboctave/CRowVector.cc liboctave/CRowVector.h liboctave/ChangeLog liboctave/dColVector.cc liboctave/dColVector.h liboctave/dMatrix.cc liboctave/dMatrix.h liboctave/dRowVector.cc liboctave/dRowVector.h |
diffstat | 13 files changed, 379 insertions(+), 81 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/CColVector.cc +++ b/liboctave/CColVector.cc @@ -78,14 +78,20 @@ ComplexColumnVector::insert (const ColumnVector& a, int r) { int a_len = a.length (); + if (r < 0 || r + a_len > length ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (r+i) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i) = a.elem (i); + } return *this; } @@ -94,14 +100,20 @@ ComplexColumnVector::insert (const ComplexColumnVector& a, int r) { int a_len = a.length (); + if (r < 0 || r + a_len > length ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (r+i) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i) = a.elem (i); + } return *this; } @@ -110,9 +122,15 @@ ComplexColumnVector::fill (double val) { int len = length (); + if (len > 0) - for (int i = 0; i < len; i++) - elem (i) = val; + { + make_unique (); + + for (int i = 0; i < len; i++) + xelem (i) = val; + } + return *this; } @@ -120,9 +138,16 @@ ComplexColumnVector::fill (const Complex& val) { int len = length (); + if (len > 0) - for (int i = 0; i < len; i++) - elem (i) = val; + { + make_unique (); + + for (int i = 0; i < len; i++) + xelem (i) = val; + } + + return *this; } @@ -130,6 +155,7 @@ ComplexColumnVector::fill (double val, int r1, int r2) { int len = length (); + if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) { (*current_liboctave_error_handler) ("range error for fill"); @@ -138,8 +164,13 @@ if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } - for (int i = r1; i <= r2; i++) - elem (i) = val; + if (r2 >= r1) + { + make_unique (); + + for (int i = r1; i <= r2; i++) + xelem (i) = val; + } return *this; } @@ -148,6 +179,7 @@ ComplexColumnVector::fill (const Complex& val, int r1, int r2) { int len = length (); + if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) { (*current_liboctave_error_handler) ("range error for fill"); @@ -156,8 +188,13 @@ if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } - for (int i = r1; i <= r2; i++) - elem (i) = val; + if (r2 >= r1) + { + make_unique (); + + for (int i = r1; i <= r2; i++) + xelem (i) = val; + } return *this; } @@ -224,6 +261,17 @@ return result; } +ComplexColumnVector +ComplexColumnVector::extract_n (int r1, int n) const +{ + ComplexColumnVector result (n); + + for (int i = 0; i < n; i++) + result.elem (i) = elem (r1+i); + + return result; +} + // column vector by column vector -> column vector operations ComplexColumnVector&
--- a/liboctave/CColVector.h +++ b/liboctave/CColVector.h @@ -83,6 +83,8 @@ ComplexColumnVector extract (int r1, int r2) const; + ComplexColumnVector extract_n (int r1, int n) const; + // column vector by column vector -> column vector operations ComplexColumnVector& operator += (const ColumnVector& a);
--- a/liboctave/CMatrix.cc +++ b/liboctave/CMatrix.cc @@ -240,9 +240,14 @@ return *this; } - for (int j = 0; j < a_nc; j++) - for (int i = 0; i < a_nr; i++) - elem (r+i, c+j) = a.elem (i, j); + if (a_nr >0 && a_nc > 0) + { + make_unique (); + + for (int j = 0; j < a_nc; j++) + for (int i = 0; i < a_nr; i++) + xelem (r+i, c+j) = a.elem (i, j); + } return *this; } @@ -251,14 +256,20 @@ ComplexMatrix::insert (const RowVector& a, int r, int c) { int a_len = a.length (); + if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (r, c+i) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r, c+i) = a.elem (i); + } return *this; } @@ -267,14 +278,20 @@ ComplexMatrix::insert (const ColumnVector& a, int r, int c) { int a_len = a.length (); + if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (r+i, c) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i, c) = a.elem (i); + } return *this; } @@ -293,8 +310,15 @@ fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); - for (int i = 0; i < a.length (); i++) - elem (r+i, c+i) = a.elem (i, i); + int a_len = a.length (); + + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i, c+i) = a.elem (i, i); + } return *this; } @@ -326,14 +350,20 @@ ComplexMatrix::insert (const ComplexColumnVector& a, int r, int c) { int a_len = a.length (); + if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (r+i, c) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i, c) = a.elem (i); + } return *this; } @@ -352,8 +382,15 @@ fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); - for (int i = 0; i < a.length (); i++) - elem (r+i, c+i) = a.elem (i, i); + int a_len = a.length (); + + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i, c+i) = a.elem (i, i); + } return *this; } @@ -363,10 +400,15 @@ { int nr = rows (); int nc = cols (); + if (nr > 0 && nc > 0) - for (int j = 0; j < nc; j++) - for (int i = 0; i < nr; i++) - elem (i, j) = val; + { + make_unique (); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + xelem (i, j) = val; + } return *this; } @@ -376,10 +418,15 @@ { int nr = rows (); int nc = cols (); + if (nr > 0 && nc > 0) - for (int j = 0; j < nc; j++) - for (int i = 0; i < nr; i++) - elem (i, j) = val; + { + make_unique (); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + xelem (i, j) = val; + } return *this; } @@ -389,6 +436,7 @@ { int nr = rows (); int nc = cols (); + if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) { @@ -399,9 +447,14 @@ if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } - for (int j = c1; j <= c2; j++) - for (int i = r1; i <= r2; i++) - elem (i, j) = val; + if (r2 >= r1 && c2 >= c1) + { + make_unique (); + + for (int j = c1; j <= c2; j++) + for (int i = r1; i <= r2; i++) + xelem (i, j) = val; + } return *this; } @@ -411,6 +464,7 @@ { int nr = rows (); int nc = cols (); + if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) { @@ -421,9 +475,14 @@ if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } - for (int j = c1; j <= c2; j++) - for (int i = r1; i <= r2; i++) - elem (i, j) = val; + if (r2 >= r1 && c2 >=c1) + { + make_unique (); + + for (int j = c1; j <= c2; j++) + for (int i = r1; i <= r2; i++) + xelem (i, j) = val; + } return *this; } @@ -766,7 +825,19 @@ for (int j = 0; j < new_c; j++) for (int i = 0; i < new_r; i++) - result.elem (i, j) = elem (r1+i, c1+j); + result.xelem (i, j) = elem (r1+i, c1+j); + + return result; +} + +ComplexMatrix +ComplexMatrix::extract_n (int r1, int c1, int nr, int nc) const +{ + ComplexMatrix result (nr, nc); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + result.xelem (i, j) = elem (r1+i, c1+j); return result; } @@ -785,7 +856,7 @@ ComplexRowVector retval (nc); for (int j = 0; j < cols (); j++) - retval.elem (j) = elem (i, j); + retval.xelem (j) = elem (i, j); return retval; } @@ -823,7 +894,7 @@ ComplexColumnVector retval (nr); for (int j = 0; j < nr; j++) - retval.elem (j) = elem (j, i); + retval.xelem (j) = elem (j, i); return retval; }
--- a/liboctave/CMatrix.h +++ b/liboctave/CMatrix.h @@ -128,6 +128,8 @@ ComplexMatrix extract (int r1, int c1, int r2, int c2) const; + ComplexMatrix extract_n (int r1, int c1, int nr, int nc) const; + // extract row or column i. ComplexRowVector row (int i) const;
--- a/liboctave/CRowVector.cc +++ b/liboctave/CRowVector.cc @@ -78,14 +78,20 @@ ComplexRowVector::insert (const RowVector& a, int c) { int a_len = a.length (); + if (c < 0 || c + a_len > length ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (c+i) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (c+i) = a.elem (i); + } return *this; } @@ -94,14 +100,20 @@ ComplexRowVector::insert (const ComplexRowVector& a, int c) { int a_len = a.length (); + if (c < 0 || c + a_len > length ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (c+i) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (c+i) = a.elem (i); + } return *this; } @@ -110,9 +122,15 @@ ComplexRowVector::fill (double val) { int len = length (); + if (len > 0) - for (int i = 0; i < len; i++) - elem (i) = val; + { + make_unique (); + + for (int i = 0; i < len; i++) + xelem (i) = val; + } + return *this; } @@ -120,9 +138,15 @@ ComplexRowVector::fill (const Complex& val) { int len = length (); + if (len > 0) - for (int i = 0; i < len; i++) - elem (i) = val; + { + make_unique (); + + for (int i = 0; i < len; i++) + xelem (i) = val; + } + return *this; } @@ -130,6 +154,7 @@ ComplexRowVector::fill (double val, int c1, int c2) { int len = length (); + if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) { (*current_liboctave_error_handler) ("range error for fill"); @@ -138,8 +163,13 @@ if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } - for (int i = c1; i <= c2; i++) - elem (i) = val; + if (c2 >= c1) + { + make_unique (); + + for (int i = c1; i <= c2; i++) + xelem (i) = val; + } return *this; } @@ -148,6 +178,7 @@ ComplexRowVector::fill (const Complex& val, int c1, int c2) { int len = length (); + if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) { (*current_liboctave_error_handler) ("range error for fill"); @@ -156,8 +187,13 @@ if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } - for (int i = c1; i <= c2; i++) - elem (i) = val; + if (c2 >= c1) + { + make_unique (); + + for (int i = c1; i <= c2; i++) + xelem (i) = val; + } return *this; } @@ -224,6 +260,17 @@ return result; } +ComplexRowVector +ComplexRowVector::extract_n (int r1, int n) const +{ + ComplexRowVector result (n); + + for (int i = 0; i < n; i++) + result.elem (i) = elem (r1+i); + + return result; +} + // row vector by row vector -> row vector operations ComplexRowVector&
--- a/liboctave/CRowVector.h +++ b/liboctave/CRowVector.h @@ -81,6 +81,8 @@ ComplexRowVector extract (int c1, int c2) const; + ComplexRowVector extract_n (int c1, int n) const; + // row vector by row vector -> row vector operations ComplexRowVector& operator += (const RowVector& a);
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,23 @@ +2003-02-10 John W. Eaton <jwe@bevo.che.wisc.edu> + + * CColVector.cc (ComplexColumnVector::extract_n): New function. + * CRowVector.cc (ComplexRowVector::extract_n): Likewise. + * CMatrix.cc (ComplexMatrix::extract_n): Likewise. + * dColVector.cc (ColumnVector::extract_n): Likewise. + * dRowVector.cc (RowVector::extract_n): Likewise. + * dMatrix.cc (Matrix::extract_n): Likewise. + + * CColVector.cc (ComplexColumnVector::insert): Improve efficiency + with make_unique and xelem. + * CRowVector.cc (ComplexRowVector::insert): Likewise. + * CMatrix.cc (ComplexMatrix::insert, ComplexMatrix::fill, + ComplexMatrix::extract, ComplexMatrix::row, + ComplexMatrix::column): Likewise. + * dColVector.cc (ColumnVector::insert): Likewise. + * dRowVector.cc (RowVector::insert): Likewise. + * dMatrix.cc (Matrix::insert, Matrix::fill, Matrix::extract, + Matrix::row, Matrix::column): Likewise. + 2003-01-30 John W. Eaton <jwe@bevo.che.wisc.edu> * lo-mappers.cc (imag (double)): Return 0.0 for all args, even NaN.
--- a/liboctave/dColVector.cc +++ b/liboctave/dColVector.cc @@ -69,14 +69,20 @@ ColumnVector::insert (const ColumnVector& a, int r) { int a_len = a.length (); + if (r < 0 || r + a_len > length ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (r+i) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i) = a.elem (i); + } return *this; } @@ -85,9 +91,15 @@ ColumnVector::fill (double val) { int len = length (); + if (len > 0) - for (int i = 0; i < len; i++) - elem (i) = val; + { + make_unique (); + + for (int i = 0; i < len; i++) + xelem (i) = val; + } + return *this; } @@ -95,6 +107,7 @@ ColumnVector::fill (double val, int r1, int r2) { int len = length (); + if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) { (*current_liboctave_error_handler) ("range error for fill"); @@ -103,8 +116,13 @@ if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } - for (int i = r1; i <= r2; i++) - elem (i) = val; + if (r2 >= r1) + { + make_unique (); + + for (int i = r1; i <= r2; i++) + xelem (i) = val; + } return *this; } @@ -158,7 +176,18 @@ ColumnVector result (new_r); for (int i = 0; i < new_r; i++) - result.elem (i) = elem (r1+i); + result.xelem (i) = elem (r1+i); + + return result; +} + +ColumnVector +ColumnVector::extract_n (int r1, int n) const +{ + ColumnVector result (n); + + for (int i = 0; i < n; i++) + result.xelem (i) = elem (r1+i); return result; }
--- a/liboctave/dColVector.h +++ b/liboctave/dColVector.h @@ -73,6 +73,8 @@ ColumnVector extract (int r1, int r2) const; + ColumnVector extract_n (int r1, int n) const; + // matrix by column vector -> column vector operations friend ColumnVector operator * (const Matrix& a, const ColumnVector& b);
--- a/liboctave/dMatrix.cc +++ b/liboctave/dMatrix.cc @@ -199,14 +199,20 @@ Matrix::insert (const RowVector& a, int r, int c) { int a_len = a.length (); + if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (r, c+i) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r, c+i) = a.elem (i); + } return *this; } @@ -215,14 +221,20 @@ Matrix::insert (const ColumnVector& a, int r, int c) { int a_len = a.length (); + if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (r+i, c) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i, c) = a.elem (i); + } return *this; } @@ -241,8 +253,15 @@ fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); - for (int i = 0; i < a.length (); i++) - elem (r+i, c+i) = a.elem (i, i); + int a_len = a.length (); + + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (r+i, c+i) = a.elem (i, i); + } return *this; } @@ -252,10 +271,15 @@ { int nr = rows (); int nc = cols (); + if (nr > 0 && nc > 0) - for (int j = 0; j < nc; j++) - for (int i = 0; i < nr; i++) - elem (i, j) = val; + { + make_unique (); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + xelem (i, j) = val; + } return *this; } @@ -265,6 +289,7 @@ { int nr = rows (); int nc = cols (); + if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) { @@ -275,9 +300,14 @@ if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } - for (int j = c1; j <= c2; j++) - for (int i = r1; i <= r2; i++) - elem (i, j) = val; + if (r2 >= r1 && c2 >= c1) + { + make_unique (); + + for (int j = c1; j <= c2; j++) + for (int i = r1; i <= r2; i++) + xelem (i, j) = val; + } return *this; } @@ -465,7 +495,19 @@ for (int j = 0; j < new_c; j++) for (int i = 0; i < new_r; i++) - result.elem (i, j) = elem (r1+i, c1+j); + result.xelem (i, j) = elem (r1+i, c1+j); + + return result; +} + +Matrix +Matrix::extract_n (int r1, int c1, int nr, int nc) const +{ + Matrix result (nr, nc); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + result.xelem (i, j) = elem (r1+i, c1+j); return result; } @@ -484,7 +526,7 @@ RowVector retval (nc); for (int j = 0; j < nc; j++) - retval.elem (j) = elem (i, j); + retval.xelem (j) = elem (i, j); return retval; } @@ -522,7 +564,7 @@ ColumnVector retval (nr); for (int j = 0; j < nr; j++) - retval.elem (j) = elem (j, i); + retval.xelem (j) = elem (j, i); return retval; }
--- a/liboctave/dMatrix.h +++ b/liboctave/dMatrix.h @@ -103,6 +103,8 @@ Matrix extract (int r1, int c1, int r2, int c2) const; + Matrix extract_n (int r1, int c1, int nr, int nc) const; + // extract row or column i. RowVector row (int i) const;
--- a/liboctave/dRowVector.cc +++ b/liboctave/dRowVector.cc @@ -72,14 +72,20 @@ RowVector::insert (const RowVector& a, int c) { int a_len = a.length (); + if (c < 0 || c + a_len > length ()) { (*current_liboctave_error_handler) ("range error for insert"); return *this; } - for (int i = 0; i < a_len; i++) - elem (c+i) = a.elem (i); + if (a_len > 0) + { + make_unique (); + + for (int i = 0; i < a_len; i++) + xelem (c+i) = a.elem (i); + } return *this; } @@ -88,9 +94,15 @@ RowVector::fill (double val) { int len = length (); + if (len > 0) - for (int i = 0; i < len; i++) - elem (i) = val; + { + make_unique (); + + for (int i = 0; i < len; i++) + xelem (i) = val; + } + return *this; } @@ -98,6 +110,7 @@ RowVector::fill (double val, int c1, int c2) { int len = length (); + if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) { (*current_liboctave_error_handler) ("range error for fill"); @@ -106,8 +119,13 @@ if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } - for (int i = c1; i <= c2; i++) - elem (i) = val; + if (c2 >= c1) + { + make_unique (); + + for (int i = c1; i <= c2; i++) + xelem (i) = val; + } return *this; } @@ -159,7 +177,18 @@ RowVector result (new_c); for (int i = 0; i < new_c; i++) - result.elem (i) = elem (c1+i); + result.xelem (i) = elem (c1+i); + + return result; +} + +RowVector +RowVector::extract_n (int r1, int n) const +{ + RowVector result (n); + + for (int i = 0; i < n; i++) + result.xelem (i) = elem (r1+i); return result; }