Term Block
A block that represents a local function whose value can be found and used as a term in a UserFunc expression (or in an <Expression>).
<Term>s are similar to Local UserFuncs;
the difference is that
<Term>s must take exactly the same arguments as
the containing function, and they are treated like an <Input> in the
containing function (although they shouldn’t appear in the inputOrder
,
and they are not arguments of the function).
Differences between Terms and local functions:
A <Term> must take the exact same arguments as the UserFunc in which it appears.
A <Term> is evaluated exactly once for every evaluation of its containing UserFunc; if, e.g., it appears multiple times in an expression, the same value will be used each time (even if the Term returns a random number). In contrast, a local function will be called anew when needed.
Often this distinction is moot; it becomes important when evaluation is contingent. For example, suppose a
<Term f>
and a local<UserFunc g>
appear inexpression = if(a, f, g())
. The expressionf
is needed only ifa
evaluates to true, andg()
must be evaluated only ifa
is false. In this casef
will be evaluated regardless, whileg()
is evaluated only ifa
is false. Therefore, using a local function instead of a variable can sometimes save time.Another difference involves non-deterministic functions, e.g., random-number generators. If
f
is a Term, andg()
a local function, each of which returns a random number, then inexpression = if(f < 0.5, f, 0.5) + if(g() < 0.5, g(), 0.5)
the same value will be used for both appearances off
, but each appearance ofg()
will evaluate to a different number.
Kinds of Terms
Terms take all the same kinds (and corresponding attributes) as UserFuncs (see UserFunc Block).
Examples
This example can be contrasted with that using local functions (see Local UserFuncs).
<UserFunc f>
kind = expression
inputOrder = [ y ]
<Input y>
kind = arbitraryVector
types = [float]
</Input>
<Term mySqrVar>
kind = expression
inputOrder = [ y ] # same args as UserFunc f
<Input y>
kind = arbitraryVector
types = [float]
</Input>
expression = y*y
</Term>
<Term myPlusOneSqrVar>
kind = expression
inputOrder = [ y ] # same args as UserFunc f
<Input y>
kind = arbitraryVector
types = [float]
</Input>
expression = (y+1)*(y+1)
</Term>
expression = mySqrVar*myPlusOneSqrVar + y
</UserFunc>
In the above example, each Term is like a UserFunc that takes the
same arguments as function f
(namely, a scalar float named y
). The
Terms are then used in f’s expression as variables.
All Terms are evaluated before f’s expression is evaluated; this differs from local functions, which are typically evaluated only as needed.