User:EnronEvolved/sandbox

Testing Template:Ze
=APL syntax and symbols=

The programming language APL is distinctive in being symbolic rather than lexical: its primitives are denoted by symbols, not words. These symbols were originally devised as a mathematical notation to describe algorithms. APL programmers often assign informal names when discussing functions and operators (for example, "product" for ×/) but the core functions and operators provided by the language are denoted by non-textual symbols.

Monadic and dyadic functions
Most symbols denote functions or operators. A monadic function takes as its argument the result of evaluating everything to its right. (Moderated in the usual way by parentheses.) A dyadic function has another argument, the first item of data on its left. Many symbols denote both monadic and dyadic functions, interpreted according to use. For example,  gives , the largest integer not above the argument, and   gives  , the lower of the two arguments.

Functions and operators
APL uses the term operator in Heaviside’s sense as a moderator of a function as opposed to some other programming language's use of the same term as something that operates on data, ref. relational operator and operators generally. Other programming languages also sometimes use this term interchangeably with function, however both terms are used in APL more precisely. Early definitions of APL symbols were very specific about how symbols were categorized. For example, the operator reduce is denoted by a forward slash and reduces an array along one axis by interposing its function operand. An example of reduce:

In the above case, the reduce or slash operator moderates the multiply function. The expression  evaluates to a scalar (1 element only) result through reducing an array by multiplication. The above case is simplified, imagine multiplying (adding, subtracting or dividing) more than just a few numbers together. (From a vector,  returns the product of all its elements.)

The above dyadic functions examples [left and right examples] (using the same  symbol, right example) demonstrate how boolean values (0s and 1s) can be used as left arguments for the   expand and   replicate functions to produce exactly opposite results. On the left side, the 2-element vector  is expanded where boolean  s occur to result in a 3-element vector   — note how APL inserted a 0 into the vector. Conversely, the exact opposite occurs on the right side — where a 3-element vector becomes just 2-elements; boolean 0s delete items using the dyadic  slash function. APL symbols also operate on lists (vector) of items using data types other than just numeric, for example a 2-element vector of character strings  could be substituted for numeric vector   above.

Syntax rules
In APL the precedence hierarchy for functions or operators is strictly positional: expressions are evaluated right-to-left. APL does not follow the usual operator precedence of other programming languages; for example, × does not bind its operands any more "tightly" than +. Instead of operator precedence, APL defines a notion of scope.

The scope of a function determines its arguments. Functions have long right scope: that is, they take as right arguments everything to their right. A dyadic function has short left scope: it takes as its left arguments the first piece of data to its left. For example, (leftmost column below is actual program code from an APL user session, indented = actual user input, not-indented = result returned by APL interpreter):

<< First note there are no parentheses and

APL is going to execute from right-to-left.


 * 1) {of topmost APL code entered at left}) 4-5 = -1.
 * 2) 3 times -1 = -3.


 * 1) Take the floor or lower of 2 and -3 = -3.


 * 1) Divide 1 by -3 = -0.3333333333 = final result.

An operator may have function or data operands and evaluate to a dyadic or monadic function. Operators have long left scope. An operator takes as its left operand the longest function to its left. For example:

APL atomic or piecemeal sub-analysis (full explanation):

Beginning rightmost: ⍳¨3 3 produces a 2-element nested APL vector 1 2 3 1 2 3 where each element is itself a vector 1 2 3. ⍳3 by itself would produce 1 2 3.

The diaeresis ¨ or mini double-dot means repeat or over each or perform each separately so iota repeats (in human i.e., reversed terms, the APL interpreter reads 3 3 over each use iota), concisely: iota for each 3.

The left operand for the over-each operator ¨ is the index ⍳ function. The derived function ⍳¨ is used monadically and takes as its right operand the vector 3 3. The left scope of each is terminated by the reduce operator, denoted by the forward slash. Its left operand is the function expression to its left: the outer product of the equals function. The result of ∘.=/ is a monadic function. With a function's usual long right scope, it takes as its right argument the result of ⍳¨3 3. Thus

Equivalent results in APL: (⍳3)(⍳3) and ⍳¨3 3 << Rightmost expression is more concise.

The matrix of 1s and 0s similarly produced by and  is called an identity matrix.

Identity matrices are useful in solving matrix determinants, groups of linear equations and multiple regression.

Some APL interpreters support the compose operator ∘ and the commute operator ⍨. The former ∘ glues functions together so that foo∘bar, for example, could be a hypothetical function that applies defined function foo to the result of defined function bar; foo and bar can represent any existing function. In cases where a dyadic function is moderated by commute and then used monadically, its right argument is taken as its left argument as well. Thus, a derived or composed function (named im at left) is used in the APL user session to return a 9-element identity matrix using its right argument, parameter or operand = 3.

Operators and axis indicator
Notes: The reduce and scan operators expect a dyadic function on their left, forming a monadic composite function applied to the vector on its right.

The product operator "." expects a dyadic function on both its left and right, forming a dyadic composite function applied to the vectors on its left and right. If the function to the left of the dot is "∘" (signifying null) then the composite function is an outer product, otherwise it is an inner product. An inner product intended for conventional matrix multiplication uses the + and × functions, replacing these with other dyadic functions can result in useful alternative operations.

Some functions can be followed by an axis indicator in (square) brackets, i.e., this appears between a function and an array and should not be confused with array subscripts written after an array. For example, given the ⌽ (reversal) function and a two-dimensional array, the function by default operates along the last axis but this can be changed using an axis indicator:

4 rows by 3 cols matrix created, using rho ⍴ and iota ⍳. The 4 x 3 matrix is then stored in a variable named A.

A is now reflected or flipped along its vertical axis as symbol ⌽ visually indicates.

A is now reflected using the [1] axis indicator or first dimension modifier. The result is that variable A has been reflected across the horizontal axis, instead of vertically.

A is now reflected both vertically ⊖ and horizontally ⌽.

A is ⍉ transposed to a 3 row by 4 col matrix such that rows-cols become exchanged, as symbol ⍉ visually portrays. Compare the result here to the original matrix stored in A, topmost matrix. These types of data transformations are useful in time series analysis and spatial coordinates, just two examples, more exist.

As a particular case, if the dyadic catenate "," function is followed by an axis indicator (or axis modifier to a symbol/function), it can be used to laminate (interpose) two arrays depending on whether the axis indicator is less than or greater than the index origin (index origin = 1 in illustration below):

At left, variable 'B' is first assigned a vector of 4 consecutive integers (e.g., ⍳4). Var C is then assigned 4 more consecutive integers (such as 4+⍳4). 'B' and C are then concatenated or raveled together for illustration purposes, resulting in a single vector (⍳8). In the particular case at left, if the dyadic catenate "," function is followed by an axis indicator ([0.5] which is less than 1), it can be used to laminate (interpose) two arrays (vectors in this case) depending on whether the axis indicator is less than or greater than the index origin(1). The first result (of B,[0.5]C) is a 2 row by 4 column matrix, vertically joining 'B' and C row-wise. The second result (of B,[1.5]C which is greater than 1) is a 4 row by 2 column matrix.

Nested arrays
Arrays are structures which have elements grouped linearly as vectors or in table form as matrices—and higher dimensions (3D or cubed, 4D or cubed over time, etc.). Arrays containing both characters and numbers are termed mixed arrays. Array structures containing elements which are also arrays are called nested arrays.

Flow control
A user may define custom functions which, like variables, are identified by name rather than by a non-textual symbol. The function header defines whether a custom function is niladic (no arguments), monadic (one right argument) or dyadic (left and right arguments), the local name of the result (to the left of the ← assign arrow), and whether it has any local variables (each separated by semicolon ';'). Whether functions with the same identifier but different adicity are distinct is implementation-defined. If allowed, then a function CURVEAREA could be defined twice to replace both monadic CIRCLEAREA and dyadic SEGMENTAREA above, with the monadic or dyadic function being selected by the context in which it was referenced.

Custom dyadic functions may usually be applied to parameters with the same conventions as built-in functions, i.e., arrays should either have the same number of elements or one of them should have a single element which is extended. There are exceptions to this, for example a function to convert pre-decimal UK currency to dollars would expect to take a parameter with precisely three elements representing pounds, shillings and pence.

Inside a program or a custom function, control may be conditionally transferred to a statement identified by a line number or explicit label; if the target is 0 (zero) this terminates the program or returns to a function's caller. The most common form uses the APL compression function, as in the template (condition)/target which has the effect of evaluating the condition to 0 (false) or 1 (true) and then using that to mask the target (if the condition is false it is ignored, if true it is left alone so control is transferred).

Hence function SEGMENTAREA may be modified to abort (just below), returning zero if the parameters (DEGREES and RADIUS below) are of different sign:

The above function SEGMENTAREA works as expected if the parameters are scalars or single-element arrays, but not if they are multiple-element arrays since the condition ends up being based on a single element of the SIGN array - on the other hand, the user function could be modified to correctly handle vectorized arguments. Operation can sometimes be unpredictable since APL defines that computers with vector-processing capabilities should parallelise and may reorder array operations as far as possible - thus, test and debug user functions particularly if they will be used with vector or even matrix arguments. This affects not only explicit application of a custom function to arrays, but also its use anywhere that a dyadic function may reasonably be used such as in generation of a table of results:

A more concise way and sometimes better way - to formulate a function is to avoid explicit transfers of control, instead using expressions which evaluate correctly in all or the expected conditions. Sometimes it is correct to let a function fail when one or both input arguments are incorrect - precisely to let user know that one or both arguments used were incorrect. The following is more concise than the above SEGMENTAREA function. The below importantly correctly handles vectorized arguments:

Avoiding explicit transfers of control also called branching, if not reviewed or carefully controlled - can promote use of excessively complex one liners, veritably "misunderstood and complex idioms" and a "write-only" style, which has done little to endear APL to influential commentators such as Edsger Dijkstra. Conversely however APL idioms can be fun, educational and useful - if used with helpful comments ⍝, for example including source and intended meaning and function of the idiom(s). Here is an APL idioms list, an IBM APL2 idioms list here and Finnish APL idiom library here.

Miscellaneous
Most APL implementations support a number of system variables and functions, usually preceded by the ⎕ (quad) and/or ")" (hook=close parenthesis) character. Note that the quad character is not the same as the Unicode missing character symbol. Particularly important and widely implemented is the ⎕IO (Index Origin) variable, since while the original IBM APL based its arrays on 1 some newer variants base them on zero:

There are also system functions available to users for saving the current workspace e.g., )SAVE and terminating the APL environment, e.g., )OFF - sometimes called hook commands or functions due to the use of a leading right parenthesis or hook. There is some standardization of these quad and hook functions.

Fonts
The Unicode Basic Multilingual Plane includes the APL symbols in the Miscellaneous Technical block, which are thus usually rendered accurately from the larger Unicode fonts installed with most modern operating systems. These fonts are rarely designed by typographers familiar with APL glyphs. So, while accurate, the glyphs may look unfamiliar to APL programmers or be difficult to distinguish from one another.

Some Unicode fonts have been designed to display APL well: APLX Upright, APL385 Unicode, and SimPL.

Before Unicode, APL interpreters were supplied with fonts in which APL characters were mapped to less commonly used positions in the ASCII character sets, usually in the upper 128 code points. These mappings (and their national variations) were sometimes unique to each APL vendor's interpreter, which made the display of APL programs on the Web, in text files and manuals - frequently problematic.

APL2 keyboard function to symbol mapping
Note the APL On/Off Key - topmost-rightmost key, just below. Also note the keyboard had some 55 unique (68 listed per tables above, including comparative symbols but several symbols appear in both monadic and dyadic tables) APL symbol keys (55 APL functions (operators) are listed in IBM's 5110 APL Reference Manual), thus with the use of alt, shift and ctrl keys - it would theoretically have allowed a maximum of some 59 (keys) *4 (with 2-key pressing) *3 (with tri-key pressing, e.g., ctrl-alt-del) or some 472 different maximum key combinations, approaching the 512 EBCDIC character max (256 chars times 2 codes for each keys-combination). Again, in theory the keyboard pictured here would have allowed for about 472 different APL symbols/functions to be keyboard-input, actively used. In practice, early versions were only using something roughly equivalent to 55 APL special symbols (excluding letters, numbers, punctuation, etc. keys). Thus, early APL was then only using about 11% (55/472) of a symbolic language's at-that-time utilization potential, based on keyboard # keys limits, again excluding numbers, letters, punctuation, etc. In another sense keyboard symbols utilization was closer to 100%, highly efficient, since EBCDIC only allowed 256 distinct chars, and ASCII only 128.

<!-- This entire section is outside the scope of the article. It relates neither to APL syntax nor symbols.

Solving puzzles
APL has proved to be extremely useful in solving mathematical puzzles, several of which are described below.

Pascal's triangle
Take Pascal's triangle, which is a triangular array of numbers in which those at the ends of the rows are 1 and each of the other numbers is the sum of the nearest two numbers in the row just above it (the apex, 1, being at the top). The following is an APL one-liner function to visually depict Pascal's triangle:

Prime numbers, contra proof via factors
Determine the number of prime numbers (prime # is a natural number greater than 1 that has no positive divisors other than 1 and itself) up to some number N. Ken Iverson is credited with the following one-liner APL solution to the problem:

Examining the converse or opposite of a mathematical solution is frequently needed (integer factors of a number): Prove for the subset of integers from 1 through 15 that they are non-prime by listing their decomposition factors. What are their non-one factors (#'s divisible by, except 1)?

Fibonacci sequence
Generate a Fibonacci number sequence, where each subsequent number in the sequence is the sum of the prior two: -->

= Non-uniform rational B-spline =

General definition


A NURBS curve is defined by its order, a set of weighted control points, and a knot vector. NURBS curves and surfaces are generalizations of both B-splines and Bézier curves and surfaces, the primary difference being the weighting of the control points, which makes NURBS curves rational.

By using a two-dimensional grid of control points, NURBS surfaces including planar patches and sections of spheres can be created. These are parametrized with two variables (typically called s and t or u and v). This can be extended to arbitrary dimensions to create NURBS mapping $$\Reals^n \to \Reals^n$$.

Order
The order of a NURBS curve defines the number of nearby control points that influence any given point on the curve. The curve is represented mathematically by a polynomial of degree one less than the order of the curve. Hence, second-order curves (which are represented by linear polynomials) are called linear curves, third-order curves are called quadratic curves, and fourth-order curves are called cubic curves. A curve of order $$n$$ must have at least $$n$$ control points.

Control points
The control points determine the shape of the curve.

Knots
In order to define a NURBS curve with $$n$$ points and order $$o$$, the parameter interval must be partitioned into at most $$n+o$$ pieces. For computational purposes, this partition is represented as a monotone increasing series of $$n+o$$ values in the parameter interval, called the knot vector. Each value in the knot vector is called a knot, and the lengths of interval between each knot are called knot spans. The number of knot spans corresponds to the number of individual pieces of the resulting NURBS curve.

A knot that appears a certain number of times can be termed a knot with that multiplicity. A knot with multiplicity two can thus be called a double knot, and a knot with multiplicity three a triple knot. Due to the monotone nature of the knot vector, identical knots are consecutive.

The knot vector is then used to generate the basis functions. It typically begins and ends with a knot of multiplicity equal to the order of the curve. Due to the properties of the basis functions, this ensures that all curve pieces are formed from polynomials of the same degree.

Basis functions
The B-spline basis functions used in the construction of NURBS curves are constructed from the knot vector. These are usually denoted by $$N_{i,n}(u)$$, where $$i$$ corresponds to the $$i$$-th control point, and $$n$$ corresponds with the degree of the basis function. The parameter dependence is frequently left out, so they can be written as $ N_{i,n} $.

The basis functions are defined recursively, such that $$ \begin{align} N_{i,n}(u) & = \frac{u - k_i}{k_{i+n} - k_i} N_{i,n-1}(u) + \frac{k_{i+n} - u}{k_{i+n} - k_i} N_{i+1,n-1}(u)  \\ & = \frac{u - k_i}{k_{i+n} - k_i} N_{i,n-1}(u) + (1 - \frac{u - k_i}{k_{i+n} - k_i}) N_{i+1,n-1}(u) \\ \end{align} $$ and $$ N_{i,0}(u) = \begin{cases} 1 & \text{if } k_i \leq u < k_{i+1}, \\ 0 & \text{otherwise}. \end{cases} $$ where $$k_i$$ is the $$i$$th knot in the knot vector. Effectively, $$N_{i,n}$$ is equal to a piecewise function formed from degree $$n$$ polynomials, produced by a linear interpolation between $$N_{i,n-1}$$ and $$N_{i+1,n-1}$$ between the $$i$$-th and $$i+1$$-th knots.

General form of a NURBS curve
Using the definitions of the basis functions $$N_{i,n}$$ from the previous paragraph, a NURBS curve takes the following form: $$C(u) = \sum_{i=1}^{k} {\frac {N_{i,n}(u)w_i} {\sum_{j=1}^k N_{j,n}(u)w_j}} \mathbf{P}_i = \frac {\sum_{i=1}^k {N_{i,n}(u)w_i \mathbf{P}_i}} {\sum_{i=1}^k {N_{i,n}(u)w_i}} $$

In this, $$k$$ is the number of control points $$\mathbf{P}_i$$ and $$w_i$$ are the corresponding weights. The denominator is a normalizing factor that evaluates to one if all weights are one.

By absorbing the denominator into the sum over the numerator, an equivalent form $$ C(u) = \sum_{i=1}^k R_{i,n}(u)\mathbf{P}_i $$ can be found, in which the functions $$ R_{i,n}(u) = {N_{i,n}(u)w_i \over \sum_{j=1}^k N_{j,n}(u)w_j} $$ are known as the rational basis functions.

General form of a NURBS surface
A NURBS surface is obtained as the tensor product of two NURBS curves, thus using two independent parameters $$u$$ and $$v$$ (with indices $$i$$ and $$j$$ respectively): $$S(u,v) = \sum_{i=1}^k \sum_{j=1}^l R_{i,j}(u,v) \mathbf{P}_{i,j} $$ with $$R_{i,j}(u,v) = \frac {N_{i,n}(u) N_{j,m}(v) w_{i,j}} {\sum_{p=1}^k \sum_{q=1}^l N_{p,n}(u) N_{q,m}(v) w_{p,q}}$$ as rational basis functions.

Comparison to other parametric curves
(Non-rational, aka simple, B-splines are a special case/subset of rational B-splines, where each control point is a regular non-homogenous coordinate [no 'w'] rather than a homogeneous coordinate. That is equivalent to having weight "1" at each control point; Rational B-splines use the 'w' of each control point as a weight. )

Properties
Note that within the interval the polynomial nature of the basis functions and the linearity of the construction make the curve perfectly smooth, so it is only at the knots that discontinuity can arise.

In many applications the fact that a single control point only influences those intervals where it is active is a highly desirable property, known as local support. In modeling, it allows the changing of one part of a surface while keeping other parts unchanged.

Again by induction, it can be proved that the sum of the basis functions for a particular value of the parameter is unity. This is known as the partition of unity property of the basis functions.

Applications
NURBS curves and surfaces are useful for a number of reasons:


 * The set of NURBS for a given order is invariant under affine transformations: operations like rotations and translations can be applied to NURBS curves and surfaces by applying them to their control points.
 * They offer one common mathematical form for both standard analytical shapes (e.g., conics) and free-form shapes.
 * They provide the flexibility to design a large variety of shapes.
 * They reduce the memory consumption when storing shapes (compared to simpler methods).
 * They can be evaluated reasonably quickly by numerically stable and accurate algorithms.

In practice, cubic curves are the ones most commonly used. Fifth- and sixth-order curves are sometimes useful, especially for obtaining continuous higher order derivatives, but curves of higher orders are practically never used because they lead to internal numerical problems and tend to require disproportionately large calculation times.

One-dimensional points just define a scalar function of the parameter. These are typically used in image processing programs to tune the brightness and color curves. Three-dimensional control points are used abundantly in 3D modeling, where they are used in the everyday meaning of the word 'point', a location in 3D space. Multi-dimensional points might be used to control sets of time-driven values, e.g. the different positional and rotational settings of a robot arm. = Genaille–Lucas rulers =

Genaille–Lucas rulers (also known as Genaille–Lucas rods) are an arithmetic tool invented by Henri Genaille, a French railway engineer, in the 1880s, in collaboration with French mathematician Édouard Lucas. The device is a variant of Napier's bones. By representing the carry graphically, the user can read off the results of simple multiplication problems directly, with no intermediate mental calculations.

Design
A full set of Genaille–Lucas rulers consists of ten strips, one for each decimal digit. On each strip is printed a column of triangles and a column of digits, divided into nine cells, also corresponding to the decimal digits. The digits in each cell are the values in the units place of the product of the cell's digit and the ruler's digit, after adding the tens value carried over from the previous partial multiplication.

The triangles in each cell then encode the tens place of the result. This is in contrast to a set of Napier's bones, where the tens place of each partial result is given by a second digit.

Multiplication
By arranging the rulers in the proper order, the user can find unit multiples of short natural numbers by sight.

Division


Soon after their development by Genaille, the rulers were adapted to a set of rods that can perform division. The division rods are aligned similarly to the multiplication rods, with the index rod on the left denoting the divisor, and the following rods spelling out the digits of the dividend. After these, a special "remainder" rod is placed on the right. The quotient is read from left to right, following the lines from one rod to the next. The path of digits ends with a number on the remainder rod, which is the remainder given by the division.

History
The rods were presented to the Association française pour l’avancement des sciences by Genaille and Lucas in 1884 and 1885, building on earlier work by Genaille first presented in 1878.

Genaille–Lucas rulers for multiplication were developed independently by Carl Schönbichler, and published in the Leipziger Illustrierte Zeitung in June 1850. Schönbichler's design printed the rulers onto parallel strips of paper with a common binding. The strips in each gathering were of different lengths, and numbered at the ends, so that they could be selected in a manner similar to tabbed index cards. Whether Schönbichler's invention was ever known to Genaille is not known.

= References =