Skip to content

Using Built-in Functions

Full list of functions provided in PFA specification is available here.

In [1]:
from titus.genpy import PFAEngine
inputs = range(1, 10)

One-parameter Functions

One-parameter functions accept a single input argument.
Examples:

  • m.sqrt
  • m.sin
  • m.cos
  • m.tan
  • m.exp
  • m.ln (natural logarithm)
  • m.log10 (logarithm base 10)
  • m.floor
  • m.ceil
  • m.round

One-parameter functions do not need to enclose arguments in square brackets: {"m.sin": 3.14} is permitted instead of {"m.sin": [3.14]}.

YAML

In [2]:
pfa = """
input: double
output: double
action: 
  - m.sqrt: [input]"""
In [3]:
engine, = PFAEngine.fromYaml(pfa)
In [4]:
for num in inputs:
    print(engine.action(num))
1.0
1.4142135623730951
1.7320508075688772
2.0
2.23606797749979
2.449489742783178
2.6457513110645907
2.8284271247461903
3.0

[ ] for single parameter function is not mandatory.

In [5]:
pfa = """
input: double
output: double
action: 
  - m.sqrt: input"""
In [6]:
engine, = PFAEngine.fromYaml(pfa)
for num in inputs:
    print(engine.action(num))
1.0
1.4142135623730951
1.7320508075688772
2.0
2.23606797749979
2.449489742783178
2.6457513110645907
2.8284271247461903
3.0

JSON

In [7]:
pfa = """
{
    "input":"double",
    "output":"double",
    "action":[
        {"m.sqrt": "input"}
    ]
}
"""

engine, = PFAEngine.fromJson(pfa)
for num in inputs:
    print(engine.action(num))
1.0
1.4142135623730951
1.7320508075688772
2.0
2.23606797749979
2.449489742783178
2.6457513110645907
2.8284271247461903
3.0

Two-parameter Functions

Two-parameter functions accept two input arguments.
Examples:

  • "+" (addition)
  • "-" (subtraction)
  • "x" (multiplication)
  • "/" (floating-point division)
  • "//" (integer division)
  • "u-" (negation)
  • "%" (modulo)
  • "%%" (remainder)
  • "xx" (exponentiation)

YAML

In [8]:
pfa = """
input: double
output: double
action:
  - {m.round: {"*": [{m.sin: {+: [input, 100]}}, 100]}}"""

engine2, = PFAEngine.fromYaml(pfa)
for num in inputs:
    print(engine2.action(num))
45
99
62
-32
-97
-73
18
93
82

JSON

In [9]:
pfa = """
{
    "input": "double",
    "output": "double",
    "action": [{
        "m.round": {
            "*": [
                {"m.sin": {"+": ["input", 100]}}, 
                100
            ]
        }
    }]
}
"""

engine2, = PFAEngine.fromJson(pfa)
for num in inputs:
    print(engine2.action(num))
45
99
62
-32
-97
-73
18
93
82