ArticleslgStudy

mathematics

Direct function

Direct function is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Direct function rather than just read about it. In short: A direct function (dfn, pronounced "dee fun") is an alternative way to define a function and operator (a higher-order function) in the programming language APL. A direct operator can also be called a dop (pronounced "dee op").

Direct function — main illustration
Direct function — illustration

Key takeaways

  • Direct function belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Direct function to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Direct function from memory before moving on to harder problems.

Reference excerpt

A direct function (dfn, pronounced "dee fun") is an alternative way to define a function and operator (a higher-order function) in the programming language APL. A direct operator can also be called a dop (pronounced "dee op"). They were invented by John Scholes in 1996. They are a unique combination of array programming, higher-order function, and functional programming, and are a major distinguishing advance of early 21st century APL over prior versions. A dfn is a sequence of possibly guarded expressions (or just a guard) between { and }, separated by ⋄ or new-lines, wherein ⍺ denotes the left argument and ⍵ the right, and ∇ denotes recursion (function self-reference). For example, the function PT tests whether each row of ⍵ is a Pythagorean triplet (by testing whether the sum of squares equals twice the square of the maximum).

The factorial function as a dfn:

Description The rules for dfns are summarized by the following "reference card":

A dfn is a sequence of possibly guarded expressions (or just a guard) between { and }, separated by ⋄ or new-lines.

The expressions and/or guards are evaluated in sequence. A guard must evaluate to a 0 or 1; its associated expression is evaluated if the value is 1. A dfn terminates after the first unguarded expression which does not end in assignment, or after the first guarded expression whose guard evaluates to 1, or if there are no more expressions. The result of a dfn is that of the last evaluated expression. If that last evaluated expression ends in assignment, the result is "shy"—not automatically displayed in the session. Names assigned in a dfn are local by default, with lexical scope. ⍺ denotes the left function argument and ⍵ the right; ⍺⍺ denotes the left operand and ⍵⍵ the right. If ⍵⍵ occurs in the definition, then the dfn is a dyadic operator; if only ⍺⍺ occurs but not ⍵⍵, then it is a monadic operator; if neither ⍺⍺ or ⍵⍵ occurs, then the dfn is a function. The special syntax ⍺←expression is used to give a default value to the left argument if a dfn is called monadically, that is, called with no left argument. The ⍺←expression is not evaluated otherwise. ∇ denotes recursion or self-reference by the function, and ∇∇ denotes self-reference by the operator. Such denotation permits anonymous recursion. Error trapping is provided through error-guards, errnums::expression. When an error is generated, the system searches dynamically through the calling functions for an error-guard that matches the error. If one is found, the execution environment is unwound to its state immediately prior to the error-guard's execution and the associated expression of the error-guard is evaluated as the result of the dfn. Additional descriptions, explanations, and tutorials on dfns are available in the cited articles.

Examples The examples here illustrate different aspects of dfns. Additional examples are found in the cited articles.

Default left argument The function {⍺+0j1×⍵} adds ⍺ to 0j1 (i or −1) times ⍵.

The significance of this function can be seen as follows:

Complex numbers can be constructed as ordered pairs of real numbers, similar to how integers can be constructed as ordered pairs of natural numbers and rational numbers as ordered pairs of integers. For complex numbers, {⍺+0j1×⍵} plays the same role as - for integers and ÷ for rational numbers. Moreover, analogous to that monadic -⍵ ⇔ 0-⍵ (negate) and monadic ÷⍵ ⇔ 1÷⍵ (reciprocal), a monadic definition of the function is useful, effected by specifying a default value of 0 for ⍺: if j←{⍺←0 ⋄ ⍺+0j1×⍵}, then j ⍵ ⇔ 0 j ⍵ ⇔ 0+0j1×⍵.

The last expression illustrates Euler's formula on ten random numbers with real and imaginary parts in the interval (−0.5,0.5).

Single recursion The ternary construction of the Cantor set starts with the interval [0,1] and at each stage removes the middle third from each remaining subinterval: [0,1]→ [0,13]∪[23,1]→ [0,19]∪[29,13]∪[23,79]∪[89,1]→⋯ The Cantor set of order ⍵ defined as a dfn:

Cantor 0 to Cantor 6 depicted as black bars:

The function sieve ⍵ computes a bit vector of length ⍵ so that bit i (for 0≤i and i<⍵) is 1 if and only if i is a prime.

The last sequence, the number of primes less than powers of 10, is an initial segment of OEIS: A006880. The last number, 50847534, is the number of primes less than 109. It is called Bertelsen's number, memorably described by MathWorld as "an erroneous name erroneously given the erroneous value of π(109)=50847478". sieve uses two different methods to mark composites with 0s, both effected using local anonymous dfns: The first uses the sieve of Eratosthenes on an initial mask of 1 and a prefix of the primes 2 3...43, using the insert operator ⌿ (right fold). (The length of the prefix obtains by comparison with the primorial function ×⍀p.) The second finds the smallest new prime q remaining in b (q←b⍳1), and sets to 0 bit q itself and bits at q times the numbers at remaining 1 bits in an initial segment of b (⍸b↑⍨⌈n÷q). This second dfn uses tail recursion.

Tail recursion Typically, the factorial function is define recursively (as above), but it can be coded to exploit tail recursion by using an accumulator left argument:

Similarly, the determinant of a square complex matrix using Gaussian elimination can be computed with tail recursion:

Multiple recursion A partition of a non-negative integer n is a vector v of positive integers such that n = +⌿v, where the order in v is not significant. For example, 2 2 and 2 1 1 are partitions of 4, and 2 1 1 and 1 2 1 and 1 1 2 are considered to be the same partition. The partition function P(n) counts the number of partitions. The function is of interest in number theory, studied by Euler, Hardy, Ramanujan, Erdős, and others. The recurrence relation

P(n)=∑k=1n(−1)k+1[P(n−12k(3k−1))+P(n−12k(3k+1))] derived from Euler's pentagonal number theorem. Written as a dfn:

The basis step 1≥⍵:0≤⍵ states that for 1≥⍵, the result of the function is 0≤⍵, 1 if ⍵ is 0 or 1 and 0 otherwise. The recursive step is highly multiply recursive. For example, pn 200 would result in the function being applied to each element of rec 200, which are:

and pn 200 requires longer than the age of the universe to compute (7.57×1047 function calls to itself). The compute time can be reduced by memoization, here implemented as the direct operator (higher-order function) M:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Direct function

Start with the simplest possible case. Write down what Direct function claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Direct function before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Direct function ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Direct function

In research
Direct function appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Direct function in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Direct function is common in secondary-school and first-year university syllabi. It links to neighbouring topics APL programming language family, Array programming languages, Formal methods, so understanding it makes those chapters shorter.
In everyday life
Look for Direct function outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Direct function” →

How to study Direct function in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Direct function means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Direct function out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Direct function in simple terms?

A direct function (dfn, pronounced "dee fun") is an alternative way to define a function and operator (a higher-order function) in the programming language APL. A direct operator can also be called a dop (pronounced "dee op").

Why does Direct function matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Direct function?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Direct function.

Tags

  • APL programming language family
  • Array programming languages
  • Formal methods
  • Functional programming
  • Higher-order functions
  • Programming language topics
  • Programming paradigms

Keep exploring