1 #ifndef SimTK_SimTKCOMMON_VECTOR_MATH_H_
2 #define SimTK_SimTKCOMMON_VECTOR_MATH_H_
45 #define SimTK_ELEMENTWISE_FUNCTION(func) \
46 template <class ELEM> \
47 VectorBase<ELEM> func(const VectorBase<ELEM>& v) { \
48 const int size = v.size(); \
49 Vector_<ELEM> temp(size); \
50 for (int i = 0; i < size; ++i) \
51 temp[i] = std::func(v[i]); \
54 template <class ELEM> \
55 RowVectorBase<ELEM> func(const RowVectorBase<ELEM>& v){\
56 const int size = v.size(); \
57 RowVector_<ELEM> temp(size); \
58 for (int i = 0; i < size; ++i) \
59 temp[i] = std::func(v[i]); \
62 template <class ELEM> \
63 MatrixBase<ELEM> func(const MatrixBase<ELEM>& v) { \
64 const int rows = v.nrow(), cols = v.ncol(); \
65 Matrix_<ELEM> temp(rows, cols); \
66 for (int i = 0; i < rows; ++i) \
67 for (int j = 0; j < cols; ++j) \
68 temp(i, j) = std::func(v(i, j)); \
71 template <int N, class ELEM> \
72 Vec<N, ELEM> func(Vec<N, ELEM> v) { \
73 for (int i = 0; i < N; ++i) \
74 v[i] = std::func(v[i]); \
77 template <int N, class ELEM> \
78 Row<N, ELEM> func(Row<N, ELEM> v) { \
79 for (int i = 0; i < N; ++i) \
80 v[i] = std::func(v[i]); \
83 template <int M, int N, class ELEM> \
84 Mat<M, N, ELEM> func(Mat<M, N, ELEM> v) { \
85 for (int i = 0; i < M; ++i) \
86 for (int j = 0; j < N; ++j) \
87 v(i, j) = std::func(v(i, j)); \
90 template <int N, class ELEM> \
91 SymMat<N, ELEM> func(SymMat<N, ELEM> v) { \
92 for (int i = 0; i < N; ++i) \
93 for (int j = 0; j <= i; ++j) \
94 v(i, j) = std::func(v(i, j)); \
111 #undef SimTK_ELEMENTWISE_FUNCTION
115 template <
class ELEM>
119 template <
class ELEM>
123 template <
class ELEM>
127 template <
int N,
class ELEM>
131 template <
int N,
class ELEM>
135 template <
int M,
int N,
class ELEM>
139 template <
int N,
class ELEM>
146 template <
class ELEM>
150 template <
class ELEM>
154 template <
class ELEM>
158 template <
int N,
class ELEM>
162 template <
int N,
class ELEM>
166 template <
int M,
int N,
class ELEM>
170 template <
int N,
class ELEM>
177 template <
class ELEM>
179 const int size = v.
size();
181 for (
int i = 0; i < size; ++i) {
188 template <
class ELEM>
190 const int size = v.
size();
192 for (
int i = 0; i < size; ++i) {
199 template <
class ELEM>
203 for (
int i = 0; i < cols; ++i)
207 template <
int N,
class ELEM>
210 for (
int i = 0; i < N; ++i) {
217 template <
int N,
class ELEM>
220 for (
int i = 0; i < N; ++i) {
227 template <
int M,
int N,
class ELEM>
230 for (
int i = 0; i < N; ++i)
234 template <
int N,
class ELEM>
237 for (
int i = 1; i < N; ++i)
238 for (
int j = 0; j < i; ++j) {
250 template <
class ELEM>
252 const int size = v.
size();
254 for (
int i = 0; i < size; ++i) {
261 template <
class ELEM>
263 const int size = v.
size();
265 for (
int i = 0; i < size; ++i) {
272 template <
class ELEM>
276 for (
int i = 0; i < cols; ++i)
280 template <
int N,
class ELEM>
283 for (
int i = 0; i < N; ++i) {
290 template <
int N,
class ELEM>
293 for (
int i = 0; i < N; ++i) {
300 template <
int M,
int N,
class ELEM>
303 for (
int i = 0; i < N; ++i)
307 template <
int N,
class ELEM>
310 for (
int i = 1; i < N; ++i)
311 for (
int j = 0; j < i; ++j) {
323 template <
class ELEM>
327 template <
class ELEM>
331 template <
class ELEM>
335 template <
int N,
class ELEM>
339 template <
int N,
class ELEM>
343 template <
int M,
int N,
class ELEM>
347 template <
int N,
class ELEM>
354 template <
class ELEM>
356 const int size = v.
size();
361 template <
class ELEM>
363 const int size = v.
size();
368 template <
class ELEM>
370 const int rows = v.
nrow(), cols = v.
ncol();
372 for (
int i = 0; i < cols; ++i)
376 template <
int N,
class ELEM>
378 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
382 template <
int N,
class ELEM>
384 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
388 template <
int M,
int N,
class ELEM>
390 for (
int i = 0; i < N; ++i)
394 template <
int N,
class ELEM>
401 template <
class ELEM,
class RandomAccessIterator>
402 ELEM
median(RandomAccessIterator start, RandomAccessIterator end) {
403 const ptrdiff_t size = (ptrdiff_t)(end-start);
404 RandomAccessIterator mid = start+(size-1)/2;
405 std::nth_element(start, mid, end);
406 if (size%2 == 0 && mid+1 < end) {
412 RandomAccessIterator
min = mid+1;
413 for (RandomAccessIterator iter = mid+1; iter < end; iter++) {
417 return (*mid+*min)/2;
421 template <
class ELEM>
424 return median<ELEM>(temp.
begin(), temp.
end());
426 template <
class ELEM>
429 return median<ELEM>(temp.
begin(), temp.
end());
431 template <
class ELEM>
433 int cols = v.
ncol(), rows = v.
nrow();
436 for (
int i = 0; i < cols; ++i) {
438 temp[i] = median<ELEM>(column);
442 template <
int N,
class ELEM>
444 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
445 return median<ELEM>(pointer, pointer+N);
447 template <
int N,
class ELEM>
449 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
450 return median<ELEM>(pointer, pointer+N);
452 template <
int M,
int N,
class ELEM>
455 for (
int i = 0; i < N; ++i)
459 template <
int N,
class ELEM>
466 #endif // SimTK_SimTKCOMMON_VECTOR_MATH_H_
VectorIterator< ELT, VectorBase< ELT > > begin()
Definition: BigMatrix.h:1473
TAbs abs() const
Elementwise absolute value; that is, the return value has the same dimension as this Vec but with eac...
Definition: Vec.h:292
const TDiag & getDiag() const
Definition: SymMat.h:802
EStandard sum() const
Definition: Row.h:240
const E & getEltLower(int i, int j) const
Definition: SymMat.h:822
RS is total spacing between rows in memory (default 1)
Definition: SymMat.h:71
This is a dataless rehash of the MatrixBase class to specialize it for RowVectors.
Definition: BigMatrix.h:177
TAbs abs() const
Definition: Row.h:226
ELEM median(RandomAccessIterator start, RandomAccessIterator end)
Definition: VectorMath.h:402
Mat< N, N, ELEM > sort(const SymMat< N, ELEM > &v)
Definition: VectorMath.h:395
VectorView_< ELT > updCol(int j)
Definition: BigMatrix.h:2341
int size() const
Definition: BigMatrix.h:1411
ELEM min(const VectorBase< ELEM > &v)
Definition: VectorMath.h:178
SimTK_ELEMENTWISE_FUNCTION(exp) SimTK_ELEMENTWISE_FUNCTION(log) SimTK_ELEMENTWISE_FUNCTION(sqrt) SimTK_ELEMENTWISE_FUNCTION(sin) SimTK_ELEMENTWISE_FUNCTION(cos) SimTK_ELEMENTWISE_FUNCTION(tan) SimTK_ELEMENTWISE_FUNCTION(asin) SimTK_ELEMENTWISE_FUNCTION(acos) SimTK_ELEMENTWISE_FUNCTION(atan) SimTK_ELEMENTWISE_FUNCTION(sinh) SimTK_ELEMENTWISE_FUNCTION(cosh) SimTK_ELEMENTWISE_FUNCTION(tanh) template< class ELEM > VectorBase< typename CNT< ELEM >
Definition: VectorMath.h:98
void abs(TAbs &mabs) const
abs() is elementwise absolute value; that is, the return value has the same dimension as this Matrix ...
Definition: BigMatrix.h:864
ELEM mean(const VectorBase< ELEM > &v)
Definition: VectorMath.h:324
ELEM sum(const VectorBase< ELEM > &v)
Definition: VectorMath.h:147
EStandard sum() const
Sum just adds up all the elements into a single return element that is the same type as this Vec's el...
Definition: Vec.h:311
This is a fixed length column vector designed for no-overhead inline computation. ...
Definition: Vec.h:131
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition: BigMatrix.h:288
ELT sum() const
Definition: BigMatrix.h:1472
TRow sum() const
This is an alternate name for colSum(); behaves like the Matlab function of the same name...
Definition: SymMat.h:842
RowVector_< ELT > sum() const
Alternate name for colSum(); behaves like the Matlab function sum().
Definition: BigMatrix.h:925
VectorBase< ELEM > sort(const VectorBase< ELEM > &v)
Definition: VectorMath.h:355
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
VectorIterator< ELT, RowVectorBase< ELT > > begin()
Definition: BigMatrix.h:1751
VectorIterator< ELT, RowVectorBase< ELT > > end()
Definition: BigMatrix.h:1754
TAbs abs() const
Elementwise absolute value; that is, the return value has the same dimensions as this Mat but with ea...
Definition: Mat.h:180
int size() const
Definition: BigMatrix.h:1690
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120
TAbs abs() const
Definition: BigMatrix.h:1421
Generic Row.
Definition: Row.h:118
const TCol & col(int j) const
Definition: Mat.h:729
TAbs abs() const
Definition: SymMat.h:179
This is a dataless rehash of the MatrixBase class to specialize it for Vectors.
Definition: BigMatrix.h:176
VectorIterator< ELT, VectorBase< ELT > > end()
Definition: BigMatrix.h:1476
ELT sum() const
Definition: BigMatrix.h:1750
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: Mat.h:51
Variable-size 2d matrix of Composite Numerical Type (ELT) elements.
Definition: BigMatrix.h:175
This is the header which should be included in user programs that would like to make use of all the S...
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition: BigMatrix.h:290
Includes internal headers providing declarations for the basic SimTK Core classes.
TRow sum() const
This is an alternate name for colSum(); behaves like the Matlab function of the same name...
Definition: Mat.h:1159
TAbs abs() const
Definition: BigMatrix.h:1700
VectorView_< ELT > col(int j) const
Definition: BigMatrix.h:2332