-
Notifications
You must be signed in to change notification settings - Fork 8
Math Library API
Convenience class for calculating a numerical average.
EXAMPLE
uses Math/Averager
local avg = Averager<<Int32>>()
avg.add( 48 )
avg.add( 49 )
avg.add( 51 )
avg.add( 52 )
@trace avg.sum # 200
@trace avg.count # 4
@trace avg.average # 50.0
| Name | Type | Description |
|---|---|---|
| count | Int32 | |
| sum | $DataType |
| Signature | Return Type | Description |
|---|---|---|
| add( value:$DataType ) | ||
| average() | $DataType | |
| description() | String | |
| operator==( other:Averager<<$DataType>> ) | Logical | |
| print_to( buffer:PrintWriter ) | ||
| to<<Object>>() | Boxed<<Averager<<$DataType>>>> | |
| to<<String>>() | String |
| Signature | Return Type | Description |
|---|---|---|
| create( axis:XYZ, angle:Degrees ) | AxisAngle | |
| create( axis:XYZ, angle:Radians ) | AxisAngle |
| Name | Type | Description |
|---|---|---|
| angle | Radians | |
| axis | XYZ |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| operator==( other:AxisAngle ) | Logical | |
| print_to( buffer:PrintWriter ) | ||
| to<<Object>>() | Boxed<<AxisAngle>> | |
| to<<String>>() | String |
Tracks the best (value,score) pair among all considered pairs.
EXAMPLE
# Find the index of the smallest real number in a list.
uses Math/Best
local numbers = [3.1, 0.2, 5.8, -1.0, 4.0]
local best = Best<<Int32,Real>>( (a,b) => a < b )
# The comparison function (a,b) should return true when score 'a' is better than score 'b'.
forEach (value at i in numbers)
best.consider( i, value )
endForEach
println "Lowest: numbers[$] = $" (best.value,best.score)
# Lowest: numbers[3] = -1.0
| Signature | Return Type | Description |
|---|---|---|
| create( better_fn:Function($ScoreType,$ScoreType)->Logical ) | Best<<$DataType,$ScoreType>> |
| Name | Type | Description |
|---|---|---|
| better_fn | Function($ScoreType,$ScoreType)->Logical | |
| exists | Logical | |
| score | $ScoreType | |
| value | $DataType |
| Signature | Return Type | Description |
|---|---|---|
| clear() | ||
| consider( candidate_value:$DataType, candidate_score:$ScoreType ) | Logical | Returns 'true' if a new best value is stored. |
| description() | String | |
| operator==( other:Best<<$DataType,$ScoreType>> ) | Logical | |
| print_to( buffer:PrintWriter ) | ||
| to<<Logical>>() | Logical | |
| to<<Object>>() | Boxed<<Best<<$DataType,$ScoreType>>>> | |
| to<<String>>() | String |
Tracks the best value among all considered values.
EXAMPLE
# Find the smallest real number in a list.
uses Math/Best
local numbers = [3.1, 0.2, 5.8, -1.0, 4.0]
local best = Best<<Real>>( (a,b) => a < b )
forEach (value in numbers)
best.consider( value )
endForEach
println "Lowest: $" (best.value)
# Lowest: numbers[3] = -1.0
| Signature | Return Type | Description |
|---|---|---|
| create( better_fn:Function($DataType,$DataType)->Logical ) | Best<<$DataType>> |
| Name | Type | Description |
|---|---|---|
| better_fn | Function($DataType,$DataType)->Logical | |
| exists | Logical | |
| value | $DataType |
| Signature | Return Type | Description |
|---|---|---|
| clear() | ||
| consider( candidate_value:$DataType ) | Logical | Returns 'true' if a new best value is stored. |
| description() | String | |
| operator==( other:Best<<$DataType>> ) | Logical | |
| print_to( buffer:PrintWriter ) | ||
| to<<Logical>>() | Logical | |
| to<<Object>>() | Boxed<<Best<<$DataType>>>> | |
| to<<String>>() | String |
extends Object
Allows integers of any size to be represented and manipulated. BigInt operations are significantly slower than operations on primitive Int32/Int64 types so BigInt objects should only be used when you require integers with more than 64 bits.
BigInt objects are immutable; operations produce new BigInt objects as side effects instead of altering operands.
BigInt numbers are modeled using sign-magnitude; they are not 2's complement and the sign cannot be directly changed through a bitwise operation (only incidentally, as when a negative number is AND'd with zero to produce a zero result).
BigInt values are stored as a series of unsigned 16-bit Int32 values. Their total bit size (in multiples of 16) is only what's necessary to represent a number's magnitude.
Similar to Java's BigInt class.
Examples:
println( BigInt(123456) ^ BigInt("1000101",2) )
println( "2^100 = $" (BigInt(2) ^ 100) )
| Name | Type | Description |
|---|---|---|
| i64_limit | BigInt | |
| one | BigInt | |
| setup_complete | Logical | |
| ten | BigInt | |
| ten_e144 | BigInt | |
| ten_e18 | BigInt | |
| ten_e36 | BigInt | |
| ten_e72 | BigInt | |
| values | BigInt[] | |
| zero | BigInt |
| Signature | Return Type | Description |
|---|---|---|
| create( n:Int32 ) | BigInt | |
| create( n:Int64 ) | BigInt | Creates a BigInt object out of an Int64 value. If 'n' is 0..15 then a pre-defined BigInt object will be returned instead. |
| init_class() |
| Name | Type | Description |
|---|---|---|
| data | Int32[] | |
| sign_flag | Int | 1 or -1 |
| Signature | Return Type | Description |
|---|---|---|
| init( other:BigInt ) | ||
| init( value:Int64, new_object:Logical ) | ||
| init( value:String, [base=10:Int32] ) | Initializes this BigInt to a value specified as a string of digits in a given base. 'base' can be 2, 10, or 16. | |
| bits() | Int32 | |
| divide_and_mod( n:BigInt ) | BigIntDivideAndModResult | |
| divide_and_mod( n:Int64 ) | BigIntDivideAndModResult | |
| format( fmt:String ) | String |
fmt - #: right-justify in this many spaces; 0 fill if begins with 0 - ,: put comma separators |
| is_negative() | Logical | |
| is_valid_int32() | Logical | |
| is_valid_int64() | Logical | |
| is_zero() | Logical | |
| normalized() | BigInt | Removes excess zero values on the most signficant end and changes a negative zero to a positive zero. |
| operator-() | BigInt | |
| operator%( n:BigInt ) | BigInt | |
| operator%( n:Int64 ) | BigInt | |
| operator&( n:BigInt ) | BigInt | |
| operator&( n:Int64 ) | BigInt | |
| operator*( n:BigInt ) | BigInt | |
| operator*( n:Int64 ) | BigInt | |
| operator+( n:BigInt ) | BigInt | |
| operator+( n:Int64 ) | BigInt | |
| operator-( n:BigInt ) | BigInt | |
| operator-( n:Int64 ) | BigInt | |
| operator/( n:BigInt ) | BigInt | |
| operator/( n:Int64 ) | BigInt | |
| operator:<<:( bits:Int32 ) | BigInt | |
| operator:>>:( bits:Int32 ) | BigInt | |
| operator:>>>:( bits:Int32 ) | BigInt | |
| operator<>( n:Int64 ) | Int | |
| operator<>( other:BigInt ) | Int | |
| operator==( n:Int64 ) | Logical | |
| operator==( other:BigInt ) | Logical | |
| operator^( n:BigInt ) | BigInt | |
| operator^( n:Int64 ) | BigInt | |
| operator|( n:BigInt ) | BigInt | |
| operator|( n:Int64 ) | BigInt | |
| operator~( n:BigInt ) | BigInt | |
| operator~( n:Int64 ) | BigInt | |
| sign() | Int32 | Returns -1, 0, or 1. |
| sqrt() | BigInt | |
| to<<Int32>>() | Int32 | |
| to<<Int64>>() | Int64 | |
| to<<String>>() | String | |
| to<<String>>( &binary, &hex ) | String | |
| to<<String>>( base:Int32 ) | String |
| Signature | Return Type | Description |
|---|---|---|
| create( division:BigInt, modulo:BigInt ) | BigIntDivideAndModResult |
| Name | Type | Description |
|---|---|---|
| division | BigInt | |
| modulo | BigInt |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| operator==( other:BigIntDivideAndModResult ) | Logical | |
| print_to( buffer:PrintWriter ) | ||
| to<<Object>>() | Boxed<<BigIntDivideAndModResult>> | |
| to<<String>>() | String |
extends Object
| Signature | Return Type | Description |
|---|---|---|
| create( degrees:Radians ) | Degrees | |
| create( value:Real ) | Degrees | |
| operator?( degrees:Degrees ) | Logical |
| Name | Type | Description |
|---|---|---|
| value | Real |
| Signature | Return Type | Description |
|---|---|---|
| clamped( min:Degrees, max:Degrees ) | Degrees | |
| cos() | Real | |
| delta_to( other:Degrees ) | Degrees | Returns the smallest number of degrees necessary to get from this angle to the specified other angle. For example, Degrees(270).delta_to(Degrees(0)) yields Degrees(90), since turning 90 degrees will get you to 0 degrees "faster" than turning -270 degrees. |
| description() | String | |
| floor() | Degrees | |
| operator-() | Degrees | |
| operator%( degrees:Real ) | Degrees | |
| operator%( other:Degrees ) | Degrees | |
| operator*( degrees:Real ) | Degrees | |
| operator*( other:Degrees ) | Degrees | |
| operator+( degrees:Real ) | Degrees | |
| operator+( other:Degrees ) | Degrees | |
| operator-( degrees:Real ) | Degrees | |
| operator-( other:Degrees ) | Degrees | |
| operator/( degrees:Real ) | Degrees | |
| operator/( other:Degrees ) | Degrees | |
| operator<>( other:Degrees ) | Real | |
| operator<>( other_value:Real ) | Real | |
| operator==( other:Degrees ) | Logical | |
| operator==( other_value:Real ) | Logical | |
| operator^( degrees:Real ) | Degrees | |
| operator^( other:Degrees ) | Degrees | |
| print_to( buffer:PrintWriter ) | ||
| sin() | Real | |
| tan() | Real | |
| to<<Object>>() | Boxed<<Degrees>> | |
| to<<Radians>>() | Radians | |
| to<<Real>>() | Real | |
| to<<String>>() | String |
extends Object
| Signature | Return Type | Description |
|---|---|---|
| create() | Matrix | |
| create( r0c0:Real, r1c0:Real, r2c0:Real, r3c0:Real, r0c1:Real, r1c1:Real, r2c1:Real, r3c1:Real, r0c2:Real, r1c2:Real, r2c2:Real, r3c2:Real, r0c3:Real, r1c3:Real, r2c3:Real, r3c3:Real ) | Matrix | |
| decimal_digit_count( value:Real ) | Int32 | |
| identity() | Matrix | |
| look_at( origin:XYZ, target:XYZ, up:XYZ ) | Matrix | |
| perspective( fov_y:Radians, aspect_ratio:Real, z_near:Real, z_far:Real ) | Matrix | Returns a right-handed projection matrix (OpenGL is also right-hand). |
| perspective( left:Real, top:Real, right:Real, bottom:Real, near:Real, far:Real ) | Matrix | Based on https://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml |
| rotate( degrees:DegreesXYZ ) | Matrix | |
| rotate( radians:RadiansXYZ ) | Matrix | |
| rotate( theta:Radians, axis:XYZ ) | Matrix | |
| rotate_x( theta:Radians ) | Matrix | |
| rotate_y( theta:Radians ) | Matrix | |
| rotate_z( theta:Radians ) | Matrix | |
| scale( k:Real ) | Matrix | |
| scale( k:Real, origin:XY ) | Matrix | |
| scale( k:XY ) | Matrix | |
| scale( k:XY, origin:XY ) | Matrix | |
| scale( k:XYZ ) | Matrix | |
| scale( k:XYZ, origin:XYZ ) | Matrix | |
| shear( x_yz:XY, y_xz:XY, z_xy:XY ) | Matrix | x_yz: amount that x is sheared proportional to changes in y and z y_xz: amount that y is sheared proportional to changes in x and z z_xy: amount that z is sheared proportional to changes in x and y |
| shear( xy:XY ) | Matrix | |
| transform( position:XYZ, size:XY, anchor:Anchor, rotation:Radians ) | Matrix | Returns a transformation matrix that will position a [0,0 1x1] coordinate box in 2DX space - everything is in 2D except for the Z coordinate that is applied at the end to move the image closer to (z > 0) or farther away from (z < 0) the camera. |
| transform( size:XY, anchor:Anchor ) | Matrix |
Performs a partial 2D transform for size and anchor. The typical full tranform set for a [0,0 1x1] box in the order they'd be pushed on a transform stack: translate( position ) rotate_z( rotation ) scale( size ) # peformed by this method translate( -anchor.position ) # peformed by this method Use transform(position,size,anchor,rotation) to perform all transforms at once in 2DX space. Use this method combined with other transforms if other transformations are needed, such as rotation around the x or y axis. |
| translate( delta:XY ) | Matrix | |
| translate( delta:XYZ ) | Matrix | |
| whole_digit_count( value:Real ) | Int32 |
| Name | Type | Description |
|---|---|---|
| r0c0 | Real | |
| r0c1 | Real | |
| r0c2 | Real | |
| r0c3 | Real | |
| r1c0 | Real | |
| r1c1 | Real | |
| r1c2 | Real | |
| r1c3 | Real | |
| r2c0 | Real | |
| r2c1 | Real | |
| r2c2 | Real | |
| r2c3 | Real | |
| r3c0 | Real | |
| r3c1 | Real | |
| r3c2 | Real | |
| r3c3 | Real |
| Signature | Return Type | Description |
|---|---|---|
| count() | Int32 | |
| description() | String | |
| get( index:Int32 ) | Real | |
| operator*( other:Matrix ) | Matrix | |
| operator*( v:XY ) | XY | |
| operator*( v:XYZ ) | XYZW | |
| operator*( v:XYZW ) | XYZW | |
| operator==( other:Matrix ) | Logical | |
| print_to( buffer:PrintWriter ) | ||
| set( index:Int32, value:Real ) | ||
| to<<Matrix32>>() | Matrix32 | |
| to<<Object>>() | Boxed<<Matrix>> | |
| to<<String>>() | String |
| Signature | Return Type | Description |
|---|---|---|
| create( r0c0:Real32, r1c0:Real32, r2c0:Real32, r3c0:Real32, r0c1:Real32, r1c1:Real32, r2c1:Real32, r3c1:Real32, r0c2:Real32, r1c2:Real32, r2c2:Real32, r3c2:Real32, r0c3:Real32, r1c3:Real32, r2c3:Real32, r3c3:Real32 ) | Matrix32 |
| Name | Type | Description |
|---|---|---|
| r0c0 | Real32 | |
| r0c1 | Real32 | |
| r0c2 | Real32 | |
| r0c3 | Real32 | |
| r1c0 | Real32 | |
| r1c1 | Real32 | |
| r1c2 | Real32 | |
| r1c3 | Real32 | |
| r2c0 | Real32 | |
| r2c1 | Real32 | |
| r2c2 | Real32 | |
| r2c3 | Real32 | |
| r3c0 | Real32 | |
| r3c1 | Real32 | |
| r3c2 | Real32 | |
| r3c3 | Real32 |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| operator==( other:Matrix32 ) | Logical | |
| print_to( buffer:PrintWriter ) | ||
| to<<Matrix>>() | Matrix | |
| to<<Object>>() | Boxed<<Matrix32>> | |
| to<<String>>() | String |
Adapted from the Iirlicht Quaternion by Nikolaus Gebhardt
| Signature | Return Type | Description |
|---|---|---|
| create( [x=0:Real], [y=0:Real], [z=0:Real], [w=1.0:Real] ) | Quaternion | |
| create( aa:AxisAngle ) | Quaternion | |
| create( from:XYZ, to:XYZ ) | Quaternion | |
| create( m:Matrix ) | Quaternion | |
| create( rotation:RadiansXYZ ) | Quaternion | |
| identity() | Quaternion |
| Name | Type | Description |
|---|---|---|
| w | Real | |
| x | Real | |
| y | Real | |
| z | Real |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| dot( other:Quaternion ) | Real | |
| inverted() | Quaternion | |
| is_identity() | Logical | |
| linear_tween( target:Quaternion, t:Real ) | Quaternion | |
| matches( other:Quaternion, tolerance:Real ) | Logical | |
| normalized() | Quaternion | |
| operator*( n:Real ) | Quaternion | |
| operator*( other:Quaternion ) | Quaternion | |
| operator*( v:XYZ ) | XYZ | |
| operator+( other:Quaternion ) | Quaternion | |
| operator/( n:Real ) | Quaternion | |
| operator==( other:Quaternion ) | Logical | |
| print_to( buffer:PrintWriter ) | ||
| rotated( by_angles:RadiansXYZ ) | Quaternion | |
| to<<AxisAngle>>() | AxisAngle | |
| to<<Matrix>>() | Matrix | |
| to<<Object>>() | Boxed<<Quaternion>> | |
| to<<RadiansXYZ>>() | RadiansXYZ | |
| to<<String>>() | String | |
| to<<Matrix>>( handle:XYZ, translate:XYZ ) | Matrix | |
| tween( target:Quaternion, t:Real, [threshold=0.05:Real] ) | Quaternion |
| Signature | Return Type | Description |
|---|---|---|
| create( degrees:Degrees ) | Radians | |
| create( value:Real ) | Radians | |
| operator?( radians:Radians ) | Logical |
| Name | Type | Description |
|---|---|---|
| value | Real |
| Signature | Return Type | Description |
|---|---|---|
| clamped( min:Radians, max:Radians ) | Radians | |
| cos() | Real | |
| delta_to( other:Radians ) | Radians | Returns the smallest number of radians necessary to get from this angle to the specified other angle. For example, Radians(pi*3/2).delta_to(Radians(0)) yields Radians(pi/2), since turning pi/2 radians will get you to 0 radians "faster" than turning -pi*3/2 radians. |
| description() | String | |
| operator-() | Radians | |
| operator%( other:Radians ) | Radians | |
| operator%( radians:Real ) | Radians | |
| operator*( other:Radians ) | Radians | |
| operator*( radians:Real ) | Radians | |
| operator+( other:Radians ) | Radians | |
| operator+( radians:Real ) | Radians | |
| operator-( other:Radians ) | Radians | |
| operator-( radians:Real ) | Radians | |
| operator/( other:Radians ) | Radians | |
| operator/( radians:Real ) | Radians | |
| operator<>( other:Degrees ) | Real | |
| operator<>( other_value:Real ) | Real | |
| operator==( other:Degrees ) | Logical | |
| operator==( other:Radians ) | Logical | |
| operator==( other_value:Real ) | Logical | |
| operator^( other:Radians ) | Radians | |
| operator^( radians:Real ) | Radians | |
| print_to( buffer:PrintWriter ) | ||
| sin() | Real | |
| tan() | Real | |
| to<<Degrees>>() | Degrees | |
| to<<Object>>() | Boxed<<Radians>> | |
| to<<Real>>() | Real | |
| to<<String>>() | String |
extends Object
This random number generator produces evenly distributed pseudo-random values.
You may create a Random object with a 'seed' value; generators created with the same seed always return the same sequence of random numbers. If a seed is not specified then the current System.time_ms is used.
Under the hood Random uses the PCG32 algorithm adapted from: https://github.com/imneme/pcg-c-basic
| Name | Type | Description |
|---|---|---|
| seed | Int64 | The original state value. |
| state | Int64 | The current state used to produce the next psuedo-random number. |
| stream | Int64 | A PCG32-specific modifier that essentially acts as an additional seed. |
| Signature | Return Type | Description |
|---|---|---|
| init() | ||
| init( existing:Random ) | Constructs this Random generator to be a duplicate of an existing Random generator. They will produce identical results given identical calls. | |
| init( seed:Int64, [stream=0x14057b7ef767814f:Int64] ) | Constructs a Random generator with the specified seed and optional stream parameter. 'stream' defaults to 0x14057b7e_f767814f. If you pass an explicit value, note that the canonical PCG algorithm performs 'stream = (stream :<<: 1) | 1' but this implementation only performs 'stream | 1'. This ensures that 'stream' isn't modified each time it is passed from one Random generator to another. | |
| byte() | Byte | Returns a random byte between 0 and 255. |
| chance( p:Real ) | Logical |
Has a proportional chance 'p' to return true (0 < p < 1.0). Example: Random.chance(0.75) # returns true 75% of the time statistically speaking. |
| cloned() | Random | Returns a duplicate Random generator. Given identical calls, it will produce identical results to this Random generator. |
| color() | Color | Returns a color with randomized (r,g,b) values and an alpha of 1.0. |
| degrees() | Degrees | |
| gaussian() | Real | Returns a random number from a normal distribution of avg=0, stddev=1 68.2% chance of being between -1..1 95.45% chance of being between -2..2 99.73% chance of being -3..3 99.994% chance of being -4..4 Real number precision limits the results to [-8.5,8.5] From: https://stackoverflow.com/a/218600/135791 |
| int() | Int | |
| int32() | Int32 | Returns a normalized, evenly distributed random integer in the range [0,2^31-1] inclusive. |
| int64() | Int64 | Returns a normalized, evenly distributed random integer in the range [0,2^63-1] inclusive. |
| int( limit:Int ) | Int |
Returns an evenly distributed random integer in the range [0,limit) - includes 0 but does not include limit. int(limit) is preferred over int % limit because it avoids the low number bias of a simple mod. Example: println( Random.int(100) ) # Prints a number 0..99 |
| int( low:Int, high:Int ) | Int | Returns a random integer between low and high, inclusive. |
| int32( limit:Int32 ) | Int32 |
Returns an evenly distributed random integer in the range [0,limit) - includes 0 but does not include limit. int32(limit) is preferred over int32 % limit because it avoids the low number bias of a simple mod. Example: println( Random.int32(100) ) # Prints a number 0..99 |
| int32( low:Int32, high:Int32 ) | Int32 | Returns a random integer between low and high, inclusive. |
| int_xy( limit:Int ) | IntXY | |
| int_xy( limit:IntXY ) | IntXY | |
| logical() | Logical | Returns either "true" or "false" with equal likelihood. |
| next32() | Int32 | Returns an pseudo-random Int32 in the full range of possible values [-80000000,7FFFffff]. |
| radians() | Radians | |
| real() | Real | Returns a random number 0 < n < 1.0 |
| real32() | Real32 | Returns a random number 0 < n < 1.0 |
| real64() | Real64 | Returns a random number 0 < n < 1.0 |
| real( limit:Real ) | Real | Returns a random number 0 < n < limit |
| real( low:Real, high:Real ) | Real | Returns a random number low < n < high |
| real32( limit:Real32 ) | Real32 | Returns a random number 0 < n < limit |
| real32( low:Real32, high:Real32 ) | Real32 | Returns a random number low < n < high |
| real64( limit:Real64 ) | Real64 | Returns a random number 0 < n < limit |
| real64( low:Real64, high:Real64 ) | Real64 | Returns a random number low < n < high |
| reset( seed:Int64, [stream=0x14057b7ef767814f:Int64] ) | Resets the Random generator ot use the specified seed and optional stream parameter. 'stream' defaults to 0x14057b7e_f767814f. If you pass an explicit value, note that the canonical PCG algorithm performs 'stream = (stream :<<: 1) | 1' but this implementation only performs 'stream | 1'. This ensures that 'stream' isn't modified each time it is passed from one Random generator to another. | |
| xy( limit:Real ) | XY | |
| xy( limit:XY ) | XY | |
| xyz() | XYZ | Creates a XYZ compound with randomized (x,y,z) values, each in the range (0.0,1.0) exclusive. |
| xyz( low:Real, high:Real ) | XYZ | Returns a XYZ compound with an evenly distributed random real number in the range (low,high) exclusive (does not include low or high). for each of the (x,y,z) values. |
| xyzw() | XYZW | Creates a XYZW compound with randomized (x,y,z,w) values, each in the range (0.0,1.0) exclusive. |
| xyzw( low:Real, high:Real ) | XYZW | Returns a XYZW compound with an evenly distributed random real number in the range (low,high) exclusive (does not include low or high). for each of the (x,y,z,w) values. |