Mercurial > hg > octave-lojdl
changeset 891:9e6bdfdfcf86
[project @ 1994-11-08 20:59:39 by jwe]
author | jwe |
---|---|
date | Tue, 08 Nov 1994 21:00:08 +0000 |
parents | 2bd199f3f865 |
children | 53ad6947655f |
files | liboctave/CMatrix.cc src/minmax.cc |
diffstat | 2 files changed, 320 insertions(+), 92 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/CMatrix.cc +++ b/liboctave/CMatrix.cc @@ -2483,6 +2483,10 @@ return d; } +// XXX FIXME XXX -- it would be nice to share some code among all the +// min/max functions below. It would also be nice to combine the +// min/max and min_loc/max_loc functions. + ComplexColumnVector ComplexMatrix::row_min (void) const { @@ -2496,15 +2500,37 @@ for (int i = 0; i < nr; i++) { - Complex res = elem (i, 0); - double absres = abs (res); - for (int j = 1; j < nc; j++) - if (abs (elem (i, j)) < absres) + int row_is_real_only = 1; + for (int j = 0; j < nc; j++) + if (imag (elem (i, j)) != 0.0) { - res = elem (i, j); - absres = abs (res); + row_is_real_only = 0; + break; } - result.elem (i) = res; + + if (row_is_real_only) + { + double res = real (elem (i, 0)); + for (int j = 1; j < nc; j++) + { + double tmp = real (elem (i, j)); + if (tmp < res) + res = tmp; + } + result.elem (i) = res; + } + else + { + Complex res = elem (i, 0); + double absres = abs (res); + for (int j = 1; j < nc; j++) + if (abs (elem (i, j)) < absres) + { + res = elem (i, j); + absres = abs (res); + } + result.elem (i) = res; + } } } @@ -2525,15 +2551,36 @@ for (int i = 0; i < nr; i++) { - Complex res = 0; - double absres = abs (elem (i, 0)); - for (int j = 0; j < nc; j++) - if (abs (elem (i, j)) < absres) - { - res = j; - absres = abs (elem (i, j)); - } - result.elem (i) = res + 1; + int column_is_real_only = 1; + for (int j = 0; j < nc; j++) + if (imag (elem (i, j)) != 0.0) + { + column_is_real_only = 0; + break; + } + + if (column_is_real_only) + { + double res = 0; + double tmp = real (elem (i, 0)); + for (int j = 1; j < nc; j++) + if (real (elem (i, j)) < tmp) + res = j; + + result.elem (i) = res + 1; + } + else + { + Complex res = 0; + double absres = abs (elem (i, 0)); + for (int j = 1; j < nc; j++) + if (abs (elem (i, j)) < absres) + { + res = j; + absres = abs (elem (i, j)); + } + result.elem (i) = res + 1; + } } } @@ -2554,15 +2601,37 @@ for (int i = 0; i < nr; i++) { - Complex res = elem (i, 0); - double absres = abs (res); - for (int j = 1; j < nc; j++) - if (abs (elem (i, j)) > absres) + int row_is_real_only = 1; + for (int j = 0; j < nc; j++) + if (imag (elem (i, j)) != 0.0) { - res = elem (i, j); - absres = abs (res); + row_is_real_only = 0; + break; } - result.elem (i) = res; + + if (row_is_real_only) + { + double res = real (elem (i, 0)); + for (int j = 1; j < nc; j++) + { + double tmp = real (elem (i, j)); + if (tmp > res) + res = tmp; + } + result.elem (i) = res; + } + else + { + Complex res = elem (i, 0); + double absres = abs (res); + for (int j = 1; j < nc; j++) + if (abs (elem (i, j)) > absres) + { + res = elem (i, j); + absres = abs (res); + } + result.elem (i) = res; + } } } @@ -2583,15 +2652,36 @@ for (int i = 0; i < nr; i++) { - Complex res = 0; - double absres = abs (elem (i, 0)); - for (int j = 0; j < nc; j++) - if (abs (elem (i, j)) > absres) - { - res = j; - absres = abs (elem (i, j)); - } - result.elem (i) = res + 1; + int column_is_real_only = 1; + for (int j = 0; j < nc; j++) + if (imag (elem (i, j)) != 0.0) + { + column_is_real_only = 0; + break; + } + + if (column_is_real_only) + { + double res = 0; + double tmp = real (elem (i, 0)); + for (int j = 1; j < nc; j++) + if (real (elem (i, j)) > tmp) + res = j; + + result.elem (i) = res + 1; + } + else + { + Complex res = 0; + double absres = abs (elem (i, 0)); + for (int j = 1; j < nc; j++) + if (abs (elem (i, j)) > absres) + { + res = j; + absres = abs (elem (i, j)); + } + result.elem (i) = res + 1; + } } } @@ -2612,15 +2702,37 @@ for (int j = 0; j < nc; j++) { - Complex res = elem (0, j); - double absres = abs (res); - for (int i = 1; i < nr; i++) - if (abs (elem (i, j)) < absres) + int column_is_real_only = 1; + for (int i = 0; i < nr; i++) + if (imag (elem (i, j)) != 0.0) { - res = elem (i, j); - absres = abs (res); + column_is_real_only = 0; + break; } - result.elem (j) = res; + + if (column_is_real_only) + { + double res = real (elem (0, j)); + for (int i = 1; i < nr; i++) + { + double tmp = real (elem (i, j)); + if (tmp < res) + res = tmp; + } + result.elem (j) = res; + } + else + { + Complex res = elem (0, j); + double absres = abs (res); + for (int i = 1; i < nr; i++) + if (abs (elem (i, j)) < absres) + { + res = elem (i, j); + absres = abs (res); + } + result.elem (j) = res; + } } } @@ -2641,15 +2753,36 @@ for (int j = 0; j < nc; j++) { - Complex res = 0; - double absres = abs (elem (0, j)); - for (int i = 0; i < nr; i++) - if (abs (elem (i, j)) < absres) - { - res = i; - absres = abs (elem (i, j)); - } - result.elem (j) = res + 1; + int column_is_real_only = 1; + for (int i = 0; i < nr; i++) + if (imag (elem (i, j)) != 0.0) + { + column_is_real_only = 0; + break; + } + + if (column_is_real_only) + { + double res = 0; + double tmp = real (elem (i, j)); + for (int i = 1; i < nr; i++) + if (real (elem (i, j)) < tmp) + res = i; + + result.elem (j) = res + 1; + } + else + { + Complex res = 0; + double absres = abs (elem (0, j)); + for (int i = 1; i < nr; i++) + if (abs (elem (i, j)) < absres) + { + res = i; + absres = abs (elem (i, j)); + } + result.elem (j) = res + 1; + } } } @@ -2670,15 +2803,37 @@ for (int j = 0; j < nc; j++) { - Complex res = elem (0, j); - double absres = abs (res); - for (int i = 1; i < nr; i++) - if (abs (elem (i, j)) > absres) + int column_is_real_only = 1; + for (int i = 0; i < nr; i++) + if (imag (elem (i, j)) != 0.0) { - res = elem (i, j); - absres = abs (res); + column_is_real_only = 0; + break; } - result.elem (j) = res; + + if (column_is_real_only) + { + double res = real (elem (0, j)); + for (int i = 1; i < nr; i++) + { + double tmp = real (elem (i, j)); + if (tmp > res) + res = tmp; + } + result.elem (j) = res; + } + else + { + Complex res = elem (0, j); + double absres = abs (res); + for (int i = 1; i < nr; i++) + if (abs (elem (i, j)) > absres) + { + res = elem (i, j); + absres = abs (res); + } + result.elem (j) = res; + } } } @@ -2699,15 +2854,36 @@ for (int j = 0; j < nc; j++) { - Complex res = 0; - double absres = abs (elem (0, j)); - for (int i = 0; i < nr; i++) - if (abs (elem (i, j)) > absres) - { - res = i; - absres = abs (elem (i, j)); - } - result.elem (j) = res + 1; + int column_is_real_only = 1; + for (int i = 0; i < nr; i++) + if (imag (elem (i, j)) != 0.0) + { + column_is_real_only = 0; + break; + } + + if (column_is_real_only) + { + double res = 0; + double tmp = real (elem (i, j)); + for (int i = 1; i < nr; i++) + if (real (elem (i, j)) > tmp) + res = i; + + result.elem (j) = res + 1; + } + else + { + Complex res = 0; + double absres = abs (elem (0, j)); + for (int i = 1; i < nr; i++) + if (abs (elem (i, j)) > absres) + { + res = i; + absres = abs (elem (i, j)); + } + result.elem (j) = res + 1; + } } }
--- a/src/minmax.cc +++ b/src/minmax.cc @@ -41,8 +41,8 @@ #define MIN(a,b) ((a) < (b) ? (a) : (b)) #endif -// This file could probably be condensed quite a bit by an appropriate -// amount of C preprocessor abuse. +// XXX FIXME XXX -- it would be nice to share code among the min/max +// functions below. static Matrix min (double d, const Matrix& m) @@ -91,14 +91,16 @@ double abs_c = abs (c); for (int j = 0; j < nc; j++) - for (int i = 0; i < nr; i++) - { - double abs_m_elem = abs (m.elem (i, j)); - if (abs_c < abs_m_elem) - result.elem (i, j) = c; - else - result.elem (i, j) = m.elem (i, j); - } + { + for (int i = 0; i < nr; i++) + { + double abs_m_elem = abs (m.elem (i, j)); + if (abs_c < abs_m_elem) + result.elem (i, j) = c; + else + result.elem (i, j) = m.elem (i, j); + } + } return result; } @@ -164,15 +166,40 @@ ComplexMatrix result (nr, nc); for (int j = 0; j < nc; j++) - for (int i = 0; i < nr; i++) - { - double abs_a_elem = abs (a.elem (i, j)); - double abs_b_elem = abs (b.elem (i, j)); - if (abs_a_elem < abs_b_elem) - result.elem (i, j) = a.elem (i, j); - else - result.elem (i, j) = b.elem (i, j); - } + { + int columns_are_real_only = 1; + for (int i = 0; i < nr; i++) + if (imag (a.elem (i, j)) != 0.0 && imag (b.elem (i, j)) != 0.0) + { + columns_are_real_only = 0; + break; + } + + if (columns_are_real_only) + { + for (int i = 0; i < nr; i++) + { + double a_elem = real (a.elem (i, j)); + double b_elem = real (b.elem (i, j)); + if (a_elem < b_elem) + result.elem (i, j) = a_elem; + else + result.elem (i, j) = b_elem; + } + } + else + { + for (int i = 0; i < nr; i++) + { + double abs_a_elem = abs (a.elem (i, j)); + double abs_b_elem = abs (b.elem (i, j)); + if (abs_a_elem < abs_b_elem) + result.elem (i, j) = a.elem (i, j); + else + result.elem (i, j) = b.elem (i, j); + } + } + } return result; } @@ -297,15 +324,40 @@ ComplexMatrix result (nr, nc); for (int j = 0; j < nc; j++) - for (int i = 0; i < nr; i++) - { - double abs_a_elem = abs (a.elem (i, j)); - double abs_b_elem = abs (b.elem (i, j)); - if (abs_a_elem > abs_b_elem) - result.elem (i, j) = a.elem (i, j); - else - result.elem (i, j) = b.elem (i, j); - } + { + int columns_are_real_only = 1; + for (int i = 0; i < nr; i++) + if (imag (a.elem (i, j)) != 0.0 && imag (b.elem (i, j)) != 0.0) + { + columns_are_real_only = 0; + break; + } + + if (columns_are_real_only) + { + for (int i = 0; i < nr; i++) + { + double a_elem = real (a.elem (i, j)); + double b_elem = real (b.elem (i, j)); + if (a_elem > b_elem) + result.elem (i, j) = a_elem; + else + result.elem (i, j) = b_elem; + } + } + else + { + for (int i = 0; i < nr; i++) + { + double abs_a_elem = abs (a.elem (i, j)); + double abs_b_elem = abs (b.elem (i, j)); + if (abs_a_elem > abs_b_elem) + result.elem (i, j) = a.elem (i, j); + else + result.elem (i, j) = b.elem (i, j); + } + } + } return result; }