Maths functions
The following Maths Functions come bundled with RiskScape for use in your function.
Built-in maths functions
RiskScape comes with a set of default functions that are useful for doing mathematics
in the context of RiskScape and Risk Analysis. They are used like any other RiskScape function.
For example, to round a floating point number, you can use the round
function like round(-34.23)
.
Where possible, RiskScape makes use of maths packages that are part of the Java language.
These packages are widely used and are proven to produce reliable mathematical results.
For example, the square_root()
RiskScape function is just a simple ‘wrapper’ for the
Java Math.sqrt()
function.
See Maths functions for RiskScape’s maths functions.
To list the maths functions available in RiskScape, run riskscape function list --category maths
.
Jython discrete functions
Note
The following sections describe using RiskScape-based code from within a Jython function. Most Python users will probably find it simpler to setup RiskScape to use CPython and use standard Python maths packages instead.
A discrete function can be constructed from points, constants and other functions, to form a single function for use with risk analysis.
Using points
The simplest use of a discrete function is to join up a series of points to create a continuous sequence of lines between them.
from nz.org.riskscape.engine.function import DiscreteFunction
ID = 'joined-points'
DESCRIPTION = 'Demonstrates a function built by connecting points to form a series of linear functions'
FUNCTION = DiscreteFunction.builder() \
.addPoint(-1, 4) \
.addPoint(1, 6) \
.addPoint(4, 8) \
.addPoint(10, 10) \
.withLinearInterpolation() \
.build()
Constants
As well as adding a point, a constant value can be added for a range:
# will return 0.45 when 0 <= x <= 10
DiscreteFunction.builder().addConstant(0, 10, 0.45)
Joining functions
Arbitrary RiskScape functions can be joined up to form a single function. Each function is added along with the range for which it’s applicable:
from nz.org.riskscape.engine.function import DiscreteFunction, Maths
ID = 'joined-polynomials'
DESCRIPTION = 'Demonstrates a function built by connecting polynomials'
quadratic = Maths.newPolynomial(0, 8, 0.25)
cubic = Maths.newPolynomial(10, 0, 4, 0.5)
FUNCTION = DiscreteFunction.builder() \
.addFunction(-10, -5, cubic) \
.addFunction(40, 1000, polynomial) \
.withLinearInterpolation() \
.build()
Ranges
By default, a discrete function will ‘close’ any upper bound on a range that
isn’t connected to a higher range. For example, adding the range
addFunction(0, 10, somePolynomial)
will make that polynomial
apply when 0 <= x <= 10
. However, if a function is added from 10 onwards,
then somePolynomial
applies when 0 <= x < 10
.
This closing behaviour can be disabled by calling .withoutUpperBoundClosing
on the function builder.