1#ifndef SimTK_SimTKCOMMON_VECTOR_MATH_H_
2#define SimTK_SimTKCOMMON_VECTOR_MATH_H_
45#define SimTK_ELEMENTWISE_FUNCTION(func) \
46template <class ELEM> \
47VectorBase<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]); \
54template <class ELEM> \
55RowVectorBase<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]); \
62template <class ELEM> \
63MatrixBase<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)); \
71template <int N, class ELEM> \
72Vec<N, ELEM> func(Vec<N, ELEM> v) { \
73 for (int i = 0; i < N; ++i) \
74 v[i] = std::func(v[i]); \
77template <int N, class ELEM> \
78Row<N, ELEM> func(Row<N, ELEM> v) { \
79 for (int i = 0; i < N; ++i) \
80 v[i] = std::func(v[i]); \
83template <int M, int N, class ELEM> \
84Mat<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)); \
90template <int N, class ELEM> \
91SymMat<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
127template <
int N,
class ELEM>
131template <
int N,
class ELEM>
135template <
int M,
int N,
class ELEM>
139template <
int N,
class ELEM>
158template <
int N,
class ELEM>
162template <
int N,
class ELEM>
166template <
int M,
int N,
class ELEM>
170template <
int N,
class ELEM>
179 const int size = v.
size();
181 for (
int i = 0; i < size; ++i) {
190 const int size = v.
size();
192 for (
int i = 0; i < size; ++i) {
203 for (
int i = 0; i < cols; ++i)
207template <
int N,
class ELEM>
210 for (
int i = 0; i < N; ++i) {
217template <
int N,
class ELEM>
220 for (
int i = 0; i < N; ++i) {
227template <
int M,
int N,
class ELEM>
230 for (
int i = 0; i < N; ++i)
234template <
int N,
class ELEM>
237 for (
int i = 1; i < N; ++i)
238 for (
int j = 0; j < i; ++j) {
252 const int size = v.
size();
254 for (
int i = 0; i < size; ++i) {
263 const int size = v.
size();
265 for (
int i = 0; i < size; ++i) {
276 for (
int i = 0; i < cols; ++i)
280template <
int N,
class ELEM>
283 for (
int i = 0; i < N; ++i) {
290template <
int N,
class ELEM>
293 for (
int i = 0; i < N; ++i) {
300template <
int M,
int N,
class ELEM>
303 for (
int i = 0; i < N; ++i)
307template <
int N,
class ELEM>
310 for (
int i = 1; i < N; ++i)
311 for (
int j = 0; j < i; ++j) {
335template <
int N,
class ELEM>
339template <
int N,
class ELEM>
343template <
int M,
int N,
class ELEM>
347template <
int N,
class ELEM>
357 std::sort(temp.
begin(), temp.
end());
363 std::sort(temp.
begin(), temp.
end());
368 const int cols = v.
ncol();
370 for (
int i = 0; i < cols; ++i)
374template <
int N,
class ELEM>
376 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
377 std::sort(pointer, pointer+N);
380template <
int N,
class ELEM>
382 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
383 std::sort(pointer, pointer+N);
386template <
int M,
int N,
class ELEM>
388 for (
int i = 0; i < N; ++i)
392template <
int N,
class ELEM>
399template <
class ELEM,
class RandomAccessIterator>
400ELEM
median(RandomAccessIterator start, RandomAccessIterator end) {
401 const ptrdiff_t size = (ptrdiff_t)(end-start);
402 RandomAccessIterator mid = start+(size-1)/2;
403 std::nth_element(start, mid, end);
404 if (size%2 == 0 && mid+1 < end) {
410 RandomAccessIterator
min = mid+1;
411 for (RandomAccessIterator iter = mid+1; iter < end; iter++) {
415 return (*mid+*
min)/2;
422 return median<ELEM>(temp.
begin(), temp.
end());
427 return median<ELEM>(temp.
begin(), temp.
end());
431 int cols = v.
ncol(), rows = v.
nrow();
434 for (
int i = 0; i < cols; ++i) {
436 temp[i] = median<ELEM>(column);
440template <
int N,
class ELEM>
442 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
443 return median<ELEM>(pointer, pointer+N);
445template <
int N,
class ELEM>
447 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
448 return median<ELEM>(pointer, pointer+N);
450template <
int M,
int N,
class ELEM>
453 for (
int i = 0; i < N; ++i)
457template <
int N,
class ELEM>
This is the header which should be included in user programs that would like to make use of all the S...
#define SimTK_ELEMENTWISE_FUNCTION(func)
Definition VectorMath.h:45
Includes internal headers providing declarations for the basic SimTK Core classes.
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition Mat.h:97
TRow sum() const
This is an alternate name for colSum(); behaves like the Matlab function of the same name.
Definition Mat.h:1207
TAbs abs() const
Elementwise absolute value; that is, the return value has the same dimensions as this Mat but with ea...
Definition Mat.h:228
const TCol & col(int j) const
Definition Mat.h:777
This is the common base class for Simbody's Vector_ and Matrix_ classes for handling large,...
Definition MatrixBase.h:68
RowVector_< ELT > sum() const
Alternate name for colSum(); behaves like the Matlab function sum().
Definition MatrixBase.h:748
VectorView_< ELT > updCol(int j)
Definition BigMatrix.h:261
void abs(TAbs &mabs) const
abs() is elementwise absolute value; that is, the return value has the same dimension as this Matrix ...
Definition MatrixBase.h:687
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition MatrixBase.h:136
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition MatrixBase.h:138
VectorView_< ELT > col(int j) const
Definition BigMatrix.h:252
This is a dataless rehash of the MatrixBase class to specialize it for RowVectors.
Definition RowVectorBase.h:42
VectorIterator< ELT, RowVectorBase< ELT > > begin()
Definition RowVectorBase.h:298
TAbs abs() const
Definition RowVectorBase.h:247
ELT sum() const
Definition RowVectorBase.h:297
int size() const
Definition RowVectorBase.h:237
VectorIterator< ELT, RowVectorBase< ELT > > end()
Definition RowVectorBase.h:301
This is a fixed-length row vector designed for no-overhead inline computation.
Definition Row.h:132
EStandard sum() const
Definition Row.h:254
TAbs abs() const
Definition Row.h:240
This is a small, fixed-size symmetric or Hermitian matrix designed for no-overhead inline computation...
Definition SymMat.h:87
const TDiag & getDiag() const
Definition SymMat.h:818
TAbs abs() const
Definition SymMat.h:195
TRow sum() const
This is an alternate name for colSum(); behaves like the Matlab function of the same name.
Definition SymMat.h:858
const E & getEltLower(int i, int j) const
Definition SymMat.h:838
This is a fixed-length column vector designed for no-overhead inline computation.
Definition Vec.h:184
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:366
TAbs abs() const
Elementwise absolute value; that is, the return value has the same dimension as this Vec but with eac...
Definition Vec.h:347
This is a dataless rehash of the MatrixBase class to specialize it for Vectors.
Definition VectorBase.h:42
VectorIterator< ELT, VectorBase< ELT > > begin()
Definition VectorBase.h:458
int size() const
Definition VectorBase.h:396
ELT sum() const
Definition VectorBase.h:457
TAbs abs() const
Definition VectorBase.h:406
VectorIterator< ELT, VectorBase< ELT > > end()
Definition VectorBase.h:461
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition Assembler.h:37
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition VectorMath.h:120
VectorBase< ELEM > sort(const VectorBase< ELEM > &v)
Definition VectorMath.h:355
ELEM max(const VectorBase< ELEM > &v)
Definition VectorMath.h:251
ELEM min(const VectorBase< ELEM > &v)
Definition VectorMath.h:178
ELEM sum(const VectorBase< ELEM > &v)
Definition VectorMath.h:147
ELEM mean(const VectorBase< ELEM > &v)
Definition VectorMath.h:324
ELEM median(RandomAccessIterator start, RandomAccessIterator end)
Definition VectorMath.h:400