The rotating polyhedra demo by Gerard Sookahet (2005), on the left as it was, and on the right with its maths written with the
syntax of TIP 759. It takes up, proc by proc, the comparison made for Calc;
the switch shows the Calc version in the right column instead, with the same left column.
TIP 759 features used: [( … )] expression substitution, native lists (a, b, c),
TIP 282 assignments x = …;, expression scripts {( … )} as the body of proc, foreach…, tuple assignments (x, y, z) = $V;, and raw commands, without brackets, for the commands the expression parser knows
(global scx scy vdist;, n = llength $lcoords;, lindex $M1 $i 0 * lindex $M2 0 $j); the procs of the demo keep their brackets. And user operators: a proc tcl::mathop::crossProd is used as $U \crossProd $V, with the precedence of *. Everything is parsed by Tcl and compiled with the proc: nothing to transform afterwards.
The columns TIP 759, operators in tcllib count the demo as it would be if the ten operators came from math::linearalgebra (see below the speed table): the definitions of \crossProd, \matrixProd, \matrixVectorProd, \project and the vector operators leave the file, and one package require line takes their place. Characters are counted with the white space the syntax needs, and only that: one blank between two words of a command (set a 1 counts 7), none for indentation, none around an operator in an expression ($X + $x counts as $X+$x), and one character for a line end that ends a command. Comments count, with one blank between words, and their line end. To check these rules, the code of each column was rewritten in this minimal form and run: the original, Calc (with its colon package) and TIP 759 give the same results as before. The Calc column of DisplayModel includes two changes that are not about the maths.
Best of 2 × 5 runs of 20 000 calls each, on the branch tip-759h-tcl91-user-op built with -O2, the data a cube (8 vertices, 6 faces). The operators are timed as expr {$U \crossProd $V} and expr {$M \matrixProd $M}. DisplayModel needs Tk and is not timed.
The product of matrices is three times faster: lmap goes over the values, where the original updates each cell by index with lset in three loops. MatrixRotation, which calls it twice, gains as much.
The rest of the TIP 759 code is written with vector operators, as in a textbook: the cross product is the axial vector of the antisymmetric part of a tensor product, a matrix is transposed into column vectors for dot products. This is the most readable form, and here the slowest, but the cause is not the syntax: the operators are procs, and a proc call costs about a microsecond, several per operation, where the original wrote the arithmetic inline. The last two columns measure the same TIP 759 code with five operators written in C (\add, \sub, \dot, \crossProd and \transpose, a prototype of about 80 lines loaded as an extension into tcl::mathop; nothing else changes): most procs are then as fast as the original or faster, and a frame of the animation (Transformations, then Intensity) takes 47 µs for a cube, against 79 µs with the original, and 87 µs with the operators as procs. Barycenter and Projection stay a little slower than the original, mostly because lmap and the operators build a new list for each point. A geometry library compiled in C, on a compact representation of vectors of numbers, would remove that too.
Where the operators would live. None of these operators is specific to the demo: \add, \sub, \dot, \transpose, \tensorProd, \axial, \crossProd, \matrixProd, \matrixVectorProd, and even \project, are the common vocabulary of linear algebra and 3D graphics. Their definitions could be provided by the Tcl library math::linearalgebra of tcllib, as procs of tcl::mathop, possibly compiled in C. A single line, package require math::linearalgebra, would then define them all beforehand: the "Vector operators" section would shrink to that line, and the code of the demo would run at the speed of the C column.
The barycenter as it is defined: the sum of the points, divided by their number. The points are added as vectors with the operator \add, so there are no separate x and y, and the same code works in 3D. Slower, as each \add is a proc call (see the speed table).
15procBarycenter{lcoords}{
16set X 0
17set Y 0
18set n [llength$lcoords]
19
20foreach vtx $lcoords{
21foreach{x y}$vtx{
22set X [expr{$X+$x}]
23set Y [expr{$Y+$y}]
24}
25}
26return[list[expr{$X/$n}][expr{$Y/$n}]]
27}
15procBarycenter{lcoords {S {00}}}{(
16foreachP$lcoords{(S=$S\add$P)};
17lmaps$S{($s/llength$lcoords)}
18)}
15procBarycenter{lcoords}{
16set X 0
17set Y 0
18set n [llength$lcoords]
19
20foreach vtx $lcoords{
21foreach{x y}$vtx{
22set X [expr{$X+$x}]
23set Y [expr{$Y+$y}]
24}
25}
26return[list[expr{$X/$n}][expr{$Y/$n}]]
27}
18procBarycenter{lcoords}{
19:{ X =0; Y =0; n =llength(lcoords)}
20
21foreach vtx $lcoords{
22:{(x, y)= vtx; X = X + x; Y = Y + y }
23}
24:list(X/n, Y/n)
25}
CrossProduct−5+3−3+51.3 µs → 7.4 µs, 5.9× slower
CrossProduct becomes the operator \crossProd, defined as in a textbook: the cross product is the axial vector of the antisymmetric part of the tensor product, \axial (($V \tensorProd $U) \sub ($U \tensorProd $V)). It takes two vectors instead of six numbers. The operators it uses are defined with the others, under Vector operators.
MatrixVectorProduct becomes the operator \matrixVectorProd: $M \matrixVectorProd $V. The matrix is transposed into its three column vectors m, n, o (\transpose, a unary operator), and the result is the three dot products with V. The original computes this transposed product: its first coordinate is m00*x + m10*y + m20*z.
MatrixProduct becomes the operator \matrixProd. No index and no loop counter: lmap goes over the rows of M1, and each row of the result is the sum of the rows of M2 weighted by the row of M1. It is also faster than the version with loops (5.5 µs per call, against 19.6 µs for the original and 8.7 µs for for loops with an unrolled k, on the same build).
48procMatrixProduct{M1 M2}{
49set M {{0000}{0000}{0000}{0000}}
50
51for{set i 0}{$i<4}{incr i}{
52for{set j 0}{$j<4}{incr j}{
53lset M $i$j0
54for{set k 0}{$k<4}{incr k}{
55lset M $i$j[expr{[lindex$M$i$j]+[lindex$M1$i$k]*[lindex$M2$k$j]}]
The code posted in the TIP 672 forum discussion, with one change: the product, at the end, is written with the operator, $Mx \matrixProd $My \matrixProd $Mz, left to right, instead of nested [MatrixProduct …].
The three vertices of the face are fetched as vectors in one tuple assignment, (P0, P1, P2) = lmap i $face {lindex $lvtx $i}, and the normal is written as in a textbook: ($P1 \sub $P0) \crossProd ($P2 \sub $P1). The norm is [( $n \dot $n )]. This costs time: three operator calls per face, each a proc (see the speed table).
The 2D projection, a common operation, becomes the operator \project: $P \project $M, a point and a rotation matrix. The matrix is transposed into column vectors, and each coordinate is the dot product of the point with a column: nx = $P \dot $m.
112((m00,m01,m02),(m10,m11,m12),(m20,m21,m22))= M;
113 nx = m00*x + m10*y + m20*z;
114 ny = m01*x + m11*y + m21*z;
115 nz =(m02*x + m12*y + m22*z +10)/vdist;
116list(nx/nz + scx/2.0, ny/nz + scy/2.0)
117}
118}
Transformations−13+8−6+571.9 µs → 79.6 µs, about the same
The body becomes an expression script: global, foreach and lappend are raw commands, the two lists start empty in one chained assignment, lnew = lvn = {}, the vertices are projected with $vtx \project $M and the normals rotated with $M \matrixVectorProd $v, and the result is the native list ($M, $lnew, $lvn). The counter i, never used, is gone, as in the Calc version.
134foreach v $lnv{lappend lvn [:MatrixVectorProduct(M, v)]}
135
136return[:(M, lnew, lvn)]
137}
Intensity−15+12−8+87.0 µs → 7.3 µs, about the same
The shading maths is one expression script per face; the dot products are written $nv \dot $lvv, lindex is a raw command inside sqrt(), and the colour is a conditional expression.
The six set become one tuple assignment, written as a [( … )] command in this plain script. The other changes of the Calc page (the remembered model, the label colour) are not about the maths and are left out here.
The operators used above, as procs of tcl::mathop: \add and \sub, the sum and the difference of two vectors or matrices; \dot, the dot product (it replaces DotProduct, which the Calc page does not show); \transpose, unary; \tensorProd, the tensor product of two vectors, a matrix; and \axial, unary, the vector of an antisymmetric matrix. They have no counterpart in the original or in Calc, which write the arithmetic inline. Written in C, they would make the vector code as fast as the inline code (see the speed table). These definitions could come from math::linearalgebra, with one package require line (see below the speed table).
# Vector operators, for the expressions: $U \add $V, $U \sub $V, $U \dot $V,
Calc has no user-defined operators: this section is TIP 759 only.
Calc-specific code−0+0−0+21
What Calc needs around the code: the package at the top of the file, and at the end one Calc::transformProcs call that compiles the procs listed, before Main, then : and = renamed away, so that a missed proc fails loudly instead of running the slow interpreted path.
Nothing here for TIP 759: [( … )] and {( … )} are part of the Tcl syntax, parsed with the proc and compiled to bytecode with it, like expr {…} today. No package, no list of procs to keep up to date, no command to remove.
12#tcl::tm::path add {path/to/colon-module/dir} ;# point this to the module directory
13::tcl::tm::path add [file dirname [info script]];# works if script and module in same directory
14package require colon
493# Compile the Calc code (the : commands) in these procs, once, now that they
494# are all defined and just before Main runs. This is the same result as