1 |
#Region "Microsoft.VisualBasic::ecda33ff5be74ed3baef75c09b88c3a8, Microsoft.VisualBasic.Core\Language\Language\Java\LogTricks.vb"
|
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 |
#End Region
|
44 |
|
45 |
Imports sys = System.Math
|
46 |
|
47 |
Namespace Language.Java
|
48 |
|
49 |
|
50 |
|
51 |
Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
|
52 |
|
53 |
This file is part of BEAST.
|
54 |
See the NOTICE file distributed with this work for additional
|
55 |
information regarding copyright ownership and licensing.
|
56 |
|
57 |
BEAST is free software; you can redistribute it and/or modify
|
58 |
it under the terms of the GNU Lesser General Public License as
|
59 |
published by the Free Software Foundation; either version 2
|
60 |
of the License, or (at your option) any later version.
|
61 |
|
62 |
BEAST is distributed in the hope that it will be useful,
|
63 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
64 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
65 |
GNU Lesser General Public License for more details.
|
66 |
|
67 |
You should have received a copy of the GNU Lesser General Public
|
68 |
License along with BEAST; if not, write to the
|
69 |
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
70 |
Boston, MA 02110-1301 USA
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
Public Module LogTricks
|
77 |
|
78 |
Public ReadOnly maxFloat As Double = Double.MaxValue
|
79 |
Public ReadOnly logLimit As Double = -maxFloat / 100
|
80 |
Public ReadOnly logZero As Double = -maxFloat
|
81 |
|
82 |
Public Const NATS As Double = 400
|
83 |
|
84 |
Public Function logSumNoCheck(x As Double, y As Double) As Double
|
85 |
Dim temp As Double = y - x
|
86 |
If sys.Abs(temp) > NATS Then
|
87 |
Return If(x > y, x, y)
|
88 |
Else
|
89 |
Return x + JavaMath.log1p(sys.Exp(temp))
|
90 |
End If
|
91 |
End Function
|
92 |
|
93 |
Public Function logSum(x As Double()) As Double
|
94 |
Dim sum As Double = x(0)
|
95 |
Dim len As Integer = x.Length
|
96 |
For i As Integer = 1 To len - 1
|
97 |
sum = logSumNoCheck(sum, x(i))
|
98 |
Next i
|
99 |
Return sum
|
100 |
End Function
|
101 |
|
102 |
Public Function logSum(x As Double, y As Double) As Double
|
103 |
Dim temp As Double = y - x
|
104 |
If temp > NATS OrElse x < logLimit Then Return y
|
105 |
If temp < -NATS OrElse y < logLimit Then Return x
|
106 |
If temp < 0 Then Return x + JavaMath.log1p(sys.Exp(temp))
|
107 |
Return y + JavaMath.log1p(sys.Exp(-temp))
|
108 |
End Function
|
109 |
|
110 |
Public Sub logInc(x As Double?, y As Double)
|
111 |
Dim temp As Double = y - x
|
112 |
If temp > NATS OrElse x < logLimit Then
|
113 |
x = y
|
114 |
ElseIf temp < -NATS OrElse y < logLimit Then
|
115 |
|
116 |
Else
|
117 |
x += JavaMath.log1p(sys.Exp(temp))
|
118 |
End If
|
119 |
End Sub
|
120 |
|
121 |
Public Function logDiff(x As Double, y As Double) As Double
|
122 |
Debug.Assert(x > y)
|
123 |
Dim temp As Double = y - x
|
124 |
If temp < -NATS OrElse y < logLimit Then Return x
|
125 |
Return x + JavaMath.log1p(-sys.Exp(temp))
|
126 |
End Function
|
127 |
End Module
|
128 |
End Namespace
|