Mirage Source

Free ORPG making software.
It is currently Thu Apr 25, 2024 4:14 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Sequencer
PostPosted: Thu Apr 09, 2009 12:49 am 
Offline
Banned
User avatar

Joined: Mon Jun 05, 2006 9:22 pm
Posts: 394
Location: USA
So here is some stuff I did for my discrete class. It's in VB.NET 2008, take a look. It's pretty interesting. Finds various terms of sequences. Let me know what you think. (I do use the Object type, only to store huge numbers because of the factorial function.) Below are some code snippets.

ModMath.vb
Code:
    Public Function Factorial(ByVal n As Integer) As Decimal
        Dim i As Integer
        Dim fac As Object
        fac = 1
        i = n

        If n = 0 Then
            Factorial = 1
        Else

            Do While i > 1
                fac *= i
                i -= 1
            Loop

            Factorial = fac
        End If

    End Function

Code:
    Public Function nCr(ByVal n As Integer, ByVal k As Integer) As Long
        nCr = Factorial(n) / (Factorial(k) * Factorial(n - k))
    End Function

Code:
    Public Function FracPwr(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Double
        Dim raise As Double
        raise = Math.Pow(z, -1)
        FracPwr = Math.Pow(Math.Pow(x, y), raise)
    End Function


ModSequences.vb
Code:
    Public Sub TanSeq(ByVal n As Integer)
        Dim j, k As Integer
        Dim Term1, Term2 As Object
        Term2 = 0

        If n Mod 2 = 0 Then
            Exit Sub
        End If

        T(n) = Math.Pow(2, n)

        For j = 1 To n
            Term1 = 0
            For k = 0 To (n - j)
                Term1 += nCr(n + 1, k)
            Next
            Term2 += Math.Pow(-1, j + n + 1) * Math.Pow(j, n) * Term1
        Next

        If Term2 < 0 Then
            Term2 *= -1
        End If

        T(n) *= (Term2 / Math.Pow(2, n))

    End Sub

Code:
    Public Function chen(ByVal k As Integer) As Double
        Dim q, r As Double

        If k Mod 4 <> 0 Then
            q = k / 4
            r = k / 2
            chen = FracPwr(-1, k, 4) / FracPwr(2, k, 2) * 1
        Else
            chen = 0
        End If

    End Function

Code:
    Public Function Bernoulli(ByVal x As Integer, ByVal n As Integer) As Double
        Dim i, j As Integer
        Dim Bern As Double
        B(0) = 1
        Bern = 0

        For i = 0 To n
            If i > 0 Then
                For j = (i - 1) To 0
                    Bern += B(j) * (Math.Pow(x, j) / Factorial(j))
                Next
                B(i + 1) = Bern
            Else
                B(i + 1) = B(i) * (Math.Pow(x, i) / Factorial(i))
            End If
        Next

        Bernoulli = B(n)
    End Function

Code:
        Public Sub EulerianGen(ByVal n As Integer, ByVal m As Integer)
        Dim i, k As Integer
        i = 0

        'Generate for n = 1 to 3 (All can be done this way, but recurrance is easier.
        If n = 0 Or n = 1 Or n = 2 Or n = 3 Then

            For k = 0 To m
                i += (Math.Pow(-1, k) * nCr(n + 1, k) * Math.Pow(m + 1 - k, n))
            Next

            Eulerian(n, m) = i
            Exit Sub

        End If

        'Recurrance can only be done for n > 3
        If m <> 0 Then
            Eulerian(n, m) = (n - m) * Eulerian(n - 1, m - 1) + (m + 1) * Eulerian(n - 1, m)
        Else
            Eulerian(n, m) = 1
        End If

    End Sub

Code:
    Public Sub StirlingGen(ByVal n As Integer, ByVal k As Integer)

        'If n and k are the same, then the Stirling Number is one.
        If n = k Then
            Stirling(n, k) = 1
            Exit Sub
        End If

        'If k is one, then the Stirling Number is also one.
        If k = 1 And n = Not 0 Then
            Stirling(n, k) = 1
            Exit Sub
        End If

        'If k is 0 and n is not 0 then the stirling number is 0.
        If k = 0 And Not n = 0 Then
            Stirling(n, k) = 0
            Exit Sub
        End If

        'If k > n then 0
        If k > n Then
            Stirling(n, k) = 0
            Exit Sub
        End If

        'Recurrance formula as long as k <= n
        If Not k > n Then
            Stirling(n, k) = Stirling(n - 1, k - 1) + k * Stirling(n - 1, k)
        Else
            Stirling(n, k) = 0
        End If

    End Sub



There is the meat. :D

[Edited for shorthand.]


Attachments:
File comment: Sequencer Program
Sequencer.rar [351.85 KiB]
Downloaded 331 times


Last edited by James on Thu Apr 09, 2009 3:45 pm, edited 2 times in total.
Top
 Profile  
 
 Post subject: Re: Sequencer
PostPosted: Thu Apr 09, 2009 1:51 pm 
Offline
Pro
User avatar

Joined: Tue Nov 13, 2007 2:42 pm
Posts: 509
Just curious why you don't use some .net short-hand assignments?

An example of a short hand assignment:
Code:
a = a + 1

Code:
a += 1


I see a few through your code that you could use.


Top
 Profile  
 
 Post subject: Re: Sequencer
PostPosted: Thu Apr 09, 2009 3:16 pm 
Offline
Banned
User avatar

Joined: Mon Jun 05, 2006 9:22 pm
Posts: 394
Location: USA
I wasn't sure if that was a C#-only shorthand or universal to.net.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group