1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#ifndef __LOG_REP_H
#define __LOG_REP_H
#ifdef LOGREP
#include "definitions.h"
#include "AddLog.h"
#include <iostream>
#include <cmath>
using namespace std;
/* logRep: enables working with much larger or smaller numbers than normally possible
by the regular double representation
* Representation of a number x by the log of x
Note: Base is 2!!
WARNING: Note that logRep can only be used for positive values
(such as probablities) - you can't have the log of a negative!
For a general real number use class doubleRep.
*/
class logRep{
public:
logRep() : _log(VERYSMALL){}
logRep(MDOUBLE a) {_log = ((a==0.0) ? VERYSMALL : log(a));}
logRep(const logRep& other) : _log(other._log) {}
logRep* clone() {return new logRep(*this);}
void output(ostream &out) const{ out<<exp(_log);}
friend MDOUBLE convert(const logRep& a);
//inline MDOUBLE convert();
inline logRep& operator=(const logRep& a);
inline logRep& operator+=(logRep a);
friend inline logRep operator+(const logRep& a, const logRep& b);
inline logRep& operator-=(const logRep& a);
friend inline logRep operator-(const logRep& a, const logRep& b);
inline logRep& operator*=(const logRep& a);
friend inline logRep operator*(const logRep& a, const logRep& b);
inline logRep& operator/=(const logRep& a);
friend inline logRep operator/(const logRep& a, const logRep& b);
friend inline bool operator==(const logRep& a, const logRep& b);
friend inline bool operator!=(const logRep& a, const logRep& b);
friend inline bool operator<(const logRep& a, const logRep& b);
friend inline bool operator<=(const logRep& a, const logRep& b);
friend inline bool operator>(const logRep& a, const logRep& b);
friend inline bool operator>=(const logRep& a, const logRep& b);
friend inline MDOUBLE log(const logRep& d);
friend inline logRep exp(const logRep& d);
private:
const MDOUBLE getLog() const {return _log;}
private:
MDOUBLE _log;
//static tAddLog_Precompute _add;
};
MDOUBLE convert(const logRep& a); //declaration of this function to be implemented cpp
inline logRep& logRep::operator=(const logRep& a){
_log=a.getLog();
return *this;
}
//inline MDOUBLE convert(){
// return exp(_log);
//}
// Original version by Adi Stern
inline logRep& logRep::operator+=(logRep a){
if (_log == VERYSMALL)
_log = a._log;
else if (a._log == VERYSMALL ) return *this;
else _log = AddLog(_log, a._log);
return *this;
}
inline logRep operator+(const logRep& a, const logRep& b){
logRep temp(a);
temp+=b;
return temp;
}
inline logRep& logRep::operator*=(const logRep& a){
if ((_log == VERYSMALL) || (a._log== VERYSMALL )){
_log = VERYSMALL;
return *this;
}
_log+=a._log;
return *this;
}
inline logRep operator*(const logRep& a, const logRep& b){
logRep temp(a);
temp*=b;
return temp;
}
inline logRep& logRep::operator/=(const logRep& a){
_log-=a._log;
return *this;
}
inline logRep operator/(const logRep& a, const logRep& b){
logRep temp(a);
temp/=b;
return temp;
}
/************************
* Comparison operators *
************************/
inline bool operator==(const logRep& a, const logRep& b){
return (a.getLog()==b.getLog());
}
inline bool operator!=(const logRep& a, const logRep& b){
return !(a==b);
}
inline bool operator<(const logRep& a, const logRep& b){
if (a.getLog()<b.getLog()) {return true;}
else {return false;}
}
inline bool operator>(const logRep& a, const logRep& b){
if (a.getLog()>b.getLog()) {return true;}
else {return false;}
}
inline bool operator<=(const logRep& a, const logRep& b){
return !(a>b);
}
inline bool operator>=(const logRep& a, const logRep& b){
return !(a<b);
}
ostream& operator<<(ostream &out, const logRep& a);
inline MDOUBLE log(const logRep& d) {return d.getLog();}
inline ostream &operator<<(ostream &out, const VlogRep &v){
for (int j=0;j<v.size();++j)
out<< v[j]<<" ";
out <<endl;
return(out);
}
inline ostream &operator<<(ostream &out, const VVlogRep &m){
for (int i=0;i<m.size();++i)
out<<m[i];
out <<endl;
return(out);
}
inline logRep exp(const logRep& d) {
logRep res;
res._log = d.getLog();
return res;
}
#endif
#endif
|