1 // $Id: vectorx.cpp,v 1.14 2011/04/23 02:02:49 bobgian Exp $
2 
3 /*
4   Copyright 2002  Peter Beerli, Mary Kuhner, Jon Yamato and Joseph Felsenstein
5 
6   This software is distributed free of charge for non-commercial use
7   and is copyrighted.  Of course, we do not guarantee that the software
8   works, and are not responsible for any damage you may cause or have.
9 */
10 
11 #include <cassert>
12 #include <cmath>
13 
14 #include "vectorx.h"
15 
16 #ifdef DMALLOC_FUNC_CHECK
17 #include "/usr/local/include/dmalloc.h"
18 #endif
19 
20 using namespace std;
21 
22 //------------------------------------------------------------------------------------
23 // maintenance functions
24 
25 // forward declarations if helper functions of the maintenace functions
26 double logsave(const double value);
27 double log0(const double value);
28 
LogVec0(const vector<double> & in,vector<double> & out)29 void LogVec0(const vector<double> &in, vector<double> &out)
30 {
31     assert(in.size() == out.size());
32     transform(in.begin(),in.end(),out.begin(),log0);
33 }
34 
35 // helper function that keeps the structural zeros intact
36 // used in LogVec0() in  PostLike::Setup0()
log0(const double value)37 double log0(const double value)
38 {
39     return ((value > 0.0) ? log(value) : 0.0);
40 }
41 
LongSquareRootOfLong(long shouldBeASquare)42 long LongSquareRootOfLong(long shouldBeASquare)
43 {
44     // this is a funny-looking way to take a square root,
45     // necessitated by the fact that the C library only provides
46     // floating-point square root, and we don't want to deal with
47     // the consequences of rounding error.
48 
49     long i;
50 
51     for (i = 1; i <= long(shouldBeASquare/2); ++i)
52     {
53         if (i * i == shouldBeASquare)
54         {
55             return i;
56         }
57     }
58     throw ("Attempt to take long square root of non-square");
59 }
60 
61 //____________________________________________________________________________________
62 
63