• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

l2.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_NORM_L2_HH
00027 # define MLN_NORM_L2_HH
00028 
00034 
00035 # include <mln/math/sqr.hh>
00036 # include <mln/math/sqrt.hh>
00037 # include <mln/algebra/vec.hh>
00038 # include <mln/value/ops.hh>
00039 
00040 
00041 namespace mln
00042 {
00043 
00044   // Forward declaration.
00045   namespace algebra
00046   {
00047     template <unsigned n, typename T> class vec;
00048   }
00049 
00050 
00051   namespace norm
00052   {
00053 
00056     template <unsigned n, typename C>
00057     mln_sum_product(C,C) l2(const C (&vec)[n]);
00058 
00059     template <unsigned n, typename C>
00060     mln_sum_product(C,C) l2(const algebra::vec<n,C>& vec);
00062 
00065     template <unsigned n, typename C>
00066     mln_sum_product(C,C) sqr_l2(const C (&vec)[n]);
00067 
00068     template <unsigned n, typename C>
00069     mln_sum_product(C,C) sqr_l2(const algebra::vec<n,C>& vec);
00071 
00074     template <unsigned n, typename C>
00075     mln_sum_product(C,C) l2_distance(const C (&vec1)[n], const C (&vec2)[n]);
00076 
00077     template <unsigned n, typename C>
00078     mln_sum_product(C,C) l2_distance(const algebra::vec<n,C>& vec1,
00079                            const algebra::vec<n,C>& vec2);
00081 
00082 
00083 # ifndef MLN_INCLUDE_ONLY
00084 
00085     namespace impl
00086     {
00087 
00088       template <unsigned n, typename C, typename V>
00089       inline
00090       mln_sum_product(C,C)
00091       l2_(const V& vec)
00092       {
00093         typedef mln_sum_product(C,C) M;
00094         M m = 0;
00095         for (unsigned i = 0; i < n; ++i)
00096           {
00097             M sqr_v_i = static_cast<M>(mln::math::sqr(vec[i]));
00098             m = static_cast<M>(m + sqr_v_i);
00099           }
00100         return mln::math::sqrt(m);
00101       }
00102 
00103       template <unsigned n, typename C, typename V>
00104       inline
00105       mln_sum_product(C,C)
00106       sqr_l2_(const V& vec)
00107       {
00108         mln_sum_product(C,C) m = 0;
00109         for (unsigned i = 0; i < n; ++i)
00110           m += mln::math::sqr(vec[i]);
00111         return m;
00112       }
00113 
00114       template <unsigned n, typename C, typename V>
00115       inline
00116       mln_sum_product(C,C)
00117       l2_distance_(const V& vec1, const V& vec2)
00118       {
00119         typedef mln_sum_product(C,C) D;
00120         D d = 0;
00121         for (unsigned i = 0; i < n; ++i)
00122           {
00123             D sqr_v1_v2 = static_cast<D>(mln::math::sqr(vec1[i] - vec2[i]));
00124             d = static_cast<D>(d + sqr_v1_v2);
00125           }
00126         return mln::math::sqrt(d);
00127       }
00128 
00129     } // end of namespace mln::norm::impl
00130 
00131 
00132     /*----------.
00133     | Facades.  |
00134     `----------*/
00135 
00136     template <unsigned n, typename C>
00137     inline
00138     mln_sum_product(C,C)
00139     l2(const C (&vec)[n])
00140     {
00141       return impl::l2_<n, C>(vec);
00142     }
00143 
00144     template <unsigned n, typename C>
00145     inline
00146     mln_sum_product(C,C)
00147     l2(const algebra::vec<n,C>& vec)
00148     {
00149       return impl::l2_<n, C>(vec);
00150     }
00151 
00152 
00153     template <unsigned n, typename C>
00154     inline
00155     mln_sum_product(C,C)
00156     sqr_l2(const C (&vec)[n])
00157     {
00158       return impl::sqr_l2_<n, C>(vec);
00159     }
00160 
00161     template <unsigned n, typename C>
00162     inline
00163     mln_sum_product(C,C)
00164     sqr_l2(const algebra::vec<n,C>& vec)
00165     {
00166       return impl::sqr_l2_<n, C>(vec);
00167     }
00168 
00169 
00170     template <unsigned n, typename C>
00171     inline
00172     mln_sum_product(C,C)
00173     l2_distance(const C (&vec1)[n], const C (&vec2)[n])
00174     {
00175       return impl::l2_distance_<n, C>(vec1, vec2);
00176     }
00177 
00178     template <unsigned n, typename C>
00179     inline
00180     mln_sum_product(C,C)
00181     l2_distance(const algebra::vec<n,C>& vec1, const algebra::vec<n,C>& vec2)
00182     {
00183       return impl::l2_distance_<n, C>(vec1, vec2);
00184     }
00185 
00186 # endif // ! MLN_INCLUDE_ONLY
00187 
00188   } // end of namespace mln::norm
00189 
00190 } // end of namespace mln
00191 
00192 
00193 #endif // ! MLN_NORM_L2_HH

Generated on Tue Oct 4 2011 15:24:01 for Milena (Olena) by  doxygen 1.7.1