Commit 6e4c77cd authored by Davis King's avatar Davis King

Updated the min and max functions so that they work with complex

numbers in the same manner as MATLAB.
parent 7f5dbb71
...@@ -64,6 +64,14 @@ namespace dlib ...@@ -64,6 +64,14 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
namespace impl
{
template <typename T>
const T& magnitude (const T& item) { return item; }
template <typename T>
T magnitude (const std::complex<T>& item) { return std::norm(item); }
}
template < template <
typename EXP typename EXP
> >
...@@ -87,9 +95,9 @@ namespace dlib ...@@ -87,9 +95,9 @@ namespace dlib
for (long c = 0; c < m.nc(); ++c) for (long c = 0; c < m.nc(); ++c)
{ {
type temp = m(r,c); type temp = m(r,c);
if (temp > max_val) if (dlib::impl::magnitude(temp) > dlib::impl::magnitude(max_val))
max_val = temp; max_val = temp;
if (temp < min_val) if (dlib::impl::magnitude(temp) < dlib::impl::magnitude(min_val))
min_val = temp; min_val = temp;
} }
} }
...@@ -118,7 +126,7 @@ namespace dlib ...@@ -118,7 +126,7 @@ namespace dlib
for (long i = 1; i < m.size(); ++i) for (long i = 1; i < m.size(); ++i)
{ {
type temp = m(i); type temp = m(i);
if (temp > val) if (dlib::impl::magnitude(temp) > dlib::impl::magnitude(val))
{ {
val = temp; val = temp;
best_idx = i; best_idx = i;
...@@ -150,7 +158,7 @@ namespace dlib ...@@ -150,7 +158,7 @@ namespace dlib
for (long i = 1; i < m.size(); ++i) for (long i = 1; i < m.size(); ++i)
{ {
type temp = m(i); type temp = m(i);
if (temp < val) if (dlib::impl::magnitude(temp) < dlib::impl::magnitude(val))
{ {
val = temp; val = temp;
best_idx = i; best_idx = i;
...@@ -181,7 +189,7 @@ namespace dlib ...@@ -181,7 +189,7 @@ namespace dlib
for (long c = 0; c < m.nc(); ++c) for (long c = 0; c < m.nc(); ++c)
{ {
type temp = m(r,c); type temp = m(r,c);
if (temp > val) if (dlib::impl::magnitude(temp) > dlib::impl::magnitude(val))
val = temp; val = temp;
} }
} }
...@@ -210,7 +218,7 @@ namespace dlib ...@@ -210,7 +218,7 @@ namespace dlib
for (long c = 0; c < m.nc(); ++c) for (long c = 0; c < m.nc(); ++c)
{ {
type temp = m(r,c); type temp = m(r,c);
if (temp < val) if (dlib::impl::magnitude(temp) < dlib::impl::magnitude(val))
val = temp; val = temp;
} }
} }
......
...@@ -1355,7 +1355,9 @@ namespace dlib ...@@ -1355,7 +1355,9 @@ namespace dlib
requires requires
- m.size() > 0 - m.size() > 0
ensures ensures
- returns the value of the smallest element of m - returns the value of the smallest element of m. If m contains complex
elements then the element returned is the one with the smallest norm
according to std::norm().
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -1367,7 +1369,9 @@ namespace dlib ...@@ -1367,7 +1369,9 @@ namespace dlib
requires requires
- m.size() > 0 - m.size() > 0
ensures ensures
- returns the value of the biggest element of m - returns the value of the biggest element of m. If m contains complex
elements then the element returned is the one with the largest norm
according to std::norm().
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment