| Mirage Source http://miragesource.net/forums/ |
|
| Sequencer http://miragesource.net/forums/viewtopic.php?f=158&t=5428 |
Page 1 of 1 |
| Author: | James [ Thu Apr 09, 2009 12:49 am ] | ||
| Post subject: | Sequencer | ||
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. [Edited for shorthand.]
|
|||
| Author: | Jacob [ Thu Apr 09, 2009 1:51 pm ] |
| Post subject: | Re: Sequencer |
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. |
|
| Author: | James [ Thu Apr 09, 2009 3:16 pm ] |
| Post subject: | Re: Sequencer |
I wasn't sure if that was a C#-only shorthand or universal to.net. |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |
|