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>
360 template <
class ELEM>
366 template <
class ELEM>
368 const int cols = v.
ncol();
370 for (
int i = 0; i < cols; ++i)
374 template <
int N,
class ELEM>
376 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
380 template <
int N,
class ELEM>
382 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
386 template <
int M,
int N,
class ELEM>
388 for (
int i = 0; i < N; ++i)
392 template <
int N,
class ELEM>
399 template <
class ELEM,
class RandomAccessIterator>
400 ELEM
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;
419 template <
class ELEM>
422 return median<ELEM>(temp.
begin(), temp.
end());
424 template <
class ELEM>
427 return median<ELEM>(temp.
begin(), temp.
end());
429 template <
class ELEM>
431 int cols = v.
ncol(), rows = v.
nrow();
434 for (
int i = 0; i < cols; ++i) {
436 temp[i] = median<ELEM>(column);
440 template <
int N,
class ELEM>
442 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
443 return median<ELEM>(pointer, pointer+N);
445 template <
int N,
class ELEM>
447 ELEM* pointer =
reinterpret_cast<ELEM*
>(&v);
448 return median<ELEM>(pointer, pointer+N);
450 template <
int M,
int N,
class ELEM>
453 for (
int i = 0; i < N; ++i)
457 template <
int N,
class ELEM>
464 #endif // SimTK_SimTKCOMMON_VECTOR_MATH_H_
VectorView_< ELT > col(int j) const
Definition: BigMatrix.h:252
RowVector_< ELT > sum() const
Alternate name for colSum(); behaves like the Matlab function sum().
Definition: MatrixBase.h:749
const TDiag & getDiag() const
Definition: SymMat.h:818
TAbs abs() const
Definition: VectorBase.h:406
ELEM median(RandomAccessIterator start, RandomAccessIterator end)
Definition: VectorMath.h:400
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
TAbs abs() const
Definition: SymMat.h:195
TAbs abs() const
Elementwise absolute value; that is, the return value has the same dimensions as this Mat but with ea...
Definition: Mat.h:226
VectorIterator< ELT, RowVectorBase< ELT > > end()
Definition: RowVectorBase.h:301
TAbs abs() const
Definition: Row.h:240
int size() const
Definition: VectorBase.h:396
Mat< N, N, ELEM > sort(const SymMat< N, ELEM > &v)
Definition: VectorMath.h:393
ELEM min(const VectorBase< ELEM > &v)
Definition: VectorMath.h:178
This is a dataless rehash of the MatrixBase class to specialize it for Vectors.
Definition: BigMatrix.h:164
TRow sum() const
This is an alternate name for colSum(); behaves like the Matlab function of the same name...
Definition: Mat.h:1205
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
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:364
This is a small, fixed-size symmetric or Hermitian matrix designed for no-overhead inline computation...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:592
EStandard sum() const
Definition: Row.h:254
VectorIterator< ELT, VectorBase< ELT > > end()
Definition: VectorBase.h:461
This is the common base class for Simbody's Vector_ and Matrix_ classes for handling large...
Definition: BigMatrix.h:163
VectorView_< ELT > updCol(int j)
Definition: BigMatrix.h:261
ELEM mean(const VectorBase< ELEM > &v)
Definition: VectorMath.h:324
ELEM sum(const VectorBase< ELEM > &v)
Definition: VectorMath.h:147
VectorIterator< ELT, VectorBase< ELT > > begin()
Definition: VectorBase.h:458
VectorBase< ELEM > sort(const VectorBase< ELEM > &v)
Definition: VectorMath.h:355
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
TRow sum() const
This is an alternate name for colSum(); behaves like the Matlab function of the same name...
Definition: SymMat.h:858
VectorIterator< ELT, RowVectorBase< ELT > > begin()
Definition: RowVectorBase.h:298
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition: MatrixBase.h:137
This is a fixed-length row vector designed for no-overhead inline computation.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:590
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition: MatrixBase.h:139
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:688
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120
TAbs abs() const
Elementwise absolute value; that is, the return value has the same dimension as this Vec but with eac...
Definition: Vec.h:345
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:591
This is a fixed-length column vector designed for no-overhead inline computation. ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:589
const E & getEltLower(int i, int j) const
Definition: SymMat.h:838
This is a dataless rehash of the MatrixBase class to specialize it for RowVectors.
Definition: BigMatrix.h:165
TAbs abs() const
Definition: RowVectorBase.h:247
ELT sum() const
Definition: RowVectorBase.h:297
This is the header which should be included in user programs that would like to make use of all the S...
Includes internal headers providing declarations for the basic SimTK Core classes.
int size() const
Definition: RowVectorBase.h:237
ELT sum() const
Definition: VectorBase.h:457
const TCol & col(int j) const
Definition: Mat.h:775