k-dot-p 0.1

utilities.h++

Go to the documentation of this file.
00001 /*
00002  *Utility functions for k-dot-p code.
00003 
00004     Copyright (C) 2010 Joseph Pingenot
00005 
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU Affero General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU Affero General Public License for more details.
00015 
00016     You should have received a copy of the GNU Affero General Public License
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 */
00020 
00021 #ifndef ___UTILITIES_HXX___
00022 #define ___UTILITIES_HXX___
00023 
00024 namespace kdotp {
00025   namespace libmodelxx {
00026     namespace utilities {
00027 
00028       /*This function will return a long unsigned int which is equal to
00029        *  value!  (i.e. "value factorial")  It is done at compile time
00030        *  and should become a numeric constant in the end.
00031        */
00032       template<long unsigned value>
00033       long unsigned compiletime_integer_factorial() {
00034   return value*compiletime_integer_factorial<value-1>();
00035       }
00036       /*oh, and we can do 2 easily!*/
00037       template<>
00038       long unsigned compiletime_integer_factorial<2>() {return 2;}
00039       /*technically, we only need 0, but we have 1 to shortcut it.*/
00040       template<>
00041       long unsigned compiletime_integer_factorial<1>() {return 1;}
00042       template<>
00043       long unsigned compiletime_integer_factorial<0>() {return 1;}
00044       
00045       template<long unsigned power>
00046       long unsigned compiletime_integer_exponentiation(long unsigned value) {
00047   return value*compiletime_integer_exponentiation<power-1>(value);
00048       }
00049       template<>
00050       long unsigned compiletime_integer_exponentiation<1>(long unsigned value) {
00051   return value;
00052       }
00053       template<>
00054       long unsigned compiletime_integer_exponentiation<0>(long unsigned value) {
00055   return 1;
00056       }
00057       
00058 
00059       /*Sets the constants such that they are the 
00060        *when calling, n_neighbors is = total_neighbors (so we get the power
00061        *  right)
00062        */
00063       template<unsigned n_neighbors>
00064       void get_second_derivative_consts_(long int *consts, unsigned total_neighbors, unsigned derivative) {
00065   get_second_derivative_consts_<n_neighbors-1>(&consts[1], total_neighbors, derivative);
00066       }
00067       /*Termination condition.  We can set one of these consts to 1.*/
00068       template<>
00069       void get_second_derivative_consts_<1>(long int *consts, unsigned total_neighbors, unsigned derivatve) {
00070   consts[0] = 1;
00071       }
00072       template<unsigned n_neighbors>
00073       void get_second_derivative_consts(long int *consts, long int *denominator) {
00074   get_second_derivative_consts_<n_neighbors>(consts);
00075   *denominator = 0;
00076   for(unsigned i=0; i<n_neighbors; i++) denominator += 2*consts[i];
00077       }
00078     }
00079   }
00080 }
00081 
00082 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines