1 #Region "Microsoft.VisualBasic::ecda33ff5be74ed3baef75c09b88c3a8, Microsoft.VisualBasic.Core\Language\Language\Java\LogTricks.vb"
2
3     Author:
4     
5           asuka (amethyst.asuka@gcmodeller.org)
6           xie (genetics@smrucc.org)
7           xieguigang (xie.guigang@live.com)
8     
9     Copyright (c) 2018 GPL3 Licensed
10     
11     
12     GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     This program is free software: you can redistribute it and/or modify
16     it under the terms of the GNU General Public License as published by
17     the Free Software Foundation, either version 3 of the License, or
18     (at your option) any later version.
19     
20     This program is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     GNU General Public License for more details.
24     
25     You should have received a copy of the GNU General Public License
26     along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     /********************************************************************************/
31
32     Summaries:
33
34         Module LogTricks
35     
36             Function: logDiff, (+2 Overloads) logSum, logSumNoCheck
37     
38             Sub: logInc
39     
40     
41     /********************************************************************************/
42
43 #End Region
44
45 Imports sys = System.Math
46
47 Namespace Language.Java
48
49     '     * LogTricks.java
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     ''<summary>
74     ''@author Marc Suchard
75     ''</summary>
76     Public Module LogTricks
77
78         Public ReadOnly maxFloat As Double = Double.MaxValue '3.40282347E+38;
79         Public ReadOnly logLimit As Double = -maxFloat / 100
80         Public ReadOnly logZero As Double = -maxFloat
81
82         Public Const NATS As Double = 400 '40;
83
84         Public Function logSumNoCheck(x As Double, y As DoubleAs 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 DoubleAs 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 DoubleAs 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