Skip to main content

Posts

Showing posts from May, 2015

Subexpressions and Assertions

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 clau...

At the end of the line

PM adopts a clean approach to statement separation. PM statements are formally separated by semicolons. However, these may be omitted if the next statement starts on a new line. This is the only syntactic role for line breaks – statements and expressions may sprawl across as many lines as you wish. For die-hard C/C++/Java programmers, it is also possible to add a semicolon at the end of a statement list. a=1; b=2; c=1 x = -b + sqrt( b**2 - 4 * a * c) / 2*a print("x="//x) Semicolons are also used in constructors for two-dimensional data structures: arrays and matrices.   Line breaks can be substituted for semicolons here too, enabling you to write something like: id_matrix = ( 1,0,0 0,1,0 0,0,1 )