(Classic) math
The math
library provides a number of useful math functions as well as special functions for plane-angle conversions, digests, trigonometry, and great-circle calculations. More are added as needed.
General Functions
random
random(<num>)
- generate a random number. The value is between 0 and the number given as an argument.
r = math:random(999)
The given example will return random digits between 0 and 3 inclusive:
rule random_example { select when pageview ".*" pre{ r = math:random(3); } { notify("Random Number","r = #{r}") with sticky = true; } }
exponent
exp(<num>)
- return e (natural logrithm) raised to the <num>th power
n = math:exp(2);
int
int(<num>)
- the integer value of <num>
n = math:int(2.999);
absolute value
abs(<num>)
- absolute value of <num>
n = math:abs(-12);
logarithm
log(<num>)
- natural log of <num>
n = math:log(2);
square root
sqrt(<num>)
- square root of <num>
n = math:sqrt(2);
power
power(<num1>,<num1>)
- <num1> to the <num2>th power.
n = math:power(2,3); // n = 8
ceiling
ceiling(<num>)
- the smallest integer not less than <num>
n = math:ceiling(2.78); // n = 3
floor
floor(<num>) - the largest integer not greater than <num>
n = math:floor(2.78); // n = 2
round
round(<num> - convert <num> to the nearest integer.
n = math:round(2.1); // n = 2 m = math:round(2.7); // n = 3
Plane Angle Conversions
deg2rad
deg2rad(<num>)
- convert degrees to radians
r = math:deg2rad(d);
grad2rad
grad2rad(<num>)
- convert gradians to radians
r = math:grad2rad(g);
rad2deg
rad2deg(<num>)
- convert radians to degrees
d = math:rad2deg(r);
grad2deg
grad2deg(<num>)
- convert gradians to degrees
d = math:grad2deg(g);
deg2grad
deg2grad(<num>)
- convert degrees to gradians
g = math:deg2grad(d);
rad2grad
rad2grad(<num>)
- convert radians to gradians
g = math:rad2grad(r);
Digest functions
md5
md5(<str>)
- calculate the hex string representing the 16 byte md5 digest of the argument string.
d = math:md5("Hello" + foo)
sha1
sha1(<str>)
- calculate the hex string representing the 20 byte SHA1 digest of the argument string.
d = math:sha1("Hello" + foo)
hmac_sha1
hmac_sha1(<str>, <key>)
- calculate the hmac signature of a string with a key, encoded as a binary string.
sig = math:hmac_sha1("Hello" + foo, "mykey")
hmac_sha1_hex
hmac_sha1_hex(<str>, <key>)
- calculate the hmac signature of a string with a key, encoded as a hex string.
sig = math:hmac_sha1_hex("Hello" + foo, "mykey")
hmac_sha1_base64
hmac_sha1_base64(<str>, <key>)
- calculate the hmac signature of a string with a key, encoded as base64.
sig = math:hmac_sha1_base64("Hello" + foo, "mykey")
hmac_sha256
hmac_sha256(<str>, <key>)
- calculate the hmac signature of a string with a key, encoded as a binary string.
sig = math:hmac_sha256("Hello" + foo, "mykey")
hmac_sha256_hex
hmac_sha256_hex(<str>, <key>)
- calculate the hmac signature of a string with a key, encoded as a hex string.
sig = math:hmac_sha256_hex("Hello" + foo, "mykey")
hmac_sha256_base64
hmac_sha256_base64(<str>, <key>)
- calculate the hmac signature of a string with a key, encoded as base64.
sig = math:hmac_sha256_base64("Hello" + foo, "mykey")
Trigonometry
Trig functions expect radians as arguments unless otherwise noted
Basic Trig
tan(<num>)
- calculate the tangent of <num>
d = math:tan(r)
sin(<num>)
- calculate the sine of <num>
d = math:sin(r)
cos(<num>)
- calculate the cosine of <num>
d = math:cos(r)
Trig Co-functions
cot(<num>)
- calculate the co-tangent of <num>
d = math:cot(r)
sec(<num>)
- calculate the secant of <num>
d = math:sec(r)
csc(<num>)
- calculate the co-secant of <num>
d = math:csc(r)
Arcus functions
atan(<num>)
- calculate the arctangent of <num>
d = math:atan(r)
asin(<num>)
- calculate the arcsine of <num>
d = math:asin(r)
acos(<num>)
- calculate the arccosine of <num>
d = math:acos(r)
Arcus Co-functions
acot(<num>)
- calculate the arc co-tangent of <num>
d = math:acot(r)
asec(<num>)
- calculate the arc secant of <num>
d = math:asec(r)
acsc(<num>)
- calculate the arc co-secant of <num>
d = math:acsc(r)
Hyperbolic functions
tanh(<num>)
- calculate the hyperbolic tangent of <num>
d = math:tanh(r)
sinh(<num>)
- calculate the hyperbolic sine of <num>
d = math:sinh(r)
cosh(<num>)
- calculate the hyperbolic cosine of <num>
d = math:cosh(r)
Hyperbolic Co-functions
coth(<num>)
- calculate the hyperbolic co-tangent of <num>
d = math:coth(r)
sech(<num>)
- calculate the hyperbolic secant of <num>
d = math:sech(r)
csch(<num>)
- calculate the hyperbolic co-secant of <num>
d = math:csch(r)
Inverse functions
atanh(<num>)
- calculate the hyperbolic arc tangent of <num>
d = math:atanh(r)
asinh(<num>)
- calculate the hyperbolic arc sine of <num>
d = math:asinh(r)
cosh(<num>)
- calculate the hyperbolic arc cosine of <num>
d = math:acosh(r)
acoth(<num>)
- calculate the hyperbolic arc co-tangent of <num>
d = math:acoth(r)
asech(<num>)
- calculate the hyperbolic arc secant of <num>
d = math:asech(r)
acsch(<num>)
- calculate the hyperbolic arc co-secant of <num>
d = math:acsch(r)
Great Circle Calculations
Great Circle Calculations are based upon a mathematical model, not geographical. Geographically, the zero axis for Latitude is located on the equator. The mathematical model places the zero axis for Latitude at the north pole.
The practical result of this model is that for geographical co-ordinates, the radian expression of the latitude must be subtracted from π/2. This will be demonstrated in the examples that follow the function definitions.
Distance
great_circle_distance(<long0>, π/2 - <lat0>, <long1>, π/2 - <lat1> |, r)
- compute the distance along a great circle given two points on a spherical surface. r is an optional parameter for the radius of the sphere. If left unspecified, r defaults to 1-the unit sphere-and is expressed in radians.
r90 = math:pi()/2; rEk = 6378; // radius of the Earth in km // point a lata = 40.4267290; lnga = -111.9025358; // point b latb = 43.8310154; lngb = -111.7747790; // convert co-ordinates to radians rlata = math:deg2rad(lata); rlnga = math:deg2rad(lnga); rlatb = math:deg2rad(latb); rlngb = math:deg2rad(lngb); // distance between two co-ordinates in radians dR = math:great_circle_distance(rlnga,r90 - rlata, rlngb,r90 - rlatb); // distance between two co-ordinates in kilometers dE = math:great_circle_distance(rlnga,r90 - rlata, rlngb,r90 - rlatb, rEk);
Direction
great_circle_direction(<long0>, π/2 - <lat0>, <long1>, π/2 - <lat1>)
- compute the direction of the great circle that connects the two points note: the direction is not static. It changes for each position along the great circle. Thus, the great circle direction is also referred to as a bearing
// using the same co-ordinates as the great distance example b = math:great_circle_direction(rlnga,r90 - rlata, rlngb,r90 - rlatb);
Mid Point
great_circle_midpoint(<long0>, π/2 - <lat0>, <long1>, π/2 - <lat1>)
- compute the co-ordinates along the great circle that lay at the midpoint of two points
// using the same co-ordinates as the great distance example b = math:great_circle_midpoint(rlnga,r90 - rlata, rlngb,r90 - rlatb); lng = b[0]; lat = b[1];
Way Point
great_circle_waypoint(<long0>, π/2 - <lat0>, <long1>, π/2 - <lat1>,<f>)
- compute the co-ordinates along the great circle that lay at fraction f of the distance between points
// using the same co-ordinates as the great distance example // find waypoint 3/4ths of the distance along the great circle b = math:great_circle_waypoint(rlnga,r90 - rlata, rlngb,r90 - rlatb,.75); lng = b[0]; lat = b[1];
Copyright Picolabs | Licensed under Creative Commons.