Click here to Skip to main content
15,891,316 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.4K   6.2K   171  
.NET Mathematical Framework
Imports System.Math
Imports BV.Math
Imports BV.Core
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Reflection


'clase con capacidad matematica a traves de delegados
'obtiene informacion matematica calculando a traves de los objetos a los
'que se conecta
Public Class BVMathDelegate

    Protected objCalc As Object

    Public Sub New()

    End Sub

    Public Sub New(ByVal CalcObject As ICalcS)
        objCalc = CalcObject
    End Sub

    Public Sub New(ByVal CalcObject As ICalcSC)
        objCalc = CalcObject
    End Sub

    Public Sub New(ByVal CalcObject As ICalcU)
        objCalc = CalcObject
    End Sub

    Public Property SelectedObject() As Object
        Get
            Return Me.objCalc
        End Get
        Set(ByVal Value As Object)
            If TypeOf Value Is ICalcS OrElse TypeOf Value Is ICalcSC OrElse TypeOf Value Is ICalcU Then
                objCalc = Value
            Else
                If Value Is Nothing Then
                    objCalc = Value
                Else
                    Throw New Exception("Invalid Type")
                End If
            End If
        End Set
    End Property


    'Derivada mediante Delegados. Devuelve la pendiente en el punto dado
    '28/10
    Public Function DerivadeDelegate(ByVal s1 As Single, ByVal ds As Single) As Single
        Dim a1, a2 As Single

        If ds = 0 Then
            Throw New OverflowException("Error at increment value")
        End If
        If s1 <> 0 Then
            a1 = Calc(s1 * (1 - ds))
            a2 = Calc(s1 * (1 + ds))
            Return (0.5F * (a2 - a1) / (s1 * ds))
        Else
            a1 = Calc(s1 - ds)
            a2 = Calc(s1 + ds)
            Return (0.5F * (a2 - a1) / (ds))
        End If
    End Function

    'Derivada mediante Delegados. Devuelve la pendiente en el punto dado
    '28/10
    Public Function DerivadeDelegateI(ByVal s1 As Single, ByVal ds As Single) As Single
        Dim a1, a2 As Single

        If ds = 0 Then
            Throw New OverflowException("Error at increment value")
        End If
        If s1 <> 0 Then
            a1 = CalcImaginary(s1 * (1 - ds))
            a2 = CalcImaginary(s1 * (1 + ds))
            Return (0.5F * (a2 - a1) / (s1 * ds))
        Else
            a1 = CalcImaginary(s1 - ds)
            a2 = CalcImaginary(s1 + ds)
            Return (0.5F * (a2 - a1) / (ds))
        End If
    End Function

    ''28/10
    'Public Function CalcDelegate(ByVal s As ComplexUndefinied) As ComplexUndefinied
    '    If TypeOf objCalc Is ICalcSC Then
    '    ElseIf TypeOf objCalc Is ICalcS Then
    '        Return CType(objCalc, ICalcU).Calc(s)
    '    End If
    'End Function
    '
    '
    'Public Function CalcDelegateI(ByVal s As Single) As Single

    'End Function

    Public Function Calc(ByVal s As ComplexUndefinied) As ComplexUndefinied
        Return CType(objCalc, ICalcU).Calc(s)
    End Function

    ''' <summary>
    ''' calcula el valor de Y=f(x) para el valor indicado de x
    ''' devuelve la parte Imaginaria
    ''' 28/10
    ''' </summary>
    ''' <param name="s"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CalcImaginary(ByVal s As Single) As Single

        If TypeOf objCalc Is ICalcU Then
            If RealVariableIsMainAttribute.IsRealMain(objCalc) Then
                Return CType(objCalc, ICalcU).Calc(New ComplexUndefinied(s, 0)).Imaginary
            End If
            Return CType(objCalc, ICalcU).Calc(New ComplexUndefinied(0, s)).Imaginary
        Else
            If TypeOf objCalc Is ICalcSC Then
                Return CType(objCalc, ICalcSC).Calc(s).Imaginary
            ElseIf TypeOf objCalc Is ICalcS Then
                Stop
                Return CType(objCalc, ICalcS).Calc(s)
            End If
        End If
    End Function

    'calcula el valor de Y=f(x) para el valor indicado de x
    'devuelve la parte real
    '28/10
    Public Function Calc(ByVal s As Single) As Single

        If TypeOf objCalc Is ICalcU Then
            If RealVariableIsMainAttribute.IsRealMain(objCalc) Then
                Return CType(objCalc, ICalcU).Calc(New ComplexUndefinied(s, 0)).Real
            End If
            Return CType(objCalc, ICalcU).Calc(New ComplexUndefinied(0, s)).Real
        Else
            If TypeOf objCalc Is ICalcSC Then
                Return CType(objCalc, ICalcSC).Calc(s).Real
            ElseIf TypeOf objCalc Is ICalcS Then
                Return CType(objCalc, ICalcS).Calc(s)
            End If
        End If
    End Function


    ''calcula el valor de Y=f(x) para el valor indicado de x
    ''28/10
    'Public Function InvCalcDelegate(ByVal s As Single) As Single

    '    If TypeOf objCalc Is ICalcU Then
    '        Dim MyType As Type
    '        Dim myAttributes() As Object
    '        Dim j As Integer

    '        MyType = objCalc.GetType
    '        myAttributes = MyType.GetCustomAttributes(False)

    '        For j = 0 To myAttributes.Length - 1
    '            If TypeOf myAttributes(j) Is RealVariableIsMainAttribute Then
    '                If CType(myAttributes(j), RealVariableIsMainAttribute).IsRealMainVariable Then
    '                    Return CType(objCalc, ICalcU).Calc(New ComplexUndefinied(s, 0)).Real
    '                End If
    '                Exit For
    '            End If
    '        Next
    '        Return CType(objCalc, ICalcU).Calc(New ComplexUndefinied(0, s)).Real

    '    Else
    '        If TypeOf objCalc Is ICalcSC Then
    '            Return CType(objCalc, ICalcSC).Calc(s).Real
    '        ElseIf TypeOf objCalc Is ICalcS Then
    '            Return CType(objCalc, ICalcS).Calc(s)
    '        End If
    '    End If
    '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)


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