123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890 |
- // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
- // Copyright 2008-2016 National ICT Australia (NICTA)
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // ------------------------------------------------------------------------
- //! \addtogroup arma_cmath
- //! @{
- //
- // wrappers for isfinite
- template<typename eT>
- arma_inline
- bool
- arma_isfinite(eT val)
- {
- arma_ignore(val);
-
- return true;
- }
- template<>
- arma_inline
- bool
- arma_isfinite(float x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::isfinite(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::isfinite(x);
- }
- #elif defined(ARMA_HAVE_ISFINITE)
- {
- return (std::isfinite(x) != 0);
- }
- #else
- {
- const float y = (std::numeric_limits<float>::max)();
-
- const volatile float xx = x;
-
- return (xx == xx) && (x >= -y) && (x <= y);
- }
- #endif
- }
- template<>
- arma_inline
- bool
- arma_isfinite(double x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::isfinite(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::isfinite(x);
- }
- #elif defined(ARMA_HAVE_ISFINITE)
- {
- return (std::isfinite(x) != 0);
- }
- #else
- {
- const double y = (std::numeric_limits<double>::max)();
-
- const volatile double xx = x;
-
- return (xx == xx) && (x >= -y) && (x <= y);
- }
- #endif
- }
- template<typename T>
- arma_inline
- bool
- arma_isfinite(const std::complex<T>& x)
- {
- if( (arma_isfinite(x.real()) == false) || (arma_isfinite(x.imag()) == false) )
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- //
- // wrappers for isinf
- template<typename eT>
- arma_inline
- bool
- arma_isinf(eT val)
- {
- arma_ignore(val);
-
- return false;
- }
- template<>
- arma_inline
- bool
- arma_isinf(float x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::isinf(x);
- }
- #elif defined(ARMA_HAVE_ISINF)
- {
- return (std::isinf(x) != 0);
- }
- #else
- {
- const float y = (std::numeric_limits<float>::max)();
-
- const volatile float xx = x;
-
- return (xx == xx) && ((x < -y) || (x > y));
- }
- #endif
- }
- template<>
- arma_inline
- bool
- arma_isinf(double x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::isinf(x);
- }
- #elif defined(ARMA_HAVE_ISINF)
- {
- return (std::isinf(x) != 0);
- }
- #else
- {
- const double y = (std::numeric_limits<double>::max)();
-
- const volatile double xx = x;
-
- return (xx == xx) && ((x < -y) || (x > y));
- }
- #endif
- }
- template<typename T>
- arma_inline
- bool
- arma_isinf(const std::complex<T>& x)
- {
- return ( arma_isinf(x.real()) || arma_isinf(x.imag()) );
- }
- //
- // wrappers for isnan
- template<typename eT>
- arma_inline
- bool
- arma_isnan(eT val)
- {
- arma_ignore(val);
-
- return false;
- }
- template<>
- arma_inline
- bool
- arma_isnan(float x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::isnan(x);
- }
- #elif defined(ARMA_HAVE_ISNAN)
- {
- return (std::isnan(x) != 0);
- }
- #else
- {
- const volatile float xx = x;
-
- return (xx != xx);
- }
- #endif
- }
- template<>
- arma_inline
- bool
- arma_isnan(double x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::isnan(x);
- }
- #elif defined(ARMA_HAVE_ISNAN)
- {
- return (std::isnan(x) != 0);
- }
- #else
- {
- const volatile double xx = x;
-
- return (xx != xx);
- }
- #endif
- }
- template<typename T>
- arma_inline
- bool
- arma_isnan(const std::complex<T>& x)
- {
- return ( arma_isnan(x.real()) || arma_isnan(x.imag()) );
- }
- // rudimentary wrappers for log1p()
- arma_inline
- float
- arma_log1p(const float x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::log1p(x);
- }
- #else
- {
- if((x >= float(0)) && (x < std::numeric_limits<float>::epsilon()))
- {
- return x;
- }
- else
- if((x < float(0)) && (-x < std::numeric_limits<float>::epsilon()))
- {
- return x;
- }
- else
- {
- return std::log(float(1) + x);
- }
- }
- #endif
- }
- arma_inline
- double
- arma_log1p(const double x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::log1p(x);
- }
- #elif defined(ARMA_HAVE_LOG1P)
- {
- return log1p(x);
- }
- #else
- {
- if((x >= double(0)) && (x < std::numeric_limits<double>::epsilon()))
- {
- return x;
- }
- else
- if((x < double(0)) && (-x < std::numeric_limits<double>::epsilon()))
- {
- return x;
- }
- else
- {
- return std::log(double(1) + x);
- }
- }
- #endif
- }
- //
- // implementation of arma_sign()
- template<typename eT>
- arma_inline
- typename arma_unsigned_integral_only<eT>::result
- arma_sign(const eT x)
- {
- return (x > eT(0)) ? eT(+1) : eT(0);
- }
- template<typename eT>
- arma_inline
- typename arma_signed_integral_only<eT>::result
- arma_sign(const eT x)
- {
- return (x > eT(0)) ? eT(+1) : ( (x < eT(0)) ? eT(-1) : eT(0) );
- }
- template<typename eT>
- arma_inline
- typename arma_real_only<eT>::result
- arma_sign(const eT x)
- {
- return (x > eT(0)) ? eT(+1) : ( (x < eT(0)) ? eT(-1) : ((x == eT(0)) ? eT(0) : x) );
- }
- template<typename eT>
- arma_inline
- typename arma_cx_only<eT>::result
- arma_sign(const eT& x)
- {
- typedef typename eT::value_type T;
-
- const T abs_x = std::abs(x);
-
- return (abs_x != T(0)) ? (x / abs_x) : x;
- }
- //
- // wrappers for trigonometric functions
- //
- // wherever possible, try to use C++11 or TR1 versions of the following functions:
- //
- // complex acos
- // complex asin
- // complex atan
- //
- // real acosh
- // real asinh
- // real atanh
- //
- // complex acosh
- // complex asinh
- // complex atanh
- //
- //
- // if C++11 or TR1 are not available, we have rudimentary versions of:
- //
- // real acosh
- // real asinh
- // real atanh
- template<typename T>
- arma_inline
- std::complex<T>
- arma_acos(const std::complex<T>& x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::acos(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::acos(x);
- }
- #else
- {
- arma_ignore(x);
- arma_stop_logic_error("acos(): C++11 compiler required");
-
- return std::complex<T>(0);
- }
- #endif
- }
- template<typename T>
- arma_inline
- std::complex<T>
- arma_asin(const std::complex<T>& x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::asin(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::asin(x);
- }
- #else
- {
- arma_ignore(x);
- arma_stop_logic_error("asin(): C++11 compiler required");
-
- return std::complex<T>(0);
- }
- #endif
- }
- template<typename T>
- arma_inline
- std::complex<T>
- arma_atan(const std::complex<T>& x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::atan(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::atan(x);
- }
- #else
- {
- arma_ignore(x);
- arma_stop_logic_error("atan(): C++11 compiler required");
-
- return std::complex<T>(0);
- }
- #endif
- }
- template<typename eT>
- arma_inline
- eT
- arma_acosh(const eT x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::acosh(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::acosh(x);
- }
- #else
- {
- if(x >= eT(1))
- {
- // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/02/
- return std::log( x + std::sqrt(x*x - eT(1)) );
- }
- else
- {
- if(std::numeric_limits<eT>::has_quiet_NaN)
- {
- return -(std::numeric_limits<eT>::quiet_NaN());
- }
- else
- {
- return eT(0);
- }
- }
- }
- #endif
- }
- template<typename eT>
- arma_inline
- eT
- arma_asinh(const eT x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::asinh(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::asinh(x);
- }
- #else
- {
- // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/02/
- return std::log( x + std::sqrt(x*x + eT(1)) );
- }
- #endif
- }
- template<typename eT>
- arma_inline
- eT
- arma_atanh(const eT x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::atanh(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::atanh(x);
- }
- #else
- {
- if( (x >= eT(-1)) && (x <= eT(+1)) )
- {
- // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/02/
- return std::log( ( eT(1)+x ) / ( eT(1)-x ) ) / eT(2);
- }
- else
- {
- if(std::numeric_limits<eT>::has_quiet_NaN)
- {
- return -(std::numeric_limits<eT>::quiet_NaN());
- }
- else
- {
- return eT(0);
- }
- }
- }
- #endif
- }
- template<typename T>
- arma_inline
- std::complex<T>
- arma_acosh(const std::complex<T>& x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::acosh(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::acosh(x);
- }
- #else
- {
- arma_ignore(x);
- arma_stop_logic_error("acosh(): C++11 compiler required");
-
- return std::complex<T>(0);
- }
- #endif
- }
- template<typename T>
- arma_inline
- std::complex<T>
- arma_asinh(const std::complex<T>& x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::asinh(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::asinh(x);
- }
- #else
- {
- arma_ignore(x);
- arma_stop_logic_error("asinh(): C++11 compiler required");
-
- return std::complex<T>(0);
- }
- #endif
- }
- template<typename T>
- arma_inline
- std::complex<T>
- arma_atanh(const std::complex<T>& x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::atanh(x);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::atanh(x);
- }
- #else
- {
- arma_ignore(x);
- arma_stop_logic_error("atanh(): C++11 compiler required");
-
- return std::complex<T>(0);
- }
- #endif
- }
- //
- // wrappers for hypot(x, y) = sqrt(x^2 + y^2)
- template<typename eT>
- inline
- eT
- arma_hypot_generic(const eT x, const eT y)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::hypot(x, y);
- }
- #elif defined(ARMA_HAVE_TR1)
- {
- return std::tr1::hypot(x, y);
- }
- #else
- {
- const eT xabs = std::abs(x);
- const eT yabs = std::abs(y);
-
- eT larger;
- eT ratio;
-
- if(xabs > yabs)
- {
- larger = xabs;
- ratio = yabs / xabs;
- }
- else
- {
- larger = yabs;
- ratio = xabs / yabs;
- }
-
- return (larger == eT(0)) ? eT(0) : (larger * std::sqrt(eT(1) + ratio * ratio));
- }
- #endif
- }
- template<typename eT>
- inline
- eT
- arma_hypot(const eT x, const eT y)
- {
- arma_ignore(x);
- arma_ignore(y);
-
- arma_stop_runtime_error("arma_hypot(): not implemented for integer or complex element types");
-
- return eT(0);
- }
- template<>
- arma_inline
- float
- arma_hypot(const float x, const float y)
- {
- return arma_hypot_generic(x,y);
- }
- template<>
- arma_inline
- double
- arma_hypot(const double x, const double y)
- {
- return arma_hypot_generic(x,y);
- }
- //
- // implementation of arma_sinc()
- template<typename eT>
- arma_inline
- eT
- arma_sinc_generic(const eT x)
- {
- typedef typename get_pod_type<eT>::result T;
-
- const eT tmp = Datum<T>::pi * x;
-
- return (tmp == eT(0)) ? eT(1) : eT( std::sin(tmp) / tmp );
- }
- template<typename eT>
- arma_inline
- eT
- arma_sinc(const eT x)
- {
- return eT( arma_sinc_generic( double(x) ) );
- }
- template<>
- arma_inline
- float
- arma_sinc(const float x)
- {
- return arma_sinc_generic(x);
- }
- template<>
- arma_inline
- double
- arma_sinc(const double x)
- {
- return arma_sinc_generic(x);
- }
- template<typename T>
- arma_inline
- std::complex<T>
- arma_sinc(const std::complex<T>& x)
- {
- return arma_sinc_generic(x);
- }
- //
- // wrappers for arg()
- template<typename eT>
- struct arma_arg
- {
- static
- inline
- eT
- eval(const eT x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return eT( std::arg(x) );
- }
- #else
- {
- arma_ignore(x);
- arma_stop_logic_error("arg(): C++11 compiler required");
-
- return eT(0);
- }
- #endif
- }
- };
- template<>
- struct arma_arg<float>
- {
- static
- arma_inline
- float
- eval(const float x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::arg(x);
- }
- #else
- {
- return std::arg( std::complex<float>( x, float(0) ) );
- }
- #endif
- }
- };
- template<>
- struct arma_arg<double>
- {
- static
- arma_inline
- double
- eval(const double x)
- {
- #if defined(ARMA_USE_CXX11)
- {
- return std::arg(x);
- }
- #else
- {
- return std::arg( std::complex<double>( x, double(0) ) );
- }
- #endif
- }
- };
- template<>
- struct arma_arg< std::complex<float> >
- {
- static
- arma_inline
- float
- eval(const std::complex<float>& x)
- {
- return std::arg(x);
- }
- };
- template<>
- struct arma_arg< std::complex<double> >
- {
- static
- arma_inline
- double
- eval(const std::complex<double>& x)
- {
- return std::arg(x);
- }
- };
- //! @}
|