Jump to content

Math UDF


NerdFencer
 Share

Recommended Posts

This is a Math UDF that I put together...

This is able to function as a far more capable replacement for the default math UDF

UPDATED!!!

Here's a list of functions in it

------Misc------

_Factorial,_Heron,_GCD,_LCM,_Distance,_Log,_Root,_Interest,_Triangular,_Bound

------Angles------

_Angle,_Reference,_Simplify

------Boolean Checks------

_IsFactorial,_IsTriangular,_IsEven,_IsOdd, IsBound

------Trig------

_Versin,_Coversin,_Exsec,_Excsc,_Haversin,_Hacoversin,_Sin,_Cos,_Tan,_Csc,_Sec,_Cot

------Inverse Trig------

_ASin,_ACos,_ATan,_ACsc,_ASec,_ACot,_AVersin,_ACoversin,_AExsec,_AExcsc,_AHaversin,_AHacoversin

------Hyperbolic------

_Sinh,_Cosh,_Tanh,_Coth,_Sech,_Csch

------Inverse Hyperbolic------

_ASinh,_ACosh,_ATanh,_ACoth,_ASech,_ACsch

------List Functions------

_MaxLst,_MinLst,_AvgLst,_SumLst, _ASortLst, _DSortLst

------Standard Functions found in Math Include------

_Min,_Max,_Radian,_Degree,_MathCheckDiv,_ATan2

Update 1-

All Trig and inverse trig functions can now be used in Radians, Degrees, or Gradians

Added _Sin, _Cos, _Tan, _ASin, _ACos, _ATan, _Reference, and _Simplify

Slight upgrade to _Angle

Update 2-

Added support for Minutes and Seconds to _Angle (and thus to all other trig functions)

Added _MaxLst, _MinLst, _AvgLst, _SumLst

Fixed _Factorial

Upgraded Error returns and function headers

Update 3-

Added _Min, _Max, _Degree, _Radian, _MathCheckDiv, _ATan2

Fixed _Factorial

This UDF can now function as a complete replacement for the default math include :)

All duplicate functions will act like the default ones unless additional arguments are supplied

Update 4-

Lots of Bug-Fixes

Started adding example scripts

Update 5-

More bug fixes

Finished example scripts

Added _IsFactorial and _Bound

_IsFactorial and _IsTriangular now also function as the inverse operations of _Factorial and _Triangular

Update 6-

Cleanup

Added _IsBound, _ASortLst, and _DSortLst

Slight bug fixes

MathFunctions.au3

MathFunctions_update_2_.au3

MathFunctions_Update_3.au3

MathFunctions_Update_4.zip

MathFunctions_Update_5.zip

MathFunctions_Update_6.zip

Edited by NerdFencer

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Link to comment
Share on other sites

NerdFencer

All Trig and inverse trig functions can now be used in Radians, Degrees, or Gradians

Added _Sin, _Cos, _Tan, _ASin, _ACos, _ATan, _Reference, and _Simplify

Slight upgrade to _Angle

This funcions are already in autoit ? (Cos;Sin;Tan / ACos;ASin;ATan)

Cheers, FireFox.

Link to comment
Share on other sites

This funcions are already in autoit ? (Cos;Sin;Tan / ACos;ASin;ATan)

The ones in this UDF support Radians, Degrees, and Gradians, where as the ones built into AutoIt only support Radians

You call them like...

_Sin(30,"deg")

or

_Sin(30,"grad")

without the second argument, they default to Radians

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Link to comment
Share on other sites

Looks nice.... noticed a couple of things...

You have the "_Triangular" Function header/description for the "_IsTriangular" Function

The parameters for the _Distance are only referred to as "two points", can that be longitude/latitude, screen pixels, etc

... or I just need a real edumacation?.!

Your script quite is long, that's why I write like this

Func _Distance($nX1,$nY1,$nX2,$nY2)
    If Not IsNumber($nX1) Then Return SetError(1,0,"")
    If Not IsNumber($nY1) Then Return SetError(2,0,"")
    If Not IsNumber($nX2) Then Return SetError(3,0,"")
    If Not IsNumber($nY2) Then Return SetError(4,0,"")
    Return Sqrt(($nX2-$nX1)^2+($nY2-$nY1)^2)
EndFunc

However your style is more to the format of the UDF's included in Autoit

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Looks nice.... noticed a couple of things...

You have the "_Triangular" Function header/description for the "_IsTriangular" Function

The parameters for the _Distance are only referred to as "two points", can that be longitude/latitude, screen pixels, etc

... or I just need a real edumacation?.!

Your script quite is long, that's why I write like this

Func _Distance($nX1,$nY1,$nX2,$nY2)
    If Not IsNumber($nX1) Then Return SetError(1,0,"")
    If Not IsNumber($nY1) Then Return SetError(2,0,"")
    If Not IsNumber($nX2) Then Return SetError(3,0,"")
    If Not IsNumber($nY2) Then Return SetError(4,0,"")
    Return Sqrt(($nX2-$nX1)^2+($nY2-$nY1)^2)
EndFunc

However your style is more to the format of the UDF's included in Autoit

8)

You are good to do this, maybe you can rename this functions better than me.

Func _StraightLineEquationDetBy2Points($anPoint1,$anPoint2)
    Local $nXValue, $nYValue, $nFreeValue
    Local $sXValue, $sYValue, $sFreeValue
    Local $avEquation[4]
    If Not IsArray($anPoint1) Or Not IsArray($anPoint2) Then
        SetError(1)
        Return 0
    ElseIf UBound($anPoint1) <> 2  Or UBound($anPoint2) <> 2 Then
        SetError(2)
        Return 0        
    EndIf
    $nXValue = ($anPoint2[1]-$anPoint1[1])
    $nYValue = ($anPoint2[0]-$anPoint1[0])*(-1)
    $nFreeValue = ((-1)*(($anPoint2[1]-$anPoint1[1])*$anPoint1[0]))+(($anPoint2[0]-$anPoint1[0])*$anPoint1[1])
    $sXValue = String($nXValue)
    $sYValue = String($nYValue)
    $sFreeValue = String($nFreeValue)
    If StringLeft($sYValue,1) <> "-" Then $sYValue = "+" & $sYValue
    If StringLeft($sFreeValue,1) <> "-" Then $sFreeValue = "+" & $sFreeValue
    $avEquation[0] = $sXValue & "x" & $sYValue & "y" & $sFreeValue & "=0"
    $avEquation[1] = $nXValue
    $avEquation[2] = $nYValue
    $avEquation[3] = $nFreeValue
    Return $avEquation
EndFunc;==>_StraightLineEquationDetBy2Points

Func _DistanceFromPointToStraightLine($anPoint,$avEquation)
    Local $nDistance
    If Not IsArray($anPoint) Or Not IsArray($avEquation) Then
        SetError(1)
        Return -1
    ElseIf UBound($anPoint) <> 2 Or UBound($avEquation) <> 4 Then
        SetError(2)
        Return -1
    EndIf
    $nDistance = Abs(($avEquation[1]*$anPoint[0])+($avEquation[2]*$anPoint[1])+$avEquation[3])/Sqrt(($avEquation[1]^2)+($avEquation[2]^2))
    Return $nDistance
EndFunc;==>_DistanceFromPointToStraightLine

Func _AreaOfTriangleDetBy3Points($anPoint1,$anPoint2,$anPoint3)
    Local $nPositive, $nNegative, $nDelta
    Local $nArea
    If Not IsArray($anPoint1) Or Not IsArray($anPoint2) Or Not IsArray($anPoint3) Then
        SetError(1)
        Return 0
    ElseIf UBound($anPoint1) <> 2  Or UBound($anPoint2) <> 2 Or UBound($anPoint3) <> 2 Then
        SetError(2)
        Return 0        
    EndIf
    $nPositive = ($anPoint1[0]*$anPoint2[1])+($anPoint1[1]*$anPoint3[0])+($anPoint2[0]*$anPoint3[1])
    $nNegative = -(($anPoint2[1]*$anPoint3[0])+($anPoint1[1]*$anPoint2[0])+($anPoint1[0]*$anPoint3[1]))
    $nDelta = $nPositive + $nNegative
    MsgBox(0,"",$nDelta)
    $nArea = (1/2)*Abs($nDelta)
    Return $nArea
EndFunc;==>_AreaOfTriangleDetBy3Points
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

You have the "_Triangular" Function header/description for the "_IsTriangular" Function

Thanks, that will be fixed in the next update

The parameters for the _Distance are only referred to as "two points", can that be longitude/latitude, screen pixels, etc

... or I just need a real edumacation?.!

The distance formula works for two points regardless of what plane they are on... Although with latitude and longitude you would technically have to compensate for the curvature of the earth...

Your script quite is long, that's why I write like this

Func _Distance($nX1,$nY1,$nX2,$nY2)
    If Not IsNumber($nX1) Then Return SetError(1,0,"")
    If Not IsNumber($nY1) Then Return SetError(2,0,"")
    If Not IsNumber($nX2) Then Return SetError(3,0,"")
    If Not IsNumber($nY2) Then Return SetError(4,0,"")
    Return Sqrt(($nX2-$nX1)^2+($nY2-$nY1)^2)
EndFunc
Hmmm, I like this... hello regex replace :)

Thanks for the feedback, I'll be posting an updated version later tonight

Note: before you find it yourself, _Factorial is broken

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Link to comment
Share on other sites

You are good to do this, maybe you can rename this functions better than me.

Func _StraightLineEquationDetBy2Points($anPoint1,$anPoint2)
    Local $nXValue, $nYValue, $nFreeValue
    Local $sXValue, $sYValue, $sFreeValue
    Local $avEquation[4]
    If Not IsArray($anPoint1) Or Not IsArray($anPoint2) Then
        SetError(1)
        Return 0
    ElseIf UBound($anPoint1) <> 2  Or UBound($anPoint2) <> 2 Then
        SetError(2)
        Return 0        
    EndIf
    $nXValue = ($anPoint2[1]-$anPoint1[1])
    $nYValue = ($anPoint2[0]-$anPoint1[0])*(-1)
    $nFreeValue = ((-1)*(($anPoint2[1]-$anPoint1[1])*$anPoint1[0]))+(($anPoint2[0]-$anPoint1[0])*$anPoint1[1])
    $sXValue = String($nXValue)
    $sYValue = String($nYValue)
    $sFreeValue = String($nFreeValue)
    If StringLeft($sYValue,1) <> "-" Then $sYValue = "+" & $sYValue
    If StringLeft($sFreeValue,1) <> "-" Then $sFreeValue = "+" & $sFreeValue
    $avEquation[0] = $sXValue & "x" & $sYValue & "y" & $sFreeValue & "=0"
    $avEquation[1] = $nXValue
    $avEquation[2] = $nYValue
    $avEquation[3] = $nFreeValue
    Return $avEquation
EndFunc;==>_StraightLineEquationDetBy2Points

Func _DistanceFromPointToStraightLine($anPoint,$avEquation)
    Local $nDistance
    If Not IsArray($anPoint) Or Not IsArray($avEquation) Then
        SetError(1)
        Return -1
    ElseIf UBound($anPoint) <> 2 Or UBound($avEquation) <> 4 Then
        SetError(2)
        Return -1
    EndIf
    $nDistance = Abs(($avEquation[1]*$anPoint[0])+($avEquation[2]*$anPoint[1])+$avEquation[3])/Sqrt(($avEquation[1]^2)+($avEquation[2]^2))
    Return $nDistance
EndFunc;==>_DistanceFromPointToStraightLine

Func _AreaOfTriangleDetBy3Points($anPoint1,$anPoint2,$anPoint3)
    Local $nPositive, $nNegative, $nDelta
    Local $nArea
    If Not IsArray($anPoint1) Or Not IsArray($anPoint2) Or Not IsArray($anPoint3) Then
        SetError(1)
        Return 0
    ElseIf UBound($anPoint1) <> 2  Or UBound($anPoint2) <> 2 Or UBound($anPoint3) <> 2 Then
        SetError(2)
        Return 0        
    EndIf
    $nPositive = ($anPoint1[0]*$anPoint2[1])+($anPoint1[1]*$anPoint3[0])+($anPoint2[0]*$anPoint3[1])
    $nNegative = -(($anPoint2[1]*$anPoint3[0])+($anPoint1[1]*$anPoint2[0])+($anPoint1[0]*$anPoint3[1]))
    $nDelta = $nPositive + $nNegative
    MsgBox(0,"",$nDelta)
    $nArea = (1/2)*Abs($nDelta)
    Return $nArea
EndFunc;==>_AreaOfTriangleDetBy3Points
... none of these functions are in the UDF....

Honestly, I think these function names are a bit too long, specific, but long

Note: Heron is the name of the Greek? mathematician who found the formula for determining the area of a triangle from the length of the three sides, hence the name of the function

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Link to comment
Share on other sites

I don't know how math you know but in your _Factorial function (maybe is not only) I found several things wrong:

1. Does not work

2. In mathematics there are some rules that are established at the beginning.

For example factorial function it makes sense only with positive integers.

Anyway I like your initiative to write mathematical functions in the AutoIt.

I tried to rewrite quickly your factorial function:

Func _Factorial($nNumber)
    If Not IsNumber($nNumber) Then
        SetError(1)
        Return ""
    ElseIf $nNumber < 0 Or Int($nNumber) <> $nNumber Then
        SetError(2)
        Return ""
    EndIf
    $nResult = 1
    For $num = 1 To $nNumber
        $nResult *= $num
    Next
    Return $nResult
EndFunc

MsgBox(0,"",_Factorial(5))
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

I don't know how math you know but in your _Factorial function (maybe is not only) I found several things wrong:

1. Does not work

2. In mathematics there are some rules that are established at the beginning.

The factorial function and triangular function split early on (some code reuse), but i forgot to go back and finish it.

I did notice that after posting update 1, it has been fixed in update 2 (about to post)

thanks for the help tho :)

Edited by NerdFencer

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Link to comment
Share on other sites

I added Update 2, take a look :)

Func _Factorial($nNumber)
    If Not IsInt($nNumber) Then Return SetError(1,0,"")
    $nOutput = 1
    For $nInt=Int($nNumber) To 1 Step -1
        $nOutput = $nOutput * $nInt
    Next
    Return $nOutput
EndFunc

No, it's wrong. Try _Factorial(-5) returns 1, this is wrong. Factorial function is only for positive integers.

Add something like: If $nNumber < 0 Then SetError(2,0,"")

When the words fail... music speaks.

Link to comment
Share on other sites

No, it's wrong. Try _Factorial(-5) returns 1, this is wrong. Factorial function is only for positive integers.

Add something like: If $nNumber < 0 Then SetError(2,0,"")

oh..... good point, ill do that

Thx :)

BTW, just posted Update 3, ill modify in a moment with the fix

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Link to comment
Share on other sites

I don't want to be mad but you should look what return _Log(1,0). :)

I see, I need to put a lower bound on the numbers b/c log is only for + numbers, thx :o

Its good to get all the bugs worked out, thanks for the help again :D

Here is the fixed version

Func _Log($nNumber,$nBase = 10)
    If Not IsInt($nNumber) Then Return SetError(1,0,"")
    If Not IsInt($nBase) Then Return SetError(2,0,"")
    If $nNumber<=0 Then Return SetError(-1,0,"")
    If $nBase<=0 Then Return SetError(-2,0,"")
    Return Log($nNumber)/Log($nBase)
EndFunc

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Link to comment
Share on other sites

Looks better visually, however I noted some major errors

C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(223,21) : WARNING: $nX: possibly used before declaration.
    If Not IsNumber($nX)
    ~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(223,42) : WARNING: $nY: possibly used before declaration.
    If Not IsNumber($nX) Or Not IsNumber($nY)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(960,43) : WARNING: $nAngle: possibly used before declaration.
    Return _Angle("rad",$sFormat,Sin(($nAngle*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(1236,69) : WARNING: $nTimesPer_Interval: possibly used before declaration.
    Return $nInitial*(1+$nRate/$nTimesPerInterval)^($nTimesPer_Interval*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(223,21) : ERROR: $nX: undeclared global variable.
    If Not IsNumber($nX)
    ~~~~~~~~~~~~~~~~~~~^

also after that, place this on the top

Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declare

and fix those, after that..

in SciTe Press > Tools > Tidy Autoit Source

and that will correct all spacing, caps, etc

thx

8)

NEWHeader1.png

Link to comment
Share on other sites

Looks better visually, however I noted some major errors

C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(223,21) : WARNING: $nX: possibly used before declaration.
    If Not IsNumber($nX)
    ~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(223,42) : WARNING: $nY: possibly used before declaration.
    If Not IsNumber($nX) Or Not IsNumber($nY)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(960,43) : WARNING: $nAngle: possibly used before declaration.
    Return _Angle("rad",$sFormat,Sin(($nAngle*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(1236,69) : WARNING: $nTimesPer_Interval: possibly used before declaration.
    Return $nInitial*(1+$nRate/$nTimesPerInterval)^($nTimesPer_Interval*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Examples\My Stuff\trash.au3(223,21) : ERROR: $nX: undeclared global variable.
    If Not IsNumber($nX)
    ~~~~~~~~~~~~~~~~~~~^

also after that, place this on the top

Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declare

and fix those, after that..

in SciTe Press > Tools > Tidy Autoit Source

and that will correct all spacing, caps, etc

thx

8)

Wow!!! :)

Ill be fixing this for a while...

Thanks for the heads up, Ill get right on this

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...