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))
[ ]
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))
JSON
In [7]:
pfa = """
{
"input":"double",
"output":"double",
"action":[
{"m.sqrt": "input"}
]
}
"""
engine, = PFAEngine.fromJson(pfa)
for num in inputs:
print(engine.action(num))
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))
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))