Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / Visual Basic

Mathemathics Framework

Rate me:
Please Sign up or sign in to vote.
4.76/5 (56 votes)
16 Sep 2008CPOL6 min read 75.5K   6.2K   171  
.NET Mathematical Framework
Imports BV.Core
Imports BV.Math
Imports IMidRange
Imports System.ComponentModel
Imports System.Drawing
Imports System.Collections.Generic
Imports BV.TopLevel
Imports System.Runtime.Serialization

''' <summary>
''' Funcion que admite expresiones matematicas
''' </summary>
''' <remarks>utiliza el Parse para resolver las expresiones</remarks>
<CLSCompliant(True), Serializable()> _
Public Class [Function]
    Inherits FunctionBase

    Protected intArrItm As List(Of FunctionItem)
    Protected intHTblRanges As Dictionary(Of IComplexRegion, FunctionHeader)


    Public Sub New()
        MyBase.New()
        intArrItm = New List(Of FunctionItem)
        intHTblRanges = New Dictionary(Of IComplexRegion, FunctionHeader)
    End Sub

#Region "Properties"

    Public Overrides ReadOnly Property IsConstant() As Boolean
        Get
            For Each itm As FunctionItem In intArrItm
                If itm.IsNegative Then

                End If
            Next
        End Get
    End Property

    <Browsable(False)> _
    Public ReadOnly Property Expresions() As String()
        Get
            Dim s() As String
            Dim i As Integer
            ReDim s(Me.intArrItm.Count - 1)
            For i = 0 To Me.intArrItm.Count - 1
                s(i) = Me.intArrItm.Item(i).Expresion
            Next
            Return s
        End Get
    End Property

    <Browsable(False)> _
    Public ReadOnly Property Parts() As FunctionItem()
        Get
            Return intArrItm.ToArray()
        End Get
    End Property

    'retorna el subitm principal para un dado valor de la variable
    Friend ReadOnly Property Header() As FunctionItem
        Get
            Dim r As IComplexRegion
            r = Me.GetCurrentInterval(Parsing.Parser.Variable.Value)
            If intChanged = Wchanged.Expresion Then
                CType(Me.intHTblRanges.Item(r), FunctionItem).Clear()
                FunctionManager.Parse(Me.intHTblRanges.Item(r))
                intChanged = Wchanged.None
            End If
            Return Me.intHTblRanges.Item(r)
        End Get
    End Property

#End Region

#Region "Add Methods"

    Friend Sub Add(ByVal Header As FunctionHeader)
        Me.intArrItm.Add(Header)
        Me.intHTblRanges.Add(Header.Region, Header)
    End Sub

#End Region

#Region "Calc Methods"

    ''' <summary>
    ''' Calcula y devuelve el valor de una determinada funcion "parseada"
    ''' en un determinado punto, optimizada para funciones complejas
    ''' 20/8/03
    ''' </summary>
    Public Overrides Function Calc(ByVal z As ComplexUndefinied) As ComplexUndefinied 'Implements ICalcU.Calc
        Static LastValue, LastZ As ComplexUndefinied

        If Me.intChanged = Wchanged.None AndAlso LastZ = z Then
            Return LastValue
        End If
        Dim r As IComplexRegion

        Parser.Value = z
        r = GetCurrentInterval(z)
        If intChanged = Wchanged.Expresion Then
            Me.intHTblRanges.Item(r).Clear()
            FunctionManager.Parse(Me.intHTblRanges.Item(r))
        End If
        intChanged = Wchanged.None
        LastZ = z
        LastValue = Me.intHTblRanges.Item(r).Value(z)
        Return LastValue
    End Function

    ''' <summary>
    ''' Calcula y devuelve el valor de una determinada funcion "parseada"
    ''' en un determinado punto, optimizada para funciones de 1 variables
    ''' 20/8/03
    ''' </summary>
    Public Overrides Function Calc(ByVal s As Double) As ComplexUndefinied 'Implements ICalcSC.Calc
        Dim r As IComplexRegion

        Parser.Value = New ComplexUndefinied(s, 0)
        r = GetCurrentInterval(s)
        If intChanged = Wchanged.Expresion Then
            Me.intHTblRanges.Item(r).Clear()
            FunctionManager.Parse(Me.intHTblRanges.Item(r))
            intChanged = Wchanged.None
        End If
        intChanged = Wchanged.None
        Return Me.intHTblRanges.Item(r).Value(Parser.Value)
    End Function

#End Region

#Region "Other Methods"

    'retorna la region disponible para el punto dado
    Protected Function GetCurrentInterval(ByVal Svalue As ComplexUndefinied) As IComplexRegion
        For Each r As IComplexRegion In Me.intHTblRanges.Keys
            If TypeOf r Is RealRegion Then
                If CType(r, RealRegion).IsInTo(Svalue) Then
                    Return r
                End If
            Else
                If r.IsInTo(Svalue) Then
                    Return r
                End If
            End If
        Next
        Stop
        Return Nothing
    End Function

    Protected Function GetCurrentInterval(ByVal xValue As Double) As IComplexRegion
        For Each r As IComplexRegion In Me.intHTblRanges.Keys
            If TypeOf r Is RealRegion Then
                If CType(r, RealRegion).IsInTo(xValue) Then
                    Return r
                End If
            Else
                If r.IsInTo(New ComplexUndefinied(xValue, 0)) Then
                    Return r
                End If
            End If
        Next
        Stop
        Return Nothing
    End Function

    Public Overrides Function ToString() As String
        If Me.intArrItm.Count = 1 Then
            'Return Me.intSpaceDec.Name & " = " & Me.Expresions(0)
            Return Me.Expresions(0)
        End If
        Return Me.intSpaceDec.Name

        'Dim i As Integer
        'ToString = CType(Me.intArrItm.Item(0), FunctionItem).FunctionName
        'For i = 1 To Me.intArrItm.Count - 1
        '    ToString += " & " & CType(Me.intArrItm.Item(i), FunctionItem).FunctionName
        'Next
        'ToString += ControlChars.NewLine

        'For i = 0 To Me.intArrItm.Count - 1
        '    ToString += CType(Me.intArrItm.Item(i), FunctionItem).FunctionName
        '    ToString += " = "
        '    ToString += CType(Me.intArrItm.Item(i), FunctionItem).Expresion()
        '    ToString += ControlChars.NewLine
        'Next
    End Function

#End Region


    ''' <summary>
    ''' Corre antes de iniciar un analisis, ya que algunas funciones
    ''' (como las integrales) dependen de los valores previos.
    ''' Esta funcion iniciliza entonces las funciones
    ''' </summary>
    Public Overrides Sub BeginCalcs()
        For Each fitm As FunctionItem In Me.intHTblRanges.Values
            fitm.BeginCalcs()
        Next
    End Sub

#Region "Serializable"

    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
        Me.intArrItm.Item(0).Expresion = info.GetString("Expresion")
    End Sub

    Public Overrides Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
        info.AddValue("Expresion", Me.intArrItm.Item(0).Expresion)
        MyBase.GetObjectData(info, context)
    End Sub

#End Region


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)


Written By
Engineer Universidad Tecnológica Nacional
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions