📄 tvmutil.h
字号:
TMP_ISLESSEQUAL(unsigned char)#undef TMP_ISLESSEQUAL template<class T1, class T2, int N>inline bool isLessEqual(const TinyVector<T1,N>&a, const TinyVector<T2,N>&b) { for (int i= 0; i<N ; ++i) if (a[i] > b[i]) return false; return true;}BZ_DECLARE_FUNCTION2_RET(isLessEqual,bool)// @}// }}}// {{{ isZero functions/** * @defgroup isZero_Functions Overloaded isZero functions * @brief Defines isZero function for int, double and TinyVector. * * isZero compares element by element and returns true only if all elements are zero. * */// @{#define TMP_ISZERO(TYPE) \inline bool isZero(const TYPE a) { \ return a == 0; \}TMP_ISZERO(double)TMP_ISZERO(long double)TMP_ISZERO(float)TMP_ISZERO(int)TMP_ISZERO(unsigned int)TMP_ISZERO(short)TMP_ISZERO(unsigned short)TMP_ISZERO(long)TMP_ISZERO(unsigned long)TMP_ISZERO(char)TMP_ISZERO(unsigned char)#undef TMP_ISZEROtemplate<class T, int N>inline bool isZero(const TinyVector<T,N>&a) { for (int i=0; i<N; ++i) if (a[i] != 0) return false; return true;}BZ_DECLARE_FUNCTION_RET(isZero,bool)// @}// }}}// {{{ sum functions/** * @defgroup sum_Functions Overloaded sum functions * @brief Defines sum function for int, double and TinyVector. * * Add elements of int, double and TinyVector and returns the sum of all elements. * */// @{#define TMP_SUM(TYPE) \inline TYPE sum(const TYPE a){ \ return a; \}TMP_SUM(double)TMP_SUM(long double)TMP_SUM(float)TMP_SUM(int)TMP_SUM(unsigned int)TMP_SUM(short)TMP_SUM(unsigned short)TMP_SUM(long)TMP_SUM(unsigned long)TMP_SUM(char)TMP_SUM(unsigned char)#undef TMP_SUM// @}// }}}// {{{ product functions/** * @defgroup product_Functions Overloaded product functions * @brief Defines product function for int, double and TinyVector. * * Add elements of int, double and TinyVector and returns the product of all elements. * */// @{#define TMP_PRODUCT(TYPE) \inline TYPE product(const TYPE a){ \ return a; \}TMP_PRODUCT(double)TMP_PRODUCT(long double)TMP_PRODUCT(float)TMP_PRODUCT(int)TMP_PRODUCT(unsigned int)TMP_PRODUCT(short)TMP_PRODUCT(unsigned short)TMP_PRODUCT(long)TMP_PRODUCT(unsigned long)TMP_PRODUCT(char)TMP_PRODUCT(unsigned char)#undef TMP_PRODUCT// // template<class T, int N>// inline T product(const TinyVector<T,N>&a) {// T value=1;// for (int i= 0; i<N ; ++i) // value *= a[i];// return value;// }// @}// }}}// {{{ max min functions/** * @defgroup Max_Min Overloaded Max Min functions * @brief Defines Max, Min function for int, double and TinyVector. * */// @{#define TMP_MAXMIN(TYPE) \inline TYPE MyMax(const TYPE a, const TYPE b){ \ return std::max(a,b); \} \inline TYPE MyMin(const TYPE a, const TYPE b){ \ return std::min(a,b); \}TMP_MAXMIN(double)TMP_MAXMIN(long double)TMP_MAXMIN(float)TMP_MAXMIN(int)TMP_MAXMIN(unsigned int)TMP_MAXMIN(short)TMP_MAXMIN(unsigned short)TMP_MAXMIN(long)TMP_MAXMIN(unsigned long)TMP_MAXMIN(char)TMP_MAXMIN(unsigned char)#undef TMP_MAXMINtemplate<class T, int N>inline TinyVector<T,N> MyMax(const TinyVector<T,N>&a, const TinyVector<T,N>&b) { return max(a,b);}template<class T, int N>inline TinyVector<T,N> MyMin(const TinyVector<T,N>&a, const TinyVector<T,N>&b) { return min(a,b);}// @}// }}}//{{{ Round functions/** * @defgroup round Overloaded round functions * @brief Defines round function for basic types and and TinyVector. * * round compares element by element and returns true only if all elements are equal. * */// @{ // template<class T>// inline T round(const T& a) {// return a;// }#define TMP_ROUND(TYPE) \inline int round(const TYPE f){ \ int i = (int)f; \ if (f>0) { \ if (f - i >= 0.5F) \ ++i; \ return i; \ } else { \ if (i - f >= 0.5F) \ --i; \ return i; \ } \}TMP_ROUND(double)TMP_ROUND(long double)TMP_ROUND(float)#undef TMP_ROUND#define TMP_ROUND(TYPE) \template<int N> \inline TinyVector<int,N> round( \ const TinyVector<TYPE,N>&f) \{ \ /* implicit casting*/ \ TinyVector<int,N> i (f); \ for (int j= 0; j< N; ++j) \ if (f[j] > 0) { \ if (f[j] - i[j] >= 0.5F) \ ++i[j]; \ } else { \ if (i[j] - f[j] >= 0.5F) \ --i[j]; \ } \ return i; \}TMP_ROUND(double)TMP_ROUND(long double)TMP_ROUND(float)#undef TMP_ROUND// @}//}}} // {{{ identity functions/** * @brief Reinitialize a matrix to an identity matrix. * * @param m Matrix to be reinitialized * @param x Initial value of diagonal elements */template<class T, int N, int M >inline TinyMatrix<T,N,M>& identity(TinyMatrix<T,N,M>& m, const T x=1){ for (int i=0; i < N; ++i) for (int j=0; j < M; ++j) m(i,j) = (i==j) ? x : 0; return m;}// }}}// {{{ diagonalise TinyMatrix/** * @brief Initialize a matrix to a diagonal matrix with diagonal elements equal to tinyvector elements * * @param m Matrix to be reinitialized * @param x Initial value of diagonal elements */template<class TM, class TV, int N>inline TinyMatrix<TM,N,N>& diagonal(TinyMatrix<TM,N,N>& m, const TinyVector<TV,N>& x){ m =0; for (int i=0; i < N; ++i) m(i,i) = x[i]; return m;}template<class TV, int N>inline TinyMatrix<TV,N,N> diagonal(const TinyVector<TV,N>& x){ TinyMatrix<TV,N,N> m(0); for (int i=0; i < N; ++i) m(i,i) = x[i]; return m;}// }}}// {{{ outerProduct/** * Create a outer product of two vectors. * If A and B are two column vectors, OuterProduct = A*B^T. * and is [size(A)] X [size(B)] matrix */template<class T1, class T2, int N, int M>inline TinyMatrix<typename promote_trait<T1,T2>::T_promote, N, M>outerProduct(const TinyVector<T1, N> & x, const TinyVector<T2, M> & y){ TinyMatrix<typename promote_trait<T1,T2>::T_promote, N, M> matrix; for (int i=0; i < N; ++i) for (int j=0; j < M; ++j) matrix(i,j) = x(i)*y(j); return matrix;}// }}}// {{{ transpose and transpose selftemplate<class T, int N, int M>inline TinyMatrix<T,M,N> transpose(const TinyMatrix<T,N,M>& x){ TinyMatrix<T,M,N> m; for (int i=0; i < N; ++i) for (int j=0; j < M; ++j) m(i,j) = x(j,i); return m;}template<class T, int N>inline TinyMatrix<T,N,N>& transposeSelf(TinyMatrix<T,N,N>& x){ for (int i=0; i < N; ++i) for (int j=i; j < N; ++j) swap(x(j,i), x(i,j)); return x;}// }}}// {{{ product/** * Compute product of matrices. * If A and B are two matrices , product = A*B. * and is [row(A)] X [column(B)] matrix *//*template<class T1, class T2, int N, int O, int M>inline TinyMatrix<typename promote_trait<T1,T2>::T_promote, N, M>product(const TinyMatrix<T1, N, O> & x, const TinyMatrix<T2, O, M> & y){ TinyMatrix<typename promote_trait<T1,T2>::T_promote, N, M> matrix=0; for (int i=0; i < N; ++i) for (int j=0; j < M; ++j) for (int k=0; k < O; ++k) matrix(i,j) += x(i,k)*y(k,j); return matrix;}*/// }}}BZ_NAMESPACE_END#endif // _LEAR_EXT_TVMUTILS_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -