
- •Foreword
- •1. Introduction
- •2. Culture Shock
- •3. Preliminaries
- •Notation Used in This Book
- •Terminology
- •Sentences (statements)
- •Word Formation (tokenizing rules)
- •Numbers
- •Characters
- •Valence of Verbs (Binary and Unary Operators)
- •How Names (Identifiers) Get Assigned
- •Order of Evaluation
- •How Names Are Substituted
- •What a verb (function) looks like
- •Running a J program
- •The Execution Window; Script Windows
- •Names Defined at Startup
- •Step-By-Step Learning: Labs
- •J Documentation
- •Getting Help
- •4. A First Look At J Programs
- •Average Daily Balance
- •Calculating Chebyshev Coefficients
- •5. Declarations
- •Arrays
- •Cells
- •Phrases To Memorize
- •Constant Lists
- •Array-creating Verbs
- •6. Loopless Code I—Verbs Have Rank
- •Examples of Implicit Loops
- •The Concept of Verb Rank
- •Verb Execution—How Rank Is Used (Monads)
- •Controlling Verb Execution By Specifying a Rank
- •Examples Of Verb Rank
- •Negative Verb Rank
- •Verb Execution—How Rank Is Used (Dyads)
- •When Dyad Frames Differ: Operand Agreement
- •Order of Execution in Implied Loops
- •A Mistake To Avoid
- •7. Starting To Write In J
- •8. More Verbs
- •Arithmetic Dyads
- •Boolean Dyads
- •Min and Max Dyads
- •Arithmetic Monads
- •Boolean Monad
- •Operations on Arrays
- •9. Loopless Code II—Adverbs / and ~
- •Modifiers
- •The Adverb Monad u/
- •The adverb ~
- •10. Continuing to Write in J
- •11. Boxing (structures)
- •Terminology
- •Boxing As an Equivalent For Structures In C
- •12. Compound Verbs
- •Verb Sequences—u@:v and u@v
- •Making a Monad Into a Dyad: The Verbs [ and ]
- •Making a Dyad Into a Monad: u&n and m&v
- •13. Empty Operands
- •Execution On a Cell Of Fills
- •Empty cells
- •If Fill-Cells Are Not Enough
- •14. Loopless Code III—Adverbs \ and \.
- •15. Verbs for Arithmetic
- •Dyads
- •Monads (all rank 0)
- •16. Loopless Code IV
- •A Few J Tricks
- •Power/If/DoWhile Conjunction u^:n and u^:v
- •Tie and Agenda (switch)
- •17. More Verbs For Boxes
- •Dyad ; (Link) And Monad ; (Raze)
- •Dyad { Revisited: the Full Story
- •Split String Into J Words: Monad ;:
- •Fetch From Structure: Dyad {::
- •Report Boxing Level: Monad L.
- •18. Verb-Definition Revisited
- •What really happens during m :n and verb define
- •Compound Verbs Can Be Assigned
- •Dual-Valence verbs: u :v
- •The Suicide Verb [:
- •Multi-Line Comments Using 0 :0
- •Final Reminder
- •The Obverse u^:_1
- •Apply Under Transformation: u&.v and u&.:v
- •Defined obverses: u :.v
- •An observation about dyadic verbs
- •20. Performance: Measurement & Tips
- •Timing Individual Sentences
- •Compounds Recognized by the Interpreter
- •Use Large Verb-Ranks! and Integrated Rank Support
- •Shining a Light: The J Performance Monitor
- •21. Input And Output
- •Foreigns
- •File Operations 1!:n; Error Handling
- •Treating a File as a Noun: Mapped Files
- •Format Data For Printing: Monad And Dyad ":
- •Format an Array: 8!:n
- •Format binary data: 3!:n
- •printf, sprintf, and qprintf
- •Convert Character To Numeric: Dyad ".
- •22. Calling a DLL Under Windows
- •Memory Management
- •Aliasing of Variables
- •23. Socket Programming
- •Asynchronous Sockets and socket_handler
- •Names and IP Addresses
- •Connecting
- •Listening
- •Other Socket Verbs
- •24. Loopless Code V—Partitions
- •Find Unique Items: Monad ~. and Monad ~:
- •Apply On Subsets: Dyad u/.
- •Apply On Partitions: Monad u;.1 and u;.2
- •Apply On Specified Partitions: Dyad u;.1 and u;.2
- •Apply On Subarray: Dyad u;.0
- •Apply On All Subarrays: Dyad u;.3 and u;._3
- •Extracting Variable-Length Fields Using ^: and ;.1
- •Example: Combining Adjacent Boxes
- •25. When Programs Are Data
- •Calling a Published Name
- •Using the Argument To a Modifier
- •Invoking a Gerund: m`:6
- •Passing the Definition Of a Verb: 128!:2 (Apply)
- •Passing an Executable Sentence: Monad ". and 5!:5
- •26. Loopless Code VI
- •28. Modifying an array: m}
- •Monad I.—Indexes of the 1s in a Boolean Vector
- •29. Control Structures
- •while./do./end. and whilst./do./end.
- •if./do./else./end., if./do./elseif./do./end.
- •try./catch./catcht./end. and throw.
- •return.
- •assert.
- •30. Modular Code
- •Locales And Locatives
- •Assignment
- •Name Lookup
- •Changing The Current Locale
- •The Shared Locale 'z'
- •Using Locales
- •31. Writing Your Own Modifiers
- •Modifiers That Do Not Refer To x. Or y.
- •Modifiers That Refer To x. Or y.
- •32. Applied Mathematics in J
- •Complex Numbers
- •Matrix Operations
- •Calculus: d., D., D:, and p..
- •Taylor Series: t., t:, and T.
- •Hypergeometric Function with H.
- •Sparse Arrays: Monad and Dyad $.
- •Random Numbers: ?
- •Computational Addons
- •Useful Scripts Supplied With J
- •33. Elementary Mathematics in J
- •Verbs for Mathematics
- •Extended Integers, Rational Numbers, and x:
- •Factors and Primes: Monad p:, Monad and Dyad q:
- •Permutations: A. and C.
- •34. Graphics
- •Plot Package
- •2D Graphics: the gl2 Library
- •Displaying Tabular Data: the Grid Control
- •3D Graphics: OpenGL
- •35. Odds And Ends
- •Dyad # Revisited
- •Boxed words to string: Monad ;:^:_1
- •Spread: #^:_1
- •Choose From Lists Item-By-Item: monad m}
- •Recursion: $:
- •Make a Table: Adverb dyad u/
- •Cartesian Product: Monad {
- •Boolean Functions: Dyad m b.
- •Operations Inside Boxes: u L: n, u S: n
- •Comparison Tolerance !.f
- •Right Shift: Monad |.!.f
- •Generalized Transpose: Dyad |:
- •Monad i: and Dyad i:
- •Fast String Searching: s: (Symbols)
- •Fast Searching: m&i.
- •CRC Calculation
- •Unicode Characters: u:
- •Window Driver And Form Editor
- •Tacit Programming
- •36. Tacit Programs
- •37. First Look At Forks
- •38. Parsing and Execution I
- •39. Parsing and Execution II
- •The Parsing Table
- •Examples Of Parsing And Execution
- •Undefined Words
- •40. Forks, Hooks, and Compound Adverbs
- •Tacit and Compound Adverbs
- •Referring To a Noun In a Tacit Verb
- •41. Readable Tacit Definitions
- •Flatten a Verb: Adverb f.
- •Special Verb-Forms Used in Tacit Definitions
- •43. Common Mistakes
- •Mechanics
- •Programming Errors
- •44. Valedictory
- •45. Glossary
- •46. Error Messages
- •47. Index
32. Applied Mathematics in J
Complex Numbers
All the mathematical functions can be applied to complex numbers. A complex constant is written with the letter j separating the real and imaginary parts, e. g. 0j1 is the square-root of _1 . Alternatively, a constant can be written in polar form in which the letters ar (or ad) separate the magnitude of the number from the angle in radians (or degrees) between the real axis and a line in the complex plane from the origin to the point representing the number:
1ar1
0.540302j0.841471
1ad90
0j1
A number of verbs are available for operations on components of complex numbers. All have rank 0.
+ y conjugate of y
+. y creates a 2-atom list of the real and imaginary components of y
. y creates a 2-atom list of the length and angle in radians of the polar form of y | y magnitude of y
j. y |
0j1 * |
y |
r. y |
^ 0j1 |
* y |
x j. y |
x + |
j. y (i. e. x is the real part, y is the imaginary part) |
xr. y x * r. y (i. e. polar form, where x is the magnitude and y the angle in radians)
! y factorial of y (more generally, the gamma function Γ(1+y))
Matrix Operations
J has primitive verbs for operations on matrices, some using the conjunction . . -/ .* y gives the determinant of y; x +/ .* y is the matrix product of x and y;
%. y is the matrix inverse of y (provided y is nonsingular); x %. y is the projection of x onto y .
In x +/ .* y, a rank-1 x is treated as a matrix with one row, and a rank-1 y is treated as a matrix with one column; but the result rank, which is 2 when rank-2 matrices are multiplied, is 1 when one operand has rank 1 and 0 when both do.
x %. y is a rough-and-ready way to get a least-squares approximation of x as a linear combination of the columns of y . If your y is singular or close to it, avoid %. and use methods based on singular value decomposition.
184
Polynomials: p.
J supports 3 different ways of specifying a polynomial:
1.as a list of coefficients of increasing powers, starting with power 0 (i. e. a constant term), where each coefficient multiplied by the corresponding power of the variable produces a term; the polynomial is the sum of the terms. This form is an unboxed numeric list.
2.as a multiplier m and a list of roots r, representing the polynomial
m(x-r0)(x-r1)…(x-rn). This form is a 2-item list of boxes m;r (if m is 1 it may be omitted, leaving just <r).
3.(for multinomials) a multinomial of n variables is represented as a list of terms, each term consisting of a coefficient and a list of n exponents (one exponent per variable). The multinomial is the sum of the terms. The form is a boxed rank-2 array in which each item is a coefficient followed by the list of n exponents. This form is distinguished from the multiplier-root form by the rank of the boxed array. (It is possible to have multiple multinomials that share a common exponent array, by having more than one coefficient preceding each list of exponents, but we will not pursue that here)
For example, three ways of expressing the polynomial 3x3-12x (which can be written 3x(x+2)(x-2))are 0 _12 0 3, 3;_2 0 2, and <2 2$_12 1 3 3 .
p. y has rank 1 and converts between coefficient and multiplier-root form of the polynomial y . Note that converting from coefficient form to multiplier-root form solves for the roots of the polynomial.
p. 0 _12 0 3 +-+------+ |3|2 _2 0| +-+------+
p. 3;2 0 _2 0 _12 0 3
If the multinomial form has only one variable (i. e. each item has length 2), monad p. will convert it to coefficient form:
p. <2 2$_12 1 3 3 0 _12 0 3
Dyad p. has rank 1 0 and is used to evaluate a polynomial in any of these forms. If x is a polynomial in coefficient or multiplier-root form, x p. y evaluates it with y giving the value of the variable:
0 _12 0 3 p. 1
_9
(3;_2 0 2) p. _2 _1 0 1 2 0 9 0 _9 0
(the second evaluation applied the polynomial to 5 different values of the variable).
If x is a multinomial, x p. <y evaluates it with the list y giving the values of the variables (y must be boxed because the right rank of dyad p. is 0 and in this case there is
185
more than one variable). So to evaluate the binomial x3+3x2y+3xy2+y3 with x=2 and y=3 we have
(<4 3$1 3 0 3 2 1 3 1 2 1 0 3) p. <2 3
125
as expected.
Calculus: d., D., D:, and p..
J provides support for differential and integral calculus. u d. n produces the verb that gives the nth derivative of u :
*: d. 1
+:
*: y is y squared; the derivative is +: y which is y doubled.
^&3 d. 1 3&*@(^&2)
^&3 y is y cubed; the derivative is 3 * y ^ 2 .
^&3 d. 2 3"0 * +:
Second derivative is 3 * 2 * y .
*: d. _1
0 0 0 0.33333&p.
The _1st derivative is the indefinite integral, which is (y ^ 3) % 3 . The form the interpreter uses is the polynomial form.
f =. *:
f d. _1 (6)
72
g =. *:@+: g d. _1 (6)
288
u d. n produces ordinary derivatives, and evaluates its u with rank 0. For partial derivatives, use u D. n where u has rank greater than 0. Each cell of y produces an array of results, one result for each atom of the cell, giving the partial derivative with respect to that atom. For example, the length of a vector is given by
veclength =: +/&.:*:"1
which squares the atoms, adds them, and takes the square root: veclength 3 4 5
7.07107
The derivative of the vector length with respect to the individual components is given by: veclength D. 1 (3 4 5)
0.424264 0.565685 0.707107
The result of u need not be a scalar. Here we define the cross product:
186
xp =: dyad : '((1|.x.)*(_1|.y.)) - ((_1|.x.)*(1|.y.))'"1 0 0 2&xp D. 1 (4 2 0)
0 2 0 _2 0 0 0 0 0
Each row is the vector-valued partial derivative of the cross product (0 0 2 xp 4 2 0) with respect to one component of y .
The interpreter will bend every effort to find a derivative for your function, but it may fail, or you may not like the derivative it chooses. m D. n, when m is a gerund u`v, produces a new verb which executes like u but whose nth derivative is v :
a =. *:`] D. 1
Here a is defined to be *: except that its derivative is ] . a
*:`]D.1
a D. 1 (5)
5
Sure enough, the derivative is ] .
If you don't want to express the derivative, you can have the interpreter approximate it for you. x u D: n y approximates the derivative at y by evaluating u at y and y+x . Both the left and right ranks of u D: n are the monadic rank of u, so you can specify different step-sizes for different atoms of y (if x is a scalar, it is used for the stepsize at all atoms of y).
You can do calculus on polynomials by manipulating the polynomial forms without having to create the verbs that operate on those forms. p.. y (rank 1) takes polynomial y in either coefficient or multiplier-root form and produces the coefficient form of the derivative of y . x p.. y (rank 0 1) produces the coefficient form of the integral of y with constant term x .
Taylor Series: t., t:, and T.
With derivatives available, Taylor series are a natural next step. u t. y is the yth Taylor coefficient of u expanded about 0, and x u t. y evaluates that term at the point x, in other words x u t. y is x^y * u t. y . All ranks of u t. are 0.
u T. n is a verb which is the n-term Taylor approximation to u (expanded about 0).
u t: y is (!y) * u t. y .
Hypergeometric Function with H.
The generalized hypergeometric function is specified by two lists of numbers, a numerator list and a denominator list. The generalized hypergeometric function is the sum over all k of the (infinite) generalized hypergeometric series, which is a power series in which the coefficient of the yk term is the product of the rising factorials of length k of the numerator items divided by a similar product for the denominator items, and then divided by !k .
187