Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Calculation Engine for .NET

, 1 Sep 2013
A calculation engine that is small, fast, and extensible.
Friend Class Statistical

    ' Methods
    Public Shared Sub Register(ByVal ce As CalcEngine)
        ce.RegisterFunction("AVERAGE", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.Average))
        ce.RegisterFunction("AVERAGEA", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.AverageA))
        ce.RegisterFunction("COUNT", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.Count))
        ce.RegisterFunction("COUNTA", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.CountA))
        ce.RegisterFunction("COUNTBLANK", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.CountBlank))
        ce.RegisterFunction("COUNTIF", 2, New CalcEngineFunction(AddressOf Statistical.CountIf))
        ce.RegisterFunction("MAX", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.Max))
        ce.RegisterFunction("MAXA", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.MaxA))
        ce.RegisterFunction("MIN", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.Min))
        ce.RegisterFunction("MINA", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.MinA))
        ce.RegisterFunction("STDEV", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.StDev))
        ce.RegisterFunction("STDEVA", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.StDevA))
        ce.RegisterFunction("STDEVP", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.StDevP))
        ce.RegisterFunction("STDEVPA", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.StDevPA))
        ce.RegisterFunction("VAR", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.Var))
        ce.RegisterFunction("VARA", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.VarA))
        ce.RegisterFunction("VARP", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.VarP))
        ce.RegisterFunction("VARPA", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Statistical.VarPA))
    End Sub

    Private Shared Function Average(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, True).Average
    End Function

    Private Shared Function AverageA(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, False).Average
    End Function

    Private Shared Function Count(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, True).Count
    End Function

    Private Shared Function CountA(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, False).Count
    End Function

    Private Shared Function CountBlank(ByVal p As List(Of Expression)) As Object
        Dim cnt As Double = 0
        Dim e As Expression
        For Each e In p
            Dim ienum As IEnumerable = TryCast(e, IEnumerable)
            If (Not ienum Is Nothing) Then
                Dim value As Object
                For Each value In ienum
                    If Statistical.IsBlank(value) Then
                        cnt += 1
                    End If
                Next
            ElseIf Statistical.IsBlank(e.Evaluate) Then
                cnt += 1
            End If
        Next
        Return cnt
    End Function

    Private Shared Function CountIf(ByVal p As List(Of Expression)) As Object
        Dim ce As New CalcEngine
        Dim cnt As Double = 0
        Dim ienum As IEnumerable = TryCast(p.Item(0), IEnumerable)
        If (Not ienum Is Nothing) Then
            Dim crit As String = CStr(p.Item(1).Evaluate)
            Dim value As Object
            For Each value In ienum
                If Not Statistical.IsBlank(value) Then
                    Dim exp As String = String.Format("{0}{1}", value, crit)
                    If CBool(ce.Evaluate(exp)) Then
                        cnt += 1
                    End If
                End If
            Next
        End If
        Return cnt
    End Function

    Private Shared Function GetTally(ByVal p As List(Of Expression), ByVal numbersOnly As Boolean) As Tally
        Dim tally As New Tally(numbersOnly)
        Dim e As Expression
        For Each e In p
            tally.Add(e)
        Next
        Return tally
    End Function

    Private Shared Function IsBlank(ByVal value As Object) As Boolean
        Return ((value Is Nothing) OrElse (TypeOf value Is String AndAlso (CStr(value).Length = 0)))
    End Function

    Private Shared Function Max(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, True).Max
    End Function

    Private Shared Function MaxA(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, False).Max
    End Function

    Private Shared Function Min(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, True).Min
    End Function

    Private Shared Function MinA(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, False).Min
    End Function

    Private Shared Function StDev(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, True).Std
    End Function

    Private Shared Function StDevA(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, False).Std
    End Function

    Private Shared Function StDevP(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, True).StdP
    End Function

    Private Shared Function StDevPA(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, False).StdP
    End Function

    Private Shared Function Var(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, True).Var
    End Function

    Private Shared Function VarA(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, False).Var
    End Function

    Private Shared Function VarP(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, True).VarP
    End Function

    Private Shared Function VarPA(ByVal p As List(Of Expression)) As Object
        Return Statistical.GetTally(p, False).VarP
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bernardo Castilho
Chief Technology Officer ComponentOne
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 1 Sep 2013
Article Copyright 2011 by Bernardo Castilho
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid