#Region FILEINFO #cs #INDEX# ==## Math2.au3 ##=============================================================== Title .........: Mathematical calculations AutoIt Version : 3.2 Language ......: English Description ...: Special Functions for mathematical calculations. Author(s) .....: Strahleman Version .......: 1.1 Create ........: 2013-09-12 Edit ..........: 2014-02-13 ======================================================================================== #ce If @ScriptFullPath = @ScriptDir & "\" & "Math2.au3" Then ConsoleWrite(@CRLF & "> +---< Math2.au3 >------------------+" & @CRLF) ConsoleWrite("> ¦ Author : Strahleman ¦" & @CRLF) ConsoleWrite("> ¦ Version : 1.1 ¦" & @CRLF) ConsoleWrite("> ¦ Created : 2013-09-12 ¦" & @CRLF) ConsoleWrite("> ¦ Last Edit : 2014-02-13 ¦" & @CRLF) ConsoleWrite("> +----------------------------------+" & @CRLF & ">" & @CRLF) ConsoleWrite('> This is an Autoit UDF. To use them, add the following line in your script:' & @CRLF) ConsoleWrite('> #include "Math2.au3"' & @CRLF & @CRLF) EndIf #EndRegion FILEINFO #Region INCLUDES #include-once #include "GDIPlus.au3" #EndRegion INCLUDES #Region CONSTANTS ; #CONSTANTS# ========================================================================== ; Description ...: All Constants for calculation. ; Author ........: Strahleman ; ====================================================================================== Global Const $inf = 1/0 ; infinity Global Const $ind = 0/0 ; undefined Global Const $pi = 3.141592653589793238462643383279 ; pi Global Const $e = 2.718281828459045235360287471352 ; Euler's number Global Const $em = 0.577215664901532860606512090082 ; Euler-Mascheroni constant Global Const $ratio = 1.618033988749894848204586834365 ; golden mean Global Const $sq2pi = 2.5066282746310005024157652848110 ; Sqrt(2pi) Global Const $DEG = 180 / $pi ; Radiants in Degree Global Const $RAD = $pi / 180 ; Degree in Radiants Global Const $Reps = __Reps() ; Number of digits of computer precision Global Const $meps = __Machine_Epsilon() ; computer precision #EndRegion CONSTANTS #Region FUNCTION #Region Internal Functions Func __Reps() Return Round(Abs(Log(__Machine_Epsilon())/log(10)),0) EndFunc Func __Machine_Epsilon() Local $n, $meps = 1 Do $meps *= 0.5 $n = 1 + 0.5 * $meps Until $n <= 1 Return $meps EndFunc Func __Machar() ; Machine Arithmetic: ;$machar[0]'ibeta is the radix in which numbers are represented, almost always 2, but occasionally 16, or even 10. ;$machar[1]'it is the number of base-ibeta digits in the floating-point mantissa M. ;$machar[2]'irnd returns a code in the range 0...5, giving information on what kind of rounding is done in addition, and on how underflow is handled. [0,3..Cut; 1,4..IEEE-conform; 2,5..Non IEEE-conform; x $zero) Then $machar[2] = 1 Local $tempa = $a + $beta $temp = $tempa + $betah If (($machar[2] = 0) And (($temp - $tempa) <> $zero)) Then $machar[2] = 2 $machar[5] = $machar[1] + 3 Local $betain = $one / $beta $a = $one For $i = 1 To $machar[5] $a = $a * $betain Next $b = $a While 1 $temp = $one - $a If (($temp - $one) <> $zero) Then ExitLoop $a = $a * $beta $machar[5] -= 1 WEnd $machar[5] = -$machar[5] $machar[10] = $a $machar[4] = -$machar[1] - 3 $a = $b While 1 $temp = $one + $a If (($temp - $one) <> $zero ) Then ExitLoop $a = $a * $beta $machar[4] = $machar[4] + 1 WEnd $machar[9] = $a $machar[3] = 0 $temp = $one + $machar[9] If (($machar[2] = 0) And (($temp * $one - $one) <> $zero)) Then $machar[3] = 1 Local $i = 0 Local $k = 1 Local $z = $betain Local $t = $one + $machar[9] Local $nxres = 0 Local $y = 0 While 1 $y = $z $z = $y * $y $a = $z * $one $temp = $z * $t If ((($a + $a) = $zero) and (Abs($z) >= $y)) Then ExitLoop $temp1 = $temp * $betain If (($temp1 * $beta) = $z) Then ExitLoop $i += 1 $k = $k + $k WEnd Local $mx = 0, $iz = 0 If $machar[0] <> 10 Then $machar[6] = $i + 1 $mx = $k + $k Else $machar[6] = 2 $iz = $machar[0] While $k >= $iz $iz *= $machar[0] $machar[6] += 1 WEnd $mx = $iz + $iz + 1 EndIf While 1 $machar[11] = $y $y *= $betain $a = $y * $one $temp = $y * $t If ((($a + $a) <> $zero) And (Abs($y) < $machar[11])) Then $k = $k + 1 $temp1 = $temp * $betain If ((($temp1 * $beta) = $y) And ($temp <> $y)) Then $nxres = 3 $machar[11] = $y ExitLoop EndIf Else ExitLoop EndIf WEnd $machar[7] = -$k If (($mx<=($k + $k - 3)) and ($machar[0] <> 10)) Then $mx = $mx + $mx $machar[6] = $machar[6] + 1 EndIf $machar[8] = $mx + $machar[7] $machar[2] = $machar[2] + $nxres If ($machar[2] >= 2) Then $machar[8] = $machar[8] - 2 EndIf $i = $machar[8] + $machar[7] If (($machar[0] = 2) And ($i = 0)) Then $machar[8] = $machar[8] - 1 If ($i > 20) Then $machar[8] = $machar[8] - 1 If ($a <> $y) Then $machar[8] = $machar[8] - 2 $machar[12] = $one - $machar[10] If (($machar[12] * $one) <> $machar[12]) Then $machar[12] = $one - $beta * $machar[10] $machar[12] = $machar[12] / ($beta * $beta * $beta * $machar[11]) $i = $machar[8] + $machar[7] + 3 For $j = 1 To $i If ($machar[0] = 2) Then $machar[12] = $machar[12] + $machar[12] If ($machar[0] <> 2) Then $machar[12] = $machar[12] * $beta Next Return $machar EndFunc Func __Swap(ByRef $a, ByRef $b) Local $tmp = $a $a = $b $b = $tmp EndFunc Func __Sign($a, $b) If $b >= 0 Then Return Abs($a) Else Return -Abs($a) EndIf EndFunc Func __Pythag($a, $b) Local $absa = Abs($a) Local $absb = Abs($b) If $absa > $absb Then Return $absa * Sqrt(1 + ($absb / $absa)^2) Else If $absb = 0 Then Return 0 Else Return $absb * Sqrt(1 + ($absa / $absb)^2) EndIf EndIf EndFunc Func __Imax($a, $b) Local $ia = Int($a) Local $ib = Int($b) If $ia > $ib Then Return $ia Else Return $ib EndIf EndFunc Func __Sort(ByRef $aSort, $Start, $End) Local $vTmp ; InsertionSort (faster for smaller segments) If ($End - $Start) < 15 Then Local $vCur For $i = $Start + 1 To $End $vTmp = $aSort[$i] If IsNumber($vTmp) Then For $j = $i - 1 To $Start Step -1 $vCur = $aSort[$j] ; If $vTmp >= $vCur Then ExitLoop If ($vTmp >= $vCur And IsNumber($vCur)) Or (Not IsNumber($vCur) And StringCompare($vTmp, $vCur) >= 0) Then ExitLoop $aSort[$j + 1] = $vCur Next Else For $j = $i - 1 To $Start Step -1 If (StringCompare($vTmp, $aSort[$j]) >= 0) Then ExitLoop $aSort[$j + 1] = $aSort[$j] Next EndIf $aSort[$j + 1] = $vTmp Next Return EndIf ; QuickSort Local $Left = $Start, $Right = $End, $Pivot = $aSort[Int(($Start + $End) / 2)] Do While $aSort[$Left] < $Pivot $Left += 1 WEnd While $aSort[$Right] > $Pivot $Right -= 1 WEnd ; Swap If $Left <= $Right Then $vTmp = $aSort[$Left] $aSort[$Left] = $aSort[$Right] $aSort[$Right] = $vTmp $Left += 1 $Right -= 1 EndIf Until $Left > $Right __Sort($aSort, $Start, $Right) __Sort($aSort, $Left, $End) EndFunc Func __gamma($x) ; gamma function Local $xx = $x If $x < 0 Then $x = -$x If $x = 0 Then Return 1/0 If $x = 1 Or $x = 2 Then Return 1 If (Round($xx,0) = $xx and $xx <= 0) Then Return 1/0 If $x = Abs(Int($x)) And $x <= 20 Then Local $a[18] = [2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800, _ 87178291200,1307674368000,20922789888000,355687428096000,6.402373705728e15,1.21645100408832e17] Return $a[$x-3] EndIf Local $g=607/128; % best results when 4<=g<=5 Local $c[15] = [ 0.99999999999999709182, _ 57.156235665862923517, _ -59.597960355475491248, _ 14.136097974741747174, _ -0.49191381609762019978, _ 0.33994649984811888699e-4, _ 0.46523628927048575665e-4, _ -0.98374475304879564677e-4, _ 0.15808870322491248884e-3, _ -0.21026444172410488319e-3, _ 0.21743961811521264320e-3, _ -0.16431810653676389022e-3, _ 0.84418223983852743293e-4, _ -0.26190838401581408670e-4, _ 0.36899182659531622704e-5] $x = $x - 1 Local $xh = $x + 0.5, $xgh = $xh + $g, $xp = $xgh^($xh*0.5), $ss = 0 For $i=13 to 0 step -1 $ss += ($c[$i + 1]) / ($x + $i + 1) Next Local $f = ($sq2pi * ($c[0] + $ss))*(($xp * Exp(-$xgh)) * $xp) If $x < 0 Then Return -$pi/($xx * $f * Sin($pi * $xx)) Return $f EndFunc Func __gammaln($x) ; Log(gamma) If $x = 0 Then Return 1/0 If $x = 1 Or $x = 2 Then Return 0 If $x = Abs(Int($x)) And $x <= 20 Then Local $a[18] = [2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6.402373705728e15,1.21645100408832e17] Return Log($a[$x-3]) EndIf Local $ser = 1.000000000190015, $stp = 2.5066282746310005 Local $coef[6] = [76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -.5395239384953e-5] Local $z = $x, $y = $z, $tmp = $z + 5.5 $tmp = ($z + 0.5) * Log($tmp) - $tmp For $i = 0 To 5 $y = $y + 1 $ser += $coef[$i]/$y Next Return Round($tmp + Log($stp * $ser / $z), $Reps-1) EndFunc Func __digamma($x) ; digamma function Local $x2 = 9.5, $x1 = 1e-6 Local $a[6] = [1/12, 1/120, 1/252, 1/240, 1/132, 691/32760] Local $y = 0 If $x < 0 And Round($x, 0) = $x Then Return $ind If $x < 0 Then $y = __digamma(-$x + 1) + $pi * 1/Tan(-$pi*$x) If $x = 0 Then $y = -1/0 If ($x > 0 And $x <= $x1) Then $y = $y - $em - 1 / $x + $pi^2/6 * $x While ($x > $x1 And $x < $x2) $y = $y - 1 / $x $x = $x + 1 WEnd If $x >= $x2 Then $y = $y + Log($x) - 0.5 / $x $y = $y - (1 / $x)^2 * ( $a[0] - (1 / $x)^2 * ( $a[1] - (1 / $x)^2 * ( $a[2] - (1 / $x)^2 * ( $a[3] - (1 / $x)^2 * ($a[4] - (1 / $x)^2 * $a[5] ))))) EndIf Return $y EndFunc Func __harmnr($n) ; harmonic number If $n < 1e10 Then Return $em + __digamma($n + 1) Else Return $em + Log($n) EndIf EndFunc Func __gser($a, $x) ;serial expansion of incomplete gamma function Local Const $ITMAX = 100 Local Const $EPS = 3.0e-7 if $x <= 0 Then if $x < 0 Then Return SetError(1, 0, 0) Return 0 else Local $ap = $a Local $del = 1 / $a Local $sum = 1 / $a for $n = 1 to $ITMAX $ap += 1 $del *= $x / $ap $sum += $del if Abs($del) < (Abs($sum) * $EPS) Then Return $sum * Exp(-$x + $a * log($x) - (__gammaln($a))) EndIf Next Return SetError(1, 0, 0) EndIf EndFunc Func __gcf($a, $x) ;contiued fraction expansion of incomplete gamma function Local Const $ITMAX = 100 Local Const $EPS = 3.0e-7 Local Const $FPMIN = 1.0e-30 Local $b = $x + 1 - $a Local $c = 1 / $FPMIN Local $d = 1 / $b Local $h = $d Local $i, $an, $del for $i = 1 To $ITMAX $an = -$i * ($i - $a) $b += 2 $d = $an * $d + $b if Abs($d) < $FPMIN Then $d = $FPMIN $c = $b + $an / $c if Abs($c) < $FPMIN Then $c = $FPMIN $d = 1 / $d $del = $d * $c $h *= $del if Abs($del - 1) < $meps Then ExitLoop Next if ($i > $ITMAX) Then Return SetError(2, 0, 0) Return Exp(-$x + $a * log($x) - (__gammaln($a))) * $h EndFunc Func __gammap($a, $x) ;incomplete gamma function P If $a <= 0 Or $x < 0 Then Return SetError(1, 0, 0) If $x < ($a + 1) Then Return __gser($a, $x) Else Return (1 - __gcf($a, $x)) EndIf EndFunc Func __gammaq($a, $x) ;incomplete gamma function Q If $a <= 0 Or $x < 0 Then SetError(1) Return EndIf If $x < ($a + 1) Then Return (1 - __gser($a, $x)) Else Return __gcf($a, $x) EndIf EndFunc Func __gammainv2($P, $a = 1, $b = 1) ;Inverse Gamma Regularized Local $l = 100 Local $n = 0 If $P <= 0 Or $P >= 1 Or $a <= 0 Or $b <= 0 Then Return SetError(1, 0, 0) Local $mn = $a * $b Local $v = $mn * $b Local $temp = Log($v + $mn^2) Local $mu = 2 * Log($mn) - 0.5 * $temp Local $sigma = -2 * Log($mn) + $temp Local $xk = Exp((__erfinv(($P * 2) - 1)*sqrt(2*$sigma^2)) + $mu) Local $xnew, $h = 1 While (Abs($h) > Sqrt($meps) * Abs($xk)) And (Abs($h) > Sqrt($meps)) And ($n < $l) $n += 1 $h = ((__gammap($a, $xk / $b)) - $P) / (Exp(($a - 1) * Log($xk) - ($xk / $b) - $a * Log($b) - __gammaln($a))) $xnew = $xk - $h If $xnew < 0 Then $xnew = $xk / 10 $h = $xk - $xnew EndIf $xk = $xnew WEnd Return $xk EndFunc Func __erfcore($x, $jint=0) ;error function ;erf($x) = erfcore($x, 0) ;erfc($x) = erfcore($x, 1) ;erfcx($x) = exp($x^2)*erfc($x) = erfcore($x,2) Local $a, $b, $c, $d, $p, $q, $y, $z, $xnum, $xden, $i, $result, $del If Not (Number($x) Or Int($x) Or $x = 0) Then Return SetError(1, 0, 0) Local $xbreak = 0.46875 If Abs($x) <= $xbreak Then Dim $a[5] = [3.16112374387056560e00, 1.13864154151050156e02, _ 3.77485237685302021e02, 3.20937758913846947e03, _ 1.85777706184603153e-1] Dim $b[4] = [2.36012909523441209e01, 2.44024637934444173e02, _ 1.28261652607737228e03, 2.84423683343917062e03] $y = Abs($x) $z = $y*$y $xnum = $a[4]*$z $xden = $z For $i = 0 To 2 $xnum = ($xnum + $a[$i])*$z $xden = ($xden + $b[$i])*$z Next $result = $x*($xnum + $a[3])/($xden + $b[3]) If $jint <> 0 Then $result = 1 - $result EndIf If $jint = 2 Then $result = Exp($z)*$result EndIf ElseIf (Abs($x) > $xbreak And Abs($x) <= 4.0) Then Dim $c[9] = [5.64188496988670089e-1, 8.88314979438837594e00, _ 6.61191906371416295e01, 2.98635138197400131e02, _ 8.81952221241769090e02, 1.71204761263407058e03, _ 2.05107837782607147e03, 1.23033935479799725e03, _ 2.15311535474403846e-8] Dim $d[8] = [1.57449261107098347e01, 1.17693950891312499e02, _ 5.37181101862009858e02, 1.62138957456669019e03, _ 3.29079923573345963e03, 4.36261909014324716e03, _ 3.43936767414372164e03, 1.23033935480374942e03] $y = Abs($x) $xnum = $c[8]*$y $xden = $y For $i = 0 To 6 $xnum = ($xnum + $c[$i])*$y $xden = ($xden + $d[$i])*$y Next $result = ($xnum + $c[7])/($xden + $d[7]) If $jint <> 2 Then $z = Int($y * 16)/16 $del = ($y-$z)*($y+$z) $result = Exp(-$z*$z)*Exp(-$del)*$result EndIf ElseIf Abs($x) > 4.0 Then Dim $p[6] = [3.05326634961232344e-1, 3.60344899949804439e-1, _ 1.25781726111229246e-1, 1.60837851487422766e-2, _ 6.58749161529837803e-4, 1.63153871373020978e-2] Dim $q[5] = [2.56852019228982242e00, 1.87295284992346047e00, _ 5.27905102951428412e-1, 6.05183413124413191e-2, _ 2.33520497626869185e-3] $y = Abs($x) $z = 1/($y*$y) $xnum = $p[5]*$z $xden = $z For $i = 0 To 3 $xnum = ($xnum + $p[$i])*$z $xden = ($xden + $q[$i])*$z Next $result = $z*($xnum + $p[4])/($xden + $q[4]) $result = (1/sqrt(4*ATan(1)) - $result)/$y If $jint <> 2 Then $z = Int($y * 16)/16 $del = ($y-$z)*($y+$z) $result = Exp(-$z*$z)*Exp(-$del)*$result If $result < $meps Then $result = 0 EndIf EndIf EndIf If $jint = 0 Then If $x > $xbreak Then $result = (0.5 - $result) + 0.5 EndIf If $x < -$xbreak Then $result = (-0.5 + $result) - 0.5 EndIf ElseIf $jint = 1 Then If $x < -$xbreak Then $result = 2.0 - $result EndIf Else ;$jint = 2 If $x < -$xbreak Then $z = Int($x * $Reps)/$Reps $del = ($x-$z)*($x+$z) $y = Exp($z*$z)*Exp($del) $result = ($y+$y) - $result EndIf EndIf If Abs($result) < $meps then $result = 0 Return $result EndFunc Func __erfinv($y) ;inverse error function Local $it = 0, $maxit = 100, $s = sqrt ($pi) / 2, $z_old = 1 Local $z_new = Sqrt(-Log(1 - Abs($y))) * _Sgn($y) While Abs($z_new - $z_old) > $meps ;Iteration of Newton-Raphson correction to full accuracy. $z_old = $z_new $z_new = $z_old - (__erfcore ($z_old) - $y) * Exp($z_old^2) * $s $it += 1 If ($it > $maxit) Then ExitLoop WEnd Return $z_new EndFunc Func __beta($a, $b) ;beta function If Not IsNumber($a) or not IsNumber($b) Then Return SetError(1, 0, 0) Return Exp(__gammaln($a)+__gammaln($b)-__gammaln($a+$b)) EndFunc Func __betaln($a, $b) ;natural logarithm of beta function If Not IsNumber($a) or not IsNumber($b) Then Return SetError(1, 0 ,0) Return __gammaln($a)+__gammaln($b)-__gammaln($a+$b) EndFunc Func __betacf($x, $a, $b) ;contiued fraction expansion of beta function ;~ Local Const $MAXIT = 1000 Local Const $FPMIN = 1e-30 Local $qab = $a + $b Local $qap = $a + 1 Local $qam = $a - 1 Local $c = 1 Local $d = 1 - $qab * $x / $qap If Abs($d) < $FPMIN Then $d = $FPMIN $d = 1 / $d Local $h = $d Local $m, $m2 Local $aa, $c, $del For $m = 1 To 1000;$MAXIT $m2 = 2 * $m $aa = $m * ($b - $m) * $x / (($qam + $m2) * ($a + $m2)) $d = 1 + $aa * $d If Abs($d) < $FPMIN Then $d = $FPMIN $c = 1 + $aa / $c If Abs($c) < $FPMIN Then $c = $FPMIN $d = 1 / $d $h *= ($d * $c) $aa = -($a + $m) * ($qab + $m) * $x / (($a + $m2) * ($qap + $m2)) $d = 1 + $aa * $d If Abs($d) < $FPMIN Then $d = $FPMIN $c = 1 + $aa / $c If Abs($c) < $FPMIN Then $c = $FPMIN $d = 1 / $d $del = $d * $c $h *= $del If Abs($del - 1) < $meps Then ExitLoop Next If $m > 1000 Then;$MAXIT Then Return SetError(2, 0, 0) EndIf Return $h EndFunc Func __betainc($x, $a, $b) ;incomplete beta function If $x < 0 or $x > 1 Then Return SetError(1, 0, 0) Local $logBeta If $x = 0 Or $x = 1 Then $logBeta = 0 Else $logBeta = __gammaln($a + $b) - __gammaln($a) - __gammaln($b) + $a * Log($x) + $b * Log(1 - $x) If $x < (($a + 1) / ($a + $b + 2)) Then Return Exp($logBeta) * __betacf($x, $a, $b) / $a Else Return 1 - (Exp($logBeta) * __betacf(1 - $x, $b, $a) / $b) EndIf EndIf EndFunc Func __bisect($x, $P, $a, $b, $xtol, $Ptol) ;bisection Local $x0 = 0, $x1 = 1 Local $Px = 0 While Abs($x1 - $x0) > $xtol $Px = ($x^($a-1) * (1-$x)^($b-1)) / (Exp(__gammaln($a) + __gammaln($b) - __gammaln($a + $b))) If Abs($Px - $P) < $Ptol Then Return $x ElseIf ($Px < $P) Then $x0 = $x ElseIf ($Px > $P) Then $x1 = $x EndIf $x = 0.5 * ($x0 + $x1) WEnd Return $x EndFunc Func __betainv($P, $a, $b) ;Inverse Beta Regularized Local $x, $m If $P = 0 Then Return 0 If $P = 1 Then Return 1 If ($P > 0 And $P <= 0.5 And $a > 0 And $b > 0) Then $m = $a / ($a + $b) If $P < 0.1 Then $x = (Exp((Log($a) + __gammaln($a) + __gammaln($b) - __gammaln($a + $b) + Log($P)))/$a)*((1-$P)^(-($b-1)/$a)) Else $x = $m EndIf If $x > $m Then $x = $m Else $x = $m EndIf $x = __bisect($x, $P, $a, $b, 0.01, 0.01) Local $l, $dP, $phi, $n = 0, $stp Do $dP = $P - __betainc($x, $a, $b) $phi = ($x^($a-1)*(1-$x)^($b-1))/(__beta($a, $b)) $n += 1 If $dP = 0 Or $n > 128 Then ExitLoop If 2 * Abs($dP / $x) > $phi Then $l = $dP / (2 * Abs($dP / $x)) Else $l = $dP / $phi EndIf $stp = $l Local $stp1 = -(($a - 1) / $x - ($b - 1) / (1 - $x)) * $l * $l / 2 Local $stp2 = $stp If Abs($stp1) < Abs($stp) Then $stp2 += $stp1 Else $stp2 *= 2 * Abs($stp / $stp1) EndIf If (($x + $stp2) > 0 And ($x + $stp2) < 1) Then $x += $stp2 Else $x = Sqrt($x) * Sqrt($m) EndIf Until Abs($stp) > ($Reps * $x) If Abs($dP) > (Sqrt($meps) * $P) Then SetError(2) Return 0 EndIf Return Round($x, $Reps-1) EndFunc Func __ln1p($x) ;Ln(1+x) ;0<=x<=1 Local $coef[8] = [0.9999964239, -0.4998741238, 0.3317990258, -0.2407338084, 0.1676540711, -0.0953293897, 0.0306884937, -0.0064535442] Local $s = 0, $i = 0 For $i = 0 To 7 $s += $coef[$i]*$x^($i+1) Next ;$i Return $s EndFunc Func __gaussj($A) ;Gauss-Jordan elimination ;Gauss-Jorden Elimination If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1), $m = 1 If $n <> UBound($A,2) Then Return SetError(2, 0, 0) Local $i,$icol,$irow,$j,$k,$l,$ll Local $indxc[$n],$indxr[$n],$ipiv[$n] Local $big,$dum,$pivinv Local $B[$n][1] For $j = 0 To $n - 1 ;11 $ipiv[$j] = 0 Next ;11 For $i = 0 To $n - 1 ;22 $big = 0 For $j = 0 To $n - 1 ;13 If $ipiv[$j] <> 1 Then For $k = 0 To $n - 1 ;12 If $ipiv[$k] = 0 Then If Abs($A[$j][$k]) >= $big Then $big = Abs($A[$j][$k]) $irow = $j $icol = $k EndIf EndIf Next ;12 EndIf Next ;13 $ipiv[$icol] = $ipiv[$icol] + 1 If $irow <> $icol Then For $l = 0 To $n - 1 ;14 __Swap($A[$irow][$l],$A[$icol][$l]) Next ;14 For $l = 0 To $m - 1 ;15 __Swap($B[$irow][$l],$B[$icol][$l]) Next ;15 EndIf $indxr[$i] = $irow $indxc[$i] = $icol $pivinv = 1/$A[$icol][$icol] $A[$icol][$icol] = 1 For $l = 0 To $n - 1 ;16 $A[$icol][$l] = $A[$icol][$l] * $pivinv Next ;16 For $l = 0 To $m - 1 ;17 $B[$icol][$l] = $B[$icol][$l] * $pivinv Next ;17 For $ll = 0 To $n - 1 ;21 If $ll <> $icol Then $dum = $A[$ll][$icol] $A[$ll][$icol] = 0 For $l = 0 To $n - 1 ;18 $A[$ll][$l] = $A[$ll][$l] - $A[$icol][$l] * $dum Next ;18 For $l = 0 To $m - 1 ;19 $B[$ll][$l] = $B[$ll][$l] - $B[$icol][$l] * $dum Next ;19 EndIf Next ;21 Next ;22 For $l = $n - 1 To 0 Step -1 ;24 If ($indxr[$l] <> $indxc[$l]) Then For $k = 0 To $n - 1 ;23 __Swap($A[$k][$indxr[$l]],$A[$k][$indxc[$l]]) Next ;23 EndIf Next ;24 Return $A EndFunc Func __luminv($A) ;inverse LU decomposition Local $n = UBound($A) Local $id[$n] Local $d __ludcmp($A, $id, $d) Local $col[$n] Local $y[$n][$n] For $j = 1 To $n For $i = 1 to $n $col[$i-1] = 0 Next $col[$j-1] = 1 __lubksb($A, $id, $col) For $i = 1 to $n $y[$i-1][$j-1] = $col[$i-1] Next Next Return $y EndFunc Func __ludcmp(ByRef $A, ByRef $indx, ByRef $d) ;LU decomposition Local $n = UBound($A) Local $vv[$n];, $indx[$n] Local Const $TINY = 1.0e-20 $d = 1 Local $i, $j, $k, $big, $dum, $imax, $sum, $temp For $i = 1 To $n ;12 $big = 0 For $j = 1 To $n ;11 $temp = Abs($A[$i-1][$j-1]) If ($temp > $big) Then $big = $temp EndIf Next ;11 $vv[$i-1] = 1 / $big Next ;12 For $j = 1 To $n ;19 For $i = 1 To $j - 1 ;14 $sum = $A[$i-1][$j-1] For $k=1 To $i - 1 ;13 $sum -= $A[$i-1][$k-1] * $A[$k-1][$j-1] Next ;13 $A[$i-1][$j-1] = $sum Next ;14 $big=0 For $i=$j to $n ;16 $sum = $A[$i-1][$j-1] For $k = 1 To $j - 1 ;15 $sum -= $A[$i-1][$k-1] * $A[$k-1][$j-1] Next ;15 $A[$i-1][$j-1] = $sum $dum = $vv[$i-1] * Abs($sum) If ($dum >= $big) Then $big = $dum $imax = $i EndIf Next ; 16 If $j <> $imax Then For $k = 1 To $n ;17 __Swap($A[$imax-1][$k-1], $A[$j-1][$k-1]) Next ;17 $d = -($d) $vv[$imax-1] = $vv[$j-1] EndIf $indx[$j-1] = $imax If ($A[$j-1][$j-1] = 0) Then $A[$j-1][$j-1] = $TINY If $j <> $n Then $dum = 1 / ($A[$j-1][$j-1]) For $i = $j + 1 To $n ;18 $A[$i-1][$j-1] *= $dum Next ;18 EndIf Next ;19 Return EndFunc Func __lubksb(ByRef $A, ByRef $indx, $b) ;LU backsubstitution Local $n = UBound($A) Local $i, $ii = 0, $ip, $j, $sum;, $b[$n] For $i = 1 To $n ;12 $ip = $indx[$i-1] $sum = $b[$ip-1] $b[$ip-1] = $b[$i-1] If $ii Then For $j = $ii To $i - 1 ;11 $sum -= $A[$i-1][$j-1] * $b[$j-1] Next ;11 elseif $sum Then $ii = $i EndIf $b[$i-1] = $sum Next ;12 For $i = $n To 1 step -1 ;14 $sum = $b[$i-1] For $j = $i + 1 To $n ;13 $sum -= $A[$i-1][$j-1] * $b[$j-1] Next ;13 $b[$i-1] = $sum / $A[$i-1][$i-1] Next ;14 Return EndFunc Func __digsum($x) ;digit sum Local $s = 0 For $i = 1 To StringLen($x) $s += Int(StringMid($x, $i, 1)) Next Return $s EndFunc Func __balance($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) Local $RADIX = 2 Local $sqrdx = $RADIX^2 Local $last = 0 Local $c, $r, $g, $f, $s While $last = 0 $last = 1 For $i = 1 To $n $c = 0 $r = 0 For $j = 1 To $n If $j <> $i Then $c += Abs($A[$j-1][$i-1]) $r += Abs($A[$i-1][$j-1]) EndIf Next If $c And $r Then $g = $r / $RADIX $f = 1 $s = $c + $r While $c < $g $f *= $RADIX $c *= $sqrdx WEnd If (($c + $r) / $f < 0.95 * $s) Then $last = 0 $g = 1 / $f For $j = 1 To $n $A[$i-1][$j-1] *= $g Next For $j = 1 To $n $A[$j-1][$i-1] *= $f Next EndIf EndIf Next WEnd Return $A EndFunc Func __elmhes($A) ;eleminate to Hessenberg matrix If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If $n <> UBound($A,2) Then Return SetError(2, 0, 0) Local $i, $j, $m Local $y, $x For $m = 2 To $n-1 $x = 0 $i = $m For $j = $m To $n If Abs($A[$j-1][$m-1 - 1]) > Abs($x) Then $x = $A[$j-1][$m-1 - 1] $i = $j EndIf Next If $i <> $m Then For $j = $m - 1 To $n __Swap($A[$i-1][$j-1], $A[$m-1][$j-1]) Next For $j = 1 To $n __Swap($A[$j-1][$i-1], $A[$j-1][$m-1]) Next EndIf If $x Then For $i = $m + 1 To $n $y = $A[$i-1][$m-1 - 1] If $y <> 0 Then $y /= $x $A[$i-1][$m-1 - 1] = $y For $j = $m To $n $A[$i-1][$j-1] -= $y * $A[$m-1][$j-1] Next For $j = 1 To $n $A[$j-1][$m-1] += $y * $A[$j-1][$i-1] Next EndIf Next EndIf Next Return $A EndFunc Func __hqr($A) ;Eigenvalues of Hessenberg matrix If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If $n <> UBound($A,2) Then Return SetError(2, 0, 0) Local $wr[$n], $wi[$n] Local $nn, $m, $l, $k, $j, $its, $i, $mmin Local $z, $y, $x, $w, $v, $u, $t, $s, $r, $q, $p, $anorm $anorm = 0 For $i = 1 To $n For $j = __Imax($i - 1, 1) To $n $anorm += Abs($A[$i-1][$j-1]) Next Next $nn = $n $t = 0 While $nn >= 1 $its = 0 Do For $l = $nn To 2 Step -1 $s = Abs($A[$l-1 - 1][$l-1 - 1]) + Abs($A[$l-1][$l-1]) If $s = 0 Then $s = $anorm If (Abs($A[$l-1][$l-1 - 1]) + $s) = $s Then $A[$l-1][$l-1 - 1] = 0 ExitLoop EndIf Next $x = $A[$nn-1][$nn-1] If $l = $nn Then ;~ ConsoleWrite("One root found") $wr[$nn-1] = $x + $t $wi[$nn-1] = 0 ;~ ConsoleWrite("; root1 = " & $wr[$nn-1] & @CRLF) $nn -= 1 Else $y = $A[$nn-1 - 1][$nn-1 - 1] $w = $A[$nn-1][$nn-1 - 1] * $A[$nn-1 - 1][$nn-1] If $l = $nn - 1 Then ;~ ConsoleWrite("Two roots found") $p = 0.5 * ($y - $x) $q = $p^2 + $w $z = Sqrt(Abs($q)) $x += $t If $q >= 0 Then $z = $p + __Sign($z, $p) $wr[$nn-1] = $x + $z $wr[$nn-1 - 1] = $wr[$nn-1] If $z Then $wr[$nn-1] = $x - $w / $z $wi[$nn-1] = 0 $wi[$nn-1 - 1] = 0 Else $wr[$nn-1] = $x + $p $wr[$nn-1 - 1] = $wr[$nn-1] $wi[$nn-1] = $z $wi[$nn-1 - 1] = -$z EndIf ;~ ConsoleWrite("; root1 = " & $wr[$nn-1] & "; root2 = " & $wr[$nn-1 - 1] & @CRLF) $nn -= 2 Else If $its = 30 Then Return SetError(2, 0, 0) If $its = 10 Or $its = 20 Then $t += $x For $i = 1 To $nn $A[$i-1][$i-1] -= $x Next $s = Abs($A[$nn-1][$nn-1 - 1]) + Abs($A[$nn-1 - 1][$nn-1 - 2]) $x = 0.75 * $s $y = $x $w = -0.4375 * $s^2 EndIf $its += 1 For $m = $nn - 2 To $l Step -1 $z = $A[$m-1][$m-1] $r = $x - $z $s = $y - $z $p = ($r * $s - $w) / $A[$m-1 + 1][$m-1] + $A[$m-1][$m-1 + 1] $q = $A[$m-1 + 1][$m-1 + 1] - $z - $r - $s $r = $A[$m-1 + 2][$m-1 + 1] $s = Abs($p) + Abs($q) + Abs($r) $p /= $s $q /= $s $r /= $s If $m = 1 Then ExitLoop $u = Abs($A[$m-1][$m-1 - 1]) * (Abs($q) + Abs($r)) $v = Abs($p) * (Abs($A[$m-1 - 1][$m-1 - 1]) + Abs($z) + Abs($A[$m-1 + 1][$m-1 + 1])) If $u + $v = $v Then ExitLoop Next For $i = $m + 2 To $nn $A[$i-1][$i-1 - 2] = 0 If $i <> $m + 2 Then $A[$i-1][$i-1 - 3] = 0 Next For $k = $m To $nn - 1 If $k <> $m Then $p = $A[$k-1][$k-1 - 1] $q = $A[$k-1 + 1][$k-1 - 1] $r = 0 If $k <> $nn - 1 Then $r = $A[$k-1 + 2][$k-1 - 1] $x = Abs($p) + Abs($q) + Abs($r) If $x <> 0 Then $p /= $x $q /= $x $r /= $x EndIf EndIf $s = __Sign(Sqrt($p^2 + $q^2 + $r^2), $p) If $s <> 0 Then If $k = $m Then If $l <> $m Then $A[$k-1][$k-1 - 1] = -$A[$k-1][$k-1 - 1] Else $A[$k-1][$k-1 - 1] = -$s * $x EndIf $p += $s $x = $p / $s $y = $q / $s $z = $r / $s $q /= $p $r /= $p For $j = $k To $nn $p = $A[$k-1][$j-1] + $q * $A[$k-1 + 1][$j-1] If $k <> $nn - 1 Then $p += $r * $A[$k-1 + 2][$j-1] $A[$k-1 + 2][$j-1] -= $p * $z EndIf $A[$k-1 + 1][$j-1] -= $p * $y $A[$k-1][$j-1] -= $p * $x Next If $nn < $k + 3 Then $mmin = $nn Else $mmin = $k + 3 EndIf For $i = $l To $mmin $p = $x * $A[$i-1][$k-1] + $y * $A[$i-1][$k-1 + 1] If $k <> $nn - 1 Then $p += $z * $A[$i-1][$k-1 + 2] $A[$i-1][$k-1 + 2] -= $p * $r EndIf $A[$i-1][$k-1 + 1] -= $p * $q $A[$i-1][$k-1] -= $p Next EndIf Next EndIf EndIf Until $l >= $nn - 1 WEnd ;~ Local $wri[$n][2] ;~ For $i = 0 to $n-1 ;~ $wri[$i][0] = $wr[$i] ;~ $wri[$i][1] = $wi[$i] ;~ Next ;~ Return $wri Return $wr EndFunc #EndRegion Internal Functions #Region Fundamental Constants Functions ; #FUNCTION# << Const >> =============================================================== ; Name ..........: _Const() ; Description ...: Returns a physical fundamental constant ; Syntax ........: _Const($x, $n) ; Parameters ....: $x = a number. ; $n = a number. ; Return values .: On Success: = Returns the physical fundamental constant. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: 'c', 'G', 'h', 'my0', 'e', 'me', 'mp', 'mu', 'NA', 'R', 'T0', 'g0', 'eps0', 'Z0', 'ke', 'alpha', 'hbar', 'Rinf', 'kB', 'F', 'mn', 'mmy', 'a0', 'myN', 'myB', 're', 'lambdaCe', 'lambdaCn', 'lambdaCp', 'p0', 'Vm', 'c1', 'c2', 'sigma', 'Phi0', 'G0', 'lP', 'mP', 'tP', 'qP', 'thetaP'. ; Example .......: Dim $u ; MsgBox(0x42040, "Fundamental Constants Functions", "_Const( 1, $u ) = " & _Const(1,$u) & " (" & $u & ")") ; ====================================================================================== Func _Const($nr, ByRef $uncert, $show=False) If Not IsNumber($nr) And Not IsString($nr) Then Return SetError(1, 0, 0) Local Const $c0 = 2.99792458e8, $G = 6.67384e-11, $h = 6.62606957e-34 Local Const $ec = 1.602176565e-19, $me = 9.10938291e-31, $mp = 1.672621777e-27, $mn = 1.674927351e-27, $mmy = 1.883531475e-28, $munit = 1.660538921e-27 Local Const $NA = 6.02214129e23, $R0 = 8.3144621e0, $my0 = (4*$pi)*1e-7 Local Const $T0 = 2.7315e+2, $g0 = 9.08665+0, $p0 = 1.01325e5 Local $constants[41] = ['c', 'G', 'h', 'my0', 'e', 'me', 'mp', 'mu', 'NA', 'R', 'T0', 'g0', 'eps0', 'Z0', 'ke', 'alpha', 'hbar', 'Rinf', 'kB', 'F', 'mn', 'mmy', 'a0', 'myN', 'myB', 're', 'lambdaCe', 'lambdaCn', 'lambdaCp', 'p0', 'Vm', 'c1', 'c2', 'sigma', 'Phi0', 'G0', 'lP', 'mP', 'tP', 'qP', 'thetaP'] If $show Then If IsNumber($nr) Then If $nr >= 1 And $nr <= UBound($constants) Then Return $constants[$nr-1] Else Return SetError(2, 0, 0) EndIf ElseIf IsString($nr) Then For $i=0 to 19 If $nr = $constants[$i] Then ExitLoop EndIf Next Return $i Else Return SetError(2, 0, 0) EndIf EndIf If IsString($nr) Then For $i = 0 to UBound($constants) - 1 If $nr = $constants[$i] Then ExitLoop Next $nr = $i + 1 EndIf Switch $nr Case 1 ;c: speed of light $uncert = 0 Return $c0 Case 2 ;G: Newtonian constant of gravitation $uncert = 8.0e-15 Return $G Case 3 ;h: Planck constant $uncert = 2.9e-41 Return $h Case 4 ;my0: magnetic constant $uncert = 0 Return $my0 Case 5 ;e: elementary charge $uncert = 3.5e-27 Return $ec Case 6 ;me: electron mass $uncert =4.0e-38 Return $me Case 7 ;mp: proton mass $uncert =7.4e-34 Return $mp Case 8 ;mu: unified atomic mass unit $uncert =7.3e-34 Return $munit Case 9 ;NA: Avogadro constant $uncert =2.7e16 Return $NA Case 10 ;R: molar gas constant $uncert =7.5e-7 Return $R0 Case 11 ;T0: absolute zero $uncert = 0 Return $T0 Case 12 ;g: standard acceleration of gravity $uncert = 0.5 Return $g0 Case 13 ;eps0: electric constant $uncert = 0 Return 1 / ($my0 * ($c0)^2) Case 14 ;Z0: impedance of vacuum $uncert = 0 Return $my0*$c0 Case 15 ;ke: Coulomb constant $uncert = 0 Return $c0^2*1e-7 Case 16 ;alpha: fine-structure constant $uncert = 2.4e-12 Return ($my0 * ($ec)^2 * $c0) / (2 * $h) Case 17 ;hbar: reduced Planck constant $uncert = 4.7e-42 Return $h / (2 * $pi) Case 18 ;Rinf: Rydberg constant $uncert = 5.5e-5 Return $me * $my0^2 * ($c0 / (2 * $h))^3 * $ec^4 Case 19 ;kB: Boltzmann constant $uncert = 1.3e-29 Return $R0 / $NA Case 20 ;F: Faraday constant $uncert = 2.1e-3 Return $NA * $ec Case 21 ;mn: neutron mass $uncert = 7.4e-34 Return $mn Case 22 ;mmy: muon mass $uncert = 9.6e-36 Return $mmy Case 23 ;a0: Bohr radius $uncert = 1.7e-20 Return $h^2 / ($pi * $me * $my0 * $c0^2 * $ec^2) Case 24 ;myN: nuclear magneton $uncert = 1.1e-34 Return ($ec * $h) / (4 * $pi * $mp) Case 25 ;myN: Bohr magneton $uncert = 2.0e-31 Return ($ec * $h) / (4 * $pi * $me) Case 26 ;re: electron radius $uncert = 2.7e-24 Return ($ec^2 * 1e-7) / $me Case 27 ;lambdaCe: eletron Compton wavelength $uncert = 1.6e-19 Return $h / ($me * $c0) Case 28 ;lambdaCn: neutron Compton wavelength $uncert = 1.1e-24 Return $h / ($mn * $c0) Case 29 ;lambdaCp: proton Compton wavelength $uncert = 9.4e-25 Return $h / ($mp * $c0) Case 30 ;p0: standard pressure $uncert = 0 Return $p0 Case 31 ;Vm: molar volume of ideal gas $uncert = 2.0e-8 Return ($R0 * $T0) / $p0 Case 32 ;c1: first radiation constant $uncert = 1.7e-31 Return 2 * $pi * $h * $c0^2 Case 33 ;c2: second radiation constant $uncert = 1.3e-8 Return ($h * $c0 * $NA) / ($R0) Case 34 ;sigma: Stefan–Boltzmann constant $uncert = 2.1e-13 Return (2 * $pi^5 * $R0^4) / (15 * $h^3 * $NA^4 * $c0^2) Case 35 ;Phi0: magnetic flux quantum $uncert = 4.6e-23 Return $h / (2 * $ec) Case 36 ;G0: conductance quantum $uncert = 2.5e-14 Return (2 * $ec^2) / $h Case 37 ;lP: Planck length $uncert = 9.7e-40 Return Sqrt(($h * $G) / (2 * $pi * $c0^3)) Case 38 ;mP: Planck mass $uncert = 1.3e-12 Return Sqrt(($h * $c0) / (2 * $pi * $G)) Case 39 ;tP: Planck time $uncert = 3.2e-48 Return Sqrt(($h * $G) / (2 * $pi * $c0^5)) Case 40 ;qP: Planck charge $uncert = 4.1e-26 Return Sqrt($h / (2 * $pi * 1e-7 * $c0)) Case 41 ;thetaP: Planck temperature $uncert = 8.5e27 Return Sqrt(($h * $c0^5) / (2 * $pi * $G)) * $NA / $R0 EndSwitch EndFunc #EndRegion Fundamental Constants Functions #Region Basic Functions ; #FUNCTION# << MathCheckDiv >> ======================================================== ; Name...........: _MathCheckDiv() ; Description ...: Checks to see if numberA is divisable by numberB ; Parameters ....: $i_NumA - Dividend ; $i_NumB - Divisor ; Return values .: On Success - 1 if not evenly divisable ; - 2 if evenly divisable ; On Failure - -1 and @error = 1 ; Author ........: Wes Wolfe-Wolvereness ; ====================================================================================== Func _MathCheckDiv($i_NumA, $i_NumB = 2) If Not Number($i_NumA) Or Not Number($i_NumB) Or Int($i_NumA) <> $i_NumA Or Int($i_NumB) <> $i_NumB Then Return SetError(1, 0, -1) If Int($i_NumA / $i_NumB) <> $i_NumA / $i_NumB Then Return 1 Else Return 2 EndIf EndFunc ;==>_MathCheckDiv ; #FUNCTION# << Degree >> ============================================================== ; Name...........: _Degree() ; Description ...: Converts radians to degrees. ; Syntax.........: _Degree( $x ) ; Parameters ....: $x = Radians to be converted into degrees. ; Return values .: On Success: = Returns the degrees converted from radians. ; On Failure: = Returns a blank string. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Erifash ; Remarks .......: Multiplies instead of dividing. ; Example .......: MsgBox(4096, "_Degree() Test", "_Degree( " & $pi & " ) = " & _Degree( $pi )) ; ====================================================================================== Func _Degree($x) If IsNumber($x) Then Return $x * $DEG Return SetError(1, 0, "") EndFunc ;==>_Degree ; #FUNCTION# << Radian >> ============================================================== ; Name...........: _Radian() ; Description ...: Converts degrees to radians. ; Syntax.........: _Radian( $x ) ; Parameters ....: $x = Degrees to be converted into radians. ; Return values .: On Success: = Returns the radians converted from degrees. ; On Failure: = Returns a blank string. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Erifash ; Remarks .......: In mathmatics and physics, the radian is a unit of ; angle measurement. One radian is approximately ; 57.2957795130823 degrees. To figure out that number, ; use the formula ( 180 / pi ). Because pi is used in ; the formula, the result is infinite in decimal places. ; The fact that AutoIt has a limited number of decimal ; places makes the result more innaccurate as you move ; down the decimal line. This is quite suitable for basic ; physics calculations though. ; Example .......: MsgBox(4096, "_Radian() Test", "_Radian( 90 ) = " & _Radian( 90 )) ; ====================================================================================== Func _Radian($x) If Number($x) Then Return $x * $RAD Return SetError(1, 0, "") EndFunc ;==>_Radian ; #FUNCTION# << Abs >> ================================================================= ; Name ..........: _Abs() ; Description ...: Returns the absolute value. ; Syntax ........: _Abs($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the absolute value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: Works with real and complex numbers. ; Example .......: MsgBox(0x42040, "Basic Functions", "_Abs( -2 ) = " & _Abs(-2) ) ; ====================================================================================== Func _Abs($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CAbs($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round(Abs($x), $Reps-1) EndFunc ; #FUNCTION# << Sgn >> ================================================================= ; Name ..........: _Sgn() ; Description ...: Returns the value of the signum function. ; Syntax ........: _Sgn($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the signum function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Basic Functions", "_Sgn( -2 ) = " & _Sgn(-2) ) ; ====================================================================================== Func _Sgn($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) If $x < 0 Then Return -1 ElseIf $x > 0 Then Return 1 Else Return 0 EndIf EndFunc ; #FUNCTION# << Pow >> ================================================================= ; Name ..........: _Pow() ; Description....: Returns the Exponatiation. ; Syntax ........: _Pow($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the Exponatiation value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $n is not a number. ; Author ........: Strahleman ; Remarks .......: Works with real and complex numbers. ; Example .......: MsgBox(0x42040, "Basic Functions", "_Pow( 10, 2 ) = " & _Pow( 10, 2) ) ; ====================================================================================== Func _Pow($x, $n) If Not IsNumber($x) Or Not IsNumber($n) Then If _CmplxCheck($x) And IsNumber($n) Then Return _CPow($x, $n) Else Return SetError(1, 0, 0) EndIf EndIf Return Round($x^$n, $Reps-1) EndFunc ; #FUNCTION# << Pow2 >> ================================================================ ; Name ..........: _Pow2() ; Description....: Exponent of next higher power of 2. ; Syntax ........: _Pow2($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the next Exponatiation of 2. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $n is not a number. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Basic Functions", "_Pow2( 5 ) = " & _Pow2(5) ) ; ====================================================================================== Func _Pow2($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) Return Ceiling(Log(Abs($x)) / Log(2)) EndFunc ; #FUNCTION# << Root >> ================================================================ ; Name ..........: _Root() ; Description ...: Returns the Root. ; Syntax ........: _Root($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the root value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $n is not a number. ; Author ........: Strahleman ; Remarks .......: Works with real and complex numbers. ; Example .......: MsgBox(0x42040, "Basic Functions", "_Root( 10, 2 ) = " & _Root( 10, 2) ) ; ====================================================================================== Func _Root($x, $n) If Not IsNumber($x) Or Not IsNumber($n) Then If _CmplxCheck($x) And IsNumber($n) Then Return _CRoot($x, $n) Else Return SetError(1, 0 ,0) EndIf EndIf Return Round($x^(1/$n), $Reps-1) EndFunc ; #FUNCTION# << Ln >> ================================================================== ; Name ..........: _Ln() ; Description ...: Returns the natural logarithm. ; Syntax ........: _Ln($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the natural logarithm function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: Works with real and complex numbers. ; Example .......: MsgBox(0x42040, "Basic Functions", "_Ln( e ) = " & _Ln($e) ) ; ====================================================================================== Func _Ln($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CLn($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round(Log($x), $Reps-1) EndFunc ; #FUNCTION# << Ln1p >> ================================================================ ; Name ..........: _Ln1p() ; Description ...: Compute log(1+x) accurately for small values of x. ; Syntax ........: _Ln1p($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the natural logarithm function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: Works with real and complex numbers. ; Example .......: MsgBox(0x42040, "Basic Functions", "_Ln1p( 1 / e ) = " & _Ln1p(($e^-1)) ) ; ====================================================================================== Func _Ln1p($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) If $x < 0 Then Return SetError(2, 0, 0) If $x < 1 Then Return __ln1p($x) Return Round(Log($x + 1), $Reps-1) EndFunc ; #FUNCTION# << Log >> ================================================================= ; Name ..........: _Log() ; Description ...: Returns the logarithm to a base. ; Syntax ........: _Log($x[, $b]) ; Parameters ....: $x = a number. ; $b = an integer. [optional] ; Return values .: On Success: = Returns the b-base logarithm function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $b is not a number. ; Author ........: Strahleman ; Remarks .......: Works with real and complex numbers. ; Example .......: MsgBox(0x42040, "Basic Functions", "_Log( 10 ) = " & _Log(10) ) ; ====================================================================================== Func _Log($x, $b=10) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CDiv(_CLn($x),_Cmplx(Log($b),0)) Else Return SetError(1, 0, 0) EndIf EndIf Return Round(Log($x)/Log($b), $Reps-1) EndFunc ; #FUNCTION# << GCD >> ================================================================= ; Name ..........: _GCD() ; Description ...: Returns the greatest common divisor. ; Syntax ........: _GCD($a, $b) ; Parameters ....: $a = a number. ; $b = a number. ; Return values .: On Success: = Returns the value of the greatest common divisor. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; Author ........: Strahleman ; Remarks .......: Using Euclid's algorithm. ; Example .......: MsgBox(0x42040, "Basic Functions", "_GCD( 2458, 9874 ) = " & _GCD(2458, 9874) ) ; ====================================================================================== Func _GCD($a, $b) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) Local $r = 0 If $a < $b Then __Swap($a, $b) While Mod($a, $b) <> 0 $r = Mod($a, $b) $a = $b $b = $r WEnd Return $b EndFunc ; #FUNCTION# << LCM >> ================================================================= ; Name ..........: _LCM() ; Description ...: Returns the least common multiple. ; Syntax ........: _LCM($x) ; Parameters ....: $a = a number. ; $b = a number. ; Return values .: On Success: = Returns the value of the least common multiple. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; Author ........: Strahleman ; Remarks .......: Using Euclid's algorithm. ; Example .......: MsgBox(0x42040, "Basic Functions", "_LCM( 2458, 9874 ) = " & _LCM(2458, 9874) ) ; ====================================================================================== Func _LCM($a, $b) If Not IsNumber($a) or Not IsNumber($b) Then Return SetError(1, 0, 0) Return Abs($a * $b) / _GCD($a, $b) EndFunc ; #FUNCTION# << Rat >> ================================================================= ; Name ..........: _Rat() ; Description ...: Returns the approximate fraction of a rational number. ; Syntax ........: _Rat($x [, $p]) ; Parameters ....: $x = a number ; $p = an integer (presicion) [optional] ; Return values .: On Success: = Returns the approximate fraction. ; On Failure: = Sets @Error and returns 0, check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $p is not a number. ; Author ........: Strahleman ; Remarks .......: Using continued fraction expansion. ; Example .......: Dim $C = _Rat($pi, 3) ; MsgBox(0x42040, "Basic Functions", "_Rat( " & $pi &", 3 ) = " & $C[0] & "/" & $C[1] ) ; ====================================================================================== Func _Rat($x, $p=-1) If not IsNumber($x) or not IsNumber($p) Then Return SetError(1, 0, 0) $p = Int($p) Local $B = _ContFrac($x, $p) Local $l = UBound($B) Local $Z[$l+1] Local $N[$l+1] Local $R[2] $Z[0]=1 $Z[1]=0 $N[0]=0 $N[1]=1 For $m=2 to $l $Z[$m]=$B[$m-1]*$Z[$m-1]+$Z[$m-2] $N[$m]=$B[$m-1]*$N[$m-1]+$N[$m-2] Next $R[0]=$Z[$l]+($B[0]*$N[$l]) $R[1]=$N[$l] Local $Y[Ubound($Z)][2] For $i = 0 to Ubound($Z)-1 $Y[$i][0] = $Z[$i] $Y[$i][1] = $N[$i] Next Return $R EndFunc ; #FUNCTION# << ContFrac >> ============================================================ ; Name ..........: _ContFrac() ; Description ...: Returns the continued fraction of a rational number. ; Syntax ........: _ContFrac($x [, $p]) ; Parameters ....: $x = a number ; $p = an integer (presicion) [optional] ; Return values .: On Success: = Returns the continued fraction expansion. ; On Failure: = Sets @Error and returns 0, check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $p is not a number. ; Author ........: Strahleman ; Remarks .......: the calculation is determined by an iterative process. ; Example .......: $C = _ContFrac(pi, 3) ; _ArrayDisplay($C, "Basic Functions") ;requires Array.au3 ; ====================================================================================== Func _ContFrac($x, $p=-1) If Not IsNumber($x) Or Not IsNumber($p) Then Return SetError(1, 0, 0) Local $a = $x Local $k = 0 Local $B[1] $B[0] = 0 if $p = -1 Then $p = 15 While ((1/$a*10^-10) > $meps And $k <= $p ) $k = $k + 1 ReDim $B[$k] $B[$k - 1] = Int($a) $a = (Int(1/($a - $B[$k - 1]) * 10^14)) / 10^14 WEnd Return $B EndFunc ; #FUNCTION# << Primes >> ============================================================== ; Name ..........: _Primes() ; Description ...: Returns all the prime numbers up to the specified value. ; Syntax ........: _Primes($n) ; Parameters ....: $n = an integer ; Return values .: On Success: = Returns an array within the prime numbers. ; On Failure: = Sets @Error and returns 0, check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n is not a number. ; 2 = $n is a negative or non-integer value . ; Author ........: Strahleman ; Remarks .......: Uses the Sieve of Eratosthenes. ; Example .......: Dim $C = _Primes(1000) ; _ArrayDisplay($C, "Basic Functions") ; ====================================================================================== Func _Primes($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 And $n <> Floor($n) Then Return SetError(2, 0, 0) Local $i = 0, $j = 0 $n = $n + 1 If ($n > 100000) Then Local $lenm = Floor(($n + 1) / 6); Local $lenp = Floor(($n - 1) / 6); Local $sievem[$lenm + 1] Local $sievep[$lenp + 1] For $i = 1 To $lenm If $i <= $lenm Then $sievem[$i] = 1 If $i <= $lenp Then $sievep[$i] = 1 Next ;$i For $i = 1 To Int((Sqrt($n) + 1) / 6) If ($sievem[$i]) Then For $j = 7 * $i - 1 To $lenm Step 6 * $i - 1 $sievem[$j] = 0 Next ;$j For $j = 5 * $i - 1 To $lenp Step 6 * $i - 1 $sievep[$j] = 0 Next ;$j EndIf If ($sievep[$i]) Then For $j = 7*$i+1 To $lenp Step 6*$i+1 $sievep[$j] = 0 Next ;$j For $j = 5*$i+1 To $lenm Step 6*$i+1 $sievem[$j] = 0 Next ;$j EndIf Next ;$i Local $M[UBound($sievem)] Local $pos = 0 For $i = 0 To UBound($sievem) - 1 If $sievem[$i] = 1 Then $M[$pos] = $i $pos += 1 EndIf Next ;$i ReDim $M[$pos] Local $R[UBound($M)] For $i = 0 to UBound($M)-1 $R[$i] = 6*$M[$i]-1 Next ;$i Redim $M[1] $M[0] = 0 Local $M[UBound($sievep)] $pos = 0 For $i = 0 To UBound($sievep) - 1 If $sievep[$i] = 1 Then $M[$pos] = $i $pos += 1 EndIf Next ;$i ReDim $M[$pos] Local $boundR = UBound($R) ReDim $R[$boundR + UBound($M)] For $i = 0 to UBound($M) - 1 $R[$i+$boundR] = 6 * $M[$i] + 1 Next ;$i Local $bound = UBound($R) - 1 __Sort($R, 0, $bound) ReDim $R[UBound($R) + 1] For $i = UBound($R) - 1 To 2 step -1 $R[$i] = $R[$i - 1] Next $R[0] = 2 $R[1] = 3 Return $R ElseIf ($n > 352) Then Local $len = Floor(($n - 1) / 2); Local $sieve[$len + 1] For $i = 1 To $len $sieve[$i] = 1 Next ;$i For $i = 1 to (sqrt($n)-1)/2 If ($sieve[$i]) Then For $j = 3*$i+1 To $len Step 2*$i+1 $sieve[$j] = 0 Next ;$j EndIf Next ;$i Local $M[UBound($sieve)] Local $pos = 0 For $i = 0 To UBound($sieve) - 1 If $sieve[$i] = 1 Then $M[$pos] = $i $pos += 1 EndIf Next ;$i ReDim $M[$pos] Local $R[UBound($M)] For $i = 0 to UBound($M)-1 $R[$i] = 2*$M[$i]+1 Next ;$i ReDim $R[UBound($R) + 1] For $i = UBound($R) - 1 To 1 step -1 $R[$i] = $R[$i - 1] Next ;$i $R[0] = 2 __Sort($R, 0, UBound($R) - 1) Return $R ElseIf $n <= 2 Then Dim $R[1] $R[0] = "" Else Local $R[70] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, _ 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, _ 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, _ 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, _ 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, _ 293, 307, 311, 313, 317, 331, 337, 347, 349 ] ReDim $R[UBound($R) + 1] $R[UBound($R)-1] = $n __Sort($R, 0, UBound($R) - 1) Local $pos = 0 For $i = 0 To UBound($R) - 1 If $R[$i] = $n Then $pos = $i Next ;$i ReDim $R[$pos] Return $R EndIf EndFunc ; #FUNCTION# << IsPrime >> ============================================================= ; Name ..........: _IsPrime() ; Description ...: Checks if the entered value is a prime number. ; Syntax ........: _IsPrime($n) ; Parameters ....: $n = an integer ; Return values .: On Success: = Returns True or False. ; On Failure: = Sets @Error and returns 0, check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n is not a number. ; 2 = $n is a negative or non-integer value . ; Author ........: Strahleman ; Remarks .......: using the Sieve of Eratosthenes. ; Example .......: Dim $C = _IsPrime(353) ; MsgBox(0x42000, "Basic Functions", "_IsPrime(" & 353 &") = " & $C ) ; ====================================================================================== Func _IsPrime($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 And $n <> Floor($n) Then Return SetError(2, 0, 0) Local $aPrimes If $n <= 352 Then $aPrimes = _Primes($n) If $aPrimes[UBound($aPrimes)-1] <> $n Then Return False Else $aPrimes = _Primes(Int(Sqrt($n))) Local $t = 0 For $i = 0 To UBound($aPrimes)-1 $t = $n / $aPrimes[$i] If $t = Int($t) Then Return False Next EndIf Return True EndFunc ; #FUNCTION# << Factor >> ============================================================== ; Name ..........: _Factor() ; Description ...: Returns the prime factors of an entered value. ; Syntax ........: _Factor($n) ; Parameters ....: $n = an integer ; Return values .: On Success: = Returns an array within the prime factors. ; On Failure: = Sets @Error and returns 0, check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n is not a number. ; 2 = $n is a negative or non-integer value . ; Author ........: Strahleman ; Remarks .......: using the Sieve of Eratosthenes. ; Example .......: Dim $C = _Factor(40) ; _ArrayDisplay($C, "Basic Functions") ; ====================================================================================== Func _Factor($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 And $n <> Floor($n) Then Return SetError(2, 0, 0) Local $X[1][2] If $n < 4 Then $X[0][0]=$n $X[0][1]=1 Return $X EndIf Local $p = _Primes(Int(Sqrt($n))) Local $i = 0 Local $l = 0 Local $k = 0 For $i = 0 To UBound($p)-1 $k = 0 If $n = 1 Then ReDim $X[$l][2] ExitLoop ElseIf _IsPrime($n) Then ReDim $X[$l+1][2] $X[$l][0]=$n $X[$l][1]=1 ExitLoop Else If mod($n, $p[$i]) = 0 Then ReDim $X[$l+1][2] While mod($n, $p[$i]) = 0 $n = $n/$p[$i] $k = $k + 1 WEnd $X[$l][0] = $p[$i] $X[$l][1] = $k $l = $l+1 EndIf EndIf Next Return $X EndFunc ; #FUNCTION# << Fac >> ================================================================= ; Name ..........: _Fac() ; Description ...: Returns the Faculty. ; Syntax ........: _Fac($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the faculty. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; 2 = $x is not an integer. ; Author ........: Strahleman ; Remarks .......: Using gamma. ; Example .......: MsgBox(0x42040, "Basic Functions", "_Fac( 6 ) = " & _Fac( 6 ) ) ; ====================================================================================== Func _Fac($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) If $x <> Floor($x) Then Return SetError(2, 0, 0) If $x > 100 Then Return Round(Exp(__gammaln($x + 1)), 0) Else Return Round(__gamma($x + 1), 0) EndIf EndFunc ; #FUNCTION# << nCr >> ================================================================= ; Name ..........: _nCr() ; Description ...: Returns the variation. ; Syntax ........: _nCr($n, $k) ; Parameters ....: $n = an integer. ; $k = an integer. ; Return values .: On Success: = Returns the combination. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; 2 = $x is not an integer. ; Author ........: Strahleman ; Remarks .......: Using gamma. ; Example .......: MsgBox(0x42040, "Basic Functions", "_nCr( 49, 6 ) = " & _nCr( 49, 6 ) ) ; ====================================================================================== Func _nCr($n, $k) If Not IsNumber($n) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $k < 0 Or $n < $k Then Return SetError(2, 0, 0) If $n > 100 Then Return Round(Exp(__gammaln($n + 1) - (__gammaln($k + 1) + __gammaln($n - $k + 1))), 0) Else Return Round(__gamma($n + 1) / (__gamma($k + 1) * __gamma($n - $k + 1)), 0) EndIf EndFunc ; #FUNCTION# << nCr2 >> ================================================================ ; Name ..........: _nCr2() ; Description ...: Returns the Combination. ; Syntax ........: _nCr2($n, $k) ; Parameters ....: $n = an integer. ; $k = an integer. ; Return values .: On Success: = Returns the combination. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; 2 = $x is not an integer. ; Author ........: Strahleman ; Remarks .......: Using gamma. ; Example .......: MsgBox(0x42040, "Basic Functions", "_nCr2( 49, 6 ) = " & _nCr2( 49, 6 ) ) ; ====================================================================================== Func _nCr2($n, $k) If Not IsNumber($n) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $k < 0 Or $n < $k Then Return SetError(2, 0, 0) If $n > 100 Then Return Round(Exp(__gammaln($n + $k) - (__gammaln($n) + __gammaln($k + 1))), 0) Else Return Round(__gamma($n + $k) / (__gamma($n) * __gamma($k + 1)), 0) EndIf EndFunc ; #FUNCTION# << nPr >> ================================================================= ; Name ..........: _nPr() ; Description ...: Returns the Perlmutation. ; Syntax ........: _nPr($n, $k) ; Parameters ....: $n = an integer. ; $k = an integer. ; Return values .: On Success: = Returns the perlmutation ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; 2 = $x is not an integer. ; Author ........: Strahleman ; Remarks .......: Using gamma. ; Example .......: MsgBox(0x42040, "Basic Functions", "_nPr( 49, 6 ) = " & _nPr( 49, 6 ) ) ; ====================================================================================== Func _nPr($n, $k) If Not IsNumber($n) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $k < 0 Or $n < $k Then Return SetError(2, 0, 0) If $n > 100 Then Return Round(Exp(__gammaln($n + 1) - (__gammaln($n - $k + 1))), 0) Else Return Round(__gamma($n + 1) / __gamma($n - $k + 1), 0) EndIf EndFunc ; #FUNCTION# << DigSum >> ============================================================== ; Name ..........: _DigSum() ; Description ...: Returns the digit sum. ; Syntax ........: _DigSum($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the digit sum. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Basic Functions", "_DigSum( 36036 ) = " & _DigSum(36036) ) ; ====================================================================================== Func _DigSum($x, $it = False) If Not IsNumber($x) Then Return SetError(1, 0, 0) Local $l = StringLen($x) If $it Then While 1 $l = StringLen($x) If $l = 1 Then ExitLoop $x = __digsum($x) WEnd Else $x = __digsum($x) EndIf Return $x EndFunc #EndRegion Basic Functions #Region Special Functions ; #FUNCTION# << Gamma >> =============================================================== ; Name ..........: _Gamma() ; Description ...: Returns the value of the gamma function. ; Syntax ........: _Gamma($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the gamma function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: The gamma function value of an increment integer returns the factorial of the value. ; Example .......: MsgBox(0x42040, "Special Functions", "_Gamma( 6 ) = " & _Gamma(6) ) ; ====================================================================================== Func _Gamma($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) Return Round(Exp(__gammaln($x)), $Reps-1) EndFunc ; #FUNCTION# << GammaLn >> ============================================================= ; Name ..........: _Gammaln() ; Description ...: Returns the value of the logarithm of the gamma function. ; Syntax ........: _Gammaln($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the logarithm of the gamma function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: the logarithm of the gamma funtion returns a better result for higher values. ; Example .......: MsgBox(0x42040, "Special Functions", "Exp( _Gammaln( 100 ) ) = " & Exp(_Gammaln(100)) ) ; ====================================================================================== Func _Gammaln($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) Return Round(__gammaln($x), $Reps-1) EndFunc ; #FUNCTION# << Erf >> ================================================================= ; Name ..........: _Erf() ; Description ...: Returns the Error function. ; Syntax ........: _Erf($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the error function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: Gaussian Error function ; Example .......: MsgBox(0x42040, "Special Functions", "_Erf( 0 ) = " & _Erf(0) ) ; ====================================================================================== Func _Erf($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) Return Round(__erfcore($x), $Reps-1) EndFunc ; #FUNCTION# << Beta >> ================================================================ ; Name ..........: _Beta() ; Description ...: Returns the value of the beta function. ; Syntax ........: _Beta($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the beta function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: Beta(a,b) = (Gamma(a) * Gamma(b)) / Gamma(a+b) ; Example .......: MsgBox(0x42040, "Special Functions", "_Beta( 1, 1 ) = " & _Beta(1,1) ) ; ====================================================================================== Func _Beta($a, $b) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) Return Round(__beta($a, $b), $Reps-1) EndFunc ; #FUNCTION# << BetaLn >> ============================================================== ; Name ..........: _Betaln() ; Description ...: Returns the value of the logarithm of the beta function. ; Syntax ........: _Betaln($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the logarithm of the beta function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: Beta(a,b) = (Gamma(a) * Gamma(b)) / Gamma(a+b) ; Example .......: MsgBox(0x42040, "Special Functions", "Exp( _Betaln( 1, 1 ) ) = " & Exp(_Betaln(1,1)) ) ; ====================================================================================== Func _Betaln($a, $b) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) Return Round(__betaln($a, $b), $Reps-1) EndFunc ; #FUNCTION# << BetaInc >> ============================================================= ; Name ..........: _Betainc() ; Description ...: Returns the value of the regularized incomplete beta function. ; Syntax ........: _Betainc($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the regularized incomplete beta function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: Betainc(x,a,b) = BetaI(x,a,b)/Beta(a,b) {BetaI = incomplete beta function} ; Example .......: MsgBox(0x42040, "Special Functions", "_Betainc( 3 ) = " & _Betainc(3) ) ; ====================================================================================== Func _Betainc($x, $a, $b) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0 ,0) Return Round(__betainc($x, $a, $b), $Reps-1) EndFunc ; #FUNCTION# << Digamma >> ============================================================= ; Name ..........: _Digamma() ; Description ...: Returns the value of the digamma function. ; Syntax ........: _Digamma($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the digamma function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: The digamma function of 1 returns the Euler-Mascheroni Constant. ; Example .......: MsgBox(0x42040, "Special Functions", "_Digamma( 6 ) = " & _Digamma(6) ) ; ====================================================================================== Func _Digamma($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) Return Round(__digamma($x), $Reps-1) EndFunc ; #FUNCTION# << Harmnr >> ============================================================== ; Name ..........: _Harmnr() ; Description ...: Returns the harmonic number. ; Syntax ........: _Harmnr($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the harmonic number. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: The harmonic number is the sum of the reciprocals of the first x natural numbers. ; Example .......: MsgBox(0x42040, "Special Functions", "_Harmnr( 3 ) = " & _Harmnr(3) ) ; ====================================================================================== Func _Harmnr($x) If Not IsNumber($x) Then Return SetError(1, 0, 0) Return Round(__harmnr($x), $Reps-1) EndFunc #EndRegion Special Functions #Region Complex Number Functions ; #FUNCTION# << CmplxCheck >> ========================================================== ; Name ..........: _CmplxCheck() ; Description ...: Check of a complex number type. ; Syntax ........: _CmplxCheck($cmp) ; Parameters ....: $cmp = a complex number. ; Return values .: On Success: = Returns TRUE if $cmp is a complex number, else FALSE ; On Failure: = Sets @Error and returns FALSE. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a complex number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Complex Number Check", "_CmplxCheck(_Cmplx( 3, 4 )) = " & _CmplxCheck(_Cmplx( 3, 4 ))) ; ====================================================================================== Func _CmplxCheck($cmp) Local $cmpSize = DllStructGetSize($cmp) If @error Then Return SetError(1, 0, False) Local $cmpReal = DllStructGetData($cmp, "Real") If @error Then Return SetError(1, 0, False) Local $cmpImag = DllStructGetData($cmp, "Imag") If @error Then Return SetError(1, 0, False) If Not( $cmpSize = 16 And IsNumber($cmpReal) And IsNumber($cmpImag) ) Then Return SetError(1, 0, False) Return True EndFunc ; #FUNCTION# << CCheck >> ============================================================== ; Name ..........: _CCheck() ; Description ...: Check routine of a complex number type. ; Syntax ........: _CCheck($cmp) ; Parameters ....: $cmp = a complex number. ; Return values .: On Success: = Returns TRUE if $cmp is a complex number, else FALSE ; On Failure: = Sets @Error and returns FALSE. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a complex number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Complex Number Check", "_CmplxCheck(_Cmplx( 3, 4 )) = " & _CmplxCheck(_Cmplx( 3, 4 ))) ; ====================================================================================== Func _CCheck(ByRef $cmp) If _CmplxCheck($cmp) Then Return True If IsNumber($cmp) Then $cmp = _Cmplx($cmp) Return True EndIf Return SetError(1, 0, False) EndFunc ; #FUNCTION# << CmplxToStr >> ========================================================== ; Name ..........: _CmplxToStr() ; Description ...: Returns the string represantation of a complex number. ; Syntax ........: _CmplxToStr($cmp [, $imagUnit]) ; Parameters ....: $cmp = complex number. ; Return values .: On Success: = Returns a string of the complex number. ; On Failure: = Sets @Error and returns "", which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the imaginary part of a Complex Number", "_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )) = " & _CReal(_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 ))) & "+" & _CImag(_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )))) ; ====================================================================================== Func _CmplxToStr($cmp, $imagUnit="i") If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $strC = "" Local $real = DllStructGetData($cmp,"Real") Local $imag = DllStructGetData($cmp,"Imag") Select Case $imag < 0 If $imag = -1 Then $strC = "-" & $imagUnit Else $strC = "-" & String(Abs($imag)) & $imagUnit EndIf Case $imag = 0 $strC = "" Case $imag > 0 If $imag = 1 Then $strC = "+" & $imagUnit Else $strC = "+" & String(Abs($imag)) & $imagUnit EndIf EndSelect Select Case $real < 0 $strC = "-" & String(Abs($real)) & $strC Case $real = 0 If $imag = 1 Then $strC = $imagUnit ElseIf $imag = -1 Then $strC = "-" & $imagUnit elseif $imag = 0 Then $strC = 0 Else $strC = String($imag) & $imagUnit EndIf Case $real > 0 $strC = String(Abs($real)) & $strC EndSelect Return $strC EndFunc ; #FUNCTION# << Cmplx >> =============================================================== ; Name ..........: _Cmplx() ; Description ...: Returns a complex number. ; Syntax ........: _Cmplx([$real[, $imag[, $pol]]]) ; Parameters ....: $real = the real part of a complex number. ; $imag = the imaginary part of a complex number. ; $pol = True: the polar coordinates are used. ; = False: the cartesian coordinates are used. (Standard) ; Return values .: On Success: = Returns the complex number as a complex number type. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $real or $imag is not a number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Create a Complex Number", "_Cmplx( 3, 4 ) = " & DllStructGetPtr(_Cmplx( 3, 4 ))) ; ====================================================================================== Func _Cmplx($real=0, $imag=0, $pol=False) If Not IsNumber($real) or Not IsNumber($imag) Then Return SetError(1, 0, 0) Local $cmp = DllStructCreate("double Real;double Imag") If Not $pol Then DllStructSetData($cmp, "Real", $real) DllStructSetData($cmp, "Imag", $imag) Else DllStructSetData($cmp, "Real", $real * Cos($imag)) DllStructSetData($cmp, "Imag", $real * Sin($imag)) EndIf Return $cmp EndFunc ; #FUNCTION# << CAbs >> ================================================================ ; Name ..........: _CAbs() ; Description ...: Returns the absolute value of a complex number. ; Syntax ........: _CAbs($cmp) ; Parameters ....: $cmp = a complex number. ; Return values .: On Success: = Returns the absolute value of the complex number. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the absolute value of a Complex Number", "_CAbs(_Cmplx( 3, 4 )) = " & _CAbs(_Cmplx( 3, 4 ))) ; ====================================================================================== Func _CAbs($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Return Sqrt(DllStructGetData($cmp, "Real")^2 + DllStructGetData($cmp, "Imag")^2) EndFunc ; #FUNCTION# << CArg >> ================================================================ ; Name ..........: _CArg() ; Description ...: Returns the argument of a complex number. ; Syntax ........: _CArg($cmp) ; Parameters ....: $cmp = a complex number. ; Return values .: On Success: = Returns the argument of the complex number in the Intervall of I = (-PI and PI]. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; 2 = the argument is indeterminate, because the real and imaginary values are 0. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the argument of a Complex Number", "_CArg(_Cmplx( 3, 4 )) = " & _CArg(_Cmplx( 3, 4 ))) ; ====================================================================================== Func _CArg($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $real = DllStructGetData($cmp, "Real") Local $imag = DllStructGetData($cmp, "Imag") Select Case $real > 0 Return ATan($imag / $real) Case $real < 0 If $imag >= 0 Then Return ATan($imag / $real) + $pi Else Return ATan($imag / $real) - $pi EndIf Case $real = 0 If $imag > 0 Then Return $pi / 2 ElseIf $imag < 0 Then Return $pi / -2 ElseIf $imag = 0 Then Return SetError(2, 0 ,0) EndIf EndSelect Return SetError(1, 0 ,0) EndFunc ; #FUNCTION# << CReal >> =============================================================== ; Name ..........: _CReal() ; Description ...: Returns the real part of a complex number. ; Syntax ........: _CReal($cmp) ; Parameters ....: $cmp = a complex number. ; Return values .: On Success: = Returns the real part of the complex. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the real part of a Complex Number", "_CReal(_Cmplx( 3, 4 )) = " & _CReal(_Cmplx( 3, 4 ))) ; ====================================================================================== Func _CReal($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Return DllStructGetData($cmp, "Real") EndFunc ; #FUNCTION# << CImag >> =============================================================== ; Name ..........: _CImag() ; Description ...: Returns the imaginary part of a complex number. ; Syntax ........: _CImag($cmp) ; Parameters ....: $cmp = a complex number. ; Return values .: On Success: = Returns the imaginary part of the complex. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the imaginary part of a Complex Number", "_CImag(_Cmplx( 3, 4 )) = " & _CImag(_Cmplx( 3, 4 ))) ; ====================================================================================== Func _CImag($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Return DllStructGetData($cmp, "Imag") EndFunc ; #FUNCTION# << CCon >> ================================================================ ; Name ..........: _CCon() ; Description ...: Returns the complex conjugate of a complex number. ; Syntax ........: _CCon($cmp) ; Parameters ....: $cmp = a complex number. ; Return values .: On Success: = Returns the complex conjugate. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the imaginary part of a Complex Number", "_CCon(_Cmplx( 3, 4 )) = " & _CmplxToStr(_CCon(_Cmplx( 3, 4 )))) ; ====================================================================================== Func _CCon($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Return _Cmplx(DllStructGetData($cmp, "Real"), -1 * DllStructGetData($cmp, "Imag")) EndFunc ; #FUNCTION# << CAdd >> ================================================================ ; Name ..........: _CAdd() ; Description ...: Returns the sum of two complex numbers. ; Syntax ........: _CAdd($cmp1, $cmp2) ; Parameters ....: $cmp1, $cmp2 = complex numbers. ; Return values .: On Success: = Returns the complex number of the addition. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp1 or $cmp2 is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the imaginary part of a Complex Number", "_CAdd(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )) = " & _CAdd(_Cmplx( 0, 1 ),_Cmplx( 1, 0 ))) ; ====================================================================================== Func _CAdd($cmp1, $cmp2) If Not _CCheck($cmp1) Or Not _CCheck($cmp2) Then Return SetError(1, 0, 0) Local $real = Round(DllStructGetData($cmp1,"Real") + DllStructGetData($cmp2,"Real") , $Reps - 1 ) Local $imag = Round(DllStructGetData($cmp1,"Imag") + DllStructGetData($cmp2,"Imag") , $Reps - 1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CSub >> ================================================================ ; Name ..........: _CSub() ; Description ...: Returns the difference of two complex numbers. ; Syntax ........: _CSub($cmp1, $cmp2) ; Parameters ....: $cmp1, $cmp2 = complex numbers. ; Return values .: On Success: = Returns the complex number of the subtraction ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp1 or $cmp2 is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the imaginary part of a Complex Number", "_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )) = " & _CReal(_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 ))) & "+" & _CImag(_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )))) ; ====================================================================================== Func _CSub($cmp1, $cmp2) If Not _CCheck($cmp1) Or Not _CCheck($cmp2) Then Return SetError(1, 0, 0) Local $real = Round(DllStructGetData($cmp1,"Real") - DllStructGetData($cmp2,"Real") , $Reps - 1 ) Local $imag = Round(DllStructGetData($cmp1,"Imag") - DllStructGetData($cmp2,"Imag") , $Reps - 1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CMul >> ================================================================ ; Name ..........: _CMul() ; Description ...: Returns the product of two complex numbers. ; Syntax ........: _CMul($cmp1, $cmp2) ; Parameters ....: $cmp = complex number. ; Return values .: On Success: = Returns the multiplication of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp1 or $cmp2 is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the product of two Complex Numbers", "_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )) = " & _CReal(_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 ))) & "+" & _CImag(_CSub(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )))) ; ====================================================================================== Func _CMul($cmp1, $cmp2) If Not _CCheck($cmp1) Or Not _CCheck($cmp2) Then Return SetError(1, 0, 0) Local $real = Round((DllStructGetData($cmp1,"Real")*DllStructGetData($cmp2,"Real"))-(DllStructGetData($cmp1,"Imag")*DllStructGetData($cmp2, "Imag")),$Reps-1) Local $imag = Round((DllStructGetData($cmp1,"Real")*DllStructGetData($cmp2,"Imag"))+(DllStructGetData($cmp1,"Imag")*DllStructGetData($cmp2, "Real")),$Reps-1) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CDiv >> ================================================================ ; Name ..........: _CDiv() ; Description ...: Returns the quotient of two complex numbers. ; Syntax ........: _CDiv($cmp1, $cmp2) ; Parameters ....: $cmp = complex number. ; Return values .: On Success: = Returns the division of complex numbers. ; On Failure: = Sets @Error and returns "", which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp1 or $cmp2 is not a (complex) number. ; 2 = Division by zero ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: MsgBox(0x42040, "Get the product of two Complex Numbers", "_CDiv(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )) = " & _CReal(_CDiv(_Cmplx( 0, 1 ),_Cmplx( 1, 0 ))) & "+" & _CImag(_CDiv(_Cmplx( 0, 1 ),_Cmplx( 1, 0 )))) ; ====================================================================================== Func _CDiv($cmp1, $cmp2) If Not _CCheck($cmp1) Or Not _CCheck($cmp2) Then Return SetError(1, 0, 0) If DllStructGetData($cmp2, "Real") = 0 and DllStructGetData($cmp2, "Imag") = 0 Then Return SetError(2, 0 ,0) Local $real = Round(1/(DllStructGetData($cmp2,"Real")^2 + DllStructGetData($cmp2,"Imag")^2) * ((DllStructGetData($cmp1,"Real")*DllStructGetData($cmp2, "Real"))+(DllStructGetData($cmp1,"Imag")*DllStructGetData($cmp2, "Imag"))),$Reps-1) Local $imag = Round(1/(DllStructGetData($cmp2,"Real")^2 + DllStructGetData($cmp2,"Imag")^2) * ((DllStructGetData($cmp2,"Real")*DllStructGetData($cmp1, "Imag"))-(DllStructGetData($cmp1,"Real")*DllStructGetData($cmp2, "Imag"))),$Reps-1) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CPow >> ================================================================ ; Name ..........: _CPow() ; Description ...: Returns the power of a complex number to an integer exponent. ; Syntax ........: _CPow($cmp, $n) ; Parameters ....: $cmp = complex number ; $n = integer number ; Return values .: On Success: = Returns the power of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp not a (complex) number or $n is not a number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CPow($cmp, $n) If Not _CCheck($cmp) Or Not IsNumber($n) Then Return SetError(1, 0, 0) Local $real = Round(_CAbs($cmp)^$n * Cos(_CArg($cmp) * $n),$Reps-1) Local $imag = Round(_CAbs($cmp)^$n * Sin(_CArg($cmp) * $n),$Reps-1) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CRoot >> =============================================================== ; Name ..........: _CRoot() ; Description ...: Returns the root of a complex number to an real exponent. ; Syntax ........: _CRoot($cmp, $n) ; Parameters ....: $cmp = complex number ; $n = float number, degree of complex root ; $m = float number, count value of the complex root ; Return values .: On Success: = Returns the root of complex number. ; On Failure: = Sets @Error, check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; 2 = the count value is greater or equal than the root number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CRoot($cmp, $n, $m=0) If Not _CCheck($cmp) Or Not IsNumber($n) Or Not IsNumber($m) Then Return SetError(1, 0, 0) Local $R = _Rat($n) If $m >= $R[0] Then SetError(2) $m = Mod($m, $R[0]) Local $real = Round(_CAbs($cmp)^(1/$n) * Cos((_CArg($cmp)+(2*$m*$pi))/$n),$Reps-1) Local $imag = Round(_CAbs($cmp)^(1/$n) * Sin((_CArg($cmp)+(2*$m*$pi))/$n),$Reps-1) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CLn >> ================================================================= ; Name ..........: _CLn() ; Description ...: Returns the natural logarithm of a complex number. ; Syntax ........: _CLn($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the natural logarithm of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CLn($cmp) If Not _CCheck($cmp)Then Return SetError(1, 0, 0) Local $real = Round(Log(_CAbs($cmp)),$Reps-1) Local $imag = Round(_CArg($cmp),$Reps-1) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CSin >> ================================================================ ; Name ..........: _CSin() ; Description ...: Returns the sine of a complex number. ; Syntax ........: _CSin($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the sine of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CSin($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round(Sin($x) * _Cosh($y) , $Reps-1 ) Local $imag = Round(Cos($x) * _Sinh($y) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CCos >> ================================================================ ; Name ..........: _CCos() ; Description ...: Returns the cosine of a complex number. ; Syntax ........: _CCos($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the cosine of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CCos($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round(Cos($x) * _Cosh($y) , $Reps-1 ) Local $imag = Round(Sin($x) * _Sinh($y) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CTan >> ================================================================ ; Name ..........: _CTan() ; Description ...: Returns the tangent of a complex number. ; Syntax ........: _CTan($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the tangent of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CTan($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round( Sin(2 * $x) / ( Cos(2 * $x) + _Cosh(2 * $y) ) , $Reps-1 ) Local $imag = Round( _Sinh(2 * $y) / ( Cos(2 * $x) + _Cosh(2 * $y) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CSec >> ================================================================ ; Name ..........: _CSec() ; Description ...: Returns the secant of a complex number. ; Syntax ........: _CSec($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the secant of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CSec($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round( (2 * Cos($x) * _Cosh($y) ) / ( Cos( 2 * $x ) + _Cosh( 2 * $y ) ) , $Reps-1 ) Local $imag = Round( (2 * Sin($x) * _Sinh($y) ) / ( Cos( 2 * $x ) + _Cosh( 2 * $y ) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CCsc >> ================================================================ ; Name ..........: _CCsc() ; Description ...: Returns the cosecant of a complex number. ; Syntax ........: _CCsc($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the cosecant of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CCsc($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") If $y = 0 And $x = 0 Then Return SetError(2, 0 ,0) Local $real = Round( (-2 * Sin($x) * _Cosh($y) ) / ( Cos( 2 * $x ) - _Cosh( 2 * $y ) ) , $Reps-1 ) Local $imag = Round( (2 * Cos($x) * _Sinh($y) ) / ( Cos( 2 * $x ) - _Cosh( 2 * $y ) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CCot >> ================================================================ ; Name ..........: _CCot() ; Description ...: Returns the cotangent of a complex number. ; Syntax ........: _CCot($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the cotangent of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CCot($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") If $y = 0 And $x = 0 Then Return SetError(2, 0, 0) Local $real = Round( -Sin(2 * $x) / ( Cos(2 * $x) - _Cosh(2 * $y) ) , $Reps-1 ) Local $imag = Round( _Sinh(2 * $y) / ( Cos(2 * $x) - _Cosh(2 * $y) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CASin >> =============================================================== ; Name ..........: _CASin() ; Description ...: Returns the area sine of a complex number. ; Syntax ........: _CASin($cmp, $n) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the area sine of complex numbers. ; On Failure: = Sets @Error and returns "", which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CASin($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round((_Sgn($x)/2) * ACos( Sqrt( ($x^2 + $y^2 - 1)^2 + (2 * $y)^2 ) - ($x^2 + $y^2) ) , $Reps-1 ) Local $imag = Round((_Sgn($y)/2) * _ACosh( Sqrt( ($x^2 + $y^2 - 1)^2 + (2 * $y)^2 ) + ($x^2 + $y^2) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CACos >> =============================================================== ; Name ..........: _CACos() ; Description ...: Returns the area cosine of a complex number. ; Syntax ........: _CACos($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the area cosine of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CACos($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round( $pi/2 - ( (_Sgn($x)/2) * ACos( Sqrt( ($x^2 + $y^2 - 1)^2 + (2 * $y)^2 ) - ($x^2 + $y^2) ) ) , $Reps-1 ) Local $imag = Round((_Sgn($y)/2) * _ACosh( Sqrt( ($x^2 + $y^2 - 1)^2 + (2 * $y)^2 ) + ($x^2 + $y^2) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CATan >> =============================================================== ; Name ..........: _CATan() ; Description ...: Returns the area tangent of a complex number. ; Syntax ........: _CATan($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the area tangent of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CATan($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = 0 If $x = 0 Then If Abs($y) <= 1 Then $real = 0 Else $real = $pi/2 * _Sgn($y) EndIf Else $real = 0.5 * ( ATan( ($x^2 + $y^2 - 1)/(2 * $x) ) + ($pi/2 * _Sgn($x)) ) EndIf $real = Round( $real, $Reps-1 ) Local $imag = Round( 0.5 * _ATanh( (2 * $y)/($x^2 + $y^2 + 1) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CASec >> =============================================================== ; Name ..........: _CASec() ; Description ...: Returns the area secant of a complex number. ; Syntax ........: _CASec($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the area secant of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CASec($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) $cmp = _CDiv(1, $cmp) if @error = 2 Then Return SetError(2, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round( $pi/2 - ( (_Sgn($x)/2) * ACos( Sqrt( ($x^2 + $y^2 - 1)^2 + (2 * $y)^2 ) - ($x^2 + $y^2) ) ) , $Reps-1 ) Local $imag = Round((_Sgn($y)/2) * _ACosh( Sqrt( ($x^2 + $y^2 - 1)^2 + (2 * $y)^2 ) + ($x^2 + $y^2) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CACsc >> =============================================================== ; Name ..........: _CACsc() ; Description ...: Returns the area cosecant of a complex number. ; Syntax ........: _CACsc($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the area cosecant of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CACsc($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) $cmp = _CDiv(1, $cmp) if @error = 2 Then Return SetError(2, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round((_Sgn($x)/2) * ACos( Sqrt( ($x^2 + $y^2 - 1)^2 + (2 * $y)^2 ) - ($x^2 + $y^2) ) , $Reps-1 ) Local $imag = Round((_Sgn($y)/2) * _ACosh( Sqrt( ($x^2 + $y^2 - 1)^2 + (2 * $y)^2 ) + ($x^2 + $y^2) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CACot >> =============================================================== ; Name ..........: _CACot() ; Description ...: Returns the area cotangent of a complex number to an real exponent. ; Syntax ........: _CACot($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the area cotangent of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CACot($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = 0 If $x = 0 Then If Abs($y) <= 1 Then $real = $pi / 2 Else $real = $pi/2 * (1 - _Sgn($y)) EndIf Else $real = 0.5 * ($pi - ( ATan( ($x^2 + $y^2 - 1)/(2 * $x) ) + ($pi/2 * _Sgn($x)) ) ) EndIf $real = Round( $real, $Reps-1 ) Local $imag = Round( 0.5 * _ATanh( (2 * $y)/($x^2 + $y^2 + 1) ) , $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CSinh >> =============================================================== ; Name ..........: _CSinh() ; Description ...: Returns the hyperbolic sine of a complex number. ; Syntax ........: _CSinh($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the hyperbolic sine of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CSinh($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round( Cos($y) * _Sinh($x), $Reps-1 ) Local $imag = Round( Sin($y) * _Cosh($x), $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CCosh >> =============================================================== ; Name ..........: _CCosh() ; Description ...: Returns the hyperbolic cosine of a complex number. ; Syntax ........: _CCosh($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the hyperbolic cosine of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CCosh($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round( Cos($y) * _Cosh($x), $Reps-1 ) Local $imag = Round( Sin($y) * _Sinh($x), $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CTanh >> =============================================================== ; Name ..........: _CTanh() ; Description ...: Returns the hyperbolic tangent of a complex number. ; Syntax ........: _CTanh($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the hyperbolic tangent of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CTanh($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round( (_Sinh(2 * $x)) / (_Cosh(2 * $x) + Cos(2 * $y)), $Reps-1 ) Local $imag = Round( (Sin(2 * $y)) / (_Cosh(2 * $x) + Cos(2 * $y)), $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CSech >> =============================================================== ; Name ..........: _CSech() ; Description ...: Returns the hyperbolic secant of a complex number. ; Syntax ........: _CSech($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the hyperbolic secant of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CSech($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") Local $real = Round( (2 * _Cosh($x) * Cos($y)) / (_Cosh(2 * $x) + Cos(2 * $y)), $Reps-1 ) Local $imag = Round( (-2 * _Sinh($x) * Sin($y)) / (_Cosh(2 * $x) + Cos(2 * $y)), $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CCsch >> =============================================================== ; Name ..........: _CCsch() ; Description ...: Returns the hyperbolic Cosecant of a complex number. ; Syntax ........: _CCsch($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the hyperbolic Cosecant of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CCsch($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") If $y = 0 And $x = 0 Then Return SetError(2, 0, 0) Local $real = Round( (2 * _Sinh($x) * Cos($y)) / (_Cosh(2 * $x) - Cos(2 * $y)), $Reps-1 ) Local $imag = Round( (-2 * _Cosh($x) * Sin($y)) / (_Cosh(2 * $x) - Cos(2 * $y)), $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CCoth >> =============================================================== ; Name ..........: _CCoth() ; Description ...: Returns the hyperbolic Cotangent of a complex number. ; Syntax ........: _CCoth($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the hyperbolic Cotangent of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CCoth($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Local $x = DllStructGetData($cmp, "Real") Local $y = DllStructGetData($cmp, "Imag") If $y = 0 And $x = 0 Then Return SetError(2, 0, 0) Local $real = Round( (_Sinh(2 * $x)) / (_Cosh(2 * $x) - Cos(2 * $y)), $Reps-1 ) Local $imag = Round( (-Sin(2 * $y)) / (_Cosh(2 * $x) - Cos(2 * $y)), $Reps-1 ) Return _Cmplx($real, $imag) EndFunc ; #FUNCTION# << CASinh >> ============================================================== ; Name ..........: _CASinh() ; Description ...: Returns the Area hyperbolic sine of a complex number. ; Syntax ........: _CASinh($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the Area hyperbolic sine of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CASinh($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Return _CLn(_CAdd($cmp, _CPow(_CAdd(_Cmplx(1,0), _CPow($cmp, 2)), 1/2))) EndFunc ; #FUNCTION# << CACosh >> ============================================================== ; Name ..........: _CACosh() ; Description ...: Returns the Area hyperbolic Cosine of a complex number. ; Syntax ........: _CACosh($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the Area hyperbolic Cosine of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CACosh($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) Return _CLn(_CAdd($cmp, _CMul(_CPow(_CAdd($cmp, _Cmplx(1,0)), 1/2), _CPow(_CSub($cmp, _Cmplx(1,0)), 1/2)))) EndFunc ; #FUNCTION# << CATanh >> ============================================================== ; Name ..........: _CATanh() ; Description ...: Returns the Area hyperbolic tangent of a complex number. ; Syntax ........: _CATanh($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the Area hyperbolic tangent of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CATanh($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) If DllStructGetData($cmp, "Real") = 1 and DllStructGetData($cmp, "Imag") = 0 Then Return SetError(2, 0, 0) Return _CMul(0.5, _CLn(_CDiv(_CAdd(_Cmplx(1, 0), $cmp), _CSub(_Cmplx(1, 0), $cmp)))) EndFunc ; #FUNCTION# << CACoth >> ============================================================== ; Name ..........: _CACoth() ; Description ...: Returns the Area hyperbolic Cotangent of a complex number. ; Syntax ........: _CACoth($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the Area hyperbolic Cotangent of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CACoth($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) If DllStructGetData($cmp, "Real") = 1 and DllStructGetData($cmp, "Imag") = 0 Then Return SetError(2, 0, 0) Return _CMul(0.5, _CLn(_CDiv(_CAdd($cmp, _Cmplx(1, 0)), _CSub($cmp, _Cmplx(1, 0))))) EndFunc ; #FUNCTION# << CASech >> ============================================================== ; Name ..........: _CASech() ; Description ...: Returns the Area hyperbolic Secant of a complex number. ; Syntax ........: _CASech($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the Area hyperbolic Secant of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CASech($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) If DllStructGetData($cmp, "Real") = 0 and DllStructGetData($cmp, "Imag") = 0 Then Return SetError(2, 0, 0) Return _CLn(_CAdd(_CMul(_CRoot(_CSub(_CDiv(_Cmplx(1,0), $cmp), _Cmplx(1,0)), 2), _CRoot(_CAdd(_CDiv(_Cmplx(1,0), $cmp), _Cmplx(1, 0)), 2)), _CDiv(_Cmplx(1,0), $cmp))) EndFunc ; #FUNCTION# << CACsch >> ============================================================== ; Name ..........: _CACsch() ; Description ...: Returns the Area hyperbolic Cosecant of a complex number. ; Syntax ........: _CACsch($cmp) ; Parameters ....: $cmp = complex number ; Return values .: On Success: = Returns the Area hyperbolic Cosecant of complex numbers. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $cmp is not a (complex) number. ; Author ........: Strahleman ; Remarks .......: the complex number type is a two, double-floating type structure ; Example .......: ; ====================================================================================== Func _CACsch($cmp) If Not _CCheck($cmp) Then Return SetError(1, 0, 0) If DllStructGetData($cmp, "Real") = 0 and DllStructGetData($cmp, "Imag") = 0 Then Return SetError(2, 0, 0) Return _CLn(_CAdd(_CRoot(_CAdd(_Cmplx(1,0), _CDiv(_Cmplx(1, 0), _CPow($cmp, 2))), 2), _CDiv(_Cmplx(1, 0), $cmp))) EndFunc #EndRegion Complex Number Functions #Region Trigonometric Functions ; #FUNCTION# << Sin >> ================================================================= ; Name ..........: _Sin() ; Description ...: Returns the standard position angle (in radians). ; Syntax ........: _Sin($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the sine function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Trigonometric Functions", "_Sin( $pi/6 ) = " & _Sin( $pi/6 ) ) ; ====================================================================================== Func _Sin($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CSin($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round(Sin($x), $Reps-1) EndFunc ; #FUNCTION# << Cos >> ================================================================= ; Name ..........: _Cos() ; Description ...: Returns the standard position angle (in radians). ; Syntax ........: _Cos($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the cosine function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Trigonometric Functions", "_Cos( $pi/3 ) = " & _Cos( $pi/3 ) ) ; ====================================================================================== Func _Cos($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CCos($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round(Cos($x), $Reps-1) EndFunc ; #FUNCTION# << Tan >> ================================================================= ; Name ..........: _Tan() ; Description ...: Returns the standard position angle (in radians). ; Syntax ........: _Tan($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the tangent function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Trigonometric Functions", "_Tan( $pi/4 ) = " & _Tan( $pi/4 ) ) ; ====================================================================================== Func _Tan($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CTan($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round(Tan($x), $Reps-1) EndFunc ; #FUNCTION# << Sec >> ================================================================= ; Name ..........: _Sec() ; Description ...: Returns the standard position angle (in radians). ; Syntax ........: _Sec($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the secant function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; 2 = $x is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Trigonometric Functions", "_Sec( $pi/4 ) = " & _Sec( $pi/4 ) ) ; ====================================================================================== Func _Sec($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CSec($x) Else Return SetError(1, 0, 0) EndIf EndIf If Cos($x) = 0 Then Return SetError(2, 0, 0) Return Round(1/Cos($x), $Reps - 1) EndFunc ; #FUNCTION# << Csc >> ================================================================= ; Name ..........: _Csc() ; Description ...: Returns the standard position angle (in radians). ; Syntax ........: _Csc($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the cosecant function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; 2 = $x is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Trigonometric Functions", "_Csc( $pi/4 ) = " & _Csc( $pi/4 ) ) ; ====================================================================================== Func _Csc($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CCsc($x) Else Return SetError(1, 0, 0) EndIf EndIf If Sin($x) = 0 Then Return SetError(2, 0, 0) Return Round(1/Sin($x), $Reps - 1) EndFunc ; #FUNCTION# << Cot >> ================================================================= ; Name ..........: _Cot() ; Description ...: Returns the standard position angle (in radians) to the point ($nX, $nY). ; Syntax ........: _Cot($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the cotangent function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; 2 = $x is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Trigonometric Functions", "_Cot( $pi/4 ) = " & _Cot( $pi/4 ) ) ; ====================================================================================== Func _Cot($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CCot($x) Else Return SetError(1, 0, 0) EndIf EndIf If Tan($x) = 0 Then Return SetError(2, 0, 0) Return Round(1/Tan($x), $Reps - 1) EndFunc #EndRegion Trigonometric Functions #Region Inverse Trigonometric Functions ; #FUNCTION# << ASin >> ================================================================ ; Name ..........: _ASin() ; Description ...: Returns value of the inverse sine function. ; Syntax ........: _ASin($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse sine function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Trigonometric Functions", "_ASin( 0.5 ) / $pi * 6 = " & _ASin(0.5)/$pi*6 ) ; ====================================================================================== Func _ASin($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CASin($y) Else Return SetError(1, 0, 0) EndIf EndIf If Abs($y) > 1 Then Return SetError(2, 0, 0) Return ASin($y) EndFunc ; #FUNCTION# << ACos >> ================================================================ ; Name ..........: _ACos() ; Description ...: Returns value of the inverse cosine function. ; Syntax ........: _ACos($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse cosine function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Trigonometric Functions", "_ACos( 0.5 ) / $pi * 3 = " & _ACos(0.5)/$pi*3 ) ; ====================================================================================== Func _ACos($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CACos($y) Else Return SetError(1, 0, 0) EndIf EndIf If Abs($y) > 1 Then Return SetError(2, 0, 0) Return ACos($y) EndFunc ; #FUNCTION# << ATan >> ================================================================ ; Name ..........: _ATan() ; Description ...: Returns value of the inverse tangent function. ; Syntax ........: _ATan($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse tangent function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Trigonometric Functions", "_ATan( 1 ) / pi = " & _ATan(1)/$pi ) ; ====================================================================================== Func _ATan($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CATan($y) Else Return SetError(1, 0, 0) EndIf EndIf Return ATan($y) EndFunc ; #FUNCTION# << ASec >> ================================================================ ; Name ..........: _ASec() ; Description ...: Returns value of the inverse secant function. ; Syntax ........: _ASec($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse secant function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Trigonometric Functions", "_ASec( $pi/6 ) = " & _ASec( $pi/12 ) ) ; ====================================================================================== Func _ASec($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CASec($y) Else Return SetError(1, 0, 0) EndIf EndIf If Abs($y) < 1 Then Return SetError(2, 0, 0) Return ACos(1/$y) EndFunc ; #FUNCTION# << ACsc >> ================================================================ ; Name ..........: _ACsc() ; Description ...: Returns value of the inverse cosecant function. ; Syntax ........: _ACsc($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse cosecant function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside of range. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Trigonometric Functions", "_ACsc( 1 ) / pi = " & _ACsc(1) / $pi ) ; ====================================================================================== Func _ACsc($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CACsc($y) Else Return SetError(1, 0, 0) EndIf EndIf If Abs($y) < 1 Then Return SetError(2, 0, 0) Return ASin(1/$y) EndFunc ; #FUNCTION# << ACot >> ================================================================ ; Name...........: _ACot() ; Description ...: Returns value of the inverse cotangent function. ; Syntax.........: _ACot($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse cotangent function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Trigonometric Functions", "_ACot( 1 ) / pi = " & _ACot(1) / $pi ) ; ====================================================================================== Func _ACot($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CACot($y) Else Return SetError(1, 0, 0) EndIf EndIf Return ACos($y / Sqrt(1 + $y^2)) EndFunc ; #FUNCTION# << ATan2 >> =============================================================== ; Name...........: _ATan2() ; Description ...: Returns the standard position angle (in radians) to the point ($nX, $nY). ; Syntax.........: _ATan2(Const $nY, Const $nX ) ; Parameters ....: $Y = y co-ordinate of the point to check. ; $X = x co-ordinate of the point to check. ; Return values .: On Success: = Returns the angle, between 0 and 2 pi. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $Y or $X is not a number. ; 2 = Point is at the origin. Can not determine direction. ; Author ........: "Nutster" David Nuttall ; Remarks .......: Angles start from right being 0, and increasing upward from there. All angles are in radians. ; Example .......: MsgBox(4096, "ATan2() Test", "_ATan2( 3, 4 ) = " & _ATan2( 3, 4 )) ; ====================================================================================== Func _ATan2(Const $Y, Const $X) If Not IsNumber($Y) Or Not IsNumber($X) Then Return SetError(1, 0, 0) Local $res If $X = 0 Then If $Y > 0 Then $res = $pi / 2.0 ElseIf $Y < 0 Then $res = 3.0 * $pi / 2.0 Else Return SetError(2, 0, 0) ; no direction can be determined. EndIf ElseIf $X < 0 Then $res = ATan($Y / $X) + $pi Else $res = ATan($Y / $X) EndIf While $res < 0 $res += 2.0 * $pi WEnd Return $res EndFunc ;==>_ATan2 #EndRegion Inverse Trigonometric Functions #Region Hyperbolic Trigonometric Functions ; #FUNCTION# << Sinh >> ================================================================ ; Name ..........: _Sinh() ; Description ...: Returns value of the hyperbolic sine function. ; Syntax ........: _Sinh($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the hyperbolic sine function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Hyperbolic Trigonometric Functions", "_Sinh( ln( Phi ) ) = " & _Sinh( Log($ratio) ) ) ; ====================================================================================== Func _Sinh($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CSinh($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round((Exp($x) - Exp(-1 * $x)) / 2, $Reps - 1) EndFunc ; #FUNCTION# << Cosh >> ================================================================ ; Name ..........: _Cosh() ; Description ...: Returns value of the hyperbolic cosine function. ; Syntax ........: _Cosh($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the hyperbolic cosine function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Hyperbolic Trigonometric Functions", "_Cosh( 0 ) = " & _Cosh( 0 ) ) ; ====================================================================================== Func _Cosh($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CCosh($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round((Exp($x) + Exp(-1 * $x)) / 2, $Reps - 1) EndFunc ; #FUNCTION# << Tanh >> ================================================================ ; Name ..........: _Tanh() ; Description ...: Returns value of the hyperbolic tangent function. ; Syntax ........: _Tanh($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the hyperbolic tangent function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Hyperbolic Trigonometric Functions", "_Tanh( 0 ) = " & _Tanh( 0 ) ) ; ====================================================================================== Func _Tanh($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CTanh($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round((Exp($x) - Exp(-1 * $x)) / (Exp($x) + Exp(-1 * $x)), $Reps - 1) EndFunc ; #FUNCTION# << Sech >> ================================================================ ; Name ..........: _Sech() ; Description ...: Returns value of the hyperbolic secant function. ; Syntax ........: _Sech($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the hyperbolic secant function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Hyperbolic Trigonometric Functions", "_Sech( 0 ) = " & _Sech( 0 ) ) ; ====================================================================================== Func _Sech($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CSech($x) Else Return SetError(1, 0, 0) EndIf EndIf Return Round(2 / (Exp($x) + Exp(-1 * $x)), $Reps - 1) EndFunc ; #FUNCTION# << Csch >> ================================================================ ; Name ..........: _Csch() ; Description ...: Returns value of the hyperbolic cosecant function. ; Syntax ........: _Csch($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the hyperbolic cosecant function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; 2 = $x is outside of range. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Hyperbolic Trigonometric Functions", "_Csch( $pi/6 ) = " & _Csch( $pi/6 ) ) ; ====================================================================================== Func _Csch($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CCsch($x) Else Return SetError(1, 0, 0) EndIf EndIf If $x = 0 Then Return SetError(2, 0, 0) Return Round(2 / (Exp($x) - Exp(-1 * $x)), $Reps - 1) EndFunc ; #FUNCTION# << Coth >> ================================================================ ; Name ..........: _Coth() ; Description ...: Returns value of the hyperbolic cotangent function. ; Syntax ........: _Coth($x) ; Parameters ....: $x = a number. ; Return values .: On Success: = Returns the hyperbolic cotangent function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Hyperbolic Trigonometric Functions", "_Coth( $pi/6 ) = " & _Coth( $pi/6 ) ) ; ====================================================================================== Func _Coth($x) If Not IsNumber($x) Then If _CmplxCheck($x) Then Return _CCoth($x) Else Return SetError(1, 0, 0) EndIf EndIf If $x = 0 Then Return SetError(2, 0, 0) Return Round((Exp($x) + Exp(-1 * $x)) / (Exp($x) - Exp(-1 * $x)), $Reps - 1) EndFunc #EndRegion Hyperbolic Trigonometric Functions #Region Inverse Hyperbolic Trigonometric Functions ; #FUNCTION# << ASinh >> =============================================================== ; Name ..........: _ASinh() ; Description ...: Returns value of the inverse hyperbolic sine function. ; Syntax ........: _ASinh($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse hyperbolic sine function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Hyperbolic Trigonometric Functions", "_ASinh( " & 1.175201193643805 & " ) = " & _ASinh(1.175201193643805) ) ; ====================================================================================== Func _ASinh($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CASinh($y) Else Return SetError(1, 0, 0) EndIf EndIf Return Round(Log(Sqrt($y^2 + 1) + $y), $Reps - 1) EndFunc ; #FUNCTION# << ACosh >> =============================================================== ; Name ..........: _ACosh() ; Description ...: Returns value of the inverse hyperbolic cosine function. ; Syntax ........: _ACosh($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse hyperbolic cosine function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Hyperbolic Trigonometric Functions", "_ACosh( " & 1.543080634815245 & " ) = " & _ACosh(1.543080634815245) ) ; ====================================================================================== Func _ACosh($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CACosh($y) Else Return SetError(1, 0, 0) EndIf EndIf If $y < 1 Then Return SetError(2, 0, 0) Return Round(Log(Sqrt($y^2-1)+$y), $Reps-1) EndFunc ; #FUNCTION# << ATanh >> =============================================================== ; Name ..........: _ATanh() ; Description ...: Returns value of the inverse hyperbolic tangent function. ; Syntax ........: _ATanh($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse hyperbolic tangent function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Hyperbolic Trigonometric Functions", "_ATanh( " & 0.761594155955765 & " ) = " & _ATanh(0.761594155955765) ) ; ====================================================================================== Func _ATanh($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CATanh($y) Else Return SetError(1, 0, 0) EndIf EndIf If Abs($y)>1 Then Return SetError(2, 0, 0) Return 0.5 * Log((1 + $y) / (1 - $y)) EndFunc ; #FUNCTION# << ASech >> =============================================================== ; Name ..........: _ASech() ; Description ...: Returns value of the inverse hyperbolic secant function. ; Syntax ........: _ASech($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse hyperbolic secant function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Hyperbolic Trigonometric Functions", "_ASech( " & 0.886818883970074 & " ) = " & _ASech(0.886818883970074) ) ; ====================================================================================== Func _ASech($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CASech($y) Else Return SetError(1, 0, 0) EndIf EndIf If $y <= 0 Or $y > 1 Then Return SetError(2, 0, 0) Return Round(Log( (Sqrt(1 - $y^2) + 1) / ($y) ), $Reps-1) EndFunc ; #FUNCTION# << ACsch >> =============================================================== ; Name ..........: _ACsch() ; Description ...: Returns value of the inverse hyperbolic cosecant function. ; Syntax ........: _ACsch($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse hyperbolic cosecant function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Hyperbolic Trigonometric Functions", "_ACsch( " & 0.850918128239322 & " ) = " & _ACsch(0.850918128239322) ) ; ====================================================================================== Func _ACsch($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CACsch($y) Else Return SetError(1, 0, 0) EndIf EndIf If $y = 0 Then Return SetError(2, 0, 0) Return Round(Log( (Sqrt(1 + $y^2) + 1) / ($y) ), $Reps - 1) EndFunc ; #FUNCTION# << ACoth >> =============================================================== ; Name ..........: _ACoth() ; Description ...: Returns value of the inverse hyperbolic cotangent function. ; Syntax ........: _ACoth($y) ; Parameters ....: $y = a number. ; Return values .: On Success: = Returns the inverse hyperbolic cotangent function value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $y is not a number. ; 2 = $y is outside the domain. ; Author ........: Strahleman ; Remarks .......: All angles are in radians. ; Example .......: MsgBox(0x42040, "Inverse Hyperbolic Trigonometric Functions", "_ACoth( " & 1.037314720727548 & " ) = " & _ACoth(1.037314720727548) ) ; ====================================================================================== Func _ACoth($y) If Not IsNumber($y) Then If _CmplxCheck($y) Then Return _CACoth($y) Else Return SetError(1, 0, 0) EndIf EndIf If Abs($y)<=1 Then Return SetError(2, 0, 0) Return 0.5 * Log(($y + 1) / ($y - 1)) EndFunc #EndRegion Inverse Hyperbolic Trigonometric Functions #Region Continuous Distribution Functions #Region normal distribution ; #FUNCTION# << NormPDF >> ============================================================= ; Name ..........: _NormPDF() ; Description ...: Returns the probability density of the normal distribution. ; Syntax ........: _NormPDF($x [, $mu [, $sigma]]) ; Parameters ....: $x = value which indicates the probability density. ; $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $mu or $sigma is not a number. ; 2 = $sigma is zero. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_NormPDF( 1 ) = " & _NormPDF(1) ) ; ====================================================================================== Func _NormPDF($x, $mu=0, $sigma=1) If Not IsNumber($x) Or Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma = 0 Then Return SetError(2, 0, 0) Return Round(1 / (Sqrt(2 * $pi * $sigma^2)) * Exp(-(($x-$mu)^2) / (2 * $sigma^2)), $Reps - 1) EndFunc ; #FUNCTION# << NormCDF >> ============================================================= ; Name ..........: _NormCDF() ; Description ...: Returns the cumulated probability distribution of the normal distribution. ; Syntax ........: _NormCDF($x [, $mu [, $sigma]]) ; Parameters ....: $x = value which indicates the probability. ; $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the probability distribution value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $mu or $sigma is not a number. ; 2 = $sigma is zero. ; Author ........: Strahleman ; Remarks .......: uses the gaussian error function ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_NormCDF( 1 ) = " & _NormCDF(1) ) ; ====================================================================================== Func _NormCDF($x, $mu=0, $sigma=1) If Not IsNumber($x) Or Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma = 0 Then Return SetError(2, 0, 0) Return Round(0.5 * (1 + __erfcore(($x - $mu) / Sqrt(2 * $sigma^2))), $Reps - 1) EndFunc ; #FUNCTION# << NormINV >> ============================================================= ; Name ..........: _NormINV() ; Description ...: Returns the inverse cumulated probability distribution of the normal distribution. ; Syntax ........: _NormINV($P [, $mu [, $sigma]]) ; Parameters ....: $P = the quantile value (number). ; $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the quantile value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $mu or $sigma is not a number. ; 2 = $sigma is zero or $P is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "SContinuous Distribution Functions", "_NormINV( 0.8413447460685434 ) = " & _NormINV(0.8413447460685434) ) ; ====================================================================================== Func _NormINV($P, $mu=0, $sigma=1) If Not IsNumber($P) Or Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma = 0 Or $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Return Round((__erfinv(($P * 2) - 1)*sqrt(2*$sigma^2)) + $mu, $Reps - 1) EndFunc ; #FUNCTION# << NormMEAN >> ============================================================ ; Name ..........: _NormMEAN() ; Description ...: Returns the mean of the normal distribution. ; Syntax ........: _NormMEAN([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is zero. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_NormMEAN( 1 ) = " & _NormMEAN(1) ) ; ====================================================================================== Func _NormMEAN($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma = 0 Then Return SetError(2, 0, 0) Return $mu EndFunc ; #FUNCTION# << NormVAR >> ============================================================= ; Name ..........: _NormVAR() ; Description ...: Returns the variance of the normal distribution. ; Syntax ........: _NormVAR([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is zero. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_NormVAR( 1 ) = " & _NormVAR(1) ) ; ====================================================================================== Func _NormVAR($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma = 0 Then Return SetError(2, 0, 0) Return $sigma^2 EndFunc ; #FUNCTION# << NormSKW >> ============================================================= ; Name ..........: _NormSKW() ; Description ...: Returns the skewness of the normal distribution. ; Syntax ........: _NormSKW([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is zero. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_NormSKW( 1 ) = " & _NormSKW(1) ) ; ====================================================================================== Func _NormSKW($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma = 0 Then Return SetError(2, 0, 0) Return 0 EndFunc ; #FUNCTION# << NormEXC >> ============================================================= ; Name ..........: _NormEXC() ; Description ...: Returns the excess of the normal distribution. ; Syntax ........: _NormEXC([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is zero. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_NormEXC( 1 ) = " & _NormEXC(1) ) ; ====================================================================================== Func _NormEXC($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma = 0 Then Return SetError(2, 0, 0) Return 0 EndFunc ; #FUNCTION# << NormRND >> ============================================================= ; Name ..........: _NormRND() ; Description ...: Returns the random number of the normal distribution. ; Syntax ........: _NormRND([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the normal distributed random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is zero. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_NormRND( 1 ) = " & _NormRND(1) ) ; ====================================================================================== Func _NormRND($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma = 0 Then Return SetError(2, 0, 0) Return _NormINV(Random(1), $mu, $sigma) EndFunc #EndRegion normal distribution #Region beta distribution ; #FUNCTION# << BetaPDF >> ============================================================= ; Name ..........: _BetaPDF() ; Description ...: Returns the probability density of the beta distribution. ; Syntax ........: _BetaPDF($x [, $a [, $b]]) ; Parameters ....: $x = value over which find the probability (number). ; $a = shape factor (number). ; $b = shape factor (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $a or $b is not a number. ; 2 = $x, $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_BetaPDF( 1 ) = " & _BetaPDF(1) ) ; ====================================================================================== Func _BetaPDF($x, $a=1, $b=1) If Not IsNumber($x) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a <= 0 Or $b <= 0 Or $x < 0 Or $x > 1) Then Return SetError(2, 0, 0) If (($x = 0 And $a < 1) Or ($x = 1 And $b < 1)) Then Return 1/0 Return Round(Exp(($a - 1) * Log($x) + ($b - 1) * Log(1 - $x) - __gammaln($a) - __gammaln($b) + __gammaln($a+$b)), $Reps-1) EndFunc ; #FUNCTION# << BetaCDF >> ============================================================= ; Name ..........: _BetaCDF() ; Description ...: Returns the cumulated probability density of the beta distribution. ; Syntax ........: _BetaCDF($x [, $a [, $b]]) ; Parameters ....: $x = value over which find the probability (number). ; $a = shape factor (number). ; $b = shape factor (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $a or $b is not a number. ; 2 = $x, $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_BetaCDF( 1 ) = " & _BetaCDF(1) ) ; ====================================================================================== Func _BetaCDF($x, $a=1, $b=1) If Not IsNumber($x) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a <= 0 Or $b <= 0 Or $x < 0 Or $x > 1) Then Return SetError(2, 0, 0) Return __betainc($x, $a, $b) EndFunc ; #FUNCTION# << BetaINV >> ============================================================= ; Name ..........: _BetaINV() ; Description ...: Returns the inverse cumulated probability density of the beta distribution. ; Syntax ........: _BetaINV($P [, $a [, $b]]) ; Parameters ....: $P = the quantile value (number). ; $a = shape factor (number). ; $b = shape factor (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $a or $b is not a number. ; 2 = $P, $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_BetaINV( 1 ) = " & _BetaINV(1) ) ; ====================================================================================== Func _BetaINV($P, $a=1, $b=1) If Not IsNumber($P) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a <= 0 Or $b <= 0 Or $P < 0 Or $P > 1) Then Return SetError(2, 0, 0) Return Round(__betainv($P, $a, $b) , $Reps - 1) EndFunc ; #FUNCTION# << BetaMEAN >> ============================================================ ; Name ..........: _BetaMEAN() ; Description ...: Returns the mean of the beta distribution. ; Syntax ........: _BetaMEAN([$a [, $b]]) ; Parameters ....: $a = shape factor (number). ; $b = shape factor (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_BetaMEAN( 1 ) = " & _BetaMEAN(1) ) ; ====================================================================================== Func _BetaMEAN($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a <= 0 Or $b <= 0) Then Return SetError(2, 0, 0) Return Round($a / ($a + $b) , $Reps - 1) EndFunc ; #FUNCTION# << BetaVAR >> ============================================================= ; Name ..........: _BetaVAR() ; Description ...: Returns the variance of the beta distribution. ; Syntax ........: _BetaVAR([$a [, $b]]) ; Parameters ....: $a = shape factor (number). ; $b = shape factor (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_BetaVAR( 1 ) = " & _BetaVAR(1) ) ; ====================================================================================== Func _BetaVAR($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a <= 0 Or $b <= 0) Then Return SetError(2, 0, 0) Return Round(($a * $b) / (($a + $b)^2 * ($a + $b + 1)) , $Reps - 1) EndFunc ; #FUNCTION# << BetaSKW >> ============================================================= ; Name ..........: _BetaSKW() ; Description ...: Returns the skewness of the beta distribution. ; Syntax ........: _BetaSKW([$a [, $b]]) ; Parameters ....: $a = shape factor (number). ; $b = shape factor (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_BetaSKW( 1 ) = " & _BetaSKW(1) ) ; ====================================================================================== Func _BetaSKW($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a <= 0 Or $b <= 0) Then Return SetError(2, 0, 0) Return (2 * ($b - $a) * Sqrt($a + $b + 1)) / (($a + $b + 2) * Sqrt($a * $b)) EndFunc ; #FUNCTION# << BetaEXC >> ============================================================= ; Name ..........: _BetaEXC() ; Description ...: Returns the excess of the beta distribution. ; Syntax ........: _BetaEXC([$a [, $b]]) ; Parameters ....: $a = shape factor (number). ; $b = shape factor (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_BetaEXC( 1 ) = " & _BetaEXC(1) ) ; ====================================================================================== Func _BetaEXC($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a <= 0 Or $b <= 0) Then Return SetError(2, 0, 0) Return (6 * (($a - $b)^2 * ($a + $b + 1) - (($a * $b) * ($a + $b + 2)) ) / (($a + $b + 3) * ($a + $b + 2) * ($a * $b))) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _BetaRND() ; Description ...: Returns the random number of the beta distribution. ; Syntax ........: _BetaRND([$a [, $b]]) ; Parameters ....: $a = shape factor (number). ; $b = shape factor (number). ; Return values .: On Success: = Returns the beta distribution random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_BetaRND( 1 ) = " & _BetaRND(1) ) ; ====================================================================================== Func _BetaRND($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a <= 0 Or $b <= 0) Then Return SetError(2, 0, 0) Return _BetaINV(Random(1), $a, $b) EndFunc #EndRegion beta distribution #Region triangular distribution ; #FUNCTION# << TriPDF >> ============================================================== ; Name ..........: _TriPDF() ; Description ...: Returns the probability density of the triangular distribution. ; Syntax ........: _TriCDF($x [, $a [, $b [, $c]]]) ; Parameters ....: $x = value over which find the probability (number). ; $a = the lower limit (number). ; $b = the upper limit (number). ; $c = the mode (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $a, $b or $c is not a number. ; 2 = $a, $b or $c are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TriPDF( 0.5 ) = " & _TriPDF(0.5) ) ; ====================================================================================== Func _TriPDF($x, $a=0, $b=1, $c=0.5) If Not IsNumber($x) Or Not IsNumber($a) Or Not IsNumber($b) Or Not IsNumber($c) Then Return SetError(1, 0, 0) If @NumParams = 3 Then $c = ($a + $b) / 2 If $a > $c Or $c > $b Then Return SetError(2, 0, 0) If ($x < $a Or $x > $b) Then Return 0 ElseIf ($x >= $a And $x <= $c) Then Return Round(2*($x - $a)/(($b - $a)*($c - $a)), $Reps - 1) ElseIf ($x > $c And $x <= $b) Then Return Round(2*($b - $x)/(($b - $a)*($b - $c)), $Reps - 1) EndIf EndFunc ; #FUNCTION# << TriCDF >> ============================================================== ; Name ..........: _TriCDF() ; Description ...: Returns the cumulated probability density of the triangular distribution. ; Syntax ........: _TriCDF($x [, $a [, $b [, $c]]]) ; Parameters ....: $x = value over which find the probability (number). ; $a = the lower limit (number). ; $b = the upper limit (number). ; $c = the mode (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $a, $b or $c is not a number. ; 2 = $a, $b or $c are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TriCDF( 0.5 ) = " & _TriCDF(0.5) ) ; ====================================================================================== Func _TriCDF($x, $a=0, $b=1, $c=0.5) If Not IsNumber($x) Or Not IsNumber($a) Or Not IsNumber($b) Or Not IsNumber($c) Then Return SetError(1, 0, 0) If @NumParams = 3 Then $c = ($a + $b) / 2 If $a > $c Or $c > $b Then Return SetError(2, 0, 0) If $x < $a Then Return 0 ElseIf $x > $b Then Return 1 ElseIf ($x >= $a And $x <= $c) Then Return Round(($x - $a)^2/(($b - $a)*($c - $a)), $Reps - 1) ElseIf ($x > $c And $x <= $b) Then Return Round(1 - (($b - $x)^2/(($b - $a)*($b - $c))), $Reps - 1) EndIf EndFunc ; #FUNCTION# << TriINV >> ============================================================== ; Name ..........: _TriINV() ; Description ...: Returns the inverse cumulated probability density of the triangular distribution. ; Syntax ........: _TriINV($P [, $a [, $b [, $c]]]) ; Parameters ....: $P = the quantile value (number). ; $a = the lower limit (number). ; $b = the upper limit (number). ; $c = the mode (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $a, $b or $c is not a number. ; 2 = $a, $b or $c are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TriINV( 0.25 ) = " & _TriINV(0.25) ) ; ====================================================================================== Func _TriINV($P, $a=0, $b=1, $c=0.5) If Not IsNumber($P) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If @NumParams = 3 Then $c = ($a + $b) / 2 If $a > $c Or $c > $b Or ($P < 0 And $P > 1) Then Return SetError(2, 0, 0) Local $d = ($c - $a)/($b - $a) If ($P >= 0 And $P <= $d) Then Return Round($a + Sqrt($P * ($b - $a) * ($c - $a)), $Reps - 1) ElseIf ($P > $d and $P <= 1) Then Return Round($b - Sqrt((1 - $P) * ($b - $a) * ($b - $c)), $Reps - 1) EndIf EndFunc ; #FUNCTION# << TriMEAN >> ============================================================= ; Name ..........: _TriMEAN() ; Description ...: Returns the mean of the triangular distribution. ; Syntax ........: _TriMEAN([$a [, $b [, $c]]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; $c = the mode (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a, $b or $c is not a number. ; 2 = $a, $b or $c are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TriMEAN( ) = " & _TriMEAN() ) ; ====================================================================================== Func _TriMEAN($a=0, $b=1, $c=0.5) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If @NumParams = 2 Then $c = ($a + $b) / 2 If $a > $c Or $c > $b Then Return SetError(2, 0, 0) Return Round(($a + $b + $c) / 3 , $Reps - 1) EndFunc ; #FUNCTION# << TriVAR >> ============================================================== ; Name ..........: _TriVAR() ; Description ...: Returns the variance of the triangular distribution. ; Syntax ........: _TriVAR([$a [, $b [, $c]]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; $c = the mode (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a, $b or $c is not a number. ; 2 = $a, $b or $c are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TriVAR( ) = " & _TriVAR() ) ; ====================================================================================== Func _TriVAR($a=0, $b=1, $c=0.5) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If @NumParams = 2 Then $c = ($a + $b) / 2 If $a > $c Or $c > $b Then Return SetError(2, 0, 0) Return Round(($a^2 + $b^2 + $c^2 - ($a * $b) - ($a * $c) - ($b * $c)) / 18 , $Reps - 1) EndFunc ; #FUNCTION# << TriSKW >> ============================================================== ; Name ..........: _TriSKW() ; Description ...: Returns the skewness of the triangular distribution. ; Syntax ........: _TriSKW([$a [, $b [, $c]]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; $c = the mode (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a, $b or $c is not a number. ; 2 = $a, $b or $c are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TriSKW( ) = " & _TriSKW() ) ; ====================================================================================== Func _TriSKW($a=0, $b=1, $c=0.5) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If @NumParams = 2 Then $c = ($a + $b) / 2 If $a > $c Or $c > $b Then Return SetError(2, 0, 0) Return Round((sqrt(2) * ($a + $b - 2 * $c) * (2 * $a - $b - $c) * ($a - 2 * $b + $c)) / (5 * ($a^2 + $b^2 + $c^2 - ($a * $b) - ($a * $c) - ($b * $c))^1.5) , $Reps - 1) EndFunc ; #FUNCTION# << TriEXC >> ============================================================== ; Name ..........: _TriEXC() ; Description ...: Returns the excess of the triangular distribution. ; Syntax ........: _TriEXC([$a [, $b [, $c]]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; $c = the mode (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a, $b or $c is not a number. ; 2 = $a, $b or $c are arranged wrong. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TriEXC( ) = " & _TriEXC() ) ; ====================================================================================== Func _TriEXC($a=0, $b=1, $c=0.5) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If @NumParams = 2 Then $c = ($a + $b) / 2 If $a > $c Or $c > $b Then Return SetError(2, 0, 0) Return Round( -0.6 , $Reps - 1) EndFunc ; #FUNCTION# << TriRND >> ============================================================== ; Name ..........: _TriRND() ; Description ...: Returns the random number of the triangular distribution. ; Syntax ........: _TriRND([$a [, $b [, $c]]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; $c = the mode (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a, $b or $c is not a number. ; 2 = $a, $b or $c are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TriRND( ) = " & _TriRND() ) ; ====================================================================================== Func _TriRND($a=0, $b=1, $c=0.5) If Not IsNumber($a) Or Not IsNumber($b) Or Not IsNumber($c) Then Return SetError(1, 0, 0) If @NumParams = 2 Then $c = ($a + $b) / 2 If $a > $c Or $c > $b Then Return SetError(2, 0, 0) Return _TriINV(Random(1), $a, $b, $c) EndFunc #EndRegion triangular distribution #Region rectangle distribution ; #FUNCTION# << RecPDF >> ============================================================== ; Name ..........: _RecPDF() ; Description ...: Returns the probability density of the rectangle distribution. ; Syntax ........: _RecPDF($x [, $a [, $b]]) ; Parameters ....: $x = value over which find the probability (number). ; $a = the lower limit (number). ; $b = the upper limit (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $a or $b is not a number. ; 2 = $a and $b are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_RecPDF( 0.5 ) = " & _RecPDF(0.5) ) ; ====================================================================================== Func _RecPDF($x, $a=0, $b=1) If Not IsNumber($x) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Then Return SetError(2, 0, 0) If ($x >= $a And $x <= $b) Then Return Round(1/($b - $a), $Reps - 1) Return 0 EndFunc ; #FUNCTION# << RecCDF >> ============================================================== ; Name ..........: _RecCDF() ; Description ...: Returns the cumulated probability density of the rectangle distribution. ; Syntax ........: _RecCDF($x [, $a [, $b]]) ; Parameters ....: $x = value over which find the probability (number). ; $a = the lower limit (number). ; $b = the upper limit (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $a or $b is not a number. ; 2 = $a and $b are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_RecCDF( 0.5 ) = " & _RecCDF(0.5) ) ; ====================================================================================== Func _RecCDF($x, $a=0, $b=1) If Not IsNumber($x) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Then Return SetError(2, 0, 0) If $x <= $a Then Return 0 ElseIf $x >= $b Then Return 1 ElseIf ($x > $a And $x < $b) Then Return Round(($x - $a)/($b - $a), $Reps - 1) EndIf EndFunc ; #FUNCTION# << RecINV >> ============================================================== ; Name ..........: _RecINV() ; Description ...: Returns the inverse cumulated probability density of the rectangle distribution. ; Syntax ........: _RecINV($P [, $a [, $b]]) ; Parameters ....: $P = the quantile value (number). ; $a = the lower limit (number). ; $b = the upper limit (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $a or $b is not a number. ; 2 = $a and $b are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_RecINV( 0.5 ) = " & _RecINV(0.5) ) ; ====================================================================================== Func _RecINV($P, $a=0, $b=1) If Not IsNumber($P) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Or ($P < 0 And $P > 1) Then Return SetError(2, 0, 0) Return $a + ($P * ($b - $a)) EndFunc ; #FUNCTION# << RecMEAN >> ============================================================= ; Name ..........: _RecMEAN() ; Description ...: Returns the mean of the rectangle distribution. ; Syntax ........: _RecMEAN([$a [, $b]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a and $b are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_RecMEAN( ) = " & _RecMEAN() ) ; ====================================================================================== Func _RecMEAN($a=0, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Then Return SetError(2, 0, 0) Return Round(($a + $b) / 2 , $Reps - 1) EndFunc ; #FUNCTION# << RecVAR >> ============================================================== ; Name ..........: _RecVAR() ; Description ...: Returns the variance of the rectangle distribution. ; Syntax ........: _RecVAR([$a [, $b]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a and $b are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_RecVAR( ) = " & _RecVAR() ) ; ====================================================================================== Func _RecVAR($a=0, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Then Return SetError(2, 0, 0) Return Round(($b - $a)^2 / 12 , $Reps - 1) EndFunc ; #FUNCTION# << RecSKW >> ============================================================== ; Name ..........: _RecSKW() ; Description ...: Returns the skewness of the rectangle distribution. ; Syntax ........: _RecSKW([$a [, $b]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a and $b are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_RecSKW( ) = " & _RecSKW() ) ; ====================================================================================== Func _RecSKW($a=0, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Then Return SetError(2, 0, 0) Return 0 EndFunc ; #FUNCTION# << RecEXC >> ============================================================== ; Name ..........: _RecEXC() ; Description ...: Returns the excess of the rectangle distribution. ; Syntax ........: _RecEXC([$a [, $b]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a and $b are arranged wrong. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_RecEXC( ) = " & _RecEXC() ) ; ====================================================================================== Func _RecEXC($a=0, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Then Return SetError(2, 0, 0) Return -1.2 EndFunc ; #FUNCTION# << RecRND >> ============================================================== ; Name ..........: _RecRND() ; Description ...: Returns the random number of the rectangle distribution. ; Syntax ........: _RecRND([$a [, $b]]) ; Parameters ....: $a = the lower limit (number). ; $b = the upper limit (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a and $b are arranged wrong. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_RecRND( ) = " & _RecRND() ) ; ====================================================================================== Func _RecRND($a=0, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Then Return SetError(2, 0, 0) Return Random($a, $b) EndFunc #EndRegion rectangle distribution #Region gamma distribution ; #FUNCTION# << GamPDF >> ============================================================== ; Name ..........: _GamPDF() ; Description ...: Returns the probability density of the gamma distribution. ; Syntax ........: _GamPDF($x [, $a [, $b]]) ; Parameters ....: $x = value over which find the probability (number). ; $a = the shape value (number). ; $b = the rate value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $a or $b is not a number. ; 2 = $x, $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_GamPDF( 1 ) = " & _GamPDF(1) ) ; ====================================================================================== Func _GamPDF($x, $a=1, $b=1) If Not IsNumber($x) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($x < 0 Or $a < 0 Or $b < 0) Then Return SetError(2, 0, 0) Return Exp(($a - 1) * Log($x) - ($x / $b) - $a * Log($b) - __gammaln($a)) EndFunc ; #FUNCTION# << GamCDF >> ============================================================== ; Name ..........: _GamCDF() ; Description ...: Returns the cumulated probability density of the gamma distribution. ; Syntax ........: _GamCDF($x [, $a [, $b]]) ; Parameters ....: $x = value over which find the probability (number). ; $a = the shape value (number). ; $b = the rate value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $a or $b is not a number. ; 2 = $x, $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_GamCDF( 1 ) = " & _GamCDF(1) ) ; ====================================================================================== Func _GamCDF($x, $a=1, $b=1) If Not IsNumber($x) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($x < 0 Or $a < 0 Or $b <= 0) Then Return SetError(2, 0, 0) Return __gammap($a, $x / $b) EndFunc ; #FUNCTION# << GamINV >> ============================================================== ; Name ..........: _GamINV() ; Description ...: Returns the inverse cumulated probability density of the gamma distribution. ; Syntax ........: _GamINV($P [, $a [, $b]]) ; Parameters ....: $P = the quantile value (number). ; $a = the shape value (number). ; $b = the rate value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $a or $b is not a number. ; 2 = $P, $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_GamINV( 0.5 ) = " & _GamINV(0.5)) ; ====================================================================================== Func _GamINV($P, $a=1, $b=1) If Not IsNumber($P) Or Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($P < 0 Or $P > 1 Or $a < 0 Or $b < 0) Then Return SetError(2, 0, 0) Return Round($b / __gammainv2(1 - $P, $a, $b), $Reps - 1) EndFunc ; #FUNCTION# << GamMEAN >> ============================================================= ; Name ..........: _GamMEAN() ; Description ...: Returns the mean of the gamma distribution. ; Syntax ........: _GamMEAN([$a [, $b]]) ; Parameters ....: $a = the shape value (number). ; $b = the rate value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_GamMEAN( ) = " & _GamMEAN()) ; ====================================================================================== Func _GamMEAN($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a < 0 Or $b < 0) Then Return SetError(2, 0, 0) Return Round($a / $b, $Reps - 1) EndFunc ; #FUNCTION# << GamVAR >> ============================================================== ; Name ..........: _GamVAR() ; Description ...: Returns the variance of the gamma distribution. ; Syntax ........: _GamVAR([$a [, $b]]) ; Parameters ....: $a = the shape value (number). ; $b = the rate value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_GamVAR( ) = " & _GamVAR()) ; ====================================================================================== Func _GamVAR($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a < 0 Or $b < 0) Then Return SetError(2, 0, 0) Return Round($a / $b^2, $Reps - 1) EndFunc ; #FUNCTION# << GamSKW >> ============================================================== ; Name ..........: _GamSKW() ; Description ...: Returns the skewness of the gamma distribution. ; Syntax ........: _GamSKW([$a [, $b]]) ; Parameters ....: $a = the shape value (number). ; $b = the rate value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_GamSKW( ) = " & _GamSKW()) ; ====================================================================================== Func _GamSKW($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a < 0 Or $b < 0) Then Return SetError(2, 0, 0) Return Round(2 / Sqrt($a), $Reps - 1) EndFunc ; #FUNCTION# << GamEXC >> ============================================================== ; Name ..........: _GamEXC() ; Description ...: Returns the excess of the gamma distribution. ; Syntax ........: _GamEXC([$a [, $b]]) ; Parameters ....: $a = the shape value (number). ; $b = the rate value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_GamEXC( ) = " & _GamEXC()) ; ====================================================================================== Func _GamEXC($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If ($a < 0 Or $b < 0) Then Return SetError(2, 0, 0) Return Round(6 / $a, $Reps - 1) EndFunc ; #FUNCTION# << GamRND >> ============================================================== ; Name ..........: _GamRND() ; Description ...: Returns the random number of the gamma distribution. ; Syntax ........: _GamRND([$a [, $b]]) ; Parameters ....: $a = the shape value (number). ; $b = the rate value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $a or $b is not a number. ; 2 = $a or $b is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_GamRND( ) = " & _GamRND()) ; ====================================================================================== Func _GamRND($a=1, $b=1) If Not IsNumber($a) Or Not IsNumber($b) Then Return SetError(1, 0, 0) If $a > $b Then Return SetError(2, 0, 0) Return _GamINV(Random(1), $a, $b) EndFunc #EndRegion gamma distribution #Region student-t distribution ; #FUNCTION# << TPDF >> ================================================================ ; Name ..........: _TPDF() ; Description ...: Returns the probability density of the student-t distribution. ; Syntax ........: _TPDF($t , $f ) ; Parameters ....: $t = value over which find the probability (number). ; $f = degrees of freedom (integer). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $t or $f is not a number. ; 2 = $f is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TPDF( 1, 1 ) = " & _TPDF(1, 1) ) ; ====================================================================================== Func _TPDF($t, $f) If Not IsNumber($t) Or Not IsNumber($f) Then Return SetError(1, 0, 0) If $f <= 0 Or Int($f) <> $f Then Return SetError(2, 0, 0) Return Round(Exp(__gammaln(($f + 1) / 2) - __gammaln($f / 2) - __gammaln(0.5) + Log($f / ($f + $t^2)) * (($f + 1) / 2) - Log(Sqrt($f))) , $Reps - 1) EndFunc ; #FUNCTION# << TCDF >> ================================================================ ; Name ..........: _TCDF() ; Description ...: Returns the cumulative probability density of the student-t distribution. ; Syntax ........: _TCDF($t , $f ) ; Parameters ....: $t = value over which find the probability (number). ; $f = degrees of freedom (integer). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $t or $f is not a number. ; 2 = $f is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TCDF( 0, 1 ) = " & _TCDF(0, 1) ) ; ====================================================================================== Func _TCDF($t, $f) If Not IsNumber($t) Or Not IsNumber($f) Then Return SetError(1, 0, 0) If $f <= 0 Or Int($f) <> $f Then Return SetError(2, 0, 0) If $t = 0 Then Return 0.5 ElseIf $t < 0 Then Return Round(0.5 * __betainc(($f / ($f + $t^2)), $f / 2, 0.5), $Reps - 1) Else Return Round(1 - 0.5 * __betainc(($f / ($f + $t^2)), $f / 2, 0.5), $Reps - 1) EndIf EndFunc ; #FUNCTION# << TINV >> ================================================================ ; Name ..........: _TINV() ; Description ...: Returns the inverse cumulated probability density of the student-t distribution. ; Syntax ........: _TINV($P , $f ) ; Parameters ....: $P = the quantile value (number). ; $f = degrees of freedom (integer). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P or $f is not a number. ; 2 = $P or $f is outside the domain or $f is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TINV( 0.75, 1 ) = " & _TINV(0.75, 1) ) ; ====================================================================================== Func _TINV($P, $f) If Not IsNumber($P) Or Not IsNumber($f) Then Return SetError(1, 0, 0) If $f <= 0 Or Int($f) <> $f Or $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) If $P = 0.5 Then Return 0 If $f = 1 Then Return Tan($pi * ($P - 0.5)) Return _Sgn($P - 0.5) * Sqrt($f * (1 / __betainv(1 - 2 * Abs($P - 0.5), $f/2, 1/2) - 1)) EndFunc ; #FUNCTION# << TMEAN >> =============================================================== ; Name ..........: _TMEAN() ; Description ...: Returns the mean of the student-t distribution. ; Syntax ........: _TMEAN( $f ) ; Parameters ....: $f = degrees of freedom (integer). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $f is not a number. ; 2 = $f is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TMEAN( ) = " & _TMEAN() ) ; ====================================================================================== Func _TMEAN($f) If Not IsNumber($f) Then Return SetError(1, 0, 0) If $f <= 1 Or Int($f) <> $f Then Return SetError(2, 0, 0) Return 0 EndFunc ; #FUNCTION# << TVAR >> ================================================================ ; Name ..........: _TVAR() ; Description ...: Returns the variance of the student-t distribution. ; Syntax ........: _TVAR( $f ) ; Parameters ....: $f = degrees of freedom (integer). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $f is not a number. ; 2 = $f is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TVAR( 10 ) = " & _TVAR(10) ) ; ====================================================================================== Func _TVAR($f) If Not IsNumber($f) Then Return SetError(1, 0, 0) If $f <= 1 Or Int($f) <> $f Then Return SetError(2, 0, 0) If $f > 1 And $f <= 2 Then Return 1/0 Return $f/($f-2) EndFunc ; #FUNCTION# << TSKW >> ================================================================ ; Name ..........: _TSKW() ; Description ...: Returns the skewness of the student-t distribution. ; Syntax ........: _TSKW( $f ) ; Parameters ....: $f = degrees of freedom (integer). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $f is not a number. ; 2 = $f is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TSKW( 10 ) = " & _TSKW(10) ) ; ====================================================================================== Func _TSKW($f) If Not IsNumber($f) Then Return SetError(1, 0, 0) If $f <= 3 Or Int($f) <> $f Then Return SetError(2, 0, 0) Return 0 EndFunc ; #FUNCTION# << TEXC >> ================================================================ ; Name ..........: _TEXC() ; Description ...: Returns the excess of the student-t distribution. ; Syntax ........: _TEXC( $f ) ; Parameters ....: $f = degrees of freedom (integer). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $f is not a number. ; 2 = $f is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TEXC( ) = " & _TEXC() ) ; ====================================================================================== Func _TEXC($f) If Not IsNumber($f) Then Return SetError(1, 0, 0) If $f <= 1 Or Int($f) <> $f Then Return SetError(2, 0, 0) If $f > 1 And $f <= 4 Then Return 1/0 Return Round(6 / ($f - 4) , $Reps - 1) EndFunc ; #FUNCTION# << TRND >> ================================================================ ; Name ..........: _TRND() ; Description ...: Returns the random number of the student-t distribution. ; Syntax ........: _TRND( $f ) ; Parameters ....: $f = degrees of freedom (integer). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $f is not a number. ; 2 = $f is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_TRND( ) = " & _TRND() ) ; ====================================================================================== Func _TRND($f) If Not IsNumber($f) Then Return SetError(1, 0, 0) If $f <= 0 Or Int($f) <> $f Then Return SetError(2, 0, 0) Return _TINV(Random(1), $f) EndFunc #EndRegion student-t distribution #Region chi-square distribution ; #FUNCTION# << ChisqPDF >> ============================================================= ; Name ..........: _ChisqPDF() ; Description ...: Returns the probability density of the Chi-squared distribution. ; Syntax ........: _ChisqPDF($x , $n) ; Parameters ....: $x = value over which find the probability (number). ; $n = degrees of freedom (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $n is not a number. ; 2 = $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ChisqPDF( 3, 5.4 ) = " & _ChisqPDF(3,5.4) ) ; ====================================================================================== Func _ChisqPDF($x, $n) If Not IsNumber($x) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 Then Return SetError(2, 0, 0) If $x <= 0 Then Return 0 Return Round(Exp((($n / 2) - 1) * Log($x) - ($x / 2) - ($n / 2) * Log(2) - __gammaln($n / 2)), $Reps - 1) EndFunc ; #FUNCTION# << ChisqCDF >> ============================================================= ; Name ..........: _ChisqCDF() ; Description ...: Returns the cumulated probability density of the Chi-squared distribution. ; Syntax ........: _ChisqCDF($x , $n) ; Parameters ....: $x = value over which find the probability (number). ; $n = degrees of freedom (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $n is not a number. ; 2 = $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ChisqCDF( 1, 5 ) = " & _ChisqCDF(1,5) ) ; ====================================================================================== Func _ChisqCDF($x, $n) If Not IsNumber($x) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 Then Return SetError(2, 0, 0) If $x <= 0 Then Return 0 Return Round(__gammap($n / 2, $x / 2), $Reps - 1) EndFunc ; #FUNCTION# << ChisqINV >> ============================================================= ; Name ..........: _ChisqINV() ; Description ...: Returns the inverse cumulated probability density of the Chi-squared distribution. ; Syntax ........: _ChisqINV($P , $n) ; Parameters ....: $P = the quantile value (number). ; $n = degrees of freedom (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $n is not a number. ; 2 = $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ChisqINV( 0.5, 1 ) = " & _ChisqINV(0.5,1) ) ; ====================================================================================== Func _ChisqINV($P, $n) If Not IsNumber($P) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 Or $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Return Round(2 * __gammainv2($P,$n/2), $Reps - 1) EndFunc ; #FUNCTION# << ChisqMEAN >> ============================================================ ; Name ..........: _ChisqMEAN() ; Description ...: Returns the mean of the Chi-squared distribution. ; Syntax ........: _ChisqMEAN($n) ; Parameters ....: $n = degrees of freedom (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n is not a number. ; 2 = $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ChisqMEAN( 5 ) = " & _ChisqMEAN(5) ) ; ====================================================================================== Func _ChisqMEAN($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 Then Return SetError(2, 0, 0) Return $n EndFunc ; #FUNCTION# << ChisqVAR >> ============================================================= ; Name ..........: _ChisqVAR() ; Description ...: Returns the variance of the Chi-squared distribution. ; Syntax ........: _ChisqVAR($n) ; Parameters ....: $n = degrees of freedom (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n is not a number. ; 2 = $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ChisqVAR( 5 ) = " & _ChisqVAR(5) ) ; ====================================================================================== Func _ChisqVAR($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 Then Return SetError(2, 0, 0) Return 2 * $n EndFunc ; #FUNCTION# << ChisqSKW >> ============================================================= ; Name ..........: _ChisqSKW() ; Description ...: Returns the skewness of the Chi-squared distribution. ; Syntax ........: _ChisqSKW($n) ; Parameters ....: $n = degrees of freedom (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n is not a number. ; 2 = $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ChisqSKW( 5 ) = " & _ChisqSKW(5) ) ; ====================================================================================== Func _ChisqSKW($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 Then Return SetError(2, 0, 0) Return Sqrt(8 / $n) EndFunc ; #FUNCTION# << ChisqEXC >> ============================================================= ; Name ..........: _ChisqEXC() ; Description ...: Returns the excess of the Chi-squared distribution. ; Syntax ........: _ChisqEXC($n) ; Parameters ....: $n = degrees of freedom (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n is not a number. ; 2 = $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ChisqEXC( 5 ) = " & _ChisqEXC(5) ) ; ====================================================================================== Func _ChisqEXC($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 Then Return SetError(2, 0, 0) Return 12 / $n EndFunc ; #FUNCTION# << ChisqRND >> ============================================================= ; Name ..........: _ChisqRND() ; Description ...: Returns the random number of the Chi-squared distribution. ; Syntax ........: _ChisqRND($n) ; Parameters ....: $n = degrees of freedom (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n is not a number. ; 2 = $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ChisqRND( 5 ) = " & _ChisqRND(5) ) ; ====================================================================================== Func _ChisqRND($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n <= 0 Then Return SetError(2, 0, 0) Return _ChisqINV(Random(1), $n) EndFunc #EndRegion chi-square distribution #Region logarithm normal distribution ; #FUNCTION# << LogNormPDF >> ========================================================== ; Name ..........: _LogNormPDF() ; Description ...: Returns the probability density of the Log-normal distribution. ; Syntax ........: _LogNormPDF($x [, $mu [, $sigma]]) ; Parameters ....: $x = value over which find the probability (number). ; $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $mu or $sigma is not a number. ; 2 = $sigma is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_LogNormPDF( 1 ) = " & _LogNormPDF(1) ) ; ====================================================================================== Func _LogNormPDF($x, $mu=0, $sigma=1) If Not IsNumber($x) Or Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma <= 0 Then Return SetError(2, 0, 0) Return Round(1/(Sqrt(2*$pi*$sigma^2)*$x)*Exp(-((Log($x)-$mu)^2)/(2*$sigma^2)), $Reps - 1) EndFunc ; #FUNCTION# << LogNormCDF >> ========================================================== ; Name ..........: _LogNormCDF() ; Description ...: Returns the cumulated probability distribution of the Log-normal distribution. ; Syntax ........: _LogNormCDF($x [, $mu [, $sigma]]) ; Parameters ....: $x = value over which find the probability distribution (number). ; $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the cumulated probability distribution value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $mu or $sigma is not a number. ; 2 = $sigma is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Statistical Functions", "_LogNormCDF( 2.5 ) = " & _LogNormCDF(2.5) ) ; ====================================================================================== Func _LogNormCDF($x, $mu=0, $sigma=1) If Not IsNumber($x) Or Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma <= 0 Then Return SetError(2, 0, 0) Return Round(0.5 + 0.5 * __erfcore((Log($x)-$mu)/Sqrt(2)*$sigma), $Reps - 1) EndFunc ; #FUNCTION# << LogNormINV >> ========================================================== ; Name ..........: _LogNormINV() ; Description ...: Returns the inverse cumulated probability distribution of the Log-normal distribution. ; Syntax ........: _LogNormINV($x [, $mu [, $sigma]]) ; Parameters ....: $x = value over which find the probability distribution (number). ; $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns the inverse cumulated probability distribution value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $mu or $sigma is not a number. ; 2 = $sigma is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Statistical Functions", "_LogNormINV( 0.9 ) = " & _LogNormINV(0.9) ) ; ====================================================================================== Func _LogNormINV($P, $mu=0, $sigma=1) If Not IsNumber($P) Or Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma <= 0 Or $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Return Round(Exp($mu + (__erfinv(($P * 2) - 1)*sqrt(2))*$sigma), $Reps - 1) EndFunc ; #FUNCTION# << LogNormMEAN >> ========================================================= ; Name ..........: _LogNormMEAN() ; Description ...: Returns the mean of the Log-normal distribution. ; Syntax ........: _LogNormMEAN([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Statistical Functions", "_LogNormMEAN( ) = " & _LogNormMEAN() ) ; ====================================================================================== Func _LogNormMEAN($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma <= 0 Then Return SetError(2, 0, 0) Return Exp($mu + 0.5 * $sigma^2) EndFunc ; #FUNCTION# << LogNormVAR >> ========================================================== ; Name ..........: _LogNormVAR() ; Description ...: Returns the variance of the Log-normal distribution. ; Syntax ........: _LogNormVAR([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Statistical Functions", "_LogNormVAR( ) = " & _LogNormVAR() ) ; ====================================================================================== Func _LogNormVAR($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma <= 0 Then Return SetError(2, 0, 0) Return Exp(2*$mu + $sigma^2)*(exp($sigma^2)-1) EndFunc ; #FUNCTION# << LogNormSKW >> ========================================================== ; Name ..........: _LogNormSKW() ; Description ...: Returns the skewness of the Log-normal distribution. ; Syntax ........: _LogNormSKW([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Statistical Functions", "_LogNormSKW( ) = " & _LogNormSKW() ) ; ====================================================================================== Func _LogNormSKW($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma <= 0 Then Return SetError(2, 0, 0) Return (Exp($sigma^2) + 2) * Sqrt(Exp($sigma^2) - 1) EndFunc ; #FUNCTION# << LogNormEXC >> ========================================================== ; Name ..........: _LogNormEXC() ; Description ...: Returns the excess of the Log-normal distribution. ; Syntax ........: _LogNormEXC([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Statistical Functions", "_LogNormEXC( ) = " & _LogNormEXC() ) ; ====================================================================================== Func _LogNormEXC($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma <= 0 Then Return SetError(2, 0, 0) Return Exp(4 * $sigma^2) + 2 * Exp(3 * $sigma^2) + 3 * Exp(2 * $sigma^2) - 6 EndFunc ; #FUNCTION# << LogNormRND >> ========================================================== ; Name ..........: _LogNormRND() ; Description ...: Returns the random number of the Log-normal distribution. ; Syntax ........: _LogNormRND([$mu [, $sigma]]) ; Parameters ....: $mu = the mean value (number). ; $sigma = the standard deviation value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $mu or $sigma is not a number. ; 2 = $sigma is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Statistical Functions", "_LogNormRND( ) = " & _LogNormRND() ) ; ====================================================================================== Func _LogNormRND($mu=0, $sigma=1) If Not IsNumber($mu) Or Not IsNumber($sigma) Then Return SetError(1, 0, 0) If $sigma <= 0 Then Return SetError(2, 0, 0) Return _LogNormINV(Random(1), $mu, $sigma) EndFunc #EndRegion logarithm normal distribution #Region exponential distribution ; #FUNCTION# << ExpPDF >> ============================================================== ; Name ..........: _ExpPDF() ; Description ...: Returns the probability density of the exponential distribution. ; Syntax ........: _ExpPDF($x [, $l]) ; Parameters ....: $x = value over which find the probability (number). ; $l = the rate value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $l is not a number. ; 2 = $x or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ExpPDF( 1 ) = " & _ExpPDF(1) ) ; ====================================================================================== Func _ExpPDF($x, $l=1) If Not IsNumber($x) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $l <= 0 Or $x < 0 Then Return SetError(2, 0, 0) Return Round($l * Exp(-$l*$x) , $Reps - 1) EndFunc ; #FUNCTION# << ExpCDF >> ============================================================== ; Name ..........: _ExpCDF() ; Description ...: Returns the cumulated probability density of the exponential distribution. ; Syntax ........: _ExpCDF($x [, $l]) ; Parameters ....: $x = value over which find the probability (number). ; $l = the rate value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $l is not a number. ; 2 = $x or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ExpCDF( 1 ) = " & _ExpCDF(1) ) ; ====================================================================================== Func _ExpCDF($x, $l=1) If Not IsNumber($x) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $l <= 0 Or $x < 0 Then Return SetError(2, 0, 0) Return Round(1 - Exp(-$l*$x) , $Reps - 1) EndFunc ; #FUNCTION# << ExpINV >> ============================================================== ; Name ..........: _ExpINV() ; Description ...: Returns the inverse cumulated probability density of the exponential distribution. ; Syntax ........: _ExpINV($P [, $l]) ; Parameters ....: $P = the qauntile value (number). ; $l = the rate value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P or $l is not a number. ; 2 = $P or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ExpINV( 0.5 ) = " & _ExpINV(0.5) ) ; ====================================================================================== Func _ExpINV($P, $l=1) If Not IsNumber($P) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $l <= 0 Or $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Return Round(Log(1 - $P) / (-$l), $Reps - 1) EndFunc ; #FUNCTION# << ExpMEAN >> ============================================================= ; Name ..........: _ExpMEAN() ; Description ...: Returns the mean of the exponential distribution. ; Syntax ........: _ExpMEAN([$l]) ; Parameters ....: $l = the rate value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $l is not a number. ; 2 = $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ExpMEAN( ) = " & _ExpMEAN() ) ; ====================================================================================== Func _ExpMEAN($l=1) If Not IsNumber($l) Then Return SetError(1, 0, 0) If $l <= 0 Then Return SetError(2, 0, 0) Return Round(1 / $l , $Reps - 1) EndFunc ; #FUNCTION# << ExpVAR >> ============================================================== ; Name ..........: _ExpVAR() ; Description ...: Returns the variance of the exponential distribution. ; Syntax ........: _ExpVAR([$l]) ; Parameters ....: $l = the rate value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $l is not a number. ; 2 = $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ExpVAR( ) = " & _ExpVAR() ) ; ====================================================================================== Func _ExpVAR($l=1) If Not IsNumber($l) Then Return SetError(1, 0, 0) If $l <= 0 Then Return SetError(2, 0, 0) Return Round(1 / $l^2 , $Reps - 1) EndFunc ; #FUNCTION# << ExpSKW >> ============================================================== ; Name ..........: _ExpSKW() ; Description ...: Returns the skewness of the exponential distribution. ; Syntax ........: _ExpSKW([$l]) ; Parameters ....: $l = the rate value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $l is not a number. ; 2 = $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ExpSKW( ) = " & _ExpSKW() ) ; ====================================================================================== Func _ExpSKW($l=1) If Not IsNumber($l) Then Return SetError(1, 0, 0) If $l <= 0 Then Return SetError(2, 0, 0) Return 2 EndFunc ; #FUNCTION# << ExpEXC >> ============================================================== ; Name ..........: _ExpEXC() ; Description ...: Returns the excess of the exponential distribution. ; Syntax ........: _ExpEXC([$l]) ; Parameters ....: $l = the rate value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $l is not a number. ; 2 = $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ExpEXC( ) = " & _ExpEXC() ) ; ====================================================================================== Func _ExpEXC($l=1) If Not IsNumber($l) Then Return SetError(1, 0, 0) If $l <= 0 Then Return SetError(2, 0, 0) Return 6 EndFunc ; #FUNCTION# << ExpRND >> ============================================================== ; Name ..........: _ExpRND() ; Description ...: Returns the random number of the exponential distribution. ; Syntax ........: _ExpRND([$l]) ; Parameters ....: $l = the rate value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $l is not a number. ; 2 = $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ExpRND( ) = " & _ExpRND() ) ; ====================================================================================== Func _ExpRND($l=1) If Not IsNumber($l) Then Return SetError(1, 0, 0) If $l <= 0 Then Return SetError(2, 0, 0) Return _ExpINV(Random(1), $l) EndFunc #EndRegion exponential distribution #Region fisher distribution ; #FUNCTION# << FPDF >> ================================================================ ; Name ..........: _FPDF() ; Description ...: Returns the probability density of the F-distribution. ; Syntax ........: _FPDF($x [, $m [, $n]]) ; Parameters ....: $x = value over which find the probability (number). ; $n = numerator degrees of freedom (number). ; $m = denumerator degrees of freedom (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $n or $m is not a number. ; 2 = $n or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_FPDF( 1 ) = " & _FPDF(1) ) ; ====================================================================================== Func _FPDF($x, $n=1, $m=1) If Not IsNumber($x) Or Not IsNumber($m) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $m <= 0 Or $n <= 0 Then Return SetError(2, 0, 0) If $x < 0 Then Return 0 Return Round(Exp(($m / 2) * Log($m / $n) + (($m / 2) - 1) * Log($x) + (-0.5 * ($m + $n)) * Log(1 + ($m * $x / $n)) + __gammaln(0.5 * ($m + $n)) - __gammaln($m / 2) - __gammaln($n / 2)), $Reps - 1) EndFunc ; #FUNCTION# << FCDF >> ================================================================ ; Name ..........: _FCDF() ; Description ...: Returns the cumulated probability density of the F-distribution. ; Syntax ........: _FCDF($x [, $m [, $n]]) ; Parameters ....: $x = value over which find the probability (number). ; $n = numerator degrees of freedom (number). ; $m = denumerator degrees of freedom (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $n or $m is not a number. ; 2 = $n or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_FCDF( 1 ) = " & _FCDF(1) ) ; ====================================================================================== Func _FCDF($x, $n=1, $m=1) If Not IsNumber($x) Or Not IsNumber($m) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $m <= 0 Or $n <= 0 Then Return SetError(2, 0, 0) If $x < 0 Then Return 0 Return Round(__betainc($n / ($n + ($m * $x)), $n / 2, $m / 2), $Reps - 1) EndFunc ; #FUNCTION# << FINV >> ================================================================ ; Name ..........: _FINV() ; Description ...: Returns the inverse cumulated probability density of the F-distribution. ; Syntax ........: _FINV($P [, $m [, $n]]) ; Parameters ....: $P = the quantile value (number). ; $n = numerator degrees of freedom (number). ; $m = denumerator degrees of freedom (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $n or $m is not a number. ; 2 = $n or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_FINV( 0.5 ) = " & _FINV(0.5) ) ; ====================================================================================== Func _FINV($P, $n=1, $m=1) If Not IsNumber($P) Or Not IsNumber($m) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $m <= 0 Or $n <= 0 Or $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Return Round((($n / __betainv(1 - $P, $m / 2, $n / 2)) - $n) * $m, $Reps - 1) EndFunc ; #FUNCTION# << FMEAN >> =============================================================== ; Name ..........: _FMEAN() ; Description ...: Returns the mean of the F-distribution. ; Syntax ........: _FMEAN([$m [, $n]]) ; Parameters ....: $n = numerator degrees of freedom (number). ; $m = denumerator degrees of freedom (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $m is not a number. ; 2 = $n or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_FMEAN( 1, 3 ) = " & _FMEAN(1,3) ) ; ====================================================================================== Func _FMEAN($n=1, $m=1) If Not IsNumber($m) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $m < 2 Or $n <= 0 Then Return SetError(2, 0, 0) Return $m / ($m - 2) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _FVAR() ; Description ...: Returns the variance of the F-distribution. ; Syntax ........: _FVAR([$m [, $n]]) ; Parameters ....: $n = numerator degrees of freedom (number). ; $m = denumerator degrees of freedom (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $m is not a number. ; 2 = $n or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_FVAR( 1, 5 ) = " & _FVAR(1,5) ) ; ====================================================================================== Func _FVAR($n=1, $m=1) If Not IsNumber($m) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $m < 4 Or $n <= 0 Then Return SetError(2, 0, 0) Return ((2 * $m^2) * ($n + $m - 2)) / ($n * ($m - 2)^2 * ($m - 4)) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _FSKW() ; Description ...: Returns the skewness of the F-distribution. ; Syntax ........: _FSKW([$m [, $n]]) ; Parameters ....: $n = numerator degrees of freedom (number). ; $m = denumerator degrees of freedom (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $m is not a number. ; 2 = $n or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_FSKW( 1, 7 ) = " & _FSKW(1,7) ) ; ====================================================================================== Func _FSKW($n=1, $m=1) If Not IsNumber($m) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $m < 6 Or $n <= 0 Then Return SetError(2, 0, 0) Return ((2 * $n + $m - 2) * Sqrt(8 * ($m - 4))) / (($m - 6) * Sqrt($n * ($n + $m - 2))) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _FEXC() ; Description ...: Returns the excess of the F-distribution. ; Syntax ........: _FEXC([$m [, $n]]) ; Parameters ....: $n = numerator degrees of freedom (number). ; $m = denumerator degrees of freedom (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $m is not a number. ; 2 = $n or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_FEXC( 1, 9 ) = " & _FEXC(1,9) ) ; ====================================================================================== Func _FEXC($n=1, $m=1) If Not IsNumber($m) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $m < 8 Or $n <= 0 Then Return SetError(2, 0, 0) Return 12 * (($n * (5 * $m - 22) * ($n + $m - 2)) + (($m - 4) * ($m - 2)^2)) / ($n * ($m - 6) * ($m - 8) * ($n + $m - 2)) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _FRND() ; Description ...: Returns the random number of the F-distribution. ; Syntax ........: _FRND([$m [, $n]]) ; Parameters ....: $n = numerator degrees of freedom (number). ; $m = denumerator degrees of freedom (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $m is not a number. ; 2 = $n or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_FRND( ) = " & _FRND() ) ; ====================================================================================== Func _FRND($n=1, $m=1) If Not IsNumber($m) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If $m <= 0 Or $n <= 0 Then Return SetError(2, 0, 0) Return _FINV(Random(1), $n, $m) EndFunc #EndRegion fisher distribution #Region weibull distribution ; #FUNCTION# << WblPDF >> ============================================================== ; Name ..........: _WblPDF() ; Description ...: Returns the probability density of the weibull distribution. ; Syntax ........: _WblPDF($x [, $lambda [, $k [, $l]]]) ; Parameters ....: $x = value over which find the probability (number). ; $lambda= the scale value (number). ; $k = the shape value (number). ; $l = the location value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $lambda, $k or $l is not a number. ; 2 = $x, $lambda, $k or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_WblPDF( 1 ) = " & _WblPDF(1) ) ; ====================================================================================== Func _WblPDF($x, $lambda=1, $k=1, $l=0) If Not IsNumber($x) Or Not IsNumber($lambda) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $x < 0 Or $lambda <= 0 Or $k <= 0 Then Return SetError(2, 0, 0) Return Round(($k / $lambda) * (($x - $l) / $lambda)^($k - 1) * Exp(-1*((($x - $l) / $lambda)^$k)) , $Reps - 1) EndFunc ; #FUNCTION# << WblCDF >> ============================================================== ; Name ..........: _WblCDF() ; Description ...: Returns the cumulated probability density of the weibull distribution. ; Syntax ........: _WblCDF($x [, $lambda [, $k [, $l]]]) ; Parameters ....: $x = value over which find the probability (number). ; $lambda= the scale value (number). ; $k = the shape value (number). ; $l = the location value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $lambda, $k or $l is not a number. ; 2 = $x, $lambda, $k or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_WblCDF( 1 ) = " & _WblCDF(1) ) ; ====================================================================================== Func _WblCDF($x, $lambda=1, $k=1, $l=0) If Not IsNumber($x) Or Not IsNumber($lambda) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $x < 0 Or $lambda <= 0 Or $k <= 0 Then Return SetError(2, 0, 0) Return Round(1 - Exp(-1*((($x - $l) / $lambda)^$k)) , $Reps - 1) EndFunc ; #FUNCTION# << WblINV >> ============================================================== ; Name ..........: _WblINV() ; Description ...: Returns the inverse cumulated probability density of the weibull distribution. ; Syntax ........: _WblINV($P [, $lambda [, $k [, $l]]]) ; Parameters ....: $P = the qunatile value (number). ; $lambda= the scale value (number). ; $k = the shape value (number). ; $l = the location value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $lambda, $k or $l is not a number. ; 2 = $P, $lambda, $k or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_WblINV( 0.5, 1, 2 ) = " & _WblINV(0.5,1,2) ) ; ====================================================================================== Func _WblINV($P, $lambda=1, $k=1, $l=0) If Not IsNumber($P) Or Not IsNumber($lambda) Or Not IsNumber($k) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $P < 0 Or $P > 1 Or $lambda <= 0 Or $k <= 0 Or $l < 0 Then Return SetError(2, 0, 0) Return Round($lambda * (-Log(1 - $P))^(1 / $k) + $l, $Reps - 1) EndFunc ; #FUNCTION# << WblMEAN >> ============================================================= ; Name ..........: _WblMEAN() ; Description ...: Returns the mean of the weibull distribution. ; Syntax ........: _WblMEAN([$lambda [, $k [, $l]]]) ; Parameters ....: $lambda= the scale value (number). ; $k = the shape value (number). ; $l = the location value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda, $k or $l is not a number. ; 2 = $lambda, $k or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_WblMEAN( ) = " & _WblMEAN() ) ; ====================================================================================== Func _WblMEAN($lambda=1, $k=1, $l=0) If Not IsNumber($lambda) Or Not IsNumber($k) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $lambda <= 0 Or $k <= 0 Or $l < 0 Then Return SetError(2, 0, 0) Return Round($l + Exp(Log($lambda) + __gammaln(1 + (1 / $k))), $Reps - 1) EndFunc ; #FUNCTION# << WblVAR >> ============================================================== ; Name ..........: _WblVAR() ; Description ...: Returns the variance of the weibull distribution. ; Syntax ........: _WblVAR([$lambda [, $k [, $l]]]) ; Parameters ....: $lambda= the scale value (number). ; $k = the shape value (number). ; $l = the location value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda, $k or $l is not a number. ; 2 = $lambda, $k or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_WblVAR( ) = " & _WblVAR() ) ; ====================================================================================== Func _WblVAR($lambda=1, $k=1, $l=0) If Not IsNumber($lambda) Or Not IsNumber($k) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $lambda <= 0 Or $k <= 0 Or $l < 0 Then Return SetError(2, 0, 0) Return Round(Exp(2 * Log($lambda) + __gammaln(1 + 2 / $k)) - Exp(2 * (Log($lambda) + __gammaln(1 + 1 / $k))), $Reps - 1) EndFunc ; #FUNCTION# << WblSKW >> ============================================================== ; Name ..........: _WblSKW() ; Description ...: Returns the skewness of the weibull distribution. ; Syntax ........: _WblSKW([$lambda [, $k [, $l]]]) ; Parameters ....: $lambda= the scale value (number). ; $k = the shape value (number). ; $l = the location value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda, $k or $l is not a number. ; 2 = $lambda, $k or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_WblSKW( ) = " & _WblSKW() ) ; ====================================================================================== Func _WblSKW($lambda=1, $k=1, $l=0) If Not IsNumber($lambda) Or Not IsNumber($k) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $lambda <= 0 Or $k <= 0 Or $l < 0 Then Return SetError(2, 0, 0) Local $mu = $l + Exp(Log($lambda) + __gammaln(1 + (1 / $k))) Local $sigma2 = Exp(2 * Log($lambda) + __gammaln(1 + 2 / $k)) - Exp(2 * (Log($lambda) + __gammaln(1 + 1 / $k))) Return Round((Exp(3 * Log($lambda) + __gammaln(1 + (3 / $k))) - 3 * $mu * $sigma2 - $mu^3) / $sigma2^1.5, $Reps - 1) EndFunc ; #FUNCTION# << WblEXC >> ============================================================== ; Name ..........: _WblEXC() ; Description ...: Returns the excess of the weibull distribution. ; Syntax ........: _WblEXC([$lambda [, $k [, $l]]]) ; Parameters ....: $lambda= the scale value (number). ; $k = the shape value (number). ; $l = the location value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda, $k or $l is not a number. ; 2 = $lambda, $k or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_WblEXC( ) = " & _WblEXC() ) ; ====================================================================================== Func _WblEXC($lambda=1, $k=1, $l=0) If Not IsNumber($lambda) Or Not IsNumber($k) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $lambda <= 0 Or $k <= 0 Or $l < 0 Then Return SetError(2, 0, 0) Local $mu = $l + Exp(Log($lambda) + __gammaln(1 + (1 / $k))) Local $sigma2 = Exp(2 * Log($lambda) + __gammaln(1 + 2 / $k)) - Exp(2 * (Log($lambda) + __gammaln(1 + 1 / $k))) Local $skew = (Exp(3 * Log($lambda) + __gammaln(1 + (3 / $k))) - 3 * $mu * $sigma2 - $mu^3) / $sigma2^1.5 Return Round(((Exp(4 * Log($lambda) + __gammaln(1 + (4 / $k))) - 4 * $skew * $mu * $sigma2^1.5 - 6 * $mu^2 * $sigma2 - $mu^4) / $sigma2^2) - 3, $Reps - 1) EndFunc ; #FUNCTION# << WblRND >> ============================================================== ; Name ..........: _WblRND() ; Description ...: Returns the random number of the weibull distribution. ; Syntax ........: _WblRND([$lambda [, $k [, $l]]]) ; Parameters ....: $lambda= the scale value (number). ; $k = the shape value (number). ; $l = the location value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda, $k or $l is not a number. ; 2 = $lambda, $k or $l is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_WblRND( ) = " & _WblRND() ) ; ====================================================================================== Func _WblRND($lambda=1, $k=1, $l=0) If Not IsNumber($lambda) Or Not IsNumber($k) Or Not IsNumber($l) Then Return SetError(1, 0, 0) If $lambda <= 0 Or $k <= 0 Or $l < 0 Then Return SetError(2, 0, 0) Return _WblINV(Random(1), $lambda, $k, $l) EndFunc #EndRegion weibull distribution #Region cauchy distribution ; #FUNCTION# << CauPDF >> ============================================================== ; Name ..........: _CauPDF() ; Description ...: Returns the probability density of the Cauchy–Lorentz distribution. ; Syntax ........: _CauPDF($x [, $m [, $s]]) ; Parameters ....: $x = value over which find the probability (number). ; $m = the location value (number). ; $s = the scale value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $m or $s is not a number. ; 2 = $s is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_CauPDF( 1 ) = " & _CauPDF(1) ) ; ====================================================================================== Func _CauPDF($x, $m=0, $s=1) If Not IsNumber($x) Or Not IsNumber($m) Or Not IsNumber($s) Then Return SetError(1, 0, 0) If $s <= 0 Then Return SetError(2, 0, 0) Return Round(1 / ($pi * $s * (1 + (($x - $m) / $s)^2)), $Reps - 1) EndFunc ; #FUNCTION# << CauCDF >> ============================================================== ; Name ..........: _CauCDF() ; Description ...: Returns the cumulated probability density of the Cauchy–Lorentz distribution. ; Syntax ........: _CauCDF($x [, $m [, $s]]) ; Parameters ....: $x = value over which find the probability (number). ; $m = the location value (number). ; $s = the scale value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $m or $s is not a number. ; 2 = $s is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_CauCDF( 0 ) = " & _CauCDF(0) ) ; ====================================================================================== Func _CauCDF($x, $m=0, $s=1) If Not IsNumber($x) Or Not IsNumber($m) Or Not IsNumber($s) Then Return SetError(1, 0, 0) If $s <= 0 Then Return SetError(2, 0, 0) Return Round(0.5 + ATan(($x - $m) / $s) / $pi, $Reps - 1) EndFunc ; #FUNCTION# << CauINV >> ============================================================== ; Name ..........: _CauINV() ; Description ...: Returns the inverse cumulated probability density of the Cauchy–Lorentz distribution. ; Syntax ........: _CauINV($x [, $m [, $s]]) ; Parameters ....: $P = the quantile value (number). ; $m = the location value (number). ; $s = the scale value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $m or $s is not a number. ; 2 = $s is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_CauINV( 0.75 ) = " & _CauINV(0.75) ) ; ====================================================================================== Func _CauINV($P, $m=0, $s=1) If Not IsNumber($P) Or Not IsNumber($m) Or Not IsNumber($s) Then Return SetError(1, 0, 0) If $s <= 0 Or $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Return Round(Tan(($P - 0.5) * $pi) * $s + $m, $Reps - 1) EndFunc ; #FUNCTION# << CauRND >> ============================================================== ; Name ..........: _CauRND() ; Description ...: Returns the random number of the Cauchy–Lorentz distribution. ; Syntax ........: _CauRND($x [, $m [, $s]]) ; Parameters ....: $m = the location value (number). ; $s = the scale value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $m or $s is not a number. ; 2 = $s is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_CauRND( ) = " & _CauRND() ) ; ====================================================================================== Func _CauRND($m=0, $s=1) If Not IsNumber($m) Or Not IsNumber($s) Then Return SetError(1, 0, 0) If $s <= 0 Then Return SetError(2, 0, 0) Return _CauINV(Random(1), $m, $s) EndFunc #EndRegion cauchy distribution #Region pareto distribution ; #FUNCTION# << ParPDF >> ============================================================== ; Name ..........: _ParPDF() ; Description ...: Returns the probability density of the pareto distribution. ; Syntax ........: _ParPDF($x [, $m [, $k]]) ; Parameters ....: $x = value over which find the probability (number). ; $m = the minimum value (number). ; $k = the shape value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $m or $k is not a number. ; 2 = $x, $m or $k is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ParPDF( 1 ) = " & _ParPDF(1) ) ; ====================================================================================== Func _ParPDF($x, $m=1, $k=1) If Not IsNumber($x) Or Not IsNumber($m) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $x < 0 Or $m <= 0 Or $k <= 0 Then Return SetError(2, 0, 0) If $x < $m Then Return 0 Return Round($k * $m^$k / $x^($k + 1), $Reps - 1) EndFunc ; #FUNCTION# << ParCDF >> ============================================================== ; Name ..........: _ParCDF() ; Description ...: Returns the cumulated probability density of the pareto distribution. ; Syntax ........: _ParCDF($x [, $m [, $k]]) ; Parameters ....: $x = value over which find the probability (number). ; $m = the minimum value (number). ; $k = the shape value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $m or $k is not a number. ; 2 = $x, $m or $k is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ParCDF( 4 ) = " & _ParCDF(4) ) ; ====================================================================================== Func _ParCDF($x, $m=1, $k=1) If Not IsNumber($x) Or Not IsNumber($m) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $x < 0 Or $m <= 0 Or $k <= 0 Then Return SetError(2, 0, 0) If $x < $m Then Return 0 Return Round(1 - ($m / $x)^$k, $Reps - 1) EndFunc ; #FUNCTION# << ParINV >> ============================================================== ; Name ..........: _ParINV() ; Description ...: Returns the inverse cumulated probability density of the pareto distribution. ; Syntax ........: _ParINV($x [, $m [, $k]]) ; Parameters ....: $P = value over which find the probability (number). ; $m = the minimum value (number). ; $k = the shape value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $m or $k is not a number. ; 2 = $P, $m or $k is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ParINV( 0.75 ) = " & _ParINV(0.75) ) ; ====================================================================================== Func _ParINV($P, $m=1, $k=1) If Not IsNumber($P) Or Not IsNumber($m) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $m <= 0 Or $k <= 0 Or $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Return $m / ((1 - $P)^(1 / $k)) EndFunc ; #FUNCTION# << ParMEAN >> ============================================================= ; Name ..........: _ParMEAN() ; Description ...: Returns the mean of the pareto distribution. ; Syntax ........: _ParMEAN([$m [, $k]]) ; Parameters ....: $m = the minimum value (number). ; $k = the shape value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $m or $k is not a number. ; 2 = $m or $k is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ParMEAN( 1, 5 ) = " & _ParMEAN(1,5) ) ; ====================================================================================== Func _ParMEAN($m=1, $k=1) If Not IsNumber($m) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $m <= 0 Or $k <= 0 Then Return SetError(2, 0, 0) If $k <= 1 Then Return 1/0 Return Round(($k * $m) / ($k - 1), $Reps - 1) EndFunc ; #FUNCTION# << ParVAR >> ============================================================== ; Name ..........: _ParVAR() ; Description ...: Returns the variance of the pareto distribution. ; Syntax ........: _ParVAR([$m [, $k]]) ; Parameters ....: $m = the minimum value (number). ; $k = the shape value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $m or $k is not a number. ; 2 = $m or $k is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ParVAR( 1, 5 ) = " & _ParVAR(1,5) ) ; ====================================================================================== Func _ParVAR($m=1, $k=1) If Not IsNumber($m) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $m <= 0 Or $k <= 1 Then Return SetError(2, 0, 0) If $k > 1 And $k <= 2 Then Return 1/0 Return Round(($m / ($k - 1))^2 * ($k / ($k - 2)), $Reps - 1) EndFunc ; #FUNCTION# << ParSKW >> ============================================================== ; Name ..........: _ParSKW() ; Description ...: Returns the skewness of the pareto distribution. ; Syntax ........: _ParSKW([$m [, $k]]) ; Parameters ....: $m = the minimum value (number). ; $k = the shape value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $m or $k is not a number. ; 2 = $m or $k is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ParSKW( 1, 5 ) = " & _ParSKW(1,5) ) ; ====================================================================================== Func _ParSKW($m=1, $k=1) If Not IsNumber($m) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $m <= 0 Or $k <= 3 Then Return SetError(2, 0, 0) Return Round(((2 * (1 + $k)) / ($k - 3)) * Sqrt(($k - 2) / $k), $Reps - 1) EndFunc ; #FUNCTION# << ParEXC >> ============================================================== ; Name ..........: _ParEXC() ; Description ...: Returns the excess of the pareto distribution. ; Syntax ........: _ParEXC([$m [, $k]]) ; Parameters ....: $m = the minimum value (number). ; $k = the shape value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $m or $k is not a number. ; 2 = $m or $k is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ParEXC( 1, 5 ) = " & _ParEXC(1,5) ) ; ====================================================================================== Func _ParEXC($m=1, $k=1) If Not IsNumber($m) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $m <= 0 Or $k <= 4 Then Return SetError(2, 0, 0) Return Round(6 * ($k^3 + $k^2 - 6*$k - 2) / ($k * ($k - 3) * ($k - 4)), $Reps - 1) EndFunc ; #FUNCTION# << ParRND >> ============================================================== ; Name ..........: _ParRND() ; Description ...: Returns the random number of the pareto distribution. ; Syntax ........: _ParRND([$m [, $k]]) ; Parameters ....: $m = the minimum value (number). ; $k = the shape value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $m or $k is not a number. ; 2 = $m or $k is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Continuous Distribution Functions", "_ParRND( 1, 5 ) = " & _ParRND(1,5) ) ; ====================================================================================== Func _ParRND($m=1, $k=1) If Not IsNumber($m) Or Not IsNumber($k) Then Return SetError(1, 0, 0) If $m <= 0 Or $k <= 0 Then Return SetError(2, 0, 0) Return _ParINV(Random(1), $m, $k) EndFunc #EndRegion pareto distribution #EndRegion Continuous Distribution Functions #Region Discrete Distribution Functions #Region discrete uniform distribution ; #FUNCTION# << UniPDF >> ============================================================== ; Name ..........: _UniPDF() ; Description ...: Returns the probability mass of the discrete uniform distribution. ; Syntax ........: _UniPDF($x [, $n [, $x0]]) ; Parameters ....: $x = value over which find the probability (number). ; $n = the range value (number). ; $x0 = the start value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $n or $x0 is not a number. ; 2 = $x, $n or $x0 is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_UniPDF( 1, 6, 1 ) = " & _UniPDF(1,6,1) ) ; ====================================================================================== Func _UniPDF($x, $n=1, $x0=0) If Not IsNumber($x) Or Not IsNumber($n) Or Not IsNumber($x0) Then Return SetError(1, 0, 0) If ($x < 0 And $x <> Int($x)) Or ($n < 1 And $n <> Int($n)) Or ($x0 <> Int($x0)) Then Return SetError(2, 0, 0) Return Round(1 / $n, $Reps - 1) EndFunc ; #FUNCTION# << UniCDF >> ============================================================== ; Name ..........: _UniCDF() ; Description ...: Returns the cumulated probability distribution of the discrete uniform distribution. ; Syntax ........: _UniCDF($x [, $n [, $x0]]) ; Parameters ....: $x = value over which find the probability (number). ; $n = the range value (number). ; $x0 = the start value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $n or $x0 is not a number. ; 2 = $x, $n or $x0 is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_UniCDF( 3, 6, 1 ) = " & _UniCDF(3,6,1) ) ; ====================================================================================== Func _UniCDF($x, $n=1, $x0=0) If Not IsNumber($x) Or Not IsNumber($n) Or Not IsNumber($x0) Then Return SetError(1, 0, 0) If ($x < 0 And $x <> Int($x)) Or ($n < 1 And $n <> Int($n)) Or ($x0 <> Int($x0)) Then Return SetError(2, 0, 0) Return Round((Floor($x) - $x0 + 1) / ($n), $Reps - 1) EndFunc ; #FUNCTION# << UniINV >> ============================================================== ; Name ..........: _UniINV() ; Description ...: Returns the inverse cumulated probability distribution of the discrete uniform distribution. ; Syntax ........: _UniINV($P [, $n [, $x0]]) ; Parameters ....: $P = the quantile value (number). ; $n = the range value (number). ; $x0 = the start value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $P, $n or $x0 is not a number. ; 2 = $P, $n or $x0 is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_UniINV( 0.55, 6, 1 ) = " & _UniINV(0.55,6,1) ) ; ====================================================================================== Func _UniINV($P, $n=1, $x0=0) If Not IsNumber($P) Or Not IsNumber($n) Or Not IsNumber($x0) Then Return SetError(1, 0, 0) If ($P < 0 Or $P > 1) Or ($n < 1 And $n <> Int($n)) Or ($x0 <> Int($x0)) Then Return SetError(2, 0 ,0) Return Round(Ceiling(($P * $n) - 1 + $x0), $Reps - 1) EndFunc ; #FUNCTION# << UniMEAN >> ============================================================= ; Name ..........: _UniMEAN() ; Description ...: Returns the mean of the discrete uniform distribution. ; Syntax ........: _UniMEAN([$n [, $x0]]) ; Parameters ....: $n = the range value (number). ; $x0 = the start value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $x0 is not a number. ; 2 = $n or $x0 is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_UniMEAN( 6, 1 ) = " & _UniMEAN(6,1) ) ; ====================================================================================== Func _UniMEAN($n=1, $x0=0) If Not IsNumber($n) Or Not IsNumber($x0) Then Return SetError(1, 0, 0) If ($n < 1 And $n <> Int($n)) Or ($x0 <> Int($x0)) Then Return SetError(2, 0, 0) Return Round((2 * $x0 + $n - 1) / 2, $Reps - 1) EndFunc ; #FUNCTION# << UniVAR >> ============================================================== ; Name ..........: _UniVAR() ; Description ...: Returns the variance of the discrete uniform distribution. ; Syntax ........: _UniVAR([$n [, $x0]]) ; Parameters ....: $n = the range value (number). ; $x0 = the start value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $x0 is not a number. ; 2 = $n or $x0 is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_UniVAR( 6, 1 ) = " & _UniVAR(6,1) ) ; ====================================================================================== Func _UniVAR($n=1, $x0=0) If Not IsNumber($n) Or Not IsNumber($x0) Then Return SetError(1, 0, 0) If ($n < 1 And $n <> Int($n)) Or ($x0 <> Int($x0)) Then Return SetError(2, 0, 0) Return Round(($n^2 - 1) / 12, $Reps - 1) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _UniSKW() ; Description ...: Returns the skewness of the discrete uniform distribution. ; Syntax ........: _UniSKW([$n [, $x0]]) ; Parameters ....: $n = the range value (number). ; $x0 = the start value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $x0 is not a number. ; 2 = $n or $x0 is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_UniSKW( 6, 1 ) = " & _UniSKW(6,1) ) ; ====================================================================================== Func _UniSKW($n=1, $x0=0) If Not IsNumber($n) Or Not IsNumber($x0) Then Return SetError(1, 0, 0) If ($n < 1 And $n <> Int($n)) Or ($x0 <> Int($x0)) Then Return SetError(2, 0, 0) Return 0 EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _UniEXC() ; Description ...: Returns the excess of the discrete uniform distribution. ; Syntax ........: _UniEXC([$n [, $x0]]) ; Parameters ....: $n = the range value (number). ; $x0 = the start value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $x0 is not a number. ; 2 = $n or $x0 is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_UniEXC( 6, 1 ) = " & _UniEXC(6,1) ) ; ====================================================================================== Func _UniEXC($n=1, $x0=0) If Not IsNumber($n) Or Not IsNumber($x0) Then Return SetError(1, 0, 0) If ($n < 1 And $n <> Int($n)) Or ($x0 <> Int($x0)) Then Return SetError(2, 0, 0) Return Round((-6 * ($n^2 + 1)) / (5 * ($n^2 - 1)), $Reps - 1) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _UniRND() ; Description ...: Returns the random number of the discrete uniform distribution. ; Syntax ........: _UniRND([$n [, $x0]]) ; Parameters ....: $n = the range value (number). ; $x0 = the start value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $x0 is not a number. ; 2 = $n or $x0 is outside the domain or not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_UniRND( 6, 1 ) = " & _UniRND(6,1) ) ; ====================================================================================== Func _UniRND($n=1, $x0=0) If Not IsNumber($n) Or Not IsNumber($x0) Then Return SetError(1, 0, 0) If ($n < 1 And $n <> Int($n)) Or ($x0 <> Int($x0)) Then Return SetError(2, 0, 0) Return _UniINV(Random(1), $n, $x0) EndFunc #EndRegion discrete uniform distribution #Region bernoulli distribution ; #FUNCTION# << BerPDF >> ============================================================== ; Name ..........: _BerPDF() ; Description ...: Returns the probability density of the bernoulli distribution. ; Syntax ........: _BerPDF($x [, $p ]) ; Parameters ....: $x = value over which find the probability (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $p is not a number. ; 2 = $x or $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BerPDF( 0 , 0.2 ) = " & _BerPDF(0,0.2) ) ; ====================================================================================== Func _BerPDF($x, $p=0.5) If Not IsNumber($x) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If ($x <> 0 And $x <> 1) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) If $x = 0 Then Return Round(1 - $p, $Reps - 1) Return Round($p, $Reps - 1) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _BerCDF() ; Description ...: Returns the cumulated probability distribution of the bernoulli distribution. ; Syntax ........: _BerCDF($x [, $p ]) ; Parameters ....: $x = value over which find the probability (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $p is not a number. ; 2 = $x or $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BerCDF( 1 , 0.2 ) = " & _BerCDF(1,0.2) ) ; ====================================================================================== Func _BerCDF($x, $p=0.5) If Not IsNumber($x) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If ($x <> 0 And $x <> 1) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) If $x >= 0 And $x < 1 Then Return Round(1 - $p, $Reps - 1) If $x >= 1 Then Return 1 Return 0 EndFunc ; #FUNCTION# << BerINV >> ============================================================== ; Name ..........: _BerINV() ; Description ...: Returns the inverse cumulated probability distribution of the bernoulli distribution. ; Syntax ........: _BerINV($Y [, $p ]) ; Parameters ....: $Y = the quantile value (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $Y or $p is not a number. ; 2 = $Y or $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BerINV( 0.8 , 0.2 ) = " & _BerINV(0.8,0.2) ) ; ====================================================================================== Func _BerINV($y, $p=0.5) If Not IsNumber($y) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Or $y < 0 Or $y > 1 Then Return SetError(2, 0, 0) If $y > (1 - $p) then Return 1 Return 0 EndFunc ; #FUNCTION# << BerMEAN >> ============================================================= ; Name ..........: _BerMEAN() ; Description ...: Returns the mean of the bernoulli distribution. ; Syntax ........: _BerMEAN([ $p ]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BerMEAN( ) = " & _BerMEAN() ) ; ====================================================================================== Func _BerMEAN($p=0.5) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round($p, $Reps - 1) EndFunc ; #FUNCTION# << BerVAR >> ============================================================== ; Name ..........: _BerVAR() ; Description ...: Returns the variance of the bernoulli distribution. ; Syntax ........: _BerVAR([ $p ]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BerVAR( ) = " & _BerVAR() ) ; ====================================================================================== Func _BerVAR($p=0.5) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round($p * (1 - $p), $Reps - 1) EndFunc ; #FUNCTION# << BerSKW >> ============================================================== ; Name ..........: _BerSKW() ; Description ...: Returns the skewness of the bernoulli distribution. ; Syntax ........: _BerSKW([ $p ]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BerSKW( ) = " & _BerSKW() ) ; ====================================================================================== Func _BerSKW($p=0.5) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round((1 - 2 * $p) / Sqrt($p * (1 - $p)), $Reps - 1) EndFunc ; #FUNCTION# << BerEXC >> ============================================================== ; Name ..........: _BerEXC() ; Description ...: Returns the excess of the bernoulli distribution. ; Syntax ........: _BerEXC([ $p ]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BerEXC( 0.2) = " & _BerEXC(0.2) ) ; ====================================================================================== Func _BerEXC($p=0.5) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round((1 - Round((6 * $p * (1 - $p)), $Reps - 1)) / ($p * (1 - $p)), $Reps - 1) EndFunc ; #FUNCTION# << BerRND >> ============================================================== ; Name ..........: _BerRND() ; Description ...: Returns the random number of the bernoulli distribution. ; Syntax ........: _BerRND([ $p ]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BerRND( ) = " & _BerRND() ) ; ====================================================================================== Func _BerRND($p=0.5) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return _BerINV(Random(1), $p) EndFunc #EndRegion bernoulli distribution #Region binomial distribution ; #FUNCTION# << BinoPDF >> ============================================================= ; Name ..........: _BinoPDF() ; Description ...: Returns the probability density of the binomial distribution. ; Syntax ........: _BinoPDF($x [, $n [, $p]]) ; Parameters ....: $x = value over which find the probability (number). ; $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $n or $p is not a number. ; 2 = $x, $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BinoPDF( 1 ) = " & _BinoPDF(1) ) ; ====================================================================================== Func _BinoPDF($x, $n=10, $p=0.5) If Not IsNumber($x) Or Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $x < 0 Or $x > $n Or $x <> Int($x) Or $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(Exp($x * Log($p) + ($n - $x) * Log(1 - $p) + __gammaln($n + 1) - __gammaln($x + 1) - __gammaln($n - $x + 1)), $Reps - 1) EndFunc ; #FUNCTION# << BinoCDF >> ============================================================= ; Name ..........: _BinoCDF() ; Description ...: Returns the cumulated probability distribution of the binomial distribution. ; Syntax ........: _BinoCDF($x [, $n [, $p]]) ; Parameters ....: $x = value over which find the probability (number). ; $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $n or $p is not a number. ; 2 = $x, $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BinoCDF( 1 ) = " & _BinoCDF(1) ) ; ====================================================================================== Func _BinoCDF($x, $n=10, $p=0.5) If Not IsNumber($x) Or Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $x < 0 Or $x > $n Or $x <> Int($x) Or $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(__betainc(1 - $p, $n - $x, $x + 1), $Reps - 1) EndFunc ; #FUNCTION# << BinoINV >> ============================================================= ; Name ..........: _BinoINV() ; Description ...: Returns the inverse cumulated probability distribution of the binomial distribution. ; Syntax ........: _BinoINV($Y [, $n [, $p]]) ; Parameters ....: $Y = the quantile value (number). ; $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $Y, $n or $p is not a number. ; 2 = $Y, $n or $p is outside the domain or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BinoINV( 0.5 ) = " & _BinoINV(0.5)) ; ====================================================================================== Func _BinoINV($y, $n=10, $p=0.5) If Not IsNumber($y) Or Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $y < 0 Or $y > 1 Or $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Local $gnp1 = __gammaln($n + 1) Local $lp = Log($p) Local $l1mp = Log(1 - $p) Local $cumdist = Exp($n * Log(1 - $p)) Local $x = 0 If $y = 1 Then Return $n While ($y > $cumdist) $x += 1 $cumdist += Exp($x * $lp + ($n - $x) * $l1mp + $gnp1 - __gammaln($x + 1) - __gammaln($n - $x + 1)) WEnd Return Round($x, $Reps - 1) EndFunc ; #FUNCTION# << BinoMEAN >> ============================================================ ; Name ..........: _BinoMEAN() ; Description ...: Returns the mean of the binomial distribution. ; Syntax ........: _BinoMEAN([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BinoMEAN( ) = " & _BinoMEAN() ) ; ====================================================================================== Func _BinoMEAN($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round($n * $p, $Reps - 1) EndFunc ; #FUNCTION# << BinoVAR >> ============================================================= ; Name ..........: _BinoVAR() ; Description ...: Returns the variance of the binomial distribution. ; Syntax ........: _BinoVAR([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BinoVAR( ) = " & _BinoVAR() ) ; ====================================================================================== Func _BinoVAR($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round($n * $p * (1 - $p), $Reps - 1) EndFunc ; #FUNCTION# << BinoSKW >> ============================================================= ; Name ..........: _BinoSKW() ; Description ...: Returns the skewness of the binomial distribution. ; Syntax ........: _BinoSKW([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BinoSKW( ) = " & _BinoSKW() ) ; ====================================================================================== Func _BinoSKW($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round((1 - 2 * $p) / (Sqrt($n * $p * (1 - $p))), $Reps - 1) EndFunc ; #FUNCTION# << BinoEXC >> ============================================================= ; Name ..........: _BinoEXC() ; Description ...: Returns the excess of the binomial distribution. ; Syntax ........: _BinoEXC([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BinoEXC( ) = " & _BinoEXC() ) ; ====================================================================================== Func _BinoEXC($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round((1 - 6 * $p * (1 - $p)) / ($n * $p * (1 - $p)), $Reps - 1) EndFunc ; #FUNCTION# << BinoRND >> ============================================================= ; Name ..........: _BinoRND() ; Description ...: Returns the random number of the binomial distribution. ; Syntax ........: _BinoRND([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_BinoRND( ) = " & _BinoRND() ) ; ====================================================================================== Func _BinoRND($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return _BinoINV(Random(1), $n, $p) EndFunc #EndRegion binomial distribution #Region negative binomial distribution ; #FUNCTION# << NBinoPDF >> ============================================================ ; Name ..........: _NBinoPDF() ; Description ...: Returns the probability density of the negative binomial distribution. ; Syntax ........: _NBinoPDF($x [, $n [, $p]]) ; Parameters ....: $x = value over which find the probability (number). ; $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $n or $p is not a number. ; 2 = $x, $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_NBinoPDF( 1 ) = " & _NBinoPDF(1) ) ; ====================================================================================== Func _NBinoPDF($x, $n=10, $p=0.5) If Not IsNumber($x) Or Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $x < 0 Or $x <> Int($x) Or $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(Exp($n * Log($p) + $x * Log(1 - $p) + __gammaln($n + $x) - __gammaln($n) - __gammaln($x+1)), $Reps - 1) EndFunc ; #FUNCTION# << NBinoCDF >> ============================================================ ; Name ..........: _NBinoCDF() ; Description ...: Returns the cumulated probability distribution of the negative binomial distribution. ; Syntax ........: _NBinoCDF($x [, $n [, $p]]) ; Parameters ....: $x = value over which find the probability (number). ; $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $n or $p is not a number. ; 2 = $x, $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_NBinoCDF( 5 ) = " & _NBinoCDF(5) ) ; ====================================================================================== Func _NBinoCDF($x, $n=10, $p=0.5) If Not IsNumber($x) Or Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $x < 0 Or $x <> Int($x) Or $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(__betainc($p, $n, $x + 1), $Reps - 1) EndFunc ; #FUNCTION# << NBinoINV >> ============================================================ ; Name ..........: _NBinoINV() ; Description ...: Returns the inverse cumulated probability distribution of the negative binomial distribution. ; Syntax ........: _NBinoINV($Y [, $n [, $p]]) ; Parameters ....: $Y = value over which find the probability (number). ; $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $Y, $n or $p is not a number. ; 2 = $Y, $n or $p is outside the domain or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_NBinoINV( ~1 ) = " & _NBinoINV(1-$meps) ) ; ====================================================================================== Func _NBinoINV($y, $n=10, $p=0.5) If Not IsNumber($y) Or Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $y < 0 Or $y > 1 Or $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Local $gn = __gammaln($n) Local $nlp = $n * Log($p) Local $l1mp = Log(1 - $p) Local $cumdist = Exp($nlp) Local $x = 0, $c = 0 If $y = 1 Then Return 1/0 While ($y > $cumdist) And Abs($c - $cumdist) > $meps $c = $cumdist $x += 1 $cumdist += Exp($nlp + $x * $l1mp + __gammaln($n + $x) - $gn - __gammaln($x + 1)) WEnd Return Round($x, $Reps - 1) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _NBinoMEAN() ; Description ...: Returns the mean of the negative binomial distribution. ; Syntax ........: _NBinoMEAN([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_NBinoMEAN( ) = " & _NBinoMEAN() ) ; ====================================================================================== Func _NBinoMEAN($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round($n * (1 - $p) / $p, $Reps - 1) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _NBinoVAR() ; Description ...: Returns the variance of the negative binomial distribution. ; Syntax ........: _NBinoVAR([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_NBinoVAR( ) = " & _NBinoVAR() ) ; ====================================================================================== Func _NBinoVAR($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round($n * (1 - $p) / $p^2, $Reps - 1) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _NBinoSKW() ; Description ...: Returns the skewness of the negative binomial distribution. ; Syntax ........: _NBinoSKW([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_NBinoSKW( ) = " & _NBinoSKW() ) ; ====================================================================================== Func _NBinoSKW($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round((2 - $p) / (Sqrt($n * (1 - $p))), $Reps - 1) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _NBinoEXC() ; Description ...: Returns the excess of the negative binomial distribution. ; Syntax ........: _NBinoEXC([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_NBinoEXC( ) = " & _NBinoEXC() ) ; ====================================================================================== Func _NBinoEXC($n=10, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(($p^2 - 6 * $p + 6) / ($n * (1 - $p)), $Reps - 1) EndFunc ; #FUNCTION# =========================================================================== ; Name ..........: _NBinoRND() ; Description ...: Returns the random number of the negative binomial distribution. ; Syntax ........: _NBinoRND([$n [, $p]]) ; Parameters ....: $n = the number of trials (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $n or $p is not a number. ; 2 = $n or $p is outside the domain or $x or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_NBinoRND( ) = " & _NBinoRND() ) ; ====================================================================================== Func _NBinoRND($n=50, $p=0.5) If Not IsNumber($n) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return _NBinoINV(Random(1), $n, $p) EndFunc #EndRegion negative binomial distribution #Region hypergeometric distribution ; #FUNCTION# << HygePDF >> ============================================================= ; Name ..........: _HygePDF() ; Description ...: Returns the probability density of the hypergeometric distribution. ; Syntax ........: _HygePDF($x [, $M [, $K [, N]]]) ; Parameters ....: $x = value over which find the probability (number). ; $M = the number of trials (number). ; $K = the number of successes (number). ; $N = the population size (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $M, $K or $N is not a number. ; 2 = $x, $M, $K or $N is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_HygePDF( 15 ) = " & _HygePDF(15) ) ; ====================================================================================== Func _HygePDF($x, $M=25, $K=30, $N=50) If Not IsNumber($x) Or Not IsNumber($M) Or Not IsNumber($K) Or Not IsNumber($N) Then Return SetError(1, 0, 0) If $x <> Int($x) Or $M <> Int($M) Or $K <> Int($K) Or $N <> Int($N) Then Return SetError(2, 0, 0) If $x < $K - ($N - $M) Or $x > $K Or $M > $N Or $K > $N Then Return SetError(2, 0, 0) Return Round(Exp(__gammaln($M+1)-__gammaln($x+1)-__gammaln($M-$x+1)+__gammaln($N-$M+1)-__gammaln($K-$x+1)-__gammaln($N-$M-$K+$x+1)-__gammaln($N+1)+__gammaln($K+1)+__gammaln($N-$K+1)), $Reps - 1) EndFunc ; #FUNCTION# << HygeCDF >> ============================================================= ; Name ..........: _HygeCDF() ; Description ...: Returns the cumulated probability distribution of the hypergeometric distribution. ; Syntax ........: _HygeCDF($x [, $M [, $K [, N]]]) ; Parameters ....: $x = value over which find the probability (number). ; $M = the number of trials (number). ; $K = the number of successes (number). ; $N = the population size (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x, $M, $K or $N is not a number. ; 2 = $x, $M, $K or $N is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_HygeCDF( 15 ) = " & _HygeCDF(15) ) ; ====================================================================================== Func _HygeCDF($x, $M=25, $K=30, $N=50) If Not IsNumber($x) Or Not IsNumber($M) Or Not IsNumber($K) Or Not IsNumber($N) Then Return SetError(1, 0, 0) If $x <> Int($x) Or $M <> Int($M) Or $K <> Int($K) Or $N <> Int($N) Then Return SetError(2, 0, 0) If $x < $K - ($N - $M) Or $x > $K Or $M > $N Or $K > $N Then Return SetError(2, 0, 0) If $x >= $M or $x >= $K Then Return 1 Else Local $sum Local $NoverK = __gammaln($N+1)-__gammaln($K+1)-__gammaln($N-$K+1) Local $limit = ($N - $M) + Ceiling(($K - ($N - $M)) / 2) Local $gMp1 = __gammaln($M+1) Local $gNmMp1 = __gammaln($N-$M+1) If $x >= $limit Then $sum = 1 For $i = $K to $x+1 step -1 $sum -= Exp($gMp1-__gammaln($i+1)-__gammaln($M-$i+1)+$gNmMp1-__gammaln($K-$i+1)-__gammaln($N-$M-$K+$i+1)-$NoverK) Next Else $sum = 0 For $i = $K - ($N - $M) To $x $sum += Exp($gMp1-__gammaln($i+1)-__gammaln($M-$i+1)+$gNmMp1-__gammaln($K-$i+1)-__gammaln($N-$M-$K+$i+1)-$NoverK) Next EndIf Return Round($sum, $Reps - 1) EndIf EndFunc ; #FUNCTION# << HygeINV >> ============================================================= ; Name ..........: _HygeINV() ; Description ...: Returns the inverse cumulated probability distribution of the hypergeometric distribution. ; Syntax ........: _HygeCDF($Y [, $M [, $K [, N]]]) ; Parameters ....: $Y = the qunatile value (number). ; $M = the number of trials (number). ; $K = the number of successes (number). ; $N = the population size (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $Y, $M, $K or $N is not a number. ; 2 = $Y, $M, $K or $N is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_HygeINV( 0.5 ) = " & _HygeINV(0.5) ) ; ====================================================================================== Func _HygeINV($y, $M=25, $K=30, $N=50) If Not IsNumber($y) Or Not IsNumber($M) Or Not IsNumber($K) Or Not IsNumber($N) Then Return SetError(1, 0, 0) If $M <> Int($M) Or $K <> Int($K) Or $N <> Int($N) Then Return SetError(2, 0, 0) If $y < 0 Or $y > 1 Or $M > $N Or $K > $N Then Return SetError(2, 0, 0) If $y = 1 Then Return $M Local $NoverK = __gammaln($N+1)-__gammaln($K+1)-__gammaln($N-$K+1) Local $x = $K - ($N - $M) Local $gMp1 = __gammaln($M+1) Local $gNmMp1 = __gammaln($N-$M+1) Local $cumdist = Exp($gMp1-__gammaln($x+1)-__gammaln($M-$x+1)+$gNmMp1-__gammaln($K-$x+1)-__gammaln($N-$M-$K+$x+1)-$NoverK) While ($y > $cumdist And $x <= $M) $x += 1 $cumdist += Exp($gMp1-__gammaln($x+1)-__gammaln($M-$x+1)+$gNmMp1-__gammaln($K-$x+1)-__gammaln($N-$M-$K+$x+1)-$NoverK) WEnd Return Round($x, $Reps - 1) EndFunc ; #FUNCTION# << HygeMEAN >> ============================================================ ; Name ..........: _HygeMEAN() ; Description ...: Returns the mean of the hypergeometric distribution. ; Syntax ........: _HygeMEAN([$M [, $K [, N]]]) ; Parameters ....: $M = the number of trials (number). ; $K = the number of successes (number). ; $N = the population size (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $M, $K or $N is not a number. ; 2 = $M, $K or $N is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_HygeMEAN( ) = " & _HygeMEAN() ) ; ====================================================================================== Func _HygeMEAN($M=25, $K=30, $N=50) If Not IsNumber($M) Or Not IsNumber($K) Or Not IsNumber($N) Then Return SetError(1, 0, 0) If $M <> Int($M) Or $K <> Int($K) Or $N <> Int($N) Then Return SetError(2, 0, 0) If $M > $N Or $K > $N Then Return SetError(2, 0, 0) Return Round($M * $K / $N, $Reps - 1) EndFunc ; #FUNCTION# << HygeVAR>> ============================================================== ; Name ..........: _HygeVAR() ; Description ...: Returns the variance of the hypergeometric distribution. ; Syntax ........: _HygeVAR([$M [, $K [, N]]]) ; Parameters ....: $M = the number of trials (number). ; $K = the number of successes (number). ; $N = the population size (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $M, $K or $N is not a number. ; 2 = $M, $K or $N is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_HygeVAR( ) = " & _HygeVAR() ) ; ====================================================================================== Func _HygeVAR($M=25, $K=30, $N=50) If Not IsNumber($M) Or Not IsNumber($K) Or Not IsNumber($N) Then Return SetError(1, 0, 0) If $M <> Int($M) Or $K <> Int($K) Or $N <> Int($N) Then Return SetError(2, 0, 0) If $M > $N Or $K > $N Then Return SetError(2, 0, 0) Return Round(($K * $M * ($N - $M) * (1 - ($K / $N))) / ($N * ($N - 1)), $Reps - 1) EndFunc ; #FUNCTION# << HygeSKW>> ============================================================== ; Name ..........: _HygeSKW() ; Description ...: Returns the skewness of the hypergeometric distribution. ; Syntax ........: _HygeSKW([$M [, $K [, N]]]) ; Parameters ....: $M = the number of trials (number). ; $K = the number of successes (number). ; $N = the population size (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $M, $K or $N is not a number. ; 2 = $M, $K or $N is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_HygeSKW( ) = " & _HygeSKW() ) ; ====================================================================================== Func _HygeSKW($M=25, $K=30, $N=50) If Not IsNumber($M) Or Not IsNumber($K) Or Not IsNumber($N) Then Return SetError(1, 0, 0) If $M <> Int($M) Or $K <> Int($K) Or $N <> Int($N) Then Return SetError(2, 0, 0) If $M > $N Or $K > $N Then Return SetError(2, 0, 0) Return Round((Sqrt($N - 1) * ($N - 2 * $K) * ($N - 2 * $M)) / (($N - 2) * Sqrt($K * $M * ($N - $K) * ($N - $M))), $Reps - 1) EndFunc ; #FUNCTION# << HygeEXC>> ============================================================== ; Name ..........: _HygeEXC() ; Description ...: Returns the excess of the hypergeometric distribution. ; Syntax ........: _HygeEXC([$M [, $K [, N]]]) ; Parameters ....: $M = the number of trials (number). ; $K = the number of successes (number). ; $N = the population size (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $M, $K or $N is not a number. ; 2 = $M, $K or $N is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_HygeEXC( ) = " & _HygeEXC() ) ; ====================================================================================== Func _HygeEXC($M=25, $K=30, $N=50) If Not IsNumber($M) Or Not IsNumber($K) Or Not IsNumber($N) Then Return SetError(1, 0, 0) If $M <> Int($M) Or $K <> Int($K) Or $N <> Int($N) Then Return SetError(2, 0, 0) If $M > $N Or $K > $N Then Return SetError(2, 0, 0) Return Round(((($N - 1) * $N^2 * ($N * ($N + 1) - 6 * $K * ($N - $K) - 6 * $M * ($N - $M))) + (6 * $M * $K * ($N - $K) * ($N - $M) * (5 * $N - 6))) / ($M * $K * ($N - $K) * ($N - $M) * ($N - 2) * ($N - 3)), $Reps - 1) EndFunc ; #FUNCTION# << HygeRND>> ============================================================== ; Name ..........: _HygeRND() ; Description ...: Returns the random number of the hypergeometric distribution. ; Syntax ........: _HygeRND([$M [, $K [, N]]]) ; Parameters ....: $M = the number of trials (number). ; $K = the number of successes (number). ; $N = the population size (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $M, $K or $N is not a number. ; 2 = $M, $K or $N is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_HygeRND( ) = " & _HygeRND() ) ; ====================================================================================== Func _HygeRND($M=25, $K=30, $N=50) If Not IsNumber($M) Or Not IsNumber($K) Or Not IsNumber($N) Then Return SetError(1, 0, 0) If $M <> Int($M) Or $K <> Int($K) Or $N <> Int($N) Then Return SetError(2, 0, 0) If $M > $N Or $K > $N Then Return SetError(2, 0, 0) Return _HygeINV(Random(1), $M, $K, $N) EndFunc #EndRegion hypergeometric distribution #Region poisson distribution ; #FUNCTION# << PoisPDF >> ============================================================= ; Name ..........: _PoissPDF() ; Description ...: Returns the probability density of the poisson distribution. ; Syntax ........: _PoissPDF($x [, $lambda ]) ; Parameters ....: $x = value over which find the probability (number). ; $lambda= the mean value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $lambda is not a number. ; 2 = $x or $lambda is outside the domain or $x is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_PoissPDF( 1 ) = " & _PoissPDF(1) ) ; ====================================================================================== Func _PoissPDF($x, $lambda=1) If Not IsNumber($x) Or Not IsNumber($lambda) Then Return SetError(1, 0, 0) If $x <> Int($x) Or $lambda <> Int($lambda) Or $x < 0 Or $lambda <= 0 Then Return SetError(2, 0, 0) Return Round(Exp($x * Log($lambda) - $lambda - __gammaln($x + 1)), $Reps - 1) EndFunc ; #FUNCTION# << PoisCDF >> ============================================================= ; Name ..........: _PoissCDF() ; Description ...: Returns the cumulated probability distribution of the poisson distribution. ; Syntax ........: _PoissCDF($x [, $lambda ]) ; Parameters ....: $x = value over which find the probability (number). ; $lambda= the mean value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $lambda is not a number. ; 2 = $x or $lambda is outside the domain or $x is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_PoissCDF( 1 ) = " & _PoissCDF(1) ) ; ====================================================================================== Func _PoissCDF($x, $lambda=1) If Not IsNumber($x) Or Not IsNumber($lambda) Then Return SetError(1, 0, 0) If $x <> Int($x) Or $lambda <> Int($lambda) Or $x < 0 Or $lambda <= 0 Then Return SetError(2, 0, 0) Return Round(__gammaq(Floor($x + 1), $lambda), $Reps - 1) EndFunc ; #FUNCTION# << PoisINV >> ============================================================= ; Name ..........: _PoissINV() ; Description ...: Returns the inverse cumulated probability distribution of the poisson distribution. ; Syntax ........: _PoissINV($x [, $lambda ]) ; Parameters ....: $Y = the quantile value (number). ; $lambda= the mean value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $Y or $lambda is not a number. ; 2 = $Y or $lambda is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_PoissINV( 0.5 ) = " & _PoissINV(0.5) ) ; ====================================================================================== Func _PoissINV($y, $lambda=1) If Not IsNumber($y) Or Not IsNumber($lambda) Then Return SetError(1, 0, 0) If $lambda <> Int($lambda) Or $lambda <= 0 Or $y < 0 Or $y > 1 Then Return SetError(2, 0, 0) If $y = 1 Then Return 1/0 Local $x = 0 Local $llambda = Log($lambda) Local $cumdist = Exp($x * $llambda - $lambda - __gammaln($x + 1)) ConsoleWrite("P("&$x&") = " & $cumdist & @CRLF) While ($y > $cumdist) $x += 1 $cumdist += Exp($x * $llambda - $lambda - __gammaln($x + 1)) WEnd Return Round($x, $Reps - 1) EndFunc ; #FUNCTION# << PoisMEAN >> ============================================================ ; Name ..........: _PoissMEAN() ; Description ...: Returns the mean of the poisson distribution. ; Syntax ........: _PoissMEAN([ $lambda ]) ; Parameters ....: $lambda= the mean value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda is not a number. ; 2 = $lambda is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_PoissMEAN( ) = " & _PoissMEAN() ) ; ====================================================================================== Func _PoissMEAN($lambda=1) If Not IsNumber($lambda) Then Return SetError(1, 0, 0) If $lambda <> Int($lambda) Or $lambda <= 0 Then Return SetError(2, 0, 0) Return Round($lambda, $Reps - 1) EndFunc ; #FUNCTION# << PoisVAR >> ============================================================= ; Name ..........: _PoissVAR() ; Description ...: Returns the variance of the poisson distribution. ; Syntax ........: _PoissVAR([ $lambda ]) ; Parameters ....: $lambda= the mean value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda is not a number. ; 2 = $lambda is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_PoissVAR( ) = " & _PoissVAR() ) ; ====================================================================================== Func _PoissVAR($lambda=1) If Not IsNumber($lambda) Then Return SetError(1, 0, 0) If $lambda <> Int($lambda) Or $lambda <= 0 Then Return SetError(2, 0, 0) Return Round($lambda, $Reps - 1) EndFunc ; #FUNCTION# << PoisSKW >> ============================================================= ; Name ..........: _PoissSKW() ; Description ...: Returns the skewness of the poisson distribution. ; Syntax ........: _PoissSKW([ $lambda ]) ; Parameters ....: $lambda= the mean value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda is not a number. ; 2 = $lambda is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_PoissSKW( ) = " & _PoissSKW() ) ; ====================================================================================== Func _PoissSKW($lambda=1) If Not IsNumber($lambda) Then Return SetError(1, 0, 0) If $lambda <> Int($lambda) Or $lambda <= 0 Then Return SetError(2, 0, 0) Return Round($lambda^-0.5, $Reps - 1) EndFunc ; #FUNCTION# << PoisEXC >> ============================================================= ; Name ..........: _PoissEXC() ; Description ...: Returns the excess of the poisson distribution. ; Syntax ........: _PoissEXC([ $lambda ]) ; Parameters ....: $lambda= the mean value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda is not a number. ; 2 = $lambda is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_PoissEXC( ) = " & _PoissEXC(2) ) ; ====================================================================================== Func _PoissEXC($lambda=1) If Not IsNumber($lambda) Then Return SetError(1, 0, 0) If $lambda <> Int($lambda) Or $lambda <= 0 Then Return SetError(2, 0, 0) Return Round(1 / $lambda, $Reps - 1) EndFunc ; #FUNCTION# << PoisRND >> ============================================================= ; Name ..........: _PoissRND() ; Description ...: Returns the random number of the poisson distribution. ; Syntax ........: _PoissRND([ $lambda ]) ; Parameters ....: $lambda= the mean value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $lambda is not a number. ; 2 = $lambda is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_PoissRND( ) = " & _PoissRND() ) ; ====================================================================================== Func _PoissRND($lambda=1) If Not IsNumber($lambda) Then Return SetError(1, 0, 0) If $lambda <> Int($lambda) Or $lambda <= 0 Then Return SetError(2, 0, 0) Return _PoissINV(Random(1), $lambda) EndFunc #EndRegion poisson distribution #Region geometric distribution ; #FUNCTION# << GeomPDF >> ============================================================= ; Name ..........: _GeomPDF() ; Description ...: Returns the probability density of the geometric distribution. ; Syntax ........: _GeomPDF($x [, $p [, $type]]) ; Parameters ....: $x = value over which find the probability (number). ; $p = the probability value (number). ; $type = the probability type: False = p is success probability (Standard); True = p is failure probability (bool). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $p is not a number or $type is not a number/boolean. ; 2 = $x or $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_GeomPDF( 1 ) = " & _GeomPDF(1) ) ; ====================================================================================== Func _GeomPDF($x, $p=0.1, $type = False) If Not( IsNumber($x) And IsNumber($p) And (IsBool($type) Or IsNumber($type))) Then Return SetError(1, 0, 0) If $x <> Int($x) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round($p * (1 - $p)^($x - Number($type)), $Reps - 1) EndFunc ; #FUNCTION# << GeomCDF >> ============================================================= ; Name ..........: _GeomCDF() ; Description ...: Returns the cumulated probability distribution of the geometric distribution. ; Syntax ........: _GeomCDF($x [, $p [, $type]]) ; Parameters ....: $x = value over which find the probability (number). ; $p = the probability value (number). ; $type = the probability type: False = p is success probability (Standard); True = p is failure probability (bool). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $p is not a number or $type is not a number/boolean. ; 2 = $x or $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_GeomCDF( 1 ) = " & _GeomCDF(1) ) ; ====================================================================================== Func _GeomCDF($x, $p=0.1, $type = False) If Not( IsNumber($x) And IsNumber($p) And (IsBool($type) Or IsNumber($type))) Then Return SetError(1, 0, 0) If $x <> Int($x) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(1 - (1 - $p)^($x + 1 - Number($type)), $Reps - 1) EndFunc ; #FUNCTION# << GeomINV >> ============================================================= ; Name ..........: _GeomINV() ; Description ...: Returns the inverse cumulated probability distribution of the geometric distribution. ; Syntax ........: _GeomINV($Y [, $p [, $type]]) ; Parameters ....: $Y = the quantile value (number). ; $p = the probability value (number). ; $type = the probability type: False = p is success probability (Standard); True = p is failure probability (bool). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $Y or $p is not a number or $type is not a number/boolean. ; 2 = $Y or $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_GeomINV( ~1 ) = " & _GeomINV(1-$meps) ) ; ====================================================================================== Func _GeomINV($y, $p=0.1, $type = False) If Not( IsNumber($y) And IsNumber($p) And (IsBool($type) Or IsNumber($type))) Then Return SetError(1, 0, 0) If $y < 0 Or $y > 1 Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) If $y = 1 Then Return 1/0 Local $x = 0 Local $cumdist = $p * (1 - $p)^($x - Number($type)) While ($y > $cumdist) $x += 1 $cumdist += $p * (1 - $p)^($x - Number($type)) WEnd Return Round($x, $Reps - 1) EndFunc ; #FUNCTION# << GeomMEAN >> ============================================================ ; Name ..........: _GeomMEAN() ; Description ...: Returns the mean of the geometric distribution. ; Syntax ........: _GeomMEAN([ $p [, $type]]) ; Parameters ....: $p = the probability value (number). ; $type = the probability type: False = p is success probability (Standard); True = p is failure probability (bool). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number or $type is not a number/boolean. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_GeomMEAN( ) = " & _GeomMEAN() ) ; ====================================================================================== Func _GeomMEAN($p=0.1, $type = False) If Not (IsNumber($p) And (IsBool($type) Or IsNumber($type))) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(1 / $p - 1 + Number($type), $Reps - 1) EndFunc ; #FUNCTION# << GeomVAR >> ============================================================= ; Name ..........: _GeomVAR() ; Description ...: Returns the variance of the geometric distribution. ; Syntax ........: _GeomVAR([ $p [, $type]]) ; Parameters ....: $p = the probability value (number). ; $type = the probability type: False = p is success probability (Standard); True = p is failure probability (bool). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number or $type is not a number/boolean. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_GeomVAR( ) = " & _GeomVAR() ) ; ====================================================================================== Func _GeomVAR($p=0.1, $type = False) If Not (IsNumber($p) And (IsBool($type) Or IsNumber($type))) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round((1 - $p) / $p^2, $Reps - 1) EndFunc ; #FUNCTION# << GeomSKW >> ============================================================= ; Name ..........: _GeomSKW() ; Description ...: Returns the skewness of the geometric distribution. ; Syntax ........: _GeomSKW([ $p [, $type]]) ; Parameters ....: $p = the probability value (number). ; $type = the probability type: False = p is success probability (Standard); True = p is failure probability (bool). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number or $type is not a number/boolean. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_GeomSKW( ) = " & _GeomSKW() ) ; ====================================================================================== Func _GeomSKW($p=0.1, $type = False) If Not (IsNumber($p) And (IsBool($type) Or IsNumber($type))) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round((2 - $p)/Sqrt(1 - $p), $Reps - 1) EndFunc ; #FUNCTION# << GeomEXC >> ============================================================= ; Name ..........: _GeomEXC() ; Description ...: Returns the variance of the geometric distribution. ; Syntax ........: _GeomEXC([ $p [, $type]]) ; Parameters ....: $p = the probability value (number). ; $type = the probability type: False = p is success probability (Standard); True = p is failure probability (bool). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number or $type is not a number/boolean. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_GeomEXC( ) = " & _GeomEXC() ) ; ====================================================================================== Func _GeomEXC($p=0.1, $type = False) If Not (IsNumber($p) And (IsBool($type) Or IsNumber($type))) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(6 + $p^2 / (1 - $p), $Reps - 1) EndFunc ; #FUNCTION# << GeomRND >> ============================================================= ; Name ..........: _GeomRND() ; Description ...: Returns the random number of the geometric distribution. ; Syntax ........: _GeomRND([ $p [, $type]]) ; Parameters ....: $p = the probability value (number). ; $type = the probability type: False = p is success probability (Standard); True = p is failure probability (bool). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number or $type is not a number/boolean. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_GeomRND( ) = " & _GeomRND() ) ; ====================================================================================== Func _GeomRND($p=0.1, $type = False) If Not (IsNumber($p) And (IsBool($type) Or IsNumber($type))) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return _GeomINV(Random(1), $p, $type) EndFunc #EndRegion geometric distribution #Region logarithmic series distribution ; #FUNCTION# << LogPDF >> ============================================================== ; Name ..........: _LogPDF() ; Description ...: Returns the probability density of the logarithmic series distribution. ; Syntax ........: _LogPDF($x [, $p ]]) ; Parameters ....: $x = value over which find the probability (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $p is not a number. ; 2 = $x or $p is outside the domain or $x is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_LogPDF( 1 ) = " & _LogPDF(1) ) ; ====================================================================================== Func _LogPDF($x, $p=0.1) If Not IsNumber($x) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $x < 1 Or $x <> Int($x) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(-($p^$x) / ($x * Log(1 - $p)), $Reps - 1) EndFunc ; #FUNCTION# << LogCDF >> ============================================================== ; Name ..........: _LogCDF() ; Description ...: Returns the cumulated probability distribution of the logarithmic series distribution. ; Syntax ........: _LogCDF($x [, $p ]]) ; Parameters ....: $x = value over which find the probability (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x or $p is not a number. ; 2 = $x or $p is outside the domain or $x is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_LogCDF( 1 ) = " & _LogCDF(1) ) ; ====================================================================================== Func _LogCDF($x, $p=0.1) If Not IsNumber($x) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $x < 1 Or $x <> Int($x) Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) If $x <= Ceiling(10 * $p) Then Local $sum = 0 For $i = 1 to $x $sum += _LogPDF($i, $p) Next Return Round($sum, $Reps - 1) Else Return Round(1 + (__betainc($p, $x+1, 10^-$Reps) * __beta($x+1, 10^-$Reps)) / (Log(1 - $p)) , $Reps - 1) EndIf EndFunc ; #FUNCTION# << LogINV >> ============================================================== ; Name ..........: _LogINV() ; Description ...: Returns the inverse cumulated probability distribution of the logarithmic series distribution. ; Syntax ........: _LogINV($Y [, $p ]]) ; Parameters ....: $Y = value over which find the probability (number). ; $p = the success probability value (number). ; Return values .: On Success: = Returns inverse cumulated probability density. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $Y or $p is not a number. ; 2 = $Y or $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_LogINV( 0.99995 ) = " & _LogINV(0.99995) ) ; ====================================================================================== Func _LogINV($y, $p=0.1) If Not IsNumber($y) Or Not IsNumber($p) Then Return SetError(1, 0, 0) If $y < 0 Or $y > 1 Or $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Local $x = 1 Local $l1mp = Log(1 - $p) Local $cumdist = -$p / $l1mp Local $x = 1 While ($y > $cumdist) $x += 1 $cumdist += -($p^$x) / ($x * $l1mp) WEnd Return Round($x, $Reps - 1) EndFunc ; #FUNCTION# << LogMEAN >> ============================================================= ; Name ..........: _LogMEAN() ; Description ...: Returns the mean of the logarithmic series distribution. ; Syntax ........: _LogMEAN([ $p ]]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns mean value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_LogMEAN( ) = " & _LogMEAN() ) ; ====================================================================================== Func _LogMEAN($p=0.1) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round(-$p / (Log(1 - $p) * (1 - $p)), $Reps - 1) EndFunc ; #FUNCTION# << LogVAR >> ============================================================== ; Name ..........: _LogVAR() ; Description ...: Returns the variance of the logarithmic series distribution. ; Syntax ........: _LogVAR([ $p ]]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns variance value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_LogVAR( ) = " & _LogVAR() ) ; ====================================================================================== Func _LogVAR($p=0.1) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return Round($p * (-Log(1 - $p) - $p) / ((1 - $p)^2 * (Log(1 - $p))^2), $Reps - 1) EndFunc ; #FUNCTION# << LogSKW >> ============================================================== ; Name ..........: _LogSKW() ; Description ...: Returns the skewness of the logarithmic series distribution. ; Syntax ........: _LogSKW([ $p ]]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns skewness value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_LogSKW( ) = " & _LogSKW() ) ; ====================================================================================== Func _LogSKW($p=0.1) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Local $l = Log(1 - $p) Return Round(-(2 * $p^2 + ($p + 1) * $l^2 + 3 * $p * $l) / (($p + $l) * Sqrt(-$p * ($p + $l))), $Reps - 1) EndFunc ; #FUNCTION# << LogEXC >> ============================================================== ; Name ..........: _LogEXC() ; Description ...: Returns the excess of the logarithmic series distribution. ; Syntax ........: _LogEXC([ $p ]]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns excess value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: To calculate the kurtosis must be added 3 to the excess ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_LogEXC( ) = " & _LogEXC() ) ; ====================================================================================== Func _LogEXC($p=0.1) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Local $l = Log(1 - $p) Return Round((-((3 * ($p / $l)^3 + $p^2 * (1 + (6 / $l^2)) + 4 * $p * (1 + (($p + 1) / $l)) + 1) * $l) / ($p * ($p / $l + 1)^2)) - 3, $Reps - 1) EndFunc ; #FUNCTION# << LogRND >> ============================================================== ; Name ..........: _LogRND() ; Description ...: Returns the random number of the logarithmic series distribution. ; Syntax ........: _LogRND([ $p ]]) ; Parameters ....: $p = the success probability value (number). ; Return values .: On Success: = Returns random number value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $p is not a number. ; 2 = $p is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Discrete Distribution Functions", "_LogRND( ) = " & _LogRND() ) ; ====================================================================================== Func _LogRND($p=0.1) If Not IsNumber($p) Then Return SetError(1, 0, 0) If $p < 0 Or $p > 1 Then Return SetError(2, 0, 0) Return _LogINV(Random(1), $p) EndFunc #EndRegion logarithmic series distribution #EndRegion Discrete Distribution Functions #Region Statistical Functions ; #FUNCTION# << Count >> =============================================================== ; Name ..........: _Count() ; Description ...: Returns the size of the array dimension. ; Syntax ........: _Count($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the size of the 1-n array dimension. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Count( $A ) = " & _Count($A) ) ; ====================================================================================== Func _Count($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Return UBound($A) EndFunc ; #FUNCTION# << Max >> ================================================================= ; Name ..........: _Max() ; Description ...: Returns the maximum element of the array. ; Syntax ........: _Max($A [, $b]) ; Parameters ....: $A = a number array / number. ; $b = a number. ; Return values .: On Success: = Returns the maximum element. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A or $b is not an array / number. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,12,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Max( $A ) = " & _Max($A) ) ; ====================================================================================== Func _Max($A, $b = 0) If @NumParams = 2 Then If IsNumber($A) And IsNumber($b) Then Local $C[2] = [$A, $b] $A = $C Else Return SetError(1, 0, 0) EndIf EndIf If Not IsArray($A) Then Return SetError(1, 0, 0) Local $max = $A[0] For $i = 0 to UBound($A)-1 If $A[$i] > $max Then $max = $A[$i] Next Return $max EndFunc ; #FUNCTION# << Min >> ================================================================= ; Name ..........: _Min() ; Description ...: Returns the minimum element of the array. ; Syntax ........: _Min($A [, $b]) ; Parameters ....: $A = a number array / number. ; $b = a number. ; Return values .: On Success: = Returns the minimum element. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A or $b is not an array / number. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Min( $A ) = " & _Min($A) ) ; ====================================================================================== Func _Min($A, $b = 0) If @NumParams = 2 Then If IsNumber($A) And IsNumber($b) Then Local $C[2] = [$A, $b] $A = $C Else Return SetError(1, 0, 0) EndIf EndIf If Not IsArray($A) Then Return SetError(1, 0, 0) Local $min = $A[0] For $i = 0 to UBound($A)-1 If $A[$i] < $min Then $min = $A[$i] Next Return $min EndFunc ; #FUNCTION# << Sum >> ================================================================= ; Name ..........: _Sum() ; Description ...: Returns the summed elements of the array. ; Syntax ........: _Sum($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the summed elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Sum( $A ) = " & _Sum($A) ) ; ====================================================================================== Func _Sum($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $sum = 0 For $i = 0 to UBound($A)-1 $sum += $A[$i] Next Return $sum EndFunc ; #FUNCTION# << Prod >> ================================================================ ; Name ..........: _Prod() ; Description ...: Returns the multiplied elements of the array. ; Syntax ........: _Prod($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the multiplied elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Prod( $A ) = " & _Prod($A) ) ; ====================================================================================== Func _Prod($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $prod = 1 For $i = 0 to UBound($A)-1 $prod *= Round($A[$i],$Reps-1) Next Return $prod EndFunc ; #FUNCTION# << SumSQ >> =============================================================== ; Name ..........: _SumSQ() ; Description ...: Returns the sum of the squared elements of the array. ; Syntax ........: _SumSQ($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the sum of the squared elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_SumSQ( $A ) = " & _SumSQ($A) ) ; ====================================================================================== Func _SumSQ($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $sum = 0 For $i = 0 to UBound($A)-1 $sum += ($A[$i])^2 Next Return $sum EndFunc ; #FUNCTION# << Median >> ============================================================== ; Name ..........: _Median() ; Description ...: Returns the median of the elements of the array. ; Syntax ........: _Median($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the median of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Median( $A ) = " & _Median($A) ) ; ====================================================================================== Func _Median($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) __Sort($A, 0, $n-1) If Mod($n, 2) <> 0 Then Return $A[($n-1) / 2] Else Return ($A[($n-1) / 2] + $A[($n-1) / 2 + 1]) / 2 EndIf EndFunc ; #FUNCTION# << Mean >> ================================================================ ; Name ..........: _Mean() ; Description ...: Returns the mean of the elements of the array. ; Syntax ........: _Mean($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the mean of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Mean( $A ) = " & _Mean($A) ) ; ====================================================================================== Func _Mean($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Return _Sum($A)/UBound($A) EndFunc ; #FUNCTION# << Mode >> ================================================================ ; Name ..........: _Mode() ; Description ...: Returns the modal value of the elements of the array. ; Syntax ........: _Mode($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the modal value. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; polymodal output are representated as an array, unimodal as a number ; Example .......: Dim $A[10] = [1,2,2,2,3,2,4,2,5,2] ; MsgBox(0x42040, "Statistical Functions", "_Mode( $A ) = " & _Mode($A) ) ; ====================================================================================== Func _Mode($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) __Sort($A, 0, $n-1) Local $cnt = 1, $pos = 0, $B[$n] $B[0] = 1 Local $max = 0 For $i = 0 To $n-2 If $A[$i] = $A[$i+1] Then $cnt += 1 Else $cnt = 1 EndIf $B[$i+1] = $cnt If $B[$i+1] > $max Then $max = $B[$i+1] EndIf Next Local $C[$n], $j = 0 For $i = 0 To $n-1 If $B[$i] = $max Then $C[$j] = $A[$i] $j += 1 EndIf Next ReDim $C[$j] If $j = 1 Then Return SetError(0, $max, $C[0]) Else Return SetError(0, $max, $C) EndIf EndFunc ; #FUNCTION# << Rng >> ================================================================= ; Name ..........: _Rng() ; Description ...: Returns the range of the elements of the array. ; Syntax ........: _Rng($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the range of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Rng( $A ) = " & _Rng($A) ) ; ====================================================================================== Func _Rng($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $min = $A[0], $max = $min For $i = 1 to UBound($A)-1 If $A[$i] < $min Then $min = $A[$i] If $A[$i] > $max Then $max = $A[$i] Next Return Abs($max - $min) EndFunc ; #FUNCTION# << Std >> ================================================================= ; Name ..........: _Std() ; Description ...: Returns the standard deviation of the elements of the array. ; Syntax ........: _Std($A) ; Parameters ....: $A = a number array. ; $sample = True: sample population (Standard). ; = False: statistical population. ; Return values .: On Success: = Returns the standard deviation of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Std( $A, 0 ) = " & _Std($A,0) ) ; ====================================================================================== Func _Std($A, $sample = TRUE) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) Local $mean = _Mean($A) Local $sum = 0 For $i = 0 to $n-1 $sum += ($A[$i] - $mean)^2 Next If $sample Then $n -= 1 Return ($sum / $n)^0.5 EndFunc ; #FUNCTION# << Var >> ================================================================= ; Name ..........: _Var() ; Description ...: Returns the variance of the elements of the array. ; Syntax ........: _Var($A) ; Parameters ....: $A = a number array. ; $sample = True: sample population (Standard). ; = False: statistical population. ; Return values .: On Success: = Returns the variance of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Var( $A, 0 ) = " & _Var($A,0) ) ; ====================================================================================== Func _Var($A, $sample = TRUE) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) Local $mean = _Mean($A) Local $sum = 0 For $i = 0 to $n-1 $sum += ($A[$i] - $mean)^2 Next If $sample Then $n -= 1 Return $sum / ($n) EndFunc ; #FUNCTION# << GeoMean >> ============================================================= ; Name ..........: _GeoMean() ; Description ...: Returns the geometric mean of the elements of the array. ; Syntax ........: _GeoMean($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the geometric mean of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_GeoMean( $A ) = " & _GeoMean($A) ) ; ====================================================================================== Func _GeoMean($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Return _Prod($A)^(1 / UBound($A)) EndFunc ; #FUNCTION# << HarMean >> ============================================================= ; Name ..........: _HarMean() ; Description ...: Returns the harmonic mean of the elements of the array. ; Syntax ........: _HarMean($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the harmonic mean of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_HarMean( $A ) = " & _HarMean($A) ) ; ====================================================================================== Func _HarMean($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) Local $sum = 0 For $i = 0 to $n-1 If $A[$i] <= 0 Then Return SetError(2, 0, 0) $sum += Round(1 / $A[$i], $Reps - 1) Next Return $n / $sum EndFunc ; #FUNCTION# << TrimMean >> ============================================================ ; Name ..........: _TrimMean() ; Description ...: Returns the harmonic mean of the elements of the array. ; Syntax ........: _TrimMean($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the harmonic mean of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_TrimMean( $A ) = " & _TrimMean($A) ) ; ====================================================================================== Func _TrimMean($A, $P) If Not IsArray($A) Or Not IsNumber($P) Then Return SetError(1, 0, 0) Local $n = UBound($A) __Sort($A, 0 ,$n-1) Local $cnt = Floor(($n * $P) / 2) Local $sum = 0 For $i = $cnt to ($n - 1 - $cnt) $sum += Round($A[$i], $Reps - 1) Next Return $sum / ($n - (2 * $cnt)) EndFunc ; #FUNCTION# << RMS >> ================================================================= ; Name ..........: _RMS() ; Description ...: Returns the Root mean square of the elements of the array. ; Syntax ........: _RMS($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the Root mean square of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_RMS( $A ) = " & _RMS($A) ) ; ====================================================================================== Func _RMS($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $sum = 0 Local $n = UBound($A) For $i = 0 To $n - 1 $sum += $A[$i]^2 Next Return Sqrt($sum / $n) EndFunc ; #FUNCTION# << Skew >> ================================================================ ; Name ..........: _Skew() ; Description ...: Returns the skewness of the elements of the array. ; Syntax ........: _Skew($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the skewness of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Skew( $A ) = " & _Skew($A) ) ; ====================================================================================== Func _Skew($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) Local $s = _Std($A) If $n < 3 Or $s = 0 Then Return SetError(2, 0, 0) Local $m = _Mean($A) Local $sum = 0 For $i = 0 To $n-1 $sum += (($A[$i] - $m) / $s)^3 Next Return $n / (($n - 1) * ($n - 2)) * $sum EndFunc ; #FUNCTION# << Kurt >> ================================================================ ; Name ..........: _Kurt() ; Description ...: Returns the kurtosis of the elements of the array. ; Syntax ........: _Kurt($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the kurtosis of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Kurt( $A ) = " & _Kurt($A) ) ; ====================================================================================== Func _Kurt($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) Local $s = _Std($A) If $n < 4 Or $s = 0 Then Return SetError(2, 0, 0) Local $m = _Mean($A) Local $sum = 0 For $i = 0 To $n-1 $sum += (($A[$i] - $m)/$s)^4 Next Return (($n * ($n + 1)) / (($n - 1) * ($n - 2) * ($n - 3)) * $sum) - ((3 * ($n - 1)^2) / (($n - 2) * ($n - 3))) + 3 EndFunc ; #FUNCTION# << Excess >> ============================================================== ; Name ..........: _Excess() ; Description ...: Returns the excess of the elements of the array. ; Syntax ........: _Excess($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the excess of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Excess( $A ) = " & _Excess($A) ) ; ====================================================================================== Func _Excess($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) Local $s = _Std($A) If $n < 4 Or $s = 0 Then Return SetError(2, 0, 0) Local $m = _Mean($A) Local $sum = 0 For $i = 0 To $n-1 $sum += (($A[$i] - $m)/$s)^4 Next Return (($n * ($n + 1)) / (($n - 1) * ($n - 2) * ($n - 3)) * $sum) -( (3 * ($n - 1)^2) / (($n - 2) * ($n - 3))) EndFunc ; #FUNCTION# << DevAvAbs >> ============================================================ ; Name ..........: _DevAvAbs() ; Description ...: Returns the average absolute deviation of the elements of the array. ; Syntax ........: _DevAvAbs($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the average absolute deviation of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_DevAvAbs( $A ) = " & _DevAvAbs($A) ) ; ====================================================================================== Func _DevAvAbs($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) Local $m = _Mean($A) Local $sum = 0 For $i = 0 To $n-1 $sum += Abs($A[$i] - $m) Next Return $sum / $n EndFunc ; #FUNCTION# << DevSQ >> =============================================================== ; Name ..........: _DevSQ() ; Description ...: Returns the squared deviation of the elements of the array. ; Syntax ........: _DevSQ($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the squared deviation of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_DevSQ( $A ) = " & _DevSQ($A) ) ; ====================================================================================== Func _DevSQ($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) Local $m = _Mean($A) Local $sum = 0 For $i = 0 To $n-1 $sum += ($A[$i] - $m)^2 Next Return $sum EndFunc ; #FUNCTION# << Quantil >> ============================================================= ; Name ..........: _Quantil() ; Description ...: Returns the quantile by 100 (percentile) of the elements of the array. ; Syntax ........: _Quantil($A, $P) ; Parameters ....: $A = a number array. ; $P = the percentage limit value (number). ; Return values .: On Success: = Returns the percentile of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array or $P is not a number. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Quantil(" & 0.75 * 100 & "%) = " & _Quantil($A, 0.75) ) ; ====================================================================================== Func _Quantil($A, $P) If Not IsArray($A) Or Not IsNumber($P) Then Return SetError(1, 0, 0) If $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Local $n = UBound($A) __Sort($A, 0, $n-1) Local $np = $n * $P If Int($np) = $np Then Return ($A[$np - 1] + $A[$np]) / 2 Else Return $A[Int($np)] EndIf EndFunc ; #FUNCTION# << Conf >> ================================================================ ; Name ..........: _Conf() ; Description ...: Returns the confidence interval of the elements of the array. ; Syntax ........: _Conf($A [, $P [, $n]]) ; Parameters ....: $A = a number array / the standard deviation (number). ; $P = the significance level (number). ; $n = the sample size (number). (must be given if $A is not an array) ; Return values .: On Success: = Returns the confidence intervall of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array or $P or $n is not a number. ; 2 = $P or $n is outside the domain. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; MsgBox(0x42040, "Statistical Functions", "_Conf( $A ) = ±" & _Conf($A) ) ; ====================================================================================== Func _Conf($A, $P = 0.05, $n = -1) Local $s = 0, $m = 0 If Not IsArray($A) And IsNumber($P) Then If IsNumber($A) and IsNumber($n) Then If $n <= 0 Then Return SetError(2, 0, 0) Else $s = $A EndIf Else Return SetError(1, 0, 0) EndIf ElseIf IsArray($A) And IsNumber($P) Then $n = UBound($A) $s = _Std($A) Else Return SetError(1, 0, 0) EndIf If $P < 0 Or $P > 1 Then Return SetError(2, 0, 0) Return __erfinv(1 - $P) * sqrt(2) * $s / Sqrt($n) EndFunc ; #FUNCTION# << Freq >> ================================================================ ; Name ..........: _Freq() ; Description ...: Returns how often a value occur within the range of the elements of the array. ; Syntax ........: _Freq($A [, $cn]) ; Parameters ....: $A = a number array. ; $cn = the class number (number)/array of intervalls. ; Return values .: On Success: = Returns the frequency of the elements. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $A is not an array or $cn is not a number/array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; If MsgBox(0x42040, "Statistical Functions", "_Freq( $A, 3 )") Then _ArrayDisplay(_Freq($A,3), "_Freq( $A, 3 )") ; ====================================================================================== Func _Freq($A, $cn = 0) If Not IsArray($A) Or Not (IsNumber($cn) Or IsArray($cn)) Then Return SetError(1, 0, 0) Local $n = UBound($A) __Sort($A, 0, $n-1) Local $min = $A[0] Local $max = $A[$n - 1] Local $B[1][2] If $cn <> 0 Then If IsArray($cn) Then Local $l = UBound($cn) ReDim $B[$l][2] For $i = 0 To $l - 1 $B[$i][0] = $cn[$i] Next If $B[$l - 1][0] <> $A[$n - 1] Then ReDim $B[$l+1][2] $B[$l][0] = $A[$n-1] EndIf Else ReDim $B[$cn][2] For $i = 1 To $cn $B[$i - 1][0] = $i * ($max - $min) / $cn + $min Next EndIf Local $pos = 0 $B[0][1] = 1 For $i = 1 To $n - 1 If $A[$i] <= $B[$pos][0] Then $B[$pos][1] += 1 Else $pos += 1 $B[$pos][1] = 1 EndIf Next Else Local $count = 1, $pos = 0 $B[0][0] = $A[0] $B[0][1] = 1 For $i = 1 To $n - 1 If $A[$i] = $B[$pos][0] Then $B[$pos][1] += 1 Else $pos += 1 ReDim $B[$pos+1][2] $B[$pos][0] = $A[$i] $B[$pos][1] = 1 EndIf Next EndIf Return $B EndFunc ; #FUNCTION# << Moments >> ============================================================= ; Name ..........: _Moments() ; Description ...: Returns the moments of the array. ; Syntax ........: _Moments($A) ; Parameters ....: $A = a number array. ; Return values .: On Success: = Returns the moments as array. ; 0 = count ; 1 = mean ; 2 = avarage deviation ; 3 = variance ; 4 = standard deviation ; 5 = skewness ; 6 = kurtosis ; 7 = excess ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $x is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional array only ; Example .......: Dim $A[10] = [1,10,2,9,3,8,4,7,5,6] ; _ArrayDisplay(_Moments($A) , "Statistical Functions") ; ====================================================================================== Func _Moments($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A) If $n <= 1 Then Return SetError(2, 0, 0) Local $B[5] Local $sum = 0 For $i = 0 to $n - 1 $sum += $A[$i] Next Local $mean = $sum / $n Local $avdev = 0 Local $sdev = 0 Local $var = 0 Local $skew = 0 Local $kurt = 0 Local $ep = 0, $p = 0 For $i = 0 to $n-1 $sum = $A[$i] - $mean $avdev += Abs($sum) $ep += $sum $p = $sum * $sum $var += $p $p *= $sum $skew += $p $p *= $sum $kurt += $p Next $B[0] = $n ;count $B[1] = $mean ;mean $B[2] = $avdev / $n ;avg. deviation $B[4] = ($var - $ep * $ep / $n) / ($n - 1) ;variance $B[3] = Sqrt($B[4]) ;standard deviation If $var Then ReDim $B[8] $B[5] = $skew / ($B[0] * ($B[4]) * ($B[3])) ;skewness $B[6] = ($kurt) / ($B[0] * ($B[4]) * ($B[4])) ;kurtosis $B[7] = $B[6] - 3 ;excess EndIf Return $B EndFunc #EndRegion Statistical Functions #Region Statistical Functions 2 ; #FUNCTION# << SumXY >> =============================================================== ; Name ..........: _SumXY() ; Description ...: Returns the sum product of the arrays. ; Syntax ........: _SumXY($X, $Y) ; Parameters ....: $X = a number array. ; $Y = a number array. ; Return values .: On Success: = Returns the sum product of the arrays. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $X or $Y is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional arrays only ; Example .......: MsgBox(0x42040, "Statistical Functions 2", "_SumXY( $X, $Y ) = " & _SumXY($X,$Y) ) ; ====================================================================================== Func _SumXY($X, $Y) If Not IsArray($X) Or Not IsArray($Y) Then Return SetError(1, 0, 0) Local $n = UBound($X) If $n <> UBound($Y) Then Return SetError(2, 0, 0) Local $sum = 0 For $i = 0 to $n-1 $sum += $X[$i]*$Y[$i] Next Return $sum EndFunc ; #FUNCTION# << Cov >> ================================================================= ; Name ..........: _Cov() ; Description ...: Returns the covariance of the arrays. ; Syntax ........: _Cov($X, $Y) ; Parameters ....: $X = a number array. ; $Y = a number array. ; Return values .: On Success: = Returns the covariance of the arrays. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $X or $Y is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional arrays only ; Example .......: MsgBox(0x42040, "Statistical Functions 2", "_Cov( $X, $Y ) = " & _Cov($X,$Y) ) ; ====================================================================================== Func _Cov($X, $Y) If Not IsArray($X) Or Not IsArray($Y) Then Return SetError(1, 0, 0) Local $n = UBound($X) If $n <> UBound($Y) Then Return SetError(2, 0, 0) Local $sum = 0, $sumx = 0, $sumy = 0 For $i = 0 to $n-1 $sum += $X[$i]*$Y[$i] $sumx += $X[$i] $sumy += $Y[$i] Next Return ($sum - (($sumx / $n) * $sumy)) / ($n - 1) EndFunc ; #FUNCTION# << RSQ >> ================================================================= ; Name ..........: _RSQ() ; Description ...: Returns the coefficient of determination of the arrays. ; Syntax ........: _RSQ($X, $Y) ; Parameters ....: $X = a number array. ; $Y = a number array. ; Return values .: On Success: = Returns the coefficient of determination. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $X or $Y is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional arrays only ; Example .......: MsgBox(0x42040, "Statistical Functions 2", "_RSQ( $X, $Y ) = " & _RSQ($X,$Y) ) ; ====================================================================================== Func _RSQ($X, $Y) If Not IsArray($X) Or Not IsArray($Y) Then Return SetError(1, 0, 0) Local $n = UBound($X) If $n <> UBound($Y) Then Return SetError(2, 0, 0) Local $sum = 0, $sumx = 0, $sumy = 0, $devsqx = 0, $devsqy = 0 For $i = 0 to $n-1 $sum += $X[$i]*$Y[$i] $sumx += $X[$i] $sumy += $Y[$i] Next $sumx /= $n Local $sumyn = $sumy / $n For $i = 0 to $n-1 $devsqx += ($X[$i] - $sumx)^2 $devsqy += ($Y[$i] - $sumyn)^2 Next Return (($sum - ($sumx * $sumy)) / ($devsqx * $devsqy)^0.5)^2 EndFunc ; #FUNCTION# << Corr >> ================================================================ ; Name ..........: _Corr() ; Description ...: Returns the correlation of the arrays. ; Syntax ........: _Corr($X, $Y) ; Parameters ....: $X = a number array. ; $Y = a number array. ; Return values .: On Success: = Returns the correlation coefficient. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $X or $Y is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional arrays only ; Example .......: MsgBox(0x42040, "Statistical Functions 2", "_Corr( $X, $Y ) = " & _Corr($X,$Y) ) ; ====================================================================================== Func _Corr($X, $Y) If Not IsArray($X) Or Not IsArray($Y) Then Return SetError(1, 0, 0) Local $n = UBound($X) If $n <> UBound($Y) Then Return SetError(2, 0, 0) Local $sum = 0, $sumx = 0, $sumy = 0, $devsqx = 0, $devsqy = 0 For $i = 0 to $n-1 $sum += $X[$i]*$Y[$i] $sumx += $X[$i] $sumy += $Y[$i] Next $sumx /= $n Local $sumyn = $sumy / $n For $i = 0 to $n-1 $devsqx += ($X[$i] - $sumx)^2 $devsqy += ($Y[$i] - $sumyn)^2 Next Return ($sum - ($sumx * $sumy)) / ($devsqx * $devsqy)^0.5 EndFunc ; #FUNCTION# << StdErr >> ============================================================== ; Name ..........: _StdErr() ; Description ...: Returns the standard error of regression of the arrays. ; Syntax ........: _StdErr($X, $Y) ; Parameters ....: $X = a number array. ; $Y = a number array. ; Return values .: On Success: = Returns the standard error of regression coefficient. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $X or $Y is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional arrays only ; Example .......: MsgBox(0x42040, "Statistical Functions 2", "_StdErr( $X, $Y ) = " & _StdErr($X,$Y) ) ; ====================================================================================== Func _StdErr($X, $Y) If Not IsArray($X) Or Not IsArray($Y) Then Return SetError(1, 0, 0) Local $n = UBound($X) If $n <> UBound($Y) Then Return SetError(2, 0, 0) Local $sum = 0, $sumx = 0, $sumy = 0, $devsqx = 0, $devsqy = 0 For $i = 0 to $n-1 $sum += $X[$i]*$Y[$i] $sumx += $X[$i] $sumy += $Y[$i] Next $sumx /= $n Local $sumyn = $sumy / $n For $i = 0 to $n-1 $devsqx += ($X[$i] - $sumx)^2 $devsqy += ($Y[$i] - $sumyn)^2 Next Return (($devsqy - (($sum - ($sumx * $sumy))^2 / $devsqx)) / ($n - 2))^0.5 EndFunc ; #FUNCTION# << Slope >> =============================================================== ; Name ..........: _Slope() ; Description ...: Returns the slope of the linear regression of the arrays. ; Syntax ........: _Slope($X, $Y) ; Parameters ....: $X = a number array. ; $Y = a number array. ; Return values .: On Success: = Returns the slope. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $X or $Y is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional arrays only ; Example .......: MsgBox(0x42040, "Statistical Functions 2", "_Slope( $X, $Y ) = " & _Slope($X,$Y) ) ; ====================================================================================== Func _Slope($X, $Y) If Not IsArray($X) Or Not IsArray($Y) Then Return SetError(1, 0, 0) Local $n = UBound($X) If $n <> UBound($Y) Then Return SetError(2, 0, 0) Local $sum = 0, $sumx = 0, $sumy = 0, $devsqx = 0, $devsqy = 0 For $i = 0 to $n-1 $sum += $X[$i]*$Y[$i] $sumx += $X[$i] $sumy += $Y[$i] Next $sumx /= $n For $i = 0 to $n-1 $devsqx += ($X[$i] - $sumx)^2 Next Return ($sum - ($sumx * $sumy)) / $devsqx EndFunc ; #FUNCTION# << Intercept >> =========================================================== ; Name ..........: _Intercept() ; Description ...: Returns the intersept of the linear regression of the arrays. ; Syntax ........: _Intercept($X, $Y) ; Parameters ....: $X = a number array. ; $Y = a number array. ; Return values .: On Success: = Returns the intersept. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $X or $Y is not an array. ; Author ........: Strahleman ; Remarks .......: input of one-dimensional arrays only ; Example .......: MsgBox(0x42040, "Statistical Functions 2", "_Intercept( $X, $Y ) = " & _Intercept($X,$Y) ) ; ====================================================================================== Func _Intercept($X, $Y) If Not IsArray($X) Or Not IsArray($Y) Then Return SetError(1, 0, 0) Local $n = UBound($X) If $n <> UBound($Y) Then Return SetError(2, 0, 0) Local $sum = 0, $sumx = 0, $sumy = 0, $devsqx = 0, $devsqy = 0 For $i = 0 to $n-1 $sum += $X[$i]*$Y[$i] $sumx += $X[$i] $sumy += $Y[$i] Next $sumx /= $n For $i = 0 to $n-1 $devsqx += ($X[$i] - $sumx)^2 Next Return ($sumy / $n) - ((($sum - (($sumx) * $sumy)) / $devsqx) * $sumx) EndFunc #EndRegion Statistical Functions 2 #Region Matrices Functions ; #FUNCTION# << MIdent >> ============================================================== ; Name ..........: _MIdent() ; Description ...: Returns the identity matrix. ; Syntax ........: _MIdent($n) ; Parameters ....: $n = size of the identity matrix. ; Return values .: On Success: = Returns the n x n identity matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A = _MIdent(4) ; _ArrayDisplay($A, "Matrix Functions") ; ====================================================================================== Func _MIdent($n) If Not IsNumber($n) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Then Return SetError(2, 0, 0) Local $A[$n][$n] For $i = 0 To $n - 1 For $j = 0 To $n - 1 If $i = $j Then $A[$i][$j] = 1 Else $A[$i][$j] = 0 EndIf Next Next Return $A EndFunc ; #FUNCTION# << MZero >> =============================================================== ; Name ..........: _MZero() ; Description ...: Returns the zero matrix. ; Syntax ........: _MZero($n[, $m]) ; Parameters ....: $n = size of the zero n x n matrix or the size of the rows. ; $m = size of the columns. ; Return values .: On Success: = Returns the n x n identity matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $n or $m is not an integer or $m is outside the domain. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A = _MZero(4) ; _ArrayDisplay($A, "Matrix Functions") ; ====================================================================================== Func _MZero($n, $m=1) If Not IsNumber($n) or not IsNumber($m) Then Return SetError(1, 0, 0) If $n < 1 Or $n <> Int($n) Then Return SetError(2, 0, 0) If @NumParams = 1 Then $m = $n ElseIf $m < 1 Or $m <> Int($m) Then Return SetError(2, 0, 0) EndIf Local $A[$n][$m] For $i = 0 To $n - 1 For $j = 0 To $m - 1 $A[$i][$j] = 0 Next Next Return $A EndFunc ; #FUNCTION# << MDiag >> =============================================================== ; Name ..........: _MDiag() ; Description ...: Returns the diagonal matrix. ; Syntax ........: _MDiag($C[, $n]) ; Parameters ....: $C = 1 x m matrix (number array) or continued number. ; $n = size of the diagonal matrix. ; Return values .: On Success: = Returns the n x n diagonal matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $C is not an array or an integer or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3]=[1,2,3], $B = _MDiag($A) ; _ArrayDisplay($B, "Matrix Functions") ; ====================================================================================== Func _MDiag($C, $n=1) If Not IsArray($C) Then If @NumParams = 2 And IsNumber($C) And IsNumber($n) Then Local $A[$n] For $i = 0 To $n-1 $A[$i] = $C Next Else Return SetError(1, 0, 0) EndIf Else Local $A = $C EndIf If @NumParams = 1 Then $n = UBound($A) Local $B[$n][$n] For $i = 0 To $n-1 For $j = 0 To $n-1 If $i = $j Then $B[$i][$i] = $A[$i] Else $B[$i][$j] = 0 EndIf Next Next Return $B EndFunc ; #FUNCTION# << MAdd >> ================================================================ ; Name ..........: _MAdd() ; Description ...: Returns the addition of two matrices. ; Syntax ........: _MAdd($A, $B) ; Parameters ....: $A = matrix (number array). ; $B = matrix (number array). ; Return values .: On Success: = Returns the sum matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A or $B is not an array. ; 2 = $A or $B are not the same size. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[2][3]=[[1,-3,2],[1,2,7]], $B[2][3]=[[0,3,5],[2,1,-1]], $C = _MAdd($A, $B) ; _ArrayDisplay($C, "Matrix Functions") ; ====================================================================================== Func _MAdd($A, $B) If Not IsArray($A) or Not IsArray($B) Then Return SetError(1, 0, 0) If UBound($A,1) <> UBound($B,1) Or UBound($A,2) <> UBound($B,2) Then Return SetError(2, 0, 0) Local $n = UBound($A, 1) Local $m = UBound($A, 2) Local $C[$n][$m] For $i = 0 to $n - 1 For $j = 0 To $m - 1 $C[$i][$j] = $A[$i][$j] + $B[$i][$j] Next Next Return $C EndFunc ; #FUNCTION# << MSub >> ================================================================ ; Name ..........: _MSub() ; Description ...: Returns the subtraction of two matrices. ; Syntax ........: _MSub($A, $B) ; Parameters ....: $A = matrix (number array). ; $B = matrix (number array). ; Return values .: On Success: = Returns the difference matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A or $B is not an array. ; 2 = $A or $B are not the same size. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[2][3]=[[1,-3,2],[1,2,7]], $B[2][3]=[[0,3,5],[2,1,-1]], $C = _MSub($A, $B) ; _ArrayDisplay($C, "Matrix Functions") ; ====================================================================================== Func _MSub($A, $B) If Not IsArray($A) or Not IsArray($B) Then Return SetError(1, 0, 0) If UBound($A,1) <> UBound($B,1) Or UBound($A,2) <> UBound($B,2) Then Return SetError(2, 0, 0) Local $n = UBound($A, 1) Local $m = UBound($A, 2) Local $C[$n][$m] For $i = 0 to $n - 1 For $j = 0 To $m - 1 $C[$i][$j] = $A[$i][$j] - $B[$i][$j] Next Next Return $C EndFunc ; #FUNCTION# << MTrans >> ============================================================== ; Name ..........: _MTrans() ; Description ...: Returns the transformation matrix. ; Syntax ........: _MTrans($A, $B) ; Parameters ....: $A = matrix (number array). ; Return values .: On Success: = Returns the transformation matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[2][3]=[[1,-3,2],[1,2,7]], $B = _MTrans($A) ; _ArrayDisplay($B, "Matrix Functions") ; ====================================================================================== Func _MTrans($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A, 1) Local $m = UBound($A, 2) If $m = 0 Then Local $C = $A ReDim $A[$n][1] For $i = 0 To $n-1 $A[$i][0] = $C[$i] Next $m = 1 EndIf Local $B[$m][$n] For $i = 0 to $n - 1 For $j = 0 To $m - 1 $B[$j][$i] = $A[$i][$j] Next Next Return $B EndFunc ; #FUNCTION# << MMult >> =============================================================== ; Name ..........: _MMult() ; Description ...: Returns the multiplication of two matrices (A · B). ; Syntax ........: _MMult($A, $B) ; Parameters ....: $A = matrix (number array). ; $B = matrix (number array). ; Return values .: On Success: = Returns the multiplicated matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A or $B is not an array. ; 2 = number of rows of $A and number of columns of $B are not of the same size. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[2][3]=[[1,-3,2],[1,2,7]], $B[3][2]=[[0,3],[5,2],[1,-1]], $C = _MMult($A, $B) ; _ArrayDisplay($C, "Matrix Functions") ; ====================================================================================== Func _MMult($A, $B) If Not IsArray($A) Then If IsNumber($A) And IsArray($B) Then Return _MScalar($A, $B) EndIf Return SetError(1, 0, 0) EndIf If Not IsArray($B) Then If IsNumber($B) And IsArray($A) Then Return _MScalar($B, $A) EndIf Return SetError(1, 0, 0) EndIf Local $k = UBound($B, 1) Local $l = UBound($A, 1) Local $n = UBound($B, 2) Local $m = UBound($A, 2) Local $D If $n = 0 Then $D = $B ReDim $B[$k][1] For $i = 0 To $k - 1 $B[$i][0] = $D[$i] Next $n = 1 EndIf If $m = 0 Then $D = $A ReDim $A[1][$l] For $i = 0 To $l - 1 $A[0][$i] = $D[$i] Next $m = $l $l = 1 EndIf If $m <> $k Then Return SetError(2, 0, 0) Local $C[$l][$n] For $i = 0 to $l - 1 For $j = 0 to $n - 1 $C[$i][$j] = 0 For $ii = 0 to $m - 1 $C[$i][$j] = Round($C[$i][$j] + $A[$i][$ii] * $B[$ii][$j], $Reps - 1) Next Next Next Return $C EndFunc ; #FUNCTION# << MScalar >> ============================================================= ; Name ..........: _MScalar() ; Description ...: Returns the multiplication of a scalar and a matrix. ; Syntax ........: _MScalar($A, $B) ; Parameters ....: $A = number or matrix (number array). ; $B = matrix (number array) or number. ; Return values .: On Success: = Returns the multiplicated matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A or $B is not an array or a number. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A = 5, $B[3][2]=[[0,3],[5,2],[1,-1]], $C = _MScalar($A, $B) ; _ArrayDisplay($C, "Matrix Functions") ; ====================================================================================== Func _MScalar($a, $B) If (IsNumber($B) And IsArray($a)) Then __Swap($a,$B) If Not IsNumber($a) and Not IsArray($B) Then Return SetError(1, 0, 0) Local $n = UBound($B,1) Local $m = UBound($B,2) Local $C If $m = 0 Then $C = $B ReDim $B[$n][1] For $i = 0 To $n-1 $B[$i][0] = $C[$i] Next $m = 1 EndIf For $i = 0 To $n - 1 For $j = 0 To $m - 1 $B[$i][$j] = $a * $B[$i][$j] Next Next Return $B EndFunc ; #FUNCTION# << MProd >> =============================================================== ; Name ..........: _MProd() ; Description ...: Returns the entrywise product of two matrices (Hadamard product A ° B). ; Syntax ........: _MScalar($A, $B) ; Parameters ....: $A = matrix (number array). ; $B = matrix (number array). ; Return values .: On Success: = Returns the entrywise multiplicated matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A or $B is not an array. ; 2 = $A and $B are not of the same size. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][2]=[[1,2],[5,4],[-1,1]], $B[3][2]=[[0,3],[5,2],[1,-1]], $C = _MProd($A, $B) ; _ArrayDisplay($C, "Matrix Functions") ; ====================================================================================== Func _MProd($A, $B) If Not IsArray($A) Then If IsNumber($A) And IsArray($B) Then Return _MScalar($A, $B) EndIf Return SetError(1, 0, 0) EndIf If Not IsArray($B) Then If IsNumber($B) And IsArray($A) Then Return _MScalar($B, $A) EndIf Return SetError(1, 0, 0) EndIf Local $n = UBound($A, 1) Local $m = UBound($A, 2) If $n <> UBound($B,1) Or $m <> UBound($B,2) Then Return SetError(2, 0, 0) If $m = 0 Then For $i = 0 To $n - 1 $A[$i] *= $B[$i] Next Else For $i = 0 To $n - 1 For $j = 0 To $m - 1 $A[$i][$j] *= $B[$i][$j] Next Next EndIf Return $A EndFunc ; #FUNCTION# << MPow >> ================================================================ ; Name ..........: _MPow() ; Description ...: Returns the powers of matrices (A^n). ; Syntax ........: _MPow($A[, $n]) ; Parameters ....: $A = matrix (number array). ; $n = number. ; Return values .: On Success: = Returns the power of a matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array or $n is not a number. ; 2 = $A is not a square matrix or $n is not an integer. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3]=[[1,-3,2],[1,2,7],[2,4,6]], $B = _MPow($A, 2) ; _ArrayDisplay($B, "Matrix Functions") ; ====================================================================================== Func _MPow($A, $n=2) If Not IsArray($A) Or Not IsNumber($n) Then Return SetError(1, 0, 0) If UBound($A,1) <> UBound($A,2) Or Abs($n) <> Int(Abs($n)) Then Return SetError(2, 0, 0) Local $l = UBound($A,1) If $n < 0 Then $A = _MInv($A) $n = Abs($n) EndIf If $n = 0 Then Return _MIdent($l) If $n = 1 Then Return $A Local $B = $A For $i = 2 To $n $B = _MMult($A, $B) Next Return $B EndFunc ; #FUNCTION# << MInv >> ================================================================ ; Name ..........: _MInv() ; Description ...: Returns the inverse of a matrix. ; Syntax ........: _MInv($A) ; Parameters ....: $A = matrix (number array). ; Return values .: On Success: = Returns the inverse of a matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; 2 = $A is not a square matrix. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3]=[[1,-3,2],[1,2,7],[2,4,6]], $B = _MInv($A) ; _ArrayDisplay($B, "Matrix Functions") ; ====================================================================================== Func _MInv($A) If Not IsArray($A) Then Return SetError(1, 0, 0) If UBound($A,1) <> UBound($A,2) Then Return SetError(2, 0, 0) Return __gaussj($A) EndFunc ; #FUNCTION# << MDet >> ================================================================ ; Name ..........: _MDet() ; Description ...: Returns the determinant of a matrix. ; Syntax ........: _MDet($A) ; Parameters ....: $A = matrix (number array). ; Return values .: On Success: = Returns the determinant of a matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; 2 = $A is not a square matrix. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3]=[[1,-3,2],[1,2,7],[2,4,6]] ; MsgBox(0x42040, "Matrix Functions", "_MDet( $A ) = " & _MDet($A) ) ; ====================================================================================== Func _MDet($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If $n <> UBound($A,2) Then Return SetError(2, 0, 0) Local $id[$n] Local $det __ludcmp($A, $id, $det) For $j = 1 To $n $det *= $A[$j-1][$j-1] Next Return Round($det, $Reps - 1) EndFunc ; #FUNCTION# << MRank >> =============================================================== ; Name ..........: _MRank() ; Description ...: Returns the rank of a matrix. ; Syntax ........: _MRank($A) ; Parameters ....: $A = matrix (number array). ; Return values .: On Success: = Returns the rank of a matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3]=[[1,-3,2],[1,2,7],[2,4,6]] ; MsgBox(0x42040, "Matrix Functions", "_MRank( $A ) = " & _MRank($A) ) ; ====================================================================================== Func _MRank($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1), $m = UBound($A,2) Local $r = 0 $A = _MElim($A) For $i = 0 To $n-1 ;Null-Zeilenvektor suchen For $j = 0 To $m-1 If $A[$i][$j] <> 0 Then $r += 1 ExitLoop EndIf Next Next If $r > _Min($n, $m) Then Return SetError(2, 0, 0) Return $r EndFunc ; #FUNCTION# << MTrace >> ============================================================== ; Name ..........: _MTrace() ; Description ...: Returns the trace of a matrix. ; Syntax ........: _MTrace($A) ; Parameters ....: $A = matrix (number array). ; Return values .: On Success: = Returns the trace of a matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; 2 = $A is not a square matrix. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3]=[[1,-3,2],[1,2,7],[2,4,6]] ; MsgBox(0x42040, "Matrix Functions", "_MTrace( $A ) = " & _MTrace($A) ) ; ====================================================================================== Func _MTrace($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If $n <> UBound($A,2) Then Return SetError(2, 0, 0) Local $sum = 0 For $i = 0 To $n-1 $sum += $A[$i][$i] Next Return $sum EndFunc ; #FUNCTION# << MAdj >> ================================================================ ; Name ..........: _MAdj() ; Description ...: Returns the adjugate of a matrix. ; Syntax ........: _MAdj($A) ; Parameters ....: $A = matrix (number array). ; Return values .: On Success: = Returns the adjugate of a matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; 2 = $A is not a square matrix. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3]=[[1,-3,2],[1,2,7],[2,4,6]], $B = _MAdj($A) ; _ArrayDisplay($B, "Matrix Functions") ; ====================================================================================== Func _MAdj($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If $n <> UBound($A,2) Then Return SetError(2, 0, 0) Return _MScalar(_MInv($A), _MDet($A)) EndFunc ; #FUNCTION# << MElim >> =============================================================== ; Name ..........: _MElim() ; Description ...: Returns the row reduced matrix. ; Syntax ........: _MElim($A, $B) ; Parameters ....: $A = matrix (number array). ; $f = full row reduction. ; Return values .: On Success: = Returns the row reduced matrix. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3] = [[1,2,3],[1,1,1],[2,2,2]], $B = _MElim($A) ; _ArrayDisplay($B, "Matrix Functions") ; ====================================================================================== Func _MElim($A, $f=1) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) Local $m = UBound($A,2) If $m = 0 Then For $i = 0 To $n-1 If $i = 0 Then $A[$i] = 1 Else $A[$i] = 0 EndIf Next Return $A ElseIf $m = 1 Then For $i = 0 To $n-1 If $i = 0 Then $A[$i][0] = 1 Else $A[$i][0] = 0 EndIf Next Return $A ElseIf $n = 1 Then For $i = 0 To $m-1 If $i = 0 Then $A[0][$i] = 1 Else $A[0][$i] = 0 EndIf Next Return $A EndIf Local $d, $s, $t, $p, $r = 0 Local $j = 0 For $j = 2 To $n ;reduce lower diagonal values For $i = $j To $n $s = -$A[$i-1][$j-2] $t = $A[$j-2][$j-2] If $t <> 0 Then For $k = $j - 1 To $m $A[$i-1][$k-1] = $s * $A[$j-2][$k-1] + $t * $A[$i-1][$k-1] Next EndIf Next Next If $f Then For $j = $m - 2 to 0 Step -1 ;reduce upper diagonal values For $i = $j To $n-2 $s = $A[$i][$j+1] $t = -$A[$i+1][$j+1] If $t <> 0 Then For $k = 0 to $m - 1 $A[$i][$k] = $A[$i+1][$k]*$s + $A[$i][$k]*$t Next EndIf Next Next EndIf Local $min = _Min($n, $m) For $i = 0 To $min - 1 ;reduce the values with pivot $s = $A[$i][$i] If $s Then For $j = 0 to $m - 1 $A[$i][$j] /= $s Next EndIf Next Return $A EndFunc ; #FUNCTION# << MEig >> ================================================================ ; Name ..........: _MEig() ; Description ...: Returns the eigenvalues of a matrix (by Hessenberg). ; Syntax ........: _MEig($A) ; Parameters ....: $A = matrix (number array). ; Return values .: On Success: = Returns the eigenvalues. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; 2 = $A is not a square matrix. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3]=[[0,2,-1],[2,-1,1],[2,-1,3]], $B = _MEig($A) ; _ArrayDisplay($B, "Matrix Functions") ; ====================================================================================== Func _MEig($A) If Not IsArray($A) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If $n <> UBound($A,2) Then Return SetError(2, 0, 0) Local $eig = __hqr(__elmhes(__balance($A))) Local $prod = 1, $sum = 0 For $i = 0 To $n-1 $eig[$i] = Round($eig[$i], $Reps - 2) $sum += $eig[$i] $prod += $eig[$i] Next __Sort($eig, 0, $n-1) If _MDet(_MSub($A, _MMult($eig, _MIdent($n)))) <> 0 Then Return SetError(3, 0, 0) Return $eig EndFunc ; #FUNCTION# << MEigVec >> ============================================================= ; Name ..........: _MEigVec() ; Description ...: Returns the eigenvector of an eigenvalue by a matrix. ; Syntax ........: _MEigVec($A[, $lambda]) ; Parameters ....: $A = matrix (number array). ; $lambda = number. ; Return values .: On Success: = Returns the eigenvector. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array or $lambda is not a number. ; 2 = $A is not a square matrix. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3] = [[2,-3,1],[3,1,3],[-5,2,-4]], $B = _MEigVec($A, -2) ; _ArrayDisplay($B, "Matrix Functions") ; ====================================================================================== Func _MEigVec($A, $lambda) If Not IsArray($A) or Not IsNumber($lambda) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If $n <> UBound($A,2) Then Return SetError(2, 0, 0) Local $B = _MSub($A, _MScalar(_MIdent($n), $lambda)) $B = _MElim($B) Local $x[$n] For $i = $n - 1 To 0 Step -1 If $B[$i][$i] = 0 Then $x[$i] = 1 Else Local $sum = 0 For $j = $i + 1 To $n - 1 $sum -= $B[$i][$j] * $x[$j] Next $x[$i] = $sum / $B[$i][$i] EndIf Next Local $fr, $den = 1 For $i = 0 To $n - 1 $fr = _Rat(Abs($x[$i]), 10) $den = _LCM($den, $fr[1]) Next If $den < 10/$meps Then For $i = 0 To $n - 1 $x[$i] *= $den Next EndIf Return SetError(0, $den, $x) EndFunc ; #FUNCTION# << MCross >> ============================================================== ; Name ..........: _MCross() ; Description ...: Returns the cross product of two vectors in R^3. ; Syntax ........: _MCross($A, $B) ; Parameters ....: $A = vector (number array). ; $B = vector (number array). ; Return values .: On Success: = Returns the cross product. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A or $B is not an array. ; 2 = $A and $B are not of size 3. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3] = [1,0,0], $B[3] = [0,1,0], $C = _MCross($A, $B) ; _ArrayDisplay($C, "Matrix Functions") ; ====================================================================================== Func _MCross($A, $B) If Not IsArray($A) Or Not IsArray($B) Then Return SetError(1, 0, 0) If UBound($A,1) <> 3 Or UBound($B,1) <> 3 Then Return SetError(2, 0, 0) Local $C[3] $C[0] = $A[1]*$B[2]-$A[2]*$B[1] $C[1] = $A[2]*$B[0]-$A[0]*$B[2] $C[2] = $A[0]*$B[1]-$A[1]*$B[0] Return $C EndFunc ; #FUNCTION# << MVLen >> =============================================================== ; Name ..........: _MVLen() ; Description ...: Returns the length of a vector in R^3. ; Syntax ........: _MVLen($A) ; Parameters ....: $A = vector (number array). ; Return values .: On Success: = Returns the length. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A is not an array. ; 2 = $A are not of size 3. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3] = [1,2,3] ; MsgBox(0x42040, "Matrix Functions", "_MVLen( $A ) = " & _MVLen($A) ) ; ====================================================================================== Func _MVLen($A) If Not IsArray($A) Then Return SetError(1, 0, 0) If UBound($A,1) <> 3 Then Return SetError(2, 0, 0) Return Round(Sqrt($A[0]^2 + $A[1]^2 + $A[2]^2) , $Reps - 1) EndFunc ; #FUNCTION# << MTrip >> =============================================================== ; Name ..........: _MTrip() ; Description ...: Returns the scalar triple product of three vectors in R^3. ; Syntax ........: _MTrip($A, $B, $C) ; Parameters ....: $A = vector (number array). ; $B = vector (number array). ; $C = vector (number array). ; Return values .: On Success: = Returns the triple product. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A, $B or $C is not an array. ; 2 = $A, $B or $C are not of size 3. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3] = [1,0,0], $B[3] = [0,1,0], $C[3] = [0,0,1], $D = _MTrip($A, $B, $C) ; MsgBox(0x42040, "Matrix Functions", "_MTrip( A, B, C ) = " & _MTrip($A, $B, $C) ) ; ====================================================================================== Func _MTrip($A, $B, $C) If Not IsArray($A) Or Not IsArray($B) Or Not IsArray($C) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If UBound($A,1) <> 3 Or UBound($B,1) <> 3 Or UBound($C,1) <> 3 Then Return SetError(2, 0, 0) Local $res = _MMult(_MTrans(_MCross($A, $B)), $C) Return $res[0][0] EndFunc ; #FUNCTION# << SLE >> ================================================================= ; Name ..........: _SLE() ; Description ...: Returns the solution of the system of linear equations. ; Syntax ........: _SLE($A, $y) ; Parameters ....: $A = matrix (number array). ; $y = vector (number array). ; Return values .: On Success: = Returns the solution of the SLE. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $A or $y is not an array. ; 2 = $A is not a square matrix or $y is not a vector. ; 3 = The system can not be solved. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $A[3][3]=[[1,-3,2],[1,2,7],[2,4,6]], $y[3]=[1,2,3], $C = _SLE($A, $y) ; _ArrayDisplay($C, "Matrix Functions") ; ====================================================================================== Func _SLE($A, $y) If Not IsArray($A) Or Not IsArray($y) Then Return SetError(1, 0, 0) Local $n = UBound($A,1) If $n <> UBound($A,2) Or $n <> UBound($y) Then Return SetError(2, 0, 0) If _MDet($A) = 0 Then Return SetError(3, 0, 0) Return _MMult(_MInv($A), $y) EndFunc #EndRegion Matrices Functions #Region Interpolation Functions ; #FUNCTION# << LinInt >> ============================================================== ; Name ..........: _LinInt() ; Description ...: Returns the linear interpolation of a function value array. ; Syntax ........: _LinInt($XY) ; Parameters ....: $XY = a number array. ; Return values .: On Success: = Returns a linear interpolation array. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; Author ........: Strahleman ; Remarks .......: Input of two-dimensional array only. ; Example .......: Dim $fx = "2+3x", $R = _LinInt(_Func($fx, -10, 10)) ; _ArrayDisplay($R, "Interpolation Functions") ; ====================================================================================== Func _LinInt($XY, $leps = -1) ;y = R(0) + R(1)x If Not IsArray($XY) Then Return SetError(1, 0, 0) If @NumParams = 1 Then $leps = $Reps - 1 Local $n = UBound($XY) Local $sx = 0, $sy = 0, $sxy = 0, $sx2 = 0, $sy2 = 0 For $i = 0 To $n-1 $sx += $XY[$i][0] $sy += $XY[$i][1] $sxy += $XY[$i][0] * $XY[$i][1] $sx2 += $XY[$i][0]^2 Next Local $R[2] $R[1] = Round((($sxy * $n) - ($sx * $sy)) / (($sx2 * $n) - ($sx)^2), $leps) $R[0] = Round(($sy / $n) - ($R[1] * $sx / $n), $leps) Return $R EndFunc ; #FUNCTION# << PowInt >> ============================================================== ; Name ..........: _PowInt() ; Description ...: Returns the power interpolation of a function value array. ; Syntax ........: _PowInt($XY) ; Parameters ....: $XY = a number array. ; Return values .: On Success: = Returns a power interpolation array. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; Author ........: Strahleman ; Remarks .......: Input of two-dimensional array only. ; Example .......: Dim $fx = "3.3x^2", $R = _PowInt(_Func($fx, -2, 2)) ; _ArrayDisplay($R, "Interpolation Functions") ; ====================================================================================== Func _PowInt($XY, $leps = -1) ;y = R(0) * x^R(1) If Not IsArray($XY) Then Return SetError(1, 0, 0) If @NumParams = 1 Then $leps = $Reps - 1 Local $n = UBound($XY) Local $XY1[1][2], $j = 0 Local $tmp = "" For $i = 0 To $n-1 If $XY[$i][0] <> 0 And $XY[$i][1] <> 0 Then ReDim $XY1[$j + 1][2] $XY1[$j][0] = Log(Abs($XY[$i][0])) $XY1[$j][1] = Log(Abs($XY[$i][1])) $j += 1 EndIf Next Local $R = _LinInt($XY1) $R[0] = Round(Exp($R[0]), $leps) $R[1] = Round($R[1], $leps) Return $R EndFunc ; #FUNCTION# << LogInt >> ============================================================== ; Name ..........: _LogInt() ; Description ...: Returns the logaritm interpolation of a function value array. ; Syntax ........: _LogInt($XY) ; Parameters ....: $XY = a number array. ; Return values .: On Success: = Returns a logaritm interpolation array. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; Author ........: Strahleman ; Remarks .......: Input of two-dimensional array only. ; Example .......: Dim $fx = "0.5+2*_Ln(x)", $R = _LogInt(_Func($fx, 1, 3)) ; _ArrayDisplay($R, "Interpolation Functions") ; ====================================================================================== Func _LogInt($XY, $leps = -1) ; y = R(0) + R(1)*ln(x) If Not IsArray($XY) Then Return SetError(1, 0, 0) If @NumParams = 1 Then $leps = $Reps - 2 Local $n = UBound($XY) Local $XY1[1][2], $j = 0 Local $tmp = "" For $i = 0 To $n-1 If $XY[$i][0] > 0 Then ReDim $XY1[$j + 1][2] $XY1[$j][0] = Log($XY[$i][0]) $XY1[$j][1] = $XY[$i][1] $j += 1 EndIf Next Local $R = _LinInt($XY1) $R[0] = Round($R[0], $leps) $R[1] = Round($R[1], $leps) Return $R EndFunc ; #FUNCTION# << ExpInt >> ============================================================== ; Name ..........: _ExpInt() ; Description ...: Returns the exponential interpolation of a function value array. ; Syntax ........: _ExpInt($XY) ; Parameters ....: $XY = a number array. ; Return values .: On Success: = Returns a exponential interpolation array. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; Author ........: Strahleman ; Remarks .......: Input of two-dimensional array only. ; Example .......: Dim $fx = "3*Exp(2x)", $R = _ExpInt(_Func($fx, -2, -1)) ; _ArrayDisplay($R, "Interpolation Functions") ; ====================================================================================== Func _ExpInt($XY, $leps = -1) ; y = R(0) * e^(R(1)*x) If Not IsArray($XY) Then Return SetError(1, 0, 0) If @NumParams = 1 Then $leps = $Reps - 2 Local $n = UBound($XY) Local $XY1[1][2], $j = 0 Local $tmp = "" For $i = 0 To $n-1 If $XY[$i][1] > 0 Then ReDim $XY1[$j + 1][2] $XY1[$j][0] = $XY[$i][0] $XY1[$j][1] = Log($XY[$i][1]) $j += 1 EndIf Next Local $R = _LinInt($XY1) $R[0] = Round(Exp($R[0]), $leps) $R[1] = Round($R[1], $leps) Return $R EndFunc ; #FUNCTION# << PolInt >> ============================================================== ; Name ..........: _PolInt() ; Description ...: Returns the polynomial interpolation of a function value array. ; Syntax ........: _PolInt($XY[, $n]) ; Parameters ....: $XY = a number array. ; $n = polynomial degree (number). ; Return values .: On Success: = Returns a polynomial interpolation array. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; 2 = $n is not a positive integer. ; Author ........: Strahleman ; Remarks .......: Input of two-dimensional array only. ; Example .......: Dim $fx = "x^2+2x-4", $R = _PolInt(_Func($fx, -3, 3), 2) ; _ArrayDisplay($R, "Interpolation Functions") ; ====================================================================================== Func _PolInt($XY, $n, $leps = -1) ; y = a_0 * x^0 + a_1 * x^1 + a_2 * x^2 + ... + a_n * x^n If Not IsArray($XY) Then Return SetError(1, 0, 0) If @NumParams = 2 Then $leps = $Reps - 2 If $n <= 0 Or $n <> Int($n) Then Return SetError(2, 0, 0) Local $m = UBound($XY) Local $A[$n + 1][$n + 1] Local $b[$n + 1][1] Local $sum = 0 For $i = 0 to $n For $j = 0 to $n If $i = 0 and $j = 0 Then $A[$i][$j] = $m Else Local $p = $i + $j $sum = 0 For $k = 0 to $m - 1 $sum += $XY[$k][0]^$p Next $A[$i][$j] = $sum EndIf Next $sum = 0 For $k = 0 to $m - 1 $sum += $XY[$k][0]^$i * $XY[$k][1] Next $b[$i][0] = $sum Next Local $R = _MMult(_MInv($A),$b) Local $S[$n + 1] For $i = 0 to $n $S[$i] = Round($R[$i][0], $leps) Next Return $S EndFunc ; #FUNCTION# << SinInt >> ============================================================== ; Name ..........: _SinInt() ; Description ...: Returns the sinusoidal interpolation of a function value array. ; Syntax ........: _SinInt($XY) ; Parameters ....: $XY = a number array. ; Return values .: On Success: = Returns a sinusoidal interpolation array. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; Author ........: Strahleman ; Remarks .......: Input of two-dimensional array only. ; Example .......: Dim $fx = "Sin(x)", $R = _SinInt(_Func($fx, 0, 10), 5) ; _ArrayDisplay($R, "Interpolation Functions") ; ====================================================================================== Func _SinInt($XY, $leps = -1) ; R(0) * Sin(R(1) * (x + R(2))) + R(3) If Not IsArray($XY) Then Return SetError(1, 0, 0) If @NumParams = 1 Then $leps = $Reps - 2 If Not IsArray($XY) Then Return SetError(1, 0, 0) Local $m = UBound($XY) Local $X[$m] Local $Y[$m] Local $cnt0 = 0 Local $min, $max, $imin = 0, $imax = 0 For $i = 0 to $m - 1 $X[$i] = $XY[$i][0] $Y[$i] = $XY[$i][1] If $Y[$imin] > $Y[$i] Then $imin = $i If $Y[$imax] < $Y[$i] Then $imax = $i Next Local $yMin = _Min($Y);Minimum Local $aPoly[3][2], $j = 0, $iminm1 = $imin - 1, $iminp1 = $imin + 1 If $iminm1 < 0 Then $iminm1 = 0 If $iminp1 > $m-1 Then $iminp1 = $m-1 For $i = $iminm1 To $iminp1 $aPoly[$j][1] = $Y[$i] $aPoly[$j][0] = $X[$i] $j += 1 Next Local $Poly = _PolInt($aPoly, 2) $min = -$Poly[1]/(2*$Poly[2]) $min = $Poly[2]*$min^2+$Poly[1]*$min+$Poly[0] If $min < $ymin Then $ymin = $min Local $yMax = _Max($Y);Maximum $j = 0 Local $imaxm1 = $imax - 1, $imaxp1 = $imax + 1 If $imaxm1 < 0 Then $imaxm1 = 0 If $imaxp1 > $m-1 Then $imaxp1 = $m-1 For $i = $imaxm1 To $imaxp1 $aPoly[$j][1] = $Y[$i] $aPoly[$j][0] = $X[$i] $j += 1 Next Local $Poly = _PolInt($aPoly, 2) $max = -$Poly[1]/(2*$Poly[2]) $max = $Poly[2]*$max^2+$Poly[1]*$max+$Poly[0] If $max > $ymax Then $ymax = $max Local $yoff = ($ymax+$yMin) / 2;Linearization Local $L[$m] For $i = 0 To $m-1 $L[$i] = ASin(($Y[$i]-($yoff)) / (($ymax-$yMin) / 2)) Next Local $ipos1 = -1, $ipos2 = -1 For $i = 1 To $m-1 ;first rising zero crossing If $L[$i-1] < 0 And $L[$i] >= 0 Then $ipos1 = $i ExitLoop EndIf Next For $i = 1 To $m-1 ;first falling zero crossing If $L[$i-1] > 0 And $L[$i] <= 0 Then $ipos2 = $i ExitLoop EndIf Next If $ipos1 < 0 Then Return SetError(2, 0, 0) If $ipos2 < 0 Then Return SetError(2, 0, 0) Local $ipos Local $hw = 1 Local $H[2*$hw+1][2] If _Min($ipos1, $ipos2) < $hw Then If _Max($ipos1, $ipos2) > $m - 1 - $hw Then Return SetError(2, 0, 0) Else $ipos = _Max($ipos1, $ipos2) EndIf Else $ipos = _Min($ipos1, $ipos2) EndIf For $i = 0 To 2*$hw $H[$i][0] = $XY[$ipos+$i-$hw][0] $H[$i][1] = $L[$ipos+$i-$hw] Next Local $slope = _LinInt($H);Slope Local $sl = __Sign($slope[1], $H[0][0]) Local $T = (2 * $pi) / $sl;Period Local $xoff;x-Offset If $X[0] = 0 Then $xoff = 0 Else $xoff = -$slope[0] / $sl EndIf $xoff = Mod($xoff + $T / 2, $T) - $T / 2 Local $R[4] ;Parameters $R[0] = Round(($ymax-$yMin) / 2, $leps) ;y-Strech (Amplitude) $R[1] = Round($sl, $leps) ;x-Strech (Slope) $R[2] = Round($xoff, $leps) ;x-Offset $R[3] = Round($yoff, $leps) ;y-Offset Return $R EndFunc #EndRegion Interpolation Functions #Region Analysis Functions ; #FUNCTION# << Func >> ================================================================ ; Name ..........: _Func() ; Description ...: Returns the X-Y pairs of values as array or the function value. ; Syntax ........: _Func($strFunc[, $x0[, $xn[, $n]]]) ; Parameters ....: $strFunc = String includes the mathematical term. ; $x0 = value / starting value (number). ; $xn = ending value (number). ; $n = numerical parts (number). ; Return values .: On Success: = Returns the function value / function value range. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $x0, $xn or $n is not a number. ; 2 = $n is not an integer. ; 3 = execution failure. ; Author ........: Strahleman, AspirinJunkie ; Remarks .......: ; Example .......: Dim $fx = "_Sin(x)-2x^2", $x = 1.5, $y = _Func($fx, $x) ; MsgBox(0x42040, "Analysis Functions", $fx & " = " & $y ) ; ====================================================================================== Func _Func($strFunc, $x0 = 0, $xn = 0, $n = 1000) If Not (IsString($strFunc) And IsNumber($x0) And IsNumber($xn) And IsNumber($n)) Then Return SetError(1, 0, 0) If $n <> Int($n) Then Return SetError(2, 0, 0) StringReplace($strFunc,"(","(") Local $cntExclOpen = @extended StringReplace($strFunc,")",")") Local $cntExclClose = @extended If $cntExclOpen <> $cntExclClose Or $cntExclOpen < 0 Then Return SetError(3, 0, 0) Local $strEx = $strFunc Local $F[36] = [ "bitxor", "exp", "_max", "_cmplxcheck", "_cmplxtostr", "_cmplx", _ "_normexc", "_betaexc", "_triexc", "_recexc", "_gamexc", "_texc", _ "_chisqexc", "_lognormexc", "_exppdf", "_expcdf", "_expinv", _ "_expmean", "_expvar", "_expskw", "_expexc", "_exprnd", "_fexc", _ "_wblexc", "_parexc", "_uniexc", "_berexc", "_binoexc", "_nbinoexc", _ "_hygeexc", "_poissexc", "_geomexc", "_logexc", "_excess", "_sumxy", _ "_funcextrema"] $strEx = StringLower($strEx) For $i = 0 to UBound($F)-1 ;save Functions within 'x' in functionname $strEx = StringReplace($strEx, $F[$i], "$F["&$i&"]") Next $strEx = StringReplace($strEx, "x", "*x"); Start Parser $strEx = StringReplace($strEx, "**", "*") $strEx = StringReplace($strEx, "(*", "(") $strEx = StringReplace($strEx, "*)", ")") $strEx = StringReplace($strEx, "+*", "+") $strEx = StringReplace($strEx, "-*", "-") $strEx = StringReplace($strEx, "/*", "/") $strEx = StringReplace($strEx, "^*", "^") If Stringleft($strEx, 1) = "*" Then $strEx = StringTrimLeft($strEx, 1); End Parser $strEx = StringReplace($strEx, "x", "#") For $i = 0 to UBound($F)-1 $strEx = StringReplace($strEx, "$F["&$i&"]", $F[$i]) Next $strFunc = StringReplace($strEx, "#", "x") If $x0 >= $xn Or @NumParams = 2 Then $strEx = StringReplace($strEx, "#", "$x0") Local $res = Execute($strEx) If @error Then Return SetError(3,0,"") Else Return $res EndIf Else $strEx = StringReplace($strEx, "#", "$XY[$i][0]") Local $XY[$n + 1][2] For $i = 0 to $n $XY[$i][0] = ($xn - $x0) * $i / $n + $x0 Local $res = Round(Execute($strEx), $Reps - 1) If @error Then Return SetError(3,0,"") Else $XY[$i][1] =$res EndIf Next Return $XY EndIf EndFunc ; #FUNCTION# << Dif >> ================================================================= ; Name ..........: _Dif() ; Description ...: Returns the derived X-Y pairs of values as array. ; Syntax ........: _Dif($XY) ; Parameters ....: $XY = a number array. ; Return values .: On Success: = Returns derived function value range. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $diff = _Dif(_Func('x^2', -2, 2)) ; _ArrayDisplay($diff, "Analysis Functions") ; ====================================================================================== Func _Dif($XY) If Not IsArray($XY) Then Return SetError(1, 0, 0) Local $n = UBound($XY, 1) Local $D[$n][2] For $i = 1 To $n-2 $D[$i][0] = $XY[$i][0] $D[$i][1] = ($XY[$i+1][1] - $XY[$i-1][1]) / ($XY[$i+1][0] - $XY[$i-1][0]) Next $D[0][0] = $XY[0][0] Local $m = ($D[2][1] - $D[1][1]) / ($XY[2][0] - $XY[1][0]) $D[0][1] = $m * $D[0][0] + $D[1][1] - $m * $D[1][0] $D[$n-1][0] = $XY[$n-1][0] $m = ($D[$n-3][1] - $D[$n-2][1]) / ($XY[$n-3][0] - $XY[$n-2][0]) $D[$n-1][1] = $m * $D[$n-1][0] + $D[$n-2][1] - $m * $D[$n-2][0] return $D EndFunc ; #FUNCTION# << Int >> ================================================================= ; Name ..........: _Int() ; Description ...: Returns the definite integral. ; Syntax ........: _Int($XY[, $a[, $b]]) ; Parameters ....: $XY = a number array. ; $a = lower limit value. ; $b = upper limit value. ; Return values .: On Success: = Returns the definite integral value. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array or $a or $b is not a number. ; Author ........: Strahleman ; Remarks .......: Simpson's rule ; Example .......: Dim $int = _Int(_Func('x^2', -2, 2), 0, 1) ; MsgBox(0x42040, "Analysis Functions", "F = " & $int ) ; ====================================================================================== Func _Int($XY, $a = "", $b = "") If Not(@NumParams = 1 Or @NumParams = 3) Then Return SetError(1, 0, 0) If Not IsArray($XY) Then Return SetError(1, 0, 0) Local $n = UBound($XY) If @NumParams = 1 Then $a = $XY[0][0] $b = $XY[$n-1][0] EndIf If Not(IsNumber($a) And IsNumber($b)) Then Return SetError(1, 0, 0) Local $i1, $i2 For $i = 0 To $n-2 If $XY[$i][0] <= $a And $XY[$i+1][0] > $a Then $i1 = $i If $XY[$i][0] < $b And $XY[$i+1][0] >= $b Then $i2 = $i+1 Next Local $In = ($XY[$i1][1] + $XY[$i2][1]) Local $f = 4 For $i = 1 to $i2 - $i1 - 1 $In += $XY[$i + $i1][1] * $f If ($f = 4) Then $f = 2 else $f = 4 EndIf Next Return $In * ($XY[$i1+1][0] - $XY[$i1][0] ) / 3 EndFunc ; #FUNCTION# << FuncRoots >> =========================================================== ; Name ..........: _FuncRoots() ; Description ...: Returns the roots of a function.. ; Syntax ........: _FuncRoots($XY) ; Parameters ....: $XY = a number array. ; Return values .: On Success: = Returns the root values. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; 2 = No Roots found. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $Xn = _FuncRoots(_Func("x^3-x", -2, 2)) ; _ArrayDisplay($Xn, "Analysis Functions") ; ====================================================================================== Func _FuncRoots($XY) If Not IsArray($XY) Then Return SetError(1, 0, 0) Local $n = UBound($XY) Local $k = 0 Local $D[1][2] For $i = 0 to $n-2 If ($XY[$i][1] < 0 And $XY[$i + 1][1] >= 0) Or ($XY[$i][1] >= 0 And $XY[$i + 1][1] < 0) Then Local $l = $XY[$i][0] Local $r = $XY[$i + 1][0] If Abs($XY[$i][1]) < $meps Then $D[$k][0] = Round($XY[$i][0], $Reps - 1) $D[$k][1] = 0 Else Local $m[2], $q = 0 $m[0] = ($l + $r) / 2 Local $s = ($XY[$i + 1][1] - $XY[$i][1]) / ($XY[$i + 1][0] - $XY[$i][0]) Local $y = $XY[$i][1] - ($XY[$i][0] * $s) $m[1] = $m[0] * $s + $y While (Abs($m[1]) > $meps And Abs($r - $l) > $meps) And $q < 1000 If $m[1] < 0 Then $l = $m[0] Else $r = $m[0] EndIf $m[0] = ($l + $r) / 2 $m[1] = $m[0] * $s + $y $q += 1 WEnd $D[$k][0] = Round($m[0], $Reps - 1) $D[$k][1] = 0 EndIf $k += 1 ReDim $D[$k + 1][2] EndIf Next If $k = 0 Then Return SetError(2, 0, $D) Else ReDim $D[$k][2] Return $D EndIf EndFunc ; #FUNCTION# << FuncExtrema >> ========================================================= ; Name ..........: _FuncExtrema() ; Description ...: Returns the extreme points of a function.. ; Syntax ........: _FuncExtrema($XY[, $type]) ; Parameters ....: $XY = a number array. ; $type = a number (1 .. Minima, 2 .. Maxima, 3 .. both [Standard]). ; Return values .: On Success: = Returns the extreme point values. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; 2 = No Extrema found. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $Xn = _FuncExtrema(_Func("x^3-x", -2, 2), 2) ; _ArrayDisplay($Xn, "Analysis Functions") ; ====================================================================================== Func _FuncExtrema($XY, $type = 0x03) If Not IsArray($XY) Then Return SetError(1, 0, 0) Local $Minima = 0x01 Local $Maxima = 0x02 Local $n = UBound($XY) Local $D[1][2] Local $k = 0 For $i = 0 to $n - 3 If $XY[$i][1] >= $XY[$i + 1][1] And $XY[$i + 1][1] <= $XY[$i + 2][1] And BitAND($type, $Minima) = $Minima Then Local $a ,$b, $c Local $M[3][3], $S[3][1] For $j = 0 to 2 $M[$j][0] = $XY[$i + $j][0]^2 $M[$j][1] = $XY[$i + $j][0] $M[$j][2] = 1 $S[$j][0] = $XY[$i + $j][1] Next Local $R = _MMult(_MInv($M), $S) $D[$k][0] = -$R[1][0] / (2 * $R[0][0]) $D[$k][1] = $R[0][0] * $D[$k][0]^2 + $R[1][0] * $D[$k][0] + $R[2][0] $k += 1 ReDim $D[$k + 1][2] ElseIf $XY[$i][1] <= $XY[$i + 1][1] And $XY[$i + 1][1] >= $XY[$i + 2][1] And BitAND($type, $Maxima) = $Maxima Then Local $a ,$b, $c Local $M[3][3], $S[3][1] For $j = 0 to 2 $M[$j][0] = $XY[$i + $j][0]^2 $M[$j][1] = $XY[$i + $j][0] $M[$j][2] = 1 $S[$j][0] = $XY[$i + $j][1] Next Local $R = _MMult(_MInv($M), $S) $D[$k][0] = -$R[1][0] / (2 * $R[0][0]) $D[$k][1] = $R[0][0] * $D[$k][0]^2 + $R[1][0] * $D[$k][0] + $R[2][0] $k += 1 ReDim $D[$k + 1][2] EndIf Next If $k = 0 Then Return SetError(2, 0, $D) Else ReDim $D[$k][2] Return $D EndIf EndFunc ; #FUNCTION# << FuncTurn >> ============================================================ ; Name ..........: _FuncTurn() ; Description ...: Returns the turning points of a function.. ; Syntax ........: _FuncTurn($XY) ; Parameters ....: $XY = a number array. ; Return values .: On Success: = Returns the turning point values. ; On Failure: = Sets @Error and returns 0. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; 2 = No turning point found. ; Author ........: Strahleman ; Remarks .......: ; Example .......: Dim $Xn = _FuncTurn(_Func("x^3-x", -2, 2)) ; _ArrayDisplay($Xn, "Analysis Functions") ; ====================================================================================== Func _FuncTurn($XY) If Not IsArray($XY) Then Return SetError(1, 0, 0) Local $n = UBound($XY) Local $Z = _Dif($XY) Local $R = _FuncExtrema($Z) Local $m = UBound($R) Local $D[1][2], $k = 0, $w For $j = 0 to $m - 1 For $i = 0 To $n - 2 If $XY[$i][0] <= $R[$j][0] And $XY[$i + 1][0] > $R[$j][0] Then If Abs($XY[$i][0] - $R[$j][0]) < $meps Then $D[$k][0] = Round($XY[$i][0], $Reps - 1) $D[$k][1] = Round($XY[$i][1], $Reps - 1) Else $w = ($XY[$i + 1][1] - $XY[$i][1]) / ($XY[$i + 1][0] - $XY[$i][0]) $D[$k][0] = Round($R[$j][0], $Reps - 1) $D[$k][1] = Round($w * $R[$j][0] + $XY[$i][1] - $w * $XY[$i][0], $Reps - 1) EndIf $k += 1 ReDim $D[$k + 1][2] EndIf Next Next If $k = 0 Then Return SetError(2, 0, $D) Else ReDim $D[$k][2] Return $D EndIf EndFunc #EndRegion Analysis Functions #Region Financial Functions ; #FUNCTION# << simpInterest >> ======================================================== ; Name ..........: _simpInterest() ; Description ...: Returns the simple interest. ; Syntax ........: _simpInterest($K_0, $p[, $n]) ; Parameters ....: $K_0 = initial balance (number). ; $p = simple interest rate per period (number). ; $n = time periods (number). ; Return values .: On Success: = Returns the simple interest. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $K_0, $p or $n is not a number. ; 1 = $n is not positive. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Financial Functions", "I = " & Round(_simpInterest(2500, 0.1299 / 12, 3), 2) & Chr(164) ) ; ====================================================================================== Func _simpInterest($K_0, $p, $n = 1) If Not(IsNumber($K_0) And IsNumber($p) And IsNumber($n)) Then Return SetError(1, 0, 0) If $n < 0 Then Return SetError(2, 0, 0) Return $K_0 * $p * $n EndFunc ; #FUNCTION# << compInterest >> ======================================================== ; Name ..........: _compInterest() ; Description ...: Returns the compound interest. ; Syntax ........: _compInterest($K_0, $p[, $n]) ; Parameters ....: $K_0 = initial balance (number). ; $p = simple interest rate per period (number). ; $n = time periods (number). ; Return values .: On Success: = Returns the compound interest. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 = $K_0, $p or $n is not a number. ; 1 = $n is not positive. ; Author ........: Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Financial Functions", "I = " & Round(_compInterest(1500, 0.043 / 4, 4 * 6), 2) & Chr(164) ) ; ====================================================================================== Func _compInterest($K_0, $p, $n = 1) If Not(IsNumber($K_0) And IsNumber($p) And IsNumber($n)) Then Return SetError(1, 0, 0) If $n < 0 Then Return SetError(2, 0, 0) Return $K_0 * ((1 + $p)^$n - 1) EndFunc #EndRegion Financial Functions #Region Base Functions ; #FUNCTION# << Base >> ================================================================ ; Name ..........: _Base() ; Description ...: Returns the new base value. ; Syntax ........: _Base($n[, $base1[, $base2]]) ; Parameters ....: $n = starting value (string). ; $base1 = old base (number). ; $base1 = new base (number). ; Return values .: On Success: = Returns the new base value. ; On Failure: = Sets @Error and returns False. ; @ERROR: = 0 = No error. ; 1 = $n, $base1 or $base2 is not a number. ; 2 = $base1 or $base2 is not an integer. ; 3 = $base1 or $base2 is outside the domain. ; 4 = converting failure. ; Author ........: eukalyptus, Strahleman ; Remarks .......: ; Example .......: MsgBox(0x42040, "Base Functions", 125 & " = " & _Base(125, 10, 16)) ; ====================================================================================== Func _Base($n, $base1 = 10, $base2 = 2) If Not (IsNumber($n) And IsNumber($base1) And IsNumber($base2)) Then Return SetError(1, 0, False) If $base1 <> Int($base1) Or $base2 <> Int($base2) Then Return SetError(2, 0, False) If $base1 < 2 Or $base1 > 128 Or $base2 < 2 Or $base2 > 128 Then Return SetError(3, 0, False) Local $iNum, $aRes, $tChr = DllStructCreate("char[64];") If $base1 <> 10 Then $aRes = DllCall("msvcrt.dll", "uint64:cdecl", "_strtoui64", "str", $n, "ptr", 0, "int", $base1) If @error Then Return SetError(4, @error, False) $iNum = $aRes[0] Else $iNum = Int($n) EndIf Local $pChr = DllStructGetPtr($tChr) $aRes = DllCall("msvcrt.dll", "ptr:cdecl", "_i64toa", "int64", $iNum, "ptr", $pChr, "int", $base2) If @error Then Return SetError(4, @error, False) Return DllStructGetData($tChr, 1) EndFunc #EndRegion Base Functions #Region Plotter ; #FUNCTION# << Plot >> ================================================================ ; Name ..........: _Plot() ; Description ...: Returns a window with a 2-D line plot of the data values is created. ; Syntax ........: _Plot($XY[, $strtitle[, $m[, $GraphColor[, $GraphBG[, $hints]]]]]) ; Parameters ....: $XY = a number array. ; $strtilte = Title (string). ; $m = 2x2 limits of Axis [[y1,y2],[x1,x2]] (number array). ; $GraphColor = line colornumber 0xRRGGBB (hex number). ; $GraphBG = Background colornumber 0xRRGGBB (hex number). ; $hints = 1 .. Cross, 2 .. Rectangle, 3 .. Circle (number). ; Return values .: On Success: = Returns the plot windows. ; On Failure: = Sets @Error and returns False. ; @ERROR: = 0 = No error. ; 1 = $XY is not an array. ; Author ........: Strahleman ; Remarks .......: ; Example .......: _plot( _Func('x^3-2x', -2, 2.3), "f(x) = x^3-2x") ; ====================================================================================== Func _Plot($XY, $strtitle= "", $m=0, $GraphColor = 0x000000, $GraphBG = 0xFFFFFF, $hints = False) Opt("MouseCoordMode", 0) Opt("GUIOnEventMode", 1) Opt("GUICloseOnESC", 1) If Not IsArray($XY) Then Return SetError(1, 0, 0) Local $Graph Local Const $GUI_Height = -50+@DesktopHeight*0.6 Local Const $GUI_Width = $GUI_Height + 10 Local Const $graTextWidth = 20 Local Const $graTextHeight = 15 Local Const $graFrame = 15 Local Const $graWidth = $GUI_Width - 2*$graFrame - $graTextWidth Local Const $graHeight = $GUI_Height - 2*$graFrame - $graTextHeight Global Const $gyMin = $graFrame Global Const $gyMax = $graFrame + $graHeight Global Const $gxMin = $graFrame + $graTextWidth Global Const $gxMax = $graFrame + $graTextWidth + $graWidth Global $yMin = 0 Global $yMax = 0 Global $xMin = 0 Global $xMax = 0 If @NumParams = 4 Then $GraphBG = BitXOR($GraphColor, 0xFFFFFF) ;complementary colors Local Const $colBG = BitOR(0xFF000000, $GraphBG) Local Const $colLine = BitOR(0xFF000000, $GraphColor) Local Const $colGrid = BitXOR($GraphColor, 0xFFCCCCCC) Local Const $colFrame = BitXOR($GraphColor, 0xFF888888) Local Const $widFrame = 2 Local Const $widZero = 2 Local Const $widGrid = 1 Local Const $widLine = 1 OnAutoItExitRegister("__GUIClose") Global $hGUI = GUICreate($strTitle, $GUI_Width, $GUI_Height, -1, -1, 0x00C80000) GUISetOnEvent(-3, '__GUIClose', $hGUI) _GDIPlus_Startup() GUISetState(@SW_SHOW, $hGUI) Local $graphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) Local $hBrushBG = _GDIPlus_BrushCreateSolid($colBG) Local $hBrushHint = _GDIPlus_BrushCreateSolid($colLine) Local $hPenFrame = _GDIPlus_PenCreate($colFrame, $widFrame) Local $hPenZero = _GDIPlus_PenCreate($colgrid, $widZero) Local $hPenGrid = _GDIPlus_PenCreate($colgrid, $widGrid) Local $hPenLine = _GDIPlus_PenCreate($colLine, $widLine) Local $bound = UBound($XY, 1) Local $arrX[$bound] Local $arrY[$bound] For $i = 0 to $bound-1 $arrX[$i] = $XY[$i][0] $arrY[$i] = $XY[$i][1] Next If IsArray($m) Then $yMin = $m[0][0] $yMax = $m[0][1] $xMin = $m[1][0] $xMax = $m[1][1] Else $yMax = _Max($arrY) $yMin = _Min($arrY) $xMax = _Max($arrX) $xMin = _Min($arrX) EndIf _GDIPlus_GraphicsFillRect($graphics, $gxMin, $gyMin, $graWidth, $graHeight, $hBrushBG) ;Background Local $hBrushAxisLbl = _GDIPlus_BrushCreateSolid(0xFF000000) Local $hFormatAxisLbl = _GDIPlus_StringFormatCreate() Local $hFFTAxisLbl = _GDIPlus_FontFamilyCreate("Arial") Local $hFontAxisLbl = _GDIPlus_FontCreate($hFFTAxisLbl, 10, 1) _GDIPlus_StringFormatSetAlign($hFormatAxisLbl, 1) ; 2 - linksbündig, 1 - zentriert, 0 - rechtsbündig Local $tLayout, $aInfo, $j Local $xparts = Floor(Log(Abs($xMax-$xMin))/Log(10)-0.4) Local $xpartsmin = Round($xMin, -$xparts) If $xpartsmin < $xMin Then $xpartsmin += 10^($xparts) Local $xpartsmax = Round($xMax, -$xparts) If $xpartsmax > $xMax Then $xpartsmax -= 10^($xparts) Local $xrng = Abs($xpartsmax-$xpartsmin)/10^($xparts) + 1 $j = 0 For $i = $xpartsmin To $xpartsmax Step 10^($xparts) $j += 1 _GDIPlus_GraphicsDrawLine($graphics, __GetPixx($i), __GetPixy($yMin), __GetPixx($i), __GetPixy($yMax), $hPenGrid) If $xrng > 8 And (Mod($j,2) Or Round($i, -$xparts) = 0) Then $tLayout = _GDIPlus_RectFCreate(__GetPixx($i), $graHeight + $graFrame + 5, 0, 0) $aInfo = _GDIPlus_GraphicsMeasureString($graphics, Round($i, -$xparts), $hFontAxisLbl, $tLayout, $hFormatAxisLbl) _GDIPlus_GraphicsDrawStringEx($graphics, Round($i, -$xparts), $hFontAxisLbl, $aInfo[0], $hFormatAxisLbl, $hBrushAxisLbl) ElseIf $xrng <= 8 Then $tLayout = _GDIPlus_RectFCreate(__GetPixx($i), $graHeight + $graFrame + 5, 0, 0) $aInfo = _GDIPlus_GraphicsMeasureString($graphics, Round($i, -$xparts), $hFontAxisLbl, $tLayout, $hFormatAxisLbl) _GDIPlus_GraphicsDrawStringEx($graphics, Round($i, -$xparts), $hFontAxisLbl, $aInfo[0], $hFormatAxisLbl, $hBrushAxisLbl) EndIf Next Local $yparts = Floor(Log(Abs($yMax-$yMin))/Log(10)-0.4) Local $ypartsmin = Round($yMin, -$yparts) If $ypartsmin < $yMin Then $ypartsmin += 10^($yparts) Local $ypartsmax = Round($yMax, -$yparts) If $ypartsmax > $yMax Then $ypartsmax -= 10^($yparts) Local $yrng = Abs($ypartsmax-$ypartsmin)/10^($yparts) + 1 $j = 0 For $i = $ypartsmin To $ypartsmax Step 10^($yparts) $j += 1 _GDIPlus_GraphicsDrawLine($graphics, __GetPixx($xMin), __GetPixy($i), __GetPixx($XMax), __GetPixy($i), $hPenGrid) If $yrng > 8 And (Mod($j,2) Or Round($i, -$yparts) = 0) Then $tLayout = _GDIPlus_RectFCreate($graFrame, __GetPixy($i)-8, 0, 0) $aInfo = _GDIPlus_GraphicsMeasureString($graphics, Round($i, -$yparts), $hFontAxisLbl, $tLayout, $hFormatAxisLbl) _GDIPlus_GraphicsDrawStringEx($graphics, Round($i, -$yparts), $hFontAxisLbl, $aInfo[0], $hFormatAxisLbl, $hBrushAxisLbl) ElseIf $yrng <= 8 Then $tLayout = _GDIPlus_RectFCreate($graFrame, __GetPixy($i)-8, 0, 0) $aInfo = _GDIPlus_GraphicsMeasureString($graphics, Round($i, -$yparts), $hFontAxisLbl, $tLayout, $hFormatAxisLbl) _GDIPlus_GraphicsDrawStringEx($graphics, Round($i, -$yparts), $hFontAxisLbl, $aInfo[0], $hFormatAxisLbl, $hBrushAxisLbl) EndIf Next If $xMin < 0 And $xMax > 0 Then _GDIPlus_GraphicsDrawLine($graphics, __GetPixx(0), __GetPixy($yMin), __GetPixx(0), __GetPixy($yMax), $hPenZero) EndIf If $yMin < 0 And $yMax > 0 Then _GDIPlus_GraphicsDrawLine($graphics, __GetPixx($xMin), __GetPixy(0), __GetPixx($XMax), __GetPixy(0), $hPenZero) EndIf For $i = 0 to UBound($arrX) - 2 If $xMin < $arrX[$i] And $xMax > $arrX[$i + 1] And $yMin < $arrY[$i] And $yMax > $arrY[$i] Then _ _GDIPlus_GraphicsDrawLine($graphics, __GetPixx($arrX[$i]), __GetPixy($arrY[$i]), __GetPixx($arrX[$i + 1]), __GetPixy($arrY[$i + 1]), $hPenLine) If $hints Then Local $hintl = __GetPixx($arrX[$i])-2, $hintt = __GetPixy($arrY[$i])-2 If __GetPixx($xMin) < $hintl And __GetPixx($xMax) > $hintl+4 And __GetPixy($yMin) > $hintt And __GetPixy($yMax) < $hintt+4 Then Switch $hints Case 1 _GDIPlus_GraphicsDrawLine($graphics, $hintl, $hintt, $hintl+4, $hintt+4, $hPenLine) _GDIPlus_GraphicsDrawLine($graphics, $hintl+4, $hintt, $hintl, $hintt+4, $hPenLine) case 2 _GDIPlus_GraphicsFillRect($graphics, $hintl, $hintt, 4, 4, $hBrushHint) case 3 _GDIPlus_GraphicsFillEllipse($graphics, $hintl, $hintt, 4, 4, $hBrushHint) case Else EndSwitch EndIf EndIf Next _GDIPlus_GraphicsDrawRect($graphics, $gxMin, $gyMin, $graWidth, $graHeight, $hPenFrame) _GDIPlus_BrushDispose($hBrushBG) _GDIPlus_BrushDispose($hBrushAxisLbl) _GDIPlus_PenDispose($hPenFrame) _GDIPlus_PenDispose($hPenGrid) _GDIPlus_PenDispose($hPenZero) _GDIPlus_PenDispose($hPenLine) While Sleep(1000) WEnd EndFunc Func __GetPixx($xvalue) Return ((($xvalue - $xmin) * ($gxmax - $gxmin)) / ($xmax - $xmin)) + $gxmin EndFunc Func __GetPixy($yvalue) Return ((($yvalue - $ymin) * ($gymin - $gymax)) / ($ymax - $ymin)) + $gymax EndFunc Func __GUIClose() _GDIPlus_Shutdown() GUIDelete() Exit EndFunc #EndRegion Plotter #EndRegion FUNCTION #cs EOF - Math2.au3 #ce