IT++ Logo
min_max.h
Go to the documentation of this file.
1
29#ifndef MIN_MAX_H
30#define MIN_MAX_H
31
32#include <itpp/base/mat.h>
33
34
35namespace itpp
36{
37
44template<class T>
45T max(const Vec<T> &v)
46{
47 T maxdata = v(0);
48 for (int i = 1; i < v.length(); i++)
49 if (v(i) > maxdata)
50 maxdata = v(i);
51 return maxdata;
52}
53
55template<class T>
56T max(const Vec<T> &v, int& index)
57{
58 T maxdata = v(0);
59 index = 0;
60 for (int i = 1; i < v.length(); i++)
61 if (v(i) > maxdata) {
62 maxdata = v(i);
63 index = i;
64 }
65 return maxdata;
66}
67
75template<class T>
76Vec<T> max(const Mat<T> &m, int dim = 1)
77{
78 it_assert((dim == 1) || (dim == 2), "max(): dimension need to be 1 or 2");
79 Vec<T> out;
80 if (dim == 1) {
81 out.set_size(m.cols(), false);
82 for (int i = 0; i < m.cols(); i++)
83 out(i) = max(m.get_col(i));
84 }
85 else {
86 out.set_size(m.rows(), false);
87 for (int i = 0; i < m.rows(); i++)
88 out(i) = max(m.get_row(i));
89 }
90 return out;
91}
92
103template<class T>
104Vec<T> max(const Mat<T> &m, ivec &index, int dim = 1)
105{
106 it_assert((dim == 1) || (dim == 2), "max(): dimension need to be 1 or 2");
107 Vec<T> out;
108 if (dim == 1) {
109 out.set_size(m.cols(), false);
110 index.set_size(m.cols(), false);
111 for (int i = 0; i < m.cols(); i++)
112 out(i) = max(m.get_col(i), index(i));
113 }
114 else {
115 out.set_size(m.rows(), false);
116 index.set_size(m.rows(), false);
117 for (int i = 0; i < m.rows(); i++)
118 out(i) = max(m.get_row(i), index(i));
119 }
120 return out;
121}
122
124template<class T>
125T min(const Vec<T> &in)
126{
127 T mindata = in[0];
128 for (int i = 1; i < in.length(); i++)
129 if (in[i] < mindata)
130 mindata = in[i];
131 return mindata;
132}
133
135template<class T>
136T min(const Vec<T> &in, int& index)
137{
138 T mindata = in[0];
139 index = 0;
140 for (int i = 1; i < in.length(); i++)
141 if (in[i] < mindata) {
142 mindata = in[i];
143 index = i;
144 }
145 return mindata;
146}
147
148
156template<class T>
157Vec<T> min(const Mat<T> &m, int dim = 1)
158{
159 it_assert((dim == 1) || (dim == 2), "min(): dimension need to be 1 or 2");
160 Vec<T> out;
161 if (dim == 1) {
162 out.set_size(m.cols(), false);
163 for (int i = 0; i < m.cols(); i++)
164 out(i) = min(m.get_col(i));
165 }
166 else {
167 out.set_size(m.rows(), false);
168 for (int i = 0; i < m.rows(); i++)
169 out(i) = min(m.get_row(i));
170 }
171 return out;
172}
173
174
185template<class T>
186Vec<T> min(const Mat<T> &m, ivec &index, int dim = 1)
187{
188 it_assert((dim == 1) || (dim == 2), "min(): dimension need to be 1 or 2");
189 Vec<T> out;
190 if (dim == 1) {
191 out.set_size(m.cols(), false);
192 index.set_size(m.cols(), false);
193 for (int i = 0; i < m.cols(); i++)
194 out(i) = min(m.get_col(i), index(i));
195 }
196 else {
197 out.set_size(m.rows(), false);
198 index.set_size(m.rows(), false);
199 for (int i = 0; i < m.rows(); i++)
200 out(i) = min(m.get_row(i), index(i));
201 }
202 return out;
203}
204
205
207template<class T>
208int max_index(const Vec<T> &in)
209{
210 int maxindex = 0;
211 for (int i = 1; i < in.length(); i++)
212 if (in[i] > in[maxindex])
213 maxindex = i;
214 return maxindex;
215}
216
218template<class T>
219void max_index(const Mat<T> &m, int &row, int &col)
220{
221 T maxdata = m(0, 0);
222 row = col = 0;
223 for (int i = 0; i < m.rows(); i++)
224 for (int j = 0; j < m.cols(); j++)
225 if (m(i, j) > maxdata) {
226 row = i;
227 col = j;
228 maxdata = m(i, j);
229 }
230}
231
233template<class T>
234int min_index(const Vec<T> &in)
235{
236 int minindex = 0;
237 for (int i = 1; i < in.length(); i++)
238 if (in[i] < in[minindex])
239 minindex = i;
240 return minindex;
241}
242
244template<class T>
245void min_index(const Mat<T> &m, int &row, int &col)
246{
247 T mindata = m(0, 0);
248 row = col = 0;
249 for (int i = 0; i < m.rows(); i++)
250 for (int j = 0; j < m.cols(); j++)
251 if (m(i, j) < mindata) {
252 row = i;
253 col = j;
254 mindata = m(i, j);
255 }
256}
257
262} //namespace itpp
263
264
265#endif /* MIN_MAX_H */
Vec< Num_T > get_row(int r) const
Get row r.
Definition: mat.h:852
int rows() const
The number of rows.
Definition: mat.h:237
int cols() const
The number of columns.
Definition: mat.h:235
Vec< Num_T > get_col(int c) const
Get column c.
Definition: mat.h:889
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
Definition: vec.h:663
int length() const
The size of the vector.
Definition: vec.h:269
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
int min_index(const Vec< T > &in)
Return the postion of the minimum element in the vector.
Definition: min_max.h:234
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
T max(const Vec< T > &v)
Maximum value of vector.
Definition: min_max.h:45
int max_index(const Vec< T > &in)
Return the postion of the maximum element in the vector.
Definition: min_max.h:208
Matrix Class Definitions.
itpp namespace
Definition: itmex.h:37

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.4