home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 41 / IOPROG_41.ISO / soft / c++ / NUMCPP11.ZIP / algorthm.hpp next >
Encoding:
C/C++ Source or Header  |  1999-05-17  |  2.3 KB  |  82 lines

  1. //===================================================================
  2. // algorthm.hpp
  3. //
  4. // Version 1.1
  5. //
  6. // Written by:
  7. //   Brent Worden
  8. //   WordenWare
  9. //   email:  Brent@Worden.org
  10. //
  11. // Copyright (c) 1998-1999 WordenWare
  12. //
  13. // Created:  August 28, 1998
  14. // Revised:  April 10, 1999
  15. //===================================================================
  16.  
  17. #ifndef _ALGORTHM_HPP_
  18. #define _ALGORTHM_HPP_
  19.  
  20. #include "numerics.h"
  21.  
  22. NUM_BEGIN
  23.  
  24. template<class X, class Y>
  25. inline double average(X x, Y y)
  26. //-------------------------------------------------------------------
  27. // Returns the average of elements in x and y.
  28. //-------------------------------------------------------------------
  29. {
  30.     return x + (y - x) / 2.0;
  31. };
  32.  
  33. template<class S, class T, class D>
  34. void pairwdiff(S sfirst, S slast, T tfirst, T tlast, D dest)
  35. //-------------------------------------------------------------------
  36. // Computes the pairwise differences between the elements in
  37. // [sfirst, slast) and the elements in [tfirst, tlast) and places
  38. // them in dest.  dest must be large enough to hold all of the m * n
  39. // differences, where m = slast - sfirst and n = tlast - tfirst.
  40. //-------------------------------------------------------------------
  41. {
  42.     S i;
  43.     T j;
  44.  
  45.     for(i = sfirst; i < slast; ++i){
  46.         for(j = tfirst; j < tlast; ++j){
  47.             *dest = *i - *j;
  48.             ++dest;
  49.         }
  50.     }
  51. };
  52.  
  53. template<class S, class T>
  54. void walshavg(S first, S last, T dest)
  55. //-------------------------------------------------------------------
  56. // Computes the Walsh averages of the elements in [first, last) and
  57. // places them in dest.  dest must be large enough to hold all of the
  58. // n averages ( n = k * (k + 1) / 2, where k = last - first).
  59. //-------------------------------------------------------------------
  60. {
  61.     S i, j;    
  62.     
  63.     for(i = first; i < last; ++i){
  64.         for(j = i; j < last; ++j){
  65.             *dest = average(*i, *j);
  66.             ++dest;
  67.         }
  68.     }
  69. };
  70.  
  71. NUM_END
  72.  
  73. #endif
  74.  
  75. //===================================================================
  76. // Revision History
  77. //
  78. // Version 1.0 - 08/28/1998 - New.
  79. // Version 1.1 - 04/10/1999 - Added Numerics namespace.
  80. //                            Fixed pointer bug in walshavg.
  81. //===================================================================
  82.