1 #Region "Microsoft.VisualBasic::1211109a06f3a9f66b9347239d1802eb, Microsoft.VisualBasic.Core\Extensions\Math\StatisticsMathExtensions\Average.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     '     Class Average
35     
36     '         Properties: Average
37     
38     '         Constructor: (+2 OverloadsSub New
39     '         FunctionToString
40     '         Operators: +
41     
42     
43     ' /********************************************************************************/
44
45 #End Region
46
47 Imports System.Runtime.CompilerServices
48
49 Namespace Math.Statistics
50
51     Public Class Average
52
53         Public Sum#, N%
54
55         Public ReadOnly Property Average As Double
56             <MethodImpl(MethodImplOptions.AggressiveInlining)>
57             Get
58                 Return Sum / N
59             End Get
60         End Property
61
62         Sub New()
63         End Sub
64
65         Sub New(data As IEnumerable(Of Double))
66             With data.ToArray
67                 Sum = .Sum
68                 N = .Length
69             End With
70         End Sub
71
72         Public Overrides Function ToString() As String
73             Return $"Means of {N} samples = {Average}"
74         End Function
75
76         Public Shared Operator +(avg As Average, x#) As Average
77             avg.Sum += x
78             avg.N += 1
79             Return avg
80         End Operator
81
82         Public Shared Widening Operator CType(avg As DoubleAs Average
83             Return New Average() + avg
84         End Operator
85     End Class
86 End Namespace