annotate armadillo-2.4.4/include/armadillo_bits/fn_misc.hpp @ 36:cc18e9a13fe8 slimline

Fix failure to set hasTimestamp on final part
author Chris Cannam
date Wed, 16 May 2012 11:44:39 +0100
parents 8b6102e2a9b0
children
rev   line source
max@0 1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
max@0 2 // Copyright (C) 2008-2011 Conrad Sanderson
max@0 3 //
max@0 4 // This file is part of the Armadillo C++ library.
max@0 5 // It is provided without any warranty of fitness
max@0 6 // for any purpose. You can redistribute this file
max@0 7 // and/or modify it under the terms of the GNU
max@0 8 // Lesser General Public License (LGPL) as published
max@0 9 // by the Free Software Foundation, either version 3
max@0 10 // of the License or (at your option) any later version.
max@0 11 // (see http://www.opensource.org/licenses for more info)
max@0 12
max@0 13
max@0 14 //! \addtogroup fn_misc
max@0 15 //! @{
max@0 16
max@0 17
max@0 18
max@0 19 //! \brief
max@0 20 //! Generate a vector with 'num' elements.
max@0 21 //! The values of the elements linearly increase from 'start' upto (and including) 'end'.
max@0 22
max@0 23 template<typename vec_type>
max@0 24 inline
max@0 25 vec_type
max@0 26 linspace
max@0 27 (
max@0 28 const typename vec_type::pod_type start,
max@0 29 const typename vec_type::pod_type end,
max@0 30 const uword num = 100u,
max@0 31 const typename arma_Mat_Col_Row_only<vec_type>::result* junk = 0
max@0 32 )
max@0 33 {
max@0 34 arma_extra_debug_sigprint();
max@0 35 arma_ignore(junk);
max@0 36
max@0 37 typedef typename vec_type::elem_type eT;
max@0 38 typedef typename vec_type::pod_type T;
max@0 39
max@0 40 vec_type x;
max@0 41
max@0 42 if(num >= 2)
max@0 43 {
max@0 44 x.set_size(num);
max@0 45
max@0 46 eT* x_mem = x.memptr();
max@0 47
max@0 48 const uword num_m1 = num - 1;
max@0 49
max@0 50 if(is_non_integral<T>::value == true)
max@0 51 {
max@0 52 const T delta = (end-start)/T(num_m1);
max@0 53
max@0 54 for(uword i=0; i<num_m1; ++i)
max@0 55 {
max@0 56 x_mem[i] = eT(start + i*delta);
max@0 57 }
max@0 58
max@0 59 x_mem[num_m1] = eT(end);
max@0 60 }
max@0 61 else
max@0 62 {
max@0 63 const double delta = (end >= start) ? double(end-start)/double(num_m1) : -double(start-end)/double(num_m1);
max@0 64
max@0 65 for(uword i=0; i<num_m1; ++i)
max@0 66 {
max@0 67 x_mem[i] = eT(double(start) + i*delta);
max@0 68 }
max@0 69
max@0 70 x_mem[num_m1] = eT(end);
max@0 71 }
max@0 72
max@0 73 return x;
max@0 74 }
max@0 75 else
max@0 76 {
max@0 77 x.set_size(1);
max@0 78
max@0 79 x[0] = eT(end);
max@0 80 }
max@0 81
max@0 82 return x;
max@0 83 }
max@0 84
max@0 85
max@0 86
max@0 87 inline
max@0 88 mat
max@0 89 linspace(const double start, const double end, const uword num = 100u)
max@0 90 {
max@0 91 arma_extra_debug_sigprint();
max@0 92 return linspace<mat>(start, end, num);
max@0 93 }
max@0 94
max@0 95
max@0 96
max@0 97 //
max@0 98 // log_add
max@0 99
max@0 100 template<typename eT>
max@0 101 inline
max@0 102 typename arma_float_only<eT>::result
max@0 103 log_add(eT log_a, eT log_b)
max@0 104 {
max@0 105 if(log_a < log_b)
max@0 106 {
max@0 107 std::swap(log_a, log_b);
max@0 108 }
max@0 109
max@0 110 const eT negdelta = log_b - log_a;
max@0 111
max@0 112 if( (negdelta < Math<eT>::log_min()) || (arma_isfinite(negdelta) == false) )
max@0 113 {
max@0 114 return log_a;
max@0 115 }
max@0 116 else
max@0 117 {
max@0 118 #if defined(ARMA_HAVE_LOG1P)
max@0 119 return (log_a + log1p(std::exp(negdelta)));
max@0 120 #else
max@0 121 return (log_a + std::log(1.0 + std::exp(negdelta)));
max@0 122 #endif
max@0 123 }
max@0 124 }
max@0 125
max@0 126
max@0 127
max@0 128 template<typename eT>
max@0 129 arma_inline
max@0 130 arma_warn_unused
max@0 131 bool
max@0 132 is_finite(const eT x, const typename arma_scalar_only<eT>::result* junk = 0)
max@0 133 {
max@0 134 arma_ignore(junk);
max@0 135
max@0 136 return arma_isfinite(x);
max@0 137 }
max@0 138
max@0 139
max@0 140
max@0 141 template<typename T1>
max@0 142 inline
max@0 143 arma_warn_unused
max@0 144 bool
max@0 145 is_finite(const Base<typename T1::elem_type,T1>& X)
max@0 146 {
max@0 147 arma_extra_debug_sigprint();
max@0 148
max@0 149 typedef typename T1::elem_type eT;
max@0 150
max@0 151 const unwrap<T1> tmp(X.get_ref());
max@0 152 const Mat<eT>& A = tmp.M;
max@0 153
max@0 154 return A.is_finite();
max@0 155 }
max@0 156
max@0 157
max@0 158
max@0 159 template<typename T1>
max@0 160 inline
max@0 161 arma_warn_unused
max@0 162 bool
max@0 163 is_finite(const BaseCube<typename T1::elem_type,T1>& X)
max@0 164 {
max@0 165 arma_extra_debug_sigprint();
max@0 166
max@0 167 typedef typename T1::elem_type eT;
max@0 168
max@0 169 const unwrap_cube<T1> tmp(X.get_ref());
max@0 170 const Cube<eT>& A = tmp.M;
max@0 171
max@0 172 return A.is_finite();
max@0 173 }
max@0 174
max@0 175
max@0 176
max@0 177 template<typename T1>
max@0 178 arma_inline
max@0 179 Op<T1, op_sympd>
max@0 180 sympd(const Base<typename T1::elem_type,T1>& X)
max@0 181 {
max@0 182 arma_extra_debug_sigprint();
max@0 183
max@0 184 return Op<T1, op_sympd>(X.get_ref());
max@0 185 }
max@0 186
max@0 187
max@0 188
max@0 189 //! @}