From 7206a12c44189298676335e9b583de72a9357d39 Mon Sep 17 00:00:00 2001 From: Davis King Date: Wed, 3 Dec 2008 23:36:11 +0000 Subject: [PATCH] - Added the tmax, tmin, and tabs templates - Changed the diag() function so that it is allowed to take non-square matrices. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402697 --- dlib/algs.h | 46 ++++++++++++++++++++++ dlib/matrix/matrix_utilities.h | 12 +----- dlib/quantum_computing/quantum_computing.h | 23 ++--------- dlib/test/matrix.cpp | 4 ++ 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/dlib/algs.h b/dlib/algs.h index 2ebc7effe..d7566784d 100644 --- a/dlib/algs.h +++ b/dlib/algs.h @@ -568,6 +568,52 @@ namespace dlib inline double put_in_range(const double& a, const double& b, const double& val) { return put_in_range(a,b,val); } +// ---------------------------------------------------------------------------------------- + + /*! tabs + + This is a template to compute the absolute value a number at compile time. + + For example, + abs<-4>::value == 4 + abs<4>::value == 4 + !*/ + + template + struct tabs { const static long value = x; }; + template + struct tabs::type> { const static long value = -x; }; + +// ---------------------------------------------------------------------------------------- + + /*! tmax + + This is a template to compute the max of two values at compile time + + For example, + abs<4,7>::value == 7 + !*/ + + template + struct tmax { const static long value = x; }; + template + struct tmax x)>::type> { const static long value = y; }; + +// ---------------------------------------------------------------------------------------- + + /*! tmin + + This is a template to compute the min of two values at compile time + + For example, + abs<4,7>::value == 4 + !*/ + + template + struct tmin { const static long value = x; }; + template + struct tmin::type> { const static long value = y; }; + // ---------------------------------------------------------------------------------------- /*!A is_function diff --git a/dlib/matrix/matrix_utilities.h b/dlib/matrix/matrix_utilities.h index e21b70c09..94dd00bde 100644 --- a/dlib/matrix/matrix_utilities.h +++ b/dlib/matrix/matrix_utilities.h @@ -2948,7 +2948,7 @@ convergence: template struct op : has_destructive_aliasing { - const static long NR = EXP::NC; + const static long NR = (EXP::NC&&EXP::NR)? (tmin::value) : (0); const static long NC = 1; typedef typename EXP::type type; typedef typename EXP::mem_manager_type mem_manager_type; @@ -2957,7 +2957,7 @@ convergence: { return m(r,r); } template - static long nr (const M& m) { return m.nr(); } + static long nr (const M& m) { return std::min(m.nc(),m.nr()); } template static long nc (const M& m) { return 1; } }; @@ -2970,14 +2970,6 @@ convergence: const matrix_exp& m ) { - // You can only get the diagonal for square matrices. - COMPILE_TIME_ASSERT(EXP::NR == EXP::NC); - DLIB_ASSERT(m.nr() == m.nc(), - "\tconst matrix_exp diag(const matrix_exp& m)" - << "\n\tYou can only apply diag() to a square matrix" - << "\n\tm.nr(): " << m.nr() - << "\n\tm.nc(): " << m.nc() - ); typedef matrix_unary_exp,op_diag> exp; return matrix_exp(exp(m)); } diff --git a/dlib/quantum_computing/quantum_computing.h b/dlib/quantum_computing/quantum_computing.h index 677bbe557..99f102668 100644 --- a/dlib/quantum_computing/quantum_computing.h +++ b/dlib/quantum_computing/quantum_computing.h @@ -8,6 +8,7 @@ #include "../matrix.h" #include "../rand.h" #include "../enable_if.h" +#include "../algs.h" #include "quantum_computing_abstract.h" namespace dlib @@ -34,22 +35,6 @@ namespace dlib // ------------------------------------------------------------------------------------ - // This is a template to compute the absolute value a number at compile time - template - struct abs { const static long value = x; }; - template - struct abs::type> { const static long value = -x; }; - - // ------------------------------------------------------------------------------------ - - // This is a template to compute the max of two values at compile time - template - struct max { const static long value = x; }; - template - struct max x)>::type> { const static long value = y; }; - - // ------------------------------------------------------------------------------------ - } typedef std::complex qc_scalar_type; @@ -657,7 +642,7 @@ namespace dlib target_mask <<= 1; } - static const long num_bits = qc_helpers::abs::value+1; + static const long num_bits = tabs::value+1; static const long dims = qc_helpers::exp_2_n::value; const qc_scalar_type operator() (long r, long c) const @@ -742,8 +727,8 @@ namespace dlib target_mask <<= 1; } - static const long num_bits = qc_helpers::max::value, - qc_helpers::abs::value>::value+1; + static const long num_bits = tmax::value, + tabs::value>::value+1; static const long dims = qc_helpers::exp_2_n::value; const qc_scalar_type operator() (long r, long c) const diff --git a/dlib/test/matrix.cpp b/dlib/test/matrix.cpp index 335cf9ad0..99205294f 100644 --- a/dlib/test/matrix.cpp +++ b/dlib/test/matrix.cpp @@ -120,6 +120,10 @@ namespace mrc.set_size(3,4); set_all_elements(mrc,1); + + DLIB_CASSERT(diag(mrc) == uniform_matrix(3,1,1),""); + DLIB_CASSERT(diag(matrix(mrc)) == uniform_matrix(3,1,1),""); + matrix mrc2; set_all_elements(mrc2,1); DLIB_CASSERT((removerc<1,1>(mrc) == mrc2),"");