matrix_math_functions.h 14.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_MATRIx_MATH_FUNCTIONS
#define DLIB_MATRIx_MATH_FUNCTIONS 

#include "matrix_math_functions_abstract.h"
#include "matrix_op.h"
#include "matrix_utilities.h"
#include "matrix.h"
#include "../algs.h"
#include <cmath>
#include <complex>
#include <limits>


namespace dlib
{

// ----------------------------------------------------------------------------------------

    DLIB_DEFINE_FUNCTION_M(op_sqrt, sqrt, std::sqrt ,7);
    DLIB_DEFINE_FUNCTION_M(op_log, log, std::log ,7);
    DLIB_DEFINE_FUNCTION_M(op_log10, log10, std::log10 ,7);
    DLIB_DEFINE_FUNCTION_M(op_exp, exp, std::exp ,7);

    DLIB_DEFINE_FUNCTION_M(op_conj, conj, std::conj ,2);

    DLIB_DEFINE_FUNCTION_M(op_ceil, ceil, std::ceil ,7);
    DLIB_DEFINE_FUNCTION_M(op_floor, floor, std::floor ,7);

    DLIB_DEFINE_FUNCTION_M(op_sin, sin, std::sin ,7);
    DLIB_DEFINE_FUNCTION_M(op_cos, cos, std::cos ,7);
    DLIB_DEFINE_FUNCTION_M(op_tan, tan, std::tan ,7);
    DLIB_DEFINE_FUNCTION_M(op_sinh, sinh, std::sinh ,7);
    DLIB_DEFINE_FUNCTION_M(op_cosh, cosh, std::cosh ,7);
    DLIB_DEFINE_FUNCTION_M(op_tanh, tanh, std::tanh ,7);
    DLIB_DEFINE_FUNCTION_M(op_asin, asin, std::asin ,7);
    DLIB_DEFINE_FUNCTION_M(op_acos, acos, std::acos ,7);
    DLIB_DEFINE_FUNCTION_M(op_atan, atan, std::atan ,7);

// ----------------------------------------------------------------------------------------

    namespace impl
    {
        template <typename type>
        inline type sigmoid (const type& val)
        {
            return static_cast<type>(1/(1 + std::exp(-val)));
        }

        template <typename type, typename S>
        inline type round_zeros_eps (const type& val, const S& eps)
        {
            // you can only round matrices that contain built in scalar types like double, long, float, etc.
            COMPILE_TIME_ASSERT(is_built_in_scalar_type<type>::value);

            if (val >= eps || val <= -eps)
                return val;
            else
                return 0;
        }

        template <typename type>
        inline type round_zeros (const type& val)
        {
            // you can only round matrices that contain built in scalar types like double, long, float, etc.
            COMPILE_TIME_ASSERT(is_built_in_scalar_type<type>::value);

            const type eps = 10*std::numeric_limits<type>::epsilon();
            if (val >= eps || val <= -eps)
                return val;
            else
                return 0;
        }

        template <typename type>
        inline type squared (const type& val)
        {
            return val*val;
        }

        template <typename type>
        inline type sign (const type& val)
        {
            if (val >= 0)
                return +1;
            else
                return -1;
        }

        template <typename type>
        type cubed (const type& val)
        {
            return val*val*val;
        }

        template <typename type, typename S>
        inline type pow1 (const type& val, const S& s)
        {
            // you can only call pow() on matrices that contain floats, doubles or long doubles.
            COMPILE_TIME_ASSERT((
                    is_same_type<type,float>::value == true || 
                    is_same_type<type,double>::value == true || 
                    is_same_type<type,long double>::value == true 
            ));

            return std::pow(val,static_cast<type>(s));
        }

        template <typename type, typename S>
        inline type pow2 (const S& s, const type& val)
        {
            // you can only call pow() on matrices that contain floats, doubles or long doubles.
            COMPILE_TIME_ASSERT((
                    is_same_type<type,float>::value == true || 
                    is_same_type<type,double>::value == true || 
                    is_same_type<type,long double>::value == true 
            ));

            return std::pow(static_cast<type>(s),val);
        }

        template <typename type>
        inline type reciprocal (const type& val)
        {
            // you can only compute reciprocal matrices that contain floats, doubles or long doubles.
            COMPILE_TIME_ASSERT((
                    is_same_type<type,float>::value == true || 
                    is_same_type<type,double>::value == true || 
                    is_same_type<type,long double>::value == true  ||
                    is_same_type<type,std::complex<float> >::value == true || 
                    is_same_type<type,std::complex<double> >::value == true || 
                    is_same_type<type,std::complex<long double> >::value == true 
            ));

            if (val != static_cast<type>(0))
                return static_cast<type>((type)1.0/val);
            else
                return 0;
        }

        template <typename type>
        inline type reciprocal_max (const type& val)
        {
            // you can only compute reciprocal_max matrices that contain floats, doubles or long doubles.
            COMPILE_TIME_ASSERT((
                    is_same_type<type,float>::value == true || 
                    is_same_type<type,double>::value == true || 
                    is_same_type<type,long double>::value == true 
            ));

            if (val != static_cast<type>(0))
                return static_cast<type>((type)1.0/val);
            else
                return std::numeric_limits<type>::max();
        }

    }

    DLIB_DEFINE_FUNCTION_M(op_sigmoid, sigmoid, impl::sigmoid, 7);
    DLIB_DEFINE_FUNCTION_MS(op_round_zeros, round_zeros, impl::round_zeros_eps, 7);
    DLIB_DEFINE_FUNCTION_M(op_round_zeros2, round_zeros, impl::round_zeros, 7);
    DLIB_DEFINE_FUNCTION_M(op_cubed, cubed, impl::cubed, 7);
    DLIB_DEFINE_FUNCTION_M(op_squared, squared, impl::squared, 6);
    DLIB_DEFINE_FUNCTION_M(op_sign, sign, impl::sign, 6);
    DLIB_DEFINE_FUNCTION_MS(op_pow1, pow, impl::pow1, 7);
    DLIB_DEFINE_FUNCTION_SM(op_pow2, pow, impl::pow2, 7);
    DLIB_DEFINE_FUNCTION_M(op_reciprocal, reciprocal, impl::reciprocal, 6);
    DLIB_DEFINE_FUNCTION_M(op_reciprocal_max, reciprocal_max, impl::reciprocal_max, 6);

// ----------------------------------------------------------------------------------------

    template <typename M, typename enabled = void>
    struct op_round : basic_op_m<M> 
    {
        op_round( const M& m_) : basic_op_m<M>(m_){}

        const static long cost = M::cost+7;
        typedef typename M::type type;
        typedef const typename M::type const_ret_type;
        const_ret_type apply (long r, long c) const
        { 
            return static_cast<type>(std::floor(this->m(r,c)+0.5)); 
        }
    };

    template <typename M>
    struct op_round<M,typename enable_if_c<std::numeric_limits<typename M::type>::is_integer>::type > 
    : basic_op_m<M>
    {
        op_round( const M& m_) : basic_op_m<M>(m_){}

        const static long cost = M::cost;
        typedef typename M::type type;
        typedef typename M::const_ret_type const_ret_type;
        const_ret_type apply (long r, long c) const
        { 
            return this->m(r,c);
        }
    };

    template <
        typename EXP
        >
    const matrix_op<op_round<EXP> > round (
        const matrix_exp<EXP>& m
    )
    {
        // you can only round matrices that contain built in scalar types like double, long, float, etc.
        COMPILE_TIME_ASSERT(is_built_in_scalar_type<typename EXP::type>::value);

        typedef op_round<EXP> op;
        return matrix_op<op>(op(m.ref()));
    }

// ----------------------------------------------------------------------------------------

    template <typename M>
    struct op_normalize : basic_op_m<M> 
    {
        typedef typename M::type type;

        op_normalize( const M& m_, const type& s_) : basic_op_m<M>(m_), s(s_){}

        const type s;

        const static long cost = M::cost+5;
        typedef const typename M::type const_ret_type;
        const_ret_type apply (long r, long c) const
        { 
            return this->m(r,c)*s;
        }
    };

    template <
        typename EXP
        >
    const matrix_op<op_normalize<EXP> > normalize (
        const matrix_exp<EXP>& m
    )
    {
        // you can only compute normalized matrices that contain floats, doubles or long doubles.
        COMPILE_TIME_ASSERT((
                is_same_type<typename EXP::type,float>::value == true || 
                is_same_type<typename EXP::type,double>::value == true || 
                is_same_type<typename EXP::type,long double>::value == true 
        ));


        typedef op_normalize<EXP> op;
        typename EXP::type temp = std::sqrt(sum(squared(m)));
        if (temp != 0.0)
            temp = 1.0/temp;

        return matrix_op<op>(op(m.ref(),temp));
    }

// ----------------------------------------------------------------------------------------

    template <typename M, typename return_type = typename M::type>
    struct op_abs : basic_op_m<M>
    {
        op_abs( const M& m_) : basic_op_m<M>(m_){}

        const static long cost = M::cost+7;
        typedef typename M::type type;
        typedef const typename M::type const_ret_type;
        const_ret_type apply ( long r, long c) const
        { 
            return static_cast<type>(std::abs(this->m(r,c))); 
        }
    };

    template <typename M, typename T>
    struct op_abs<M, std::complex<T> > : basic_op_m<M>
    {
        op_abs( const M& m_) : basic_op_m<M>(m_){}

        const static long cost = M::cost;
        typedef T type;
        typedef const T const_ret_type;
        const_ret_type apply ( long r, long c) const
        { 
            return static_cast<type>(std::abs(this->m(r,c))); 
        }
    };

    template <
        typename EXP
        >
    const matrix_op<op_abs<EXP> > abs (
        const matrix_exp<EXP>& m
    )
    {
        typedef op_abs<EXP> op;
        return matrix_op<op>(op(m.ref()));
    }

// ----------------------------------------------------------------------------------------

    template <typename M>
    struct op_complex_matrix : basic_op_m<M>
    {
        op_complex_matrix( const M& m_) : basic_op_m<M>(m_){}

        const static long cost = M::cost+1;
        typedef std::complex<typename M::type> type;
        typedef const std::complex<typename M::type> const_ret_type;
        const_ret_type apply ( long r, long c) const
        { 
            return type(this->m(r,c));
        }
    };

    template <
        typename EXP
        >
    const matrix_op<op_complex_matrix<EXP> > complex_matrix (
        const matrix_exp<EXP>& m
    )
    {
        typedef op_complex_matrix<EXP> op;
        return matrix_op<op>(op(m.ref()));
    }

// ----------------------------------------------------------------------------------------

    template <typename M1, typename M2>
    struct op_complex_matrix2 : basic_op_mm<M1,M2>
    {
        op_complex_matrix2( const M1& m1_, const M2& m2_) : basic_op_mm<M1,M2>(m1_,m2_){}

        const static long cost = M1::cost+M2::cost+1;
        typedef std::complex<typename M1::type> type;
        typedef const std::complex<typename M1::type> const_ret_type;

        const_ret_type apply ( long r, long c) const
        { return type(this->m1(r,c), this->m2(r,c)); }
    };

    template <
        typename EXP1,
        typename EXP2
        >
    const matrix_op<op_complex_matrix2<EXP1,EXP2> > complex_matrix (
        const matrix_exp<EXP1>& real_part,
        const matrix_exp<EXP2>& imag_part 
    )
    {
        COMPILE_TIME_ASSERT((is_same_type<typename EXP1::type,typename EXP2::type>::value == true));
        COMPILE_TIME_ASSERT(EXP1::NR == EXP2::NR || EXP1::NR == 0 || EXP2::NR == 0);
        COMPILE_TIME_ASSERT(EXP1::NC == EXP2::NC || EXP1::NC == 0 || EXP2::NC == 0);

        DLIB_ASSERT(real_part.nr() == imag_part.nr() &&
               real_part.nc() == imag_part.nc(), 
            "\tconst matrix_exp::type complex_matrix(real_part, imag_part)"
            << "\n\tYou can only make a complex matrix from two equally sized matrices"
            << "\n\treal_part.nr(): " << real_part.nr()
            << "\n\treal_part.nc(): " << real_part.nc() 
            << "\n\timag_part.nr(): " << imag_part.nr()
            << "\n\timag_part.nc(): " << imag_part.nc() 
            );

        typedef op_complex_matrix2<EXP1,EXP2> op;
        return matrix_op<op>(op(real_part.ref(),imag_part.ref()));
    }

// ----------------------------------------------------------------------------------------

    template <typename M>
    struct op_norm : basic_op_m<M>
    {
        op_norm( const M& m_) : basic_op_m<M>(m_){}

        const static long cost = M::cost+6;
        typedef typename M::type::value_type type;
        typedef const typename M::type::value_type const_ret_type;
        const_ret_type apply ( long r, long c) const
        { return std::norm(this->m(r,c)); }
    };

    template <
        typename EXP
        >
    const matrix_op<op_norm<EXP> > norm (
        const matrix_exp<EXP>& m
    )
    {
        typedef op_norm<EXP> op;
        return matrix_op<op>(op(m.ref()));
    }

// ----------------------------------------------------------------------------------------

    template <typename M>
    struct op_real : basic_op_m<M>
    {
        op_real( const M& m_) : basic_op_m<M>(m_){}

        const static long cost = M::cost;
        typedef typename M::type::value_type type;
        typedef const typename M::type::value_type const_ret_type;
        const_ret_type apply ( long r, long c) const
        { return std::real(this->m(r,c)); }
    };

    template <
        typename EXP
        >
    const matrix_op<op_real<EXP> > real (
        const matrix_exp<EXP>& m
    )
    {
        typedef op_real<EXP> op;
        return matrix_op<op>(op(m.ref()));
    }

// ----------------------------------------------------------------------------------------

    template <typename M>
    struct op_imag : basic_op_m<M>
    {
        op_imag( const M& m_) : basic_op_m<M>(m_){}

        const static long cost = M::cost;
        typedef typename M::type::value_type type;
        typedef const typename M::type::value_type const_ret_type;
        const_ret_type apply (long r, long c) const
        { return std::imag(this->m(r,c)); }
    };

    template <
        typename EXP
        >
    const matrix_op<op_imag<EXP> > imag (
        const matrix_exp<EXP>& m
    )
    {
        typedef op_imag<EXP> op;
        return matrix_op<op>(op(m.ref()));
    }

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_MATRIx_MATH_FUNCTIONS