One of the features designed to make PM convenient for numerical
computation is its notation for subexpressions. Any PM expression may be
followed by a list of subexpressions introduced by the where keyword.
Hdist = 1 –sqrt( 2*s1*s2/ss) * exp ( - (m1-m2)**2 / ss / 4 )
where ss = s1**2 + S2**2
Subexpressions may not refer to each other in the same list, but you can add any number of lists after a given expression:
Hdist = 1 –sqrt( 2*s1*s2/ss) * exp ( - (m1-m2)**2 / ss / 4 )
where ss = s1**2 + S2**2
where m1=mean(x), m2=mean(y), s1=stddev(x), s2=stddev(y)
In some contexts where clauses apply to a list of expressions, for example:
for i in a[r], j in b[r], k in c[r]
where r=grid(xlo..xhi,ylo..yhi,zlo..zhi) do
…
endfor
A PM expression may also
be combined with an assertion by placing a check
clause between the expression and any following subexpressions:
x=solve(f,y) check f(x)-y < tolerance
The check clause is executed after the main expression, while subexpressions are, of course, executed before it. Combining the two enables you to code any combination of pre and post conditions:
x = solve(f,x) check f(x)=old_x, old_x in input_domain where old_x = x
As you would expect, check expressions may optionally be excluded from code generation by the PM compiler/interpreter.
Comments
Post a Comment