min_max.h
Go to the documentation of this file.
#ifndef MIN_MAX_H
#define MIN_MAX_H
#include <itpp/base/mat.h>
namespace itpp
{
template<class T>
T max(const Vec<T> &v)
{
T maxdata = v(0);
for (int i = 1; i < v.length(); i++)
if (v(i) > maxdata)
maxdata = v(i);
return maxdata;
}
template<class T>
T max(const Vec<T> &v, int& index)
{
T maxdata = v(0);
index = 0;
for (int i = 1; i < v.length(); i++)
if (v(i) > maxdata) {
maxdata = v(i);
index = i;
}
return maxdata;
}
template<class T>
Vec<T> max(const Mat<T> &m, int dim = 1)
{
it_assert((dim == 1) || (dim == 2), "max(): dimension need to be 1 or 2");
Vec<T> out;
if (dim == 1) {
out.set_size(m.cols(), false);
for (int i = 0; i < m.cols(); i++)
out(i) = max(m.get_col(i));
}
else {
out.set_size(m.rows(), false);
for (int i = 0; i < m.rows(); i++)
out(i) = max(m.get_row(i));
}
return out;
}
template<class T>
Vec<T> max(const Mat<T> &m, ivec &index, int dim = 1)
{
it_assert((dim == 1) || (dim == 2), "max(): dimension need to be 1 or 2");
Vec<T> out;
if (dim == 1) {
out.set_size(m.cols(), false);
index.set_size(m.cols(), false);
for (int i = 0; i < m.cols(); i++)
out(i) = max(m.get_col(i), index(i));
}
else {
out.set_size(m.rows(), false);
index.set_size(m.rows(), false);
for (int i = 0; i < m.rows(); i++)
out(i) = max(m.get_row(i), index(i));
}
return out;
}
template<class T>
T min(const Vec<T> &in)
{
T mindata = in[0];
for (int i = 1; i < in.length(); i++)
if (in[i] < mindata)
mindata = in[i];
return mindata;
}
template<class T>
T min(const Vec<T> &in, int& index)
{
T mindata = in[0];
index = 0;
for (int i = 1; i < in.length(); i++)
if (in[i] < mindata) {
mindata = in[i];
index = i;
}
return mindata;
}
template<class T>
Vec<T> min(const Mat<T> &m, int dim = 1)
{
it_assert((dim == 1) || (dim == 2), "min(): dimension need to be 1 or 2");
Vec<T> out;
if (dim == 1) {
out.set_size(m.cols(), false);
for (int i = 0; i < m.cols(); i++)
out(i) = min(m.get_col(i));
}
else {
out.set_size(m.rows(), false);
for (int i = 0; i < m.rows(); i++)
out(i) = min(m.get_row(i));
}
return out;
}
template<class T>
Vec<T> min(const Mat<T> &m, ivec &index, int dim = 1)
{
it_assert((dim == 1) || (dim == 2), "min(): dimension need to be 1 or 2");
Vec<T> out;
if (dim == 1) {
out.set_size(m.cols(), false);
index.set_size(m.cols(), false);
for (int i = 0; i < m.cols(); i++)
out(i) = min(m.get_col(i), index(i));
}
else {
out.set_size(m.rows(), false);
index.set_size(m.rows(), false);
for (int i = 0; i < m.rows(); i++)
out(i) = min(m.get_row(i), index(i));
}
return out;
}
template<class T>
int max_index(const Vec<T> &in)
{
int maxindex = 0;
for (int i = 1; i < in.length(); i++)
if (in[i] > in[maxindex])
maxindex = i;
return maxindex;
}
template<class T>
void max_index(const Mat<T> &m, int &row, int &col)
{
T maxdata = m(0, 0);
row = col = 0;
for (int i = 0; i < m.rows(); i++)
for (int j = 0; j < m.cols(); j++)
if (m(i, j) > maxdata) {
row = i;
col = j;
maxdata = m(i, j);
}
}
template<class T>
int min_index(const Vec<T> &in)
{
int minindex = 0;
for (int i = 1; i < in.length(); i++)
if (in[i] < in[minindex])
minindex = i;
return minindex;
}
template<class T>
void min_index(const Mat<T> &m, int &row, int &col)
{
T mindata = m(0, 0);
row = col = 0;
for (int i = 0; i < m.rows(); i++)
for (int j = 0; j < m.cols(); j++)
if (m(i, j) < mindata) {
row = i;
col = j;
mindata = m(i, j);
}
}
} //namespace itpp
#endif /* MIN_MAX_H */
itpp::Array
General array class.
Definition array.h:105
itpp::Array::set_size
void set_size(int n, bool copy=false)
Resizing an Array<T>.
Definition array.h:257
itpp::Array::length
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition itassert.h:94
itpp::min_index
int min_index(const Vec< T > &in)
Return the postion of the minimum element in the vector.
Definition min_max.h:234
itpp::min
T min(const Vec< T > &in)
Minimum value of vector.
Definition min_max.h:125
itpp::max
T max(const Vec< T > &v)
Maximum value of vector.
Definition min_max.h:45
itpp::max_index
int max_index(const Vec< T > &in)
Return the postion of the maximum element in the vector.
Definition min_max.h:208
mat.h
Matrix Class Definitions.
itpp
itpp namespace
Definition itmex.h:37

Generated on Mon Jun 10 2024 11:49:18 for IT++ by Doxygen 1.9.8