home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
teach.zip
/
TEACH6A.ATF
< prev
next >
Wrap
Text File
|
1997-09-18
|
79KB
|
961 lines
XNÉIO 0 1 °
XNÉCT 0 1E²13 °
XCÉFC 1 6 .,*0_² °
XNÉRL 0 1817129560 °
XCÉPR 1 1 °
XCÉLX 1 5 TEACH °
XNA 2 4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 °
NARRAY 3 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 °
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 °
X 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 °
*(1997 9 14 12 30 44 504) °
XFDISCLAIMER ÉFX 'DISCLAIMER' 'Σ Copyright statement' 'disclaimer' °
*(1996 4 6 16 2 32 304) °
FDISPLAY ÉFX 'D╜S DISPLAY A;ÉIO;R;C;HL;HC;HT;HB;VL;VB;V;W;N;B' °
'Σ DISPLAY A GENERAL ARRAY IN PICTORIAL FORM' °
'Σ NORMAL CALL IS MONADIC. DYADIC CALL USED ONLY IN' °
'Σ RECURSION TO SPECIFY DISPLAY RANK, SHAPE, AND DEPTH.' 'ÉIO╜0' °
'»(0=ÉNC ''S'')/''S╜µA''' 'R╜╞µ,S Σ PSEUDO RANK.' °
'C╜''┐┌└┘'' Σ UR, UL, LL, AND LR CORNERS.' °
'HL╜''─'' Σ HORIZONTAL LINE.' °
'HC╜HL,''Θ╕'',HL,''~+ε'' Σ HORIZONTAL BORDERS.' °
'HT╜HC[(0<R)⌡1+0<╞²1╞,S]' 'ΣW╜,0╧■╞0µΓ(1⌐µA)╞A' °
'HB╜HC[3+3╛(''2⌡~A╧«A'' ÉEA ''1+╞ε0⌡(1⌐⌡/µA)╞,A'')+3⌡1<µµS]' °
'VL╜''│'' Σ VERTICAL LINE.' °
'VB╜VL,''Φ╟'' Σ VERTICAL BORDER.' °
'V╜VB[(1<R)⌡1+0<²1╞²1╟,S]' °
'»(0εµA)/''A╜(1⌐µA)µΓ╞A'' Σ SHOW PROTOTYPE OF EMPTIES.' °
'╕(1<╧A)/GEN' '╕(2<µµA)/D3' °
'D╜«A Σ SIMPLE ARRAYS.' 'W╜1╞µD╜(²2╞1 1,µD)µD' °
'N╜²1+1╟µD' '╕(0=µµA)/SS' °
'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[ °
3]' '╕0' 'SS:HB╜((0 '' '')=╞0µΓA)/'' -''' °
'D╜'' '',('' '',[0]D,[0]HB,Nµ'' ''),'' ''' '╕0' °
'GEN:D╜«DISPLAY■A Σ ENCLOSED ...' 'N╜Dδ.⌠'' ''' °
'D╜(Nδ~1ΦN)≡D' 'D╜(δ≡~'' ''╤D)/D' 'D╜((1,µS)µS)DISPLAY D' °
'╕(2≥µ,S)╟D3E,0' 'D3:D╜0 ²1╟0 1╟«ΓA Σ MULT-DIMENSIONAL ...' °
'W╜1╞µD' 'N╜²1+1╟µD' °
'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[ °
3]' 'D3E:N╜²2+µ,S' °
X 'V╜C[Nµ1],[0]VB[1+0<²2╟,S],[0](((²3+╞µD),N)µVL),[0]C[Nµ2]' 'D╜V,D' °
*(1996 4 6 16 2 32 304) °
FEXIT ÉFX 'EXIT' 'Σ Exit from function' '''To log off type: )OFF''' °
X '╕' °
*(1996 4 6 16 2 32 304) °
FGO ÉFX 'GO;T;E;B' 'Σ Expression driver' 'L0:B╜E╜''''' 'æ╜'' ''' °
'T╜æ' '╕(^/'' ''=T)/L0' '╕((^/'')OFF ''=5╞6╟T)doif ''EXIT'')/0' °
'╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/L0' °
'''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/L0' '╕B/L0' °
'''This is not a valid APL2 expression''' 'æ╜''*''' °
X '╕(''?''⌠╞1╟æ)/L0' 'E' '╕L0' °
*(1997 8 14 12 49 25 500) °
FHELP ÉFX 'HELP;N;I;T' 'Σ Help to student' '''WHAT TO DO'',r' °
''' ° To get out of the lesson''' ''' ENTER: EXIT''' °
''' ° To log off APL2''' °
''' FIRST, ENTER: EXIT THEN ENTER: )OFF''' °
''' ° To get help''' ''' ENTER: HELP''' °
''' ° When you see the prompt on a blank line''' °
''' ENTER AN APL2 EXPRESSION - OR JUST PRESS: ENTER''' 'do' °
''' ° If you get this line'',r' °
'''This is not a valid APL2 expression'',r,''*'',r' °
''' YOU CAN EITHER''' ''' A. PRESS: ENTER''' °
''' B. PRESS: ? and then ENTER to see what was incorrect''' °
''' in your expression causing that response'',r' 'do' °
'''HINTS'',r' °
'''This lesson is made up of '',(«N╜6),'' components named TEACHx, whe °
re''' '''the x stands for a digit:'',r' 'I╜0' °
'L0:T╜''TEACH'',(«I╜I+1)' 'T,'' '',1╟notb(ÉCR T)[2;]' '╕(N>I)/L0' °
'do' °
'''You may review either of these components separately. To do that,'' °
' °
'''first enter EXIT (and RETURN), then enter the name of the lesson.'' °
' '''component (e.g. TEACH4).'',r' °
'''To re-start the lesson, just enter TEACH'',r' °
'''When the screen fills up, it is a good idea to move the cursor to'' °
' '''the start of a convenient paragraph, and press ENTER'',r' °
'''You may also wish to press PAGE UP to review the prevous pages.''' °
'do' '''RESOURCES'',r' °
'''You may also enter ONE of the following words at a time'',r' °
'''RANK Describes the concept of RANK using geometry''' °
'''TABLES A quick review of tables in the past''' °
X '''REVIEW Review APL2 concepts studied in previous lesson''' 'endd' °
NM 4 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 °
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 °
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 °
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 °
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 °
X 111 112 113 114 115 116 117 118 119 120 °
XNNUMBERS 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 °
*(1996 4 6 16 2 32 304) °
FRANK ÉFX 'RANK' 'Σ Rank, shape and dimension' °
'''We have introduced two concepts that at first glance appear to be'' °
' °
'''related: RANK, and DIMENSION. The concept of RANK can be particular °
ly''' °
'''confusing, since the same word is used in mathematics with a simila °
r''' '''but DISTINCT meaning.'',r' °
'''These concepts can be best understood if we relate them to a subjec °
t''' '''we all have studied in High School, namely Geometry.''' 'do' °
'''We can begin with a single plane. A point in a plane has no width o °
r''' °
'''height. We say that a point has no dimensions. Two distinct points' °
'' °
'''in a plane can be connected by a straight line. If we assign to one °
''' °
'''of these points the value of zero, and to the other point the value °
of''' °
'''one, then we can specify any intermediate point of the line by a''' °
'''number between zero and one. The line represents a single DIMENSION °
,''' °
'''and the distance from point zero to any point on that line is a one °
''' '''dimensional VECTOR.''' 'do' °
'''To describe a point in the plane but not on the line requires some' °
'' °
'''additional concepts. Suppose that we draw a second line from point °
zero''' °
'''in such a way that the two lines form an angle. We will call the''' °
'''first line the X-axis, and the second line the Y-axis. We say that °
these''' '''two axes SPAN the plane. What do we mean by that?''' 'do' °
'''We mean that we can reach any point in the plane by drawing two''' °
'''VECTORS. The first vector is along the X-axis, and the second vecto °
r''' °
'''is along a line parallel to the Y-axis. Since it takes a single''' °
'''number to specify a point along one line, we will need two numbers' °
'' °
'''(one for X and one for Y) to specify an arbitrary point in a plane. °
'',r' °
'''Now consider a plane to be just a surface in three dimensions. We c °
an''' °
'''draw a third line from point zero in such a way that the points on' °
'' °
'''this line do not lie on the plane. We will call this line the Z-axi °
s.''' 'do' °
'''The three axes now SPAN three dimensional space. To find an arbitra °
ry''' °
'''point in space, we must specify three vectors. one along the X-axis °
,''' °
'''one along a line parallel to the Y-axis, and finally, one parallel °
to''' '''the Z-axis.'',r' °
'''Now let us relate these points in space to scalars, vectors and arr °
ays''' 'do' °
'''A scalar can be thought of as a point in space without reference to °
''' °
'''an axis. A vector is the distance from zero to a point. If this vec °
tor''' °
'''is made up of only one scalar component, we say it has one DIMENSIO °
N.''' °
'''If a vector has two scalar components, it specifies locations in a' °
'' °
'''TWO DIMENSIONAL plane. A three component vector specifies a point i °
n''' '''three dimensional plane.''' 'do' °
'''Now suppose that we have the vector (a,b). It represents a point th °
at''' °
'''can be reached by going first along the X-axis, and then going alon °
g''' °
'''a direction parallel to the Y-axis. In APL2 short-hand this can be' °
'' '''written as'',r,r,'' (a⌡1 0) + b⌡0 1'',r' °
'''The first part says that we go a units in the first direction, and °
b''' °
'''units in the second direction. The vectors 1 0 and 0 1 have a speci °
al''' °
'''name in math. They are called the BASIS of the coordinate system.'' °
' 'do' °
'''Now suppose that we have a point in three dimensions. Following the °
''' °
'''same argument, we find that it takes three scalars (a,b,c) to defin °
e''' '''a point in three dimensions. What about a line?'',r' °
'''In a plane, a line is defined by two points (a,b) and (d,e). In thr °
ee''' °
'''dimensions, a line is also specified by two points (a,b,c) and (d,e °
,f)''' 'do' °
'''If the two points are distinct, the two vectors in either case not' °
'' °
'''only specify a line, but also SPAN a plane, namely the plane contai °
ning''' °
'''the two points AND the point zero. One way to represent such a plan °
e''' '''is to write the two vectors as an ARRAY.'',r' °
''' a b a b c''' ''' d e or d e f'',r' °
'''The APL2 RANK of these two arrays is 2. This is not the same as''' °
'''MATHEMATICAL RANK. In LINEAR ALGEBRA (a field of mathematics), the' °
'' °
'''two arrays are called MATRICES. They also represent points in space °
,''' °
'''but the concept of RANK deals not with the fact that a matrix is''' °
'''rectangular. Instead, mathematical rank denotes the dimensionality' °
'' °
'''of the space spanned by the row (or column) vectors. If for example °
''' °
'''we have (m n)=k⌡a b, then the rank of the first matrix is 1, since' °
'' '''the two vectors are co-linear.''' 'do' °
'''Now, suppose that we wish to specify two distinct lines. If the sec °
ond''' °
'''pair of lines is given by (m,n) (p,q) in two dimensions, and by (m, °
n,o)''' °
'''(p,q,r) in three dimensions, we can combine all as arrays of APL2 r °
ank 3''' °
'r,'' a b a b c'',r,'' d e d e f'',r,'' °
or''' ''' m n m n o'',r,'' p q p q r'',r' °
'''Here, the APL2 rank refers to the number of APL2 axes, and not to t °
Xhe''' '''geometric meaning of the underlying data.''' 'endd' °
*(1996 4 6 16 2 32 304) °
FREVIEW ÉFX 'REVIEW;V' 'Σ Review lesson 5' °
'''In lesson 5 we studied APL2 notation for absolute value, residue,'' °
' '''encode and decode. We also introduced the scan operator.'',r' °
'''The absolute value is expressed in APL2 with the symbol |'',r' °
'show ''|²2 2''' °
'''Write now an expression that will produce numeric vector V with''' °
'''positive and negative values. Then apply the monadic absolute value °
''' '''primitive:''' 'do' °
'''The residue of A when divided by B is given by B|A. It is also''' °
'''called A MODULO B'',r' 'show ''5|2 4 6 8''' °
'''Now write an expression that will produce the residue of vector V'' °
' '''modulo 2:''' 'do' °
'''Decode converts a decimal number into components in any other base' °
'' °
'''system. For example to convert 586 into hexadecimal, we would write °
'',r' 'show ''16*1 2 3 4'' ''16 16 16ÿ586''' °
'''Why did we evaluate expression 16*1 2 3 4 first?'',r' °
'''Since hexadecimal notation uses the symbols A B C D E F to represen °
t''' '''respectively 10 11 12 13 14 15, we would have to write'',r' °
'show ''''''0123456789ABCDEF''''[1+16 16 16ÿ586]''' °
'''For the decode, suppose you have an integer in the range (0,1000),' °
'' '''say 586. Convert this integer to its octal representation:''' °
'do' °
'''The expression L¥R where L is a scalar or a vector of length µR wi °
ll''' °
'''convert vector R back to its numeric representation in base L'',r' °
'show ''5¥0 2 4 4'' ''10¥0 23 0''' °
'''Now take 4 3 2 1, the representation of 586 to base 5, and use''' °
'''decode to represent the integer in decimal notation.''' 'do' °
'''The scan operator when applied to a vector, generates a vector of'' °
' '''the same length. The Nth component of this vector contains the''' °
'''result of applying the reduce operator to the first N elements of'' °
' '''the vector'',r' 'show ''÷/6'' ''÷/6 3'' ''÷/6 3 2'' ''÷\6 3 2''' °
'''For the scan operator, generate a vector whose Nth component is the °
X''' '''sum of the first N elements of V.''' 'do' 'endd' °
*(1997 5 31 11 59 20 572) °
FRULE ÉFX 'V RULE W' 'Σ Display rules' '╕(2=ÉNC ''V'')/L0' °
X '2 rule W ╪ ╕0' 'L0:V rule W' °
*(1997 5 24 12 27 15 400) °
FTABLES ÉFX 'TABLES' 'Σ History of tables' °
'''One might ask, who invented tables? The answer is quite surprising. °
''' °
'''We find tables in the oldest mathematical documents in existence.'' °
,r' °
'''One of the earliest mathematical "textbooks" of Ancient Egypt''' °
'''is the Ahmes Papyrus, dating to about 1650 BC. This document contai °
ns''' °
'''a table that converts fractions of the form A/B to unit fractions o °
f''' '''the form 1/C'',r' '''For example 2/5 = 1/3 + 1/15''' °
''' 2/7 = 1/4 + 1/28''' °
''' 2/13 = 1/8 + 1/52 + 1/104'',r' °
'''The Babylonian cuneiform tablets also contain examples of tables.'' °
' '''These include multiplication tables with reciprocals in their''' °
'''hexadecimal (base 60) system.''' 'do' °
'''The Chinese were particularly fond of Magic Squares, namely square' °
'' °
'''integer arrays, such that the sum of rows or columns produced the'' °
' '''same number'',r' ''' 4 9 2''' ''' 3 5 7''' ''' 8 1 6'',r' °
'''The sum of any row, column or diagonals is 15.'',r' 'do' °
'''Ptolemy, who lived in the second century A.D. in Alexandria, Egypt' °
'' °
'''was the earliest mathematician who published extensive tables.''' °
'''In his book called the Almagest, there are tables for chords on a'' °
' °
'''circle as a function of angle. This is the ancient equivalent of''' °
'''trigonometric tables. In his book called Optics, he has a table of' °
'' °
'''angles of refraction from air to glass and from air to water.'',r' °
'''The use of tables became an indispensable part of computations''' °
'''with the publication by Briggs of the table of Napier''''s logarith °
Xms''' '''in 1614.''' 'endd' °
*(1997 7 4 11 40 43 488) °
FTEACH ÉFX 'TEACH' 'Σ Start sixth lesson in APL2 by Zdenek V JIZBA' °
'exit ''TEACH''' 'initialize' 'TEACH1 Σ Arrays and rank' °
'TEACH2 Σ Scalar functions' 'TEACH3 Σ IBM function DISPLAY' °
'TEACH4 Σ Non-scalar primitive functions' °
'TEACH5 Σ Reduction operator and arrays' °
X 'TEACH6 Σ The scan operator and arrays' 'problems' °
*(1997 8 28 12 11 30 424) °
FTEACH1 ÉFX 'TEACH1;A;MANY' 'Σ Study of ARRAYS' 'exit ''TEACH1''' °
'''To review previous lesson, enter REVIEW.''' 'do' °
''' APL ARRAYS'',r' °
'''So far we have studied APL2 objects called "scalars" and "vectors". °
''' °
'''Scalars are objects of rank 0, and vectors are objects of rank 1.'' °
' °
'''In this lesson we will study objects of rank 2, 3 and higher. We''' °
'''will see how all of the primitive functions studied so far work''' °
'''on these higher rank objects. We will also see how to use the REDUC °
E''' '''and SCAN operators on rank 2 or higher "arrays".'',r' °
'''We have used the concepts "scalar" and "vector", because these shou °
ld''' °
'''be familiar to you if you have some background in math. In APL, the °
se''' °
'''concepts are tolerated for that reason. However the proper name for °
''' °
'''a scalar is "RANK 0 ARRAY", and for a vector "RANK 1 ARRAY". The re °
ason''' °
'''for this is that in APL, ALL data are considered to be ARRAYS. With °
this''' °
'''understanding, we will continue to apply the term SCALAR to the two °
types''' °
'''of primitive data items: CHARACTERS and NUMBERS. We will also use t °
he''' '''term VECTOR as a synonym of any rank 1 APL2 object.''' 'do' °
'''To refresh your memory, SHAPE (µ) returns the way information is''' °
'''stored in the array. This shape is ALWAYS a vector (i.e a rank 1 ar °
ray).''' °
'''The "shape of the shape" of an array is therefore a vector of lengt °
h 1,''' °
'''and is called RANK. A number (or a single character) belong to the' °
'' °
'''class of objects we call "rank 0 arrays". Here is an example'',r' °
'show ''µ5'' ''µµ5''' °
'''The blank line below µ5 means that 5 has no length. It is a point i °
n''' °
'''space with no dimensions. However, µµ5 which is the same as µ(µ5) i °
s''' °
'''a vector. The zero tell us that (µ5) is an "empty vector"; one migh °
t''' °
'''say a "vector of length zero". Let us look at other examples'',r' °
'show ''µ3.14159'' ''µ''''A'''''' ''µ''''5'''''' ''µ''''$''''''' °
'''A single number no matter how large or small, is a "rank 0 array".' °
'' °
'''A single character, such as a letter, a digit in quotes or a specia °
l''' °
'''symbol is also a "rank 0 array" (scalar). We have seen that the com °
ma''' °
'''when applied monadically (called RAVEL), generates a "rank 1 array" °
(a''' °
'''vector). If we apply ravel to a scalar (rank 0 array), the resultin °
g''' °
'''vector will have only one element, so its SHAPE will be 1 and its R °
ANK''' '''will also be 1'',r' 'show ''µ,3.14159'' ''µ(µ,3.14159)''' °
'''The interpretation of scalars as opposed to length 1 vectors will v °
ary.''' °
'''You could, for example, think of ,3.14159 as a vector on the real n °
umber''' °
'''axis with its point 3.14159 units away from the origin.'',r' °
''' |.....|.....|.....|>''' ''' 0 1 2 3'',r' °
'''Now let us look at other rank 1 arrays (vectors)'',r' °
'show ''µ2 2'' ''µµ2 2''' °
'''We could think of 2 2 as a point in the plane two units to the righ °
t''' '''of zero and two units above zero'',vec' °
'''On the other hand, it could be thought of merely as a LIST with two °
''' °
'''components, or a pair of points along one line. The interpretation' °
'' °
'''of what a rank 2 array means will depend on what the problem is.'', °
r' '''Here are some more examples of shapes and ranks'',r' °
'show ''µ2 3 4'' ''µµ2 3 4'' ''µ''''ONE'''''' ''µµ''''ONE''''''' °
'''Later we will see that rank can be thought as a the NUMBER OF AXES' °
'' °
'''it takes to display the object. From this point of view, ARRAYS are °
''' °
'''APL objects that have 0(scalars) 1(vectors) 2 or more axes. If all °
of''' °
'''this leaves you confused, you might try to look at a more geometric °
al''' '''explanation by entering RANK.''' 'do' °
''' GENERATING HIGHER RANK OBJECTS'',r' °
'''You will recall that one way of generating APL2 objects is by using °
''' '''the dyadic form of (µ) called RESHAPE'',r' 'show ''3 4µ∞6''' °
'''This example shows an array with 3 rows and 4 columns. Here is an'' °
' '''array made up of characters only'',r' °
'show ''2 4µ''''ONE TWO ''''''' °
'''This example shows an array with 2 rows and 4 columns, but we do''' °
'''not see the fourth column, because it contains blanks. The next''' °
'''array is called A, and it contains both numbers and text'',r' °
'show ''A╜2 3 4µ(?12µ9),''''HANDHEADFOOT'''''' ''A''' °
'''This example is an array with 2 planes, 3 rows and 4 columns.''' °
'''It has three axes, namely [depth, height, width].'',r' °
'show ''µA'' ''µµA''' °
'''By extension, it is possible to generate arrays with 4, 5, ... and' °
'' °
'''more axes. However, in practice, arrays with rank greater than 3''' °
'''are seldom used. This is not because such arrays are not useful. It °
''' °
'''is that most of us just have not learned how to use them effectivel °
y.''' 'do' °
'''You can try generating objects with many axes like this'',r' °
'show ''MANY╜2 3 4 5 6µ1'' ''µMANY'' ''µµMANY''' °
'''MANY has rank of 5, and if you display it it does not even fit''' °
X '''onto the screen.''' 'endd' °
*(1997 5 22 13 9 23 356) °
FTEACH2 ÉFX 'TEACH2;NUMBERS;TABLE' 'Σ Arrays and primitive functions' °
'exit ''TEACH2''' °
'''Let us now study "two dimensional" arrays (rank =2)'',r' °
'show ''NUMBERS╜3 4µ∞12'' ''TABLE╜4 5µ'''' ONE TWOTHREE FOUR'''''' ' °
'TABLE''' °
'''Array NUMBERS contains only numbers, while array TABLE contains''' °
'''only text. Let us first see what we can do with arrays of numbers'' °
,r' 'show ''NUMBERS'' ''5+NUMBERS'' ''NUMBERS⌡2''' °
'''As with vectors, we can do arithmetic with numbers. Indeed, we can' °
'' °
'''use all the primitive numeric functions that we have studied so far °
'',r' 'show ''NUMBERS*2'' ''3<NUMBERS'' ''4|NUMBERS''' °
'''Every primitive function that allows a scalar to be applied to ever °
y''' °
'''element of an array, is called a SCALAR FUNCTION. This includes all °
''' °
'''mathematical functions ( + - ⌡ ÷ * ⌐ ╛ ...), all relational functio °
ns''' °
''' = ⌠ < ≥ ...) and all logical functions when applied to boolean arr °
ays''' '''( δ ^ ~ ...).''' 'do' °
'''Can we apply arrays to arrays of different ranks? (Yes, but we are °
not''' °
'''ready for that yet.) How about applying arrays to arrays of the sam °
e rank?''' 'do' °
'''Yes. One way is if both arrays have the SAME SHAPE'',r' °
'show ''NUMBERS+NUMBERS'' ''NUMBERS÷NUMBERS'' ''NUMBERS⌡6<NUMBERS''' °
'''What about arrays containing text?'',r' °
'show ''TABLE'' ''TABLE⌠''''O''''''' °
'''In other words, all the primitive functions that we have studied''' °
'''so far can be also used with arrays without modification!'',r' °
'''Of what use are arrays? In mathematics, rectangular arrays can be'' °
' °
'''thought of as collections of vectors forming a table. These numeric °
''' °
'''arrays are at the foundation of many mathematical concepts. In''' °
'''practical applications, numeric tables have been used for centuries °
''' °
'''to look up values of many functions (look up tables). For a brief'' °
' '''summary of the history of tables, enter TABLES.'',r' 'do' °
'''We have seen that APL2 arrays can be processed with primitive scala °
r''' °
'''functions. We can summarize our findings with the following rules'' °
X,r' '1 RULE 12' 'do' '1 RULE 13' 'do' '1 RULE 14' 'endd' °
*(1997 5 24 12 0 9 268) °
FTEACH3 ÉFX 'TEACH3;V;A' 'Σ Empty arrays' 'exit ''TEACH3''' °
''' EMPTY ARRAYS AND THE FUNCTION "DISPLAY"'',r' °
'''We have seen that one way to generate arrays is to use dyadic resha °
pe.''' °
'''The left argument to reshape must be an integer vector, but zero is °
''' '''also allowed as a valid component of that vector.'',r' °
'''We will now study what it means to have zero in the left argument t °
o''' '''reshape.''' 'do' °
'''As we have seen earlier, one example of a null vector is an APL2 ob °
ject''' °
'''that has the rank of a vector (1), but a shape of zero. A null vect °
or can''' '''be generated in several ways'',r' °
'show ''µ5'' ''0µ5'' ''∞0'' '''''''''''' ''(2 3 4)[∞0]''' °
'''Since we cannot see a NULL vector, we cannot see its internal struc °
ture.''' °
'''Because it is important to be able to tell what ANY APL2 array cont °
ains,''' °
'''IBM provides a special utility function called DISPLAY'',r' °
'show ''DISPLAY 0µ5'' ''DISPLAY ''''''''''' °
'''As you can see, not all nulls are the same. The first example shows °
''' °
'''a null numeric vector, while the second example shows a null charac °
ter''' '''vector. Now let us generate some null arrays'',r' °
'show ''DISPLAY 0 0µ1'' ''DISPLAY 0 0µ''''X''''''' °
'''The symbols on top and side identify both axes to be null. (The sym °
bol''' °
'''at bottom distinguishes numeric from character vectors). A null arr °
ay''' '''can have a non-null axis'',r' °
'show ''DISPLAY 0 5µ5'' ''DISPLAY 5 0µ''''X''''''' °
'''The non null axis does exist, and indeed retains its shape.'',r' °
'''What about arrays of rank greater than 2? Function DISPLAY has a''' °
'''compact way to represent such arrays'',r' °
'show ''DISPLAY 0 2 3µ1'' ''DISPLAY 2 0 3µ1'' ''DISPLAY 2 3 0µ1''' °
'''One obvious question is why all this concern with empty arrays.''' °
'''The technical answer is COMPLETENESS. Valid APL2 expressions should °
''' °
'''always return valid APL2 objects. To insure that this condition is' °
'' °
'''satisfied, we need to have empty arrays. A more practical answer is °
''' °
'''that empty arrays are useful. Here is a simple example. Suppose tha °
t''' °
'''your application consists of generating a vector by catenating data °
''' °
'''to an existing vector. To start the process, it is simple to begin' °
'' '''with an empty vector'',r' °
'show ''V╜0µ0'' ''DISPLAY V'' ''V╜V,∞2'' ''DISPLAY V'' ''V╜V,∞4'' ''DI °
SPLAY V''' °
'''Similarly, in other applications one may wish to begin with an arra °
Xy'',r' 'show ''A╜3 0µ1'' ''DISPLAY A'' ''DISPLAY A,3''' 'endd' °
*(1997 8 28 12 18 55 552) °
FTEACH4 ÉFX 'TEACH4' 'Σ Reduction and Scan operators on arrays' °
'exit ''TEACH4''' °
'''What about non scalar primitive APL2 functions. We have already see °
n''' °
'''that the monadic ∞ works only with scalar integers, so clearly it i °
s''' °
'''not going to work with arrays. The dyadic ∞ requires the left argum °
ent''' '''to be a VECTOR. The right argument can be any array'',r' °
'show ''2 3 4∞5'' ''2 3 4∞3 4 5'' ''2 3 4∞3 4µ∞12''' °
'''We have already seen that the monadic shape by definition will work °
''' °
'''on any array, but reshape again requires the left argument to be a' °
'' '''vector'',r' 'show ''2 3µ5'' ''2 3µ∞6'' ''2 3µ4 3µ∞12''' °
'''There is an exception: A scalar left argument is allowed, but it''' °
'''is treated as a one element vector:'',r' °
'show ''DISPLAY 5µ1 2 3'' ''DISPLAY (,5)µ1 2 3''' °
'''Can you explain the following expression?'',r' °
'show ''(∞0)µ2 2µ∞4''' °
'''Clearly the right argument of reshape can be an array, but the''' °
'''elements are selected by rows. To understand why this is so, let us °
''' °
'''take a look at ravel. You will recall that ravel changes a scalar'' °
' '''to a vector of length 1, and leaves a vector unchanged. For an''' °
'''array, ravel will convert it into a vector exactly opposite to the' °
'' '''way reshape converts a vector into an array'',r' °
'show ''∞6'' ''2 3µ∞6'' '',2 3µ∞6''' °
'''The only other primitive non scalar function that we have studied'' °
' '''is the indexing function (squad)'',r' °
'show ''∞6'' ''3╙∞6'' ''(,3)╙∞6''' °
'''In the above example, the scalar 3 or one element vector ,3 extract °
s''' °
'''the third element of the right argument vector. When the right''' °
'''argument is an array, we need to specify as many directions in the' °
'' '''left argument, as the number of axes (rank) of the array'',r' °
'show ''2 3╙3 4µ∞12'' ''1 2 3╙3 3 3µ∞27''' °
'''This brings us to BRACKET INDEXING. In lesson 4 we learned that an' °
'' °
'''expression inside brackets can be used to access selected elements' °
'' °
'''of a vector. The only requirement was that the expression MUST retu °
rn''' °
'''integers in the range of the vector length. We also learned that''' °
'''NOTHING inside brackets produced the whole vector'',r' °
'show ''V╜∞3'' ''V[1 1 1 2 2 3]'' ''V[∞0]'' ''V[]''' °
'''Bracket indexing extends to arrays, but it requires additional''' °
'''convention in notation. Suppose we generate an array A'',r' °
'show ''A╜4 4µ∞16''' °
'''How should we specify the second row, second column? Clearly A[2 2] °
''' °
'''is inadequate because it is too limiting. (We would not be able to' °
'' °
'''extract more than one value at a time.) Since the array A has two'' °
' °
'''axes, namely rows and columns, it makes sense to be able to write'' °
' °
'''separate expressions for each axis. This is done by writing two''' °
'''expressions separated by a semicolon'',r' °
'show ''A[1 2;1 2]'' ''A[1;1 2 2 3 3 3 4 4 4 4]''' °
'''Let us now look at the shape of the results of bracket indexing of' °
'' '''arrays. Recall that 1 is a scalar, but ,1 is a vector'',r' °
'show ''µA[1;1 2 3]'' ''µA[,1;1 2 3]'' ''µA[2;3]''' °
'''When there is no expression in a slot reserved for an axis, this''' °
'''NOTHING is interpreted as if that axis were to be taken whole'',r' °
'show ''A[1 2;]'' ''A[;]''' °
'''The notation for higher order arrays is readily extended'',r' °
'show ''M╜2 3 4 5µ∞120'' ''M[1;2 3;1 4;5]'' ''M[1;2;;]''' °
'''We have therefore'',r' '1 RULE 15' 'do' °
'''Finally what if one or more expressions inside the brackets is a''' °
'''null vector?'',r' 'show ''µA[∞0;2 3]'' ''DISPLAY A[∞0;2 3]''' °
'''This gives us the following rule'',r' '1 RULE 16' °
'''Are we done with bracket notation and arrays? Not quite!''' 'do' °
'''Suppose that one or more of the axis specifications is not a scalar °
''' '''or a vector, but an array'',r' °
'show ''V[2 2µ∞3]'' ''A[1;2 2µ∞4]'' ''M[2;3;4;2 2µ∞4]''' °
X '''Therefore'',r' '1 RULE 17' 'do' '1 RULE 18' 'endd' °
*(1997 5 24 12 18 19 380) °
FTEACH5 ÉFX 'TEACH5' °
'Σ Reduction and Scan operators on arrays (continued)' °
'exit ''TEACH5''' °
'''We have seen how all primitive APL2 functions can be aplied to''' °
'''arrays. In the same manner the use of APL2 operators can be also''' °
'''extended to arrays.'',r' °
'''Before we try reduction to arrays, let us review how reduction work °
s''' '''on scalars and vectors.'',r' 'show ''⌡/8'' ''µ⌡/8'' ''µµ⌡/8''' °
'''The reduction of a scalar produces that scalar. In other words, as' °
'' '''far as scalars are concerned, reduction has no effect'',r' °
'show ''∞/''''A'''''' ''|/²3.14159''' °
'''The reduction of a vector ALWAYS produces a scalar'',r' °
'show ''+/∞5'' ''µµ+/∞5''' °
'''Finally, the reduction of a null vector produces the identity value °
''' '''of the function'',r' 'show ''⌡/∞0'' ''╛/∞0'' ''=/''''''''''' °
'''In the second example, we get the largest positive integer that APL °
2''' °
'''allows. Why? Because for arbitrary numeric arguments the minimum wi °
ll''' °
'''always be less than this value. The exception, of course, is when o °
nly''' °
'''null vectors are reduced. In some applications that possibility can °
''' '''arise, and APL2 must provide for.''' 'do' °
'''Now let us see how reduction works on arrays'',r' °
'show ''NUMBERS╜3 4µ∞12'' ''NUMBERS'' ''+/NUMBERS'' ''µ+/NUMBERS''' °
'''NUMBERS is an array with three rows and four columns. When this tab °
le''' °
'''is reduced with the + function, we see a vector with three componen °
ts.''' °
'''The entries are the row sums. Let us see how reduction works in arr °
ays''' '''of rank 3'',r' °
'show ''ARRAY╜3 4 5µ∞60'' ''µARRAY'' ''+/ARRAY'' ''µ+/ARRAY''' °
'''The first two dimensions are left unchanged while the third one is' °
'' '''reduced. Let us now try even higher rank arrays'',r' °
'show ''µ⌡/2 3 4 5µ1'' ''µδ/3 4 2 1 2 3 4µ0''' °
'''In each example it is the LAST dimension of the array that gets to °
be''' '''reduced'',r' '1 RULE 19' 'do' °
'''Why did we use the word "SIMPLE"? Because, as we will see shortly,' °
'' '''it is possible to select the axis to be reduced.''' 'do' °
'''Suppose that we desire to add the columns of array NUMBERS, instead °
''' °
'''of its rows. APL2 provides a special symbol that can be used to''' °
'''specify reduction of the first axis'',r' °
'show ''+≡NUMBERS'' ''µARRAY'' ''⌡≡ARRAY''' 'do' °
'''Since the first and last axis of a vector is the same, the ≡''' °
'''symbol produces the same effect as the /'',r' °
'show ''+≡∞5'' ''+/∞5''' '''For arrays of rank greater than two'',r' °
'show ''µ+≡2 3 4µ∞24'' ''µ⌡≡3 4 2 2 2µ∞2''' °
'''For most application this alternate form of the reduction operator' °
'' °
'''is adequate. However, if you ever have an application where you use °
''' '''arrays of rank greater than 2, you might have a problem'',r' °
'show ''µARRAY''' °
'''What do you do, if you want to reduce the middle axis of ARRAY, the °
''' °
'''one that has 4 rows? To handle this situation, APL2 supports a''' °
'''syntactic feature called AXIS SPECIFICATION. Whenever you want to d °
o''' °
'''an operation that should be applied to a specific axis of an array, °
''' °
'''you can insert the value of that axis in square brackets between th °
e''' '''function and the named array'',r' °
'show ''⌐/[2]ARRAY'' ''⌐/[1]ARRAY'' ''⌐/[3]ARRAY''' °
'''Notice that the second and third examples are equivalent to ≡ and / °
''' '''respectively.''' 'do' °
'''Earlier it was pointed out that reduction of an EMPTY vector return °
s''' °
'''the IDENTITY value of a function. The same is true for empty arrays °
'',r' °
'show ''DISPLAY +/0 2 3µ∞30'' ''DISPLAY ⌐/0 2 0µ∞30'' ''DISPLAY ╛/3 4 °
0µ0''' °
'''In the first example reduction over a non-null axis produced a null °
''' °
'''vector. The second example shows that a null array is left unchange °
d''' °
'''by reduction over a null axis. In the last example, reduction over °
a''' °
'''null axis produced the identity value for the non-null array.''' °
X 'endd' °
*(1997 8 28 12 22 15 408) °
FTEACH6 ÉFX 'TEACH6' 'Σ SCAN of arrays' 'exit ''TEACH6''' °
'''By now it should be obvious that SCAN when applied to arrays will'' °
' '''work as one would expect by generalizing on what we learned in''' °
'''reduction'',r' 'show ''3 4µ∞12'' ''⌡\3 4µ∞12'' ''⌡±3 4µ∞12''' °
'''Scan does not change the shape of the array, and as in vectors,''' °
'''the Nth element is the same as if the first N elements were reduced °
'',r' °
'show ''⌡\3 1µ1 5 9'' ''⌡\3 2µ1 2 5 6 9 10'' ''⌡\3 3µ1 2 3 5 6 7 9 10 °
11''' °
'''Of course scan of empty vectors leaves the shape and rank unchanged °
X'',r' 'show ''DISPLAY 3 0µ∞3'' ''DISPLAY ⌡±3 0µ∞3''' 'endd' °
XAV╜1 2 3 °
*(1997 7 13 12 28 49 504) °
Faddquote ÉFX 'u╜addquote w' °
'Σ Put quotes around a string, and double existing quotes' °
X 'u╜ÉAV[40],((1+w=ÉAV[40])/w),ÉAV[40]' °
*(1997 7 24 13 20 38 476) °
Fav ÉFX 'av;A;N;I;ÉIO' 'Σ Display characters in ÉAV' 'ÉIO╜0' °
'A╜22 78µ'' ''' 'N╜3 0«φ12 22µ1+∞356' 'A[;,(6⌡∞12)°.+2 3 4]╜N' °
'A[;6+6⌡∞12]╜φ12 22µÉAV' 'ΣA[8 10 13;6]╜'' ''' 'A[13;6]╜'' ''' °
X 'A[14+∞8;68 69 70 72]╜'' ''' 'A' °
*(1991 11 11 8 25 13 316) °
Fdate ÉFX 'u╜date w' 'Σ Format date and time of day' 'u╜«6╞w' °
X 'u╜('' ''⌠u)Γu' 'u╜εu,■''-- .. ''' °
XCdig 1 10 1234567890 °
*(1997 9 9 13 0 45 372) °
Fdisclaimer ÉFX 'disclaimer' 'Σ Copyright statement' °
'(10µ'' ''),''Copyright, Z. V. Jizba, 1995,1996,1997'',r' °
''' This and subsequent workspaces labelled TEACHxx are made available °
''' °
'''at no cost to anyone who desires to learn how to use effectively''' °
'''the IBM/OS2 version of APL2.'',r' °
'''This software is provided "AS IS" with no WARRANTY of any kind, eit °
her''' °
'''express or implied. Any risk in its use resides with you, the user °
of''' '''these tutorials.'',r' ''' ACKNOWLEDGEMENTS'',r' °
''' In writing these tutorials, I am greatly indebted to Roy Sykes, wh °
ose''' °
'''excellent lectures increased my understanding of the language.''' °
'''Discussions with the late Harry Bertucelli clarified a number of''' °
'''concepts and caused me to change some improper terminology that was °
''' °
'''used in previous versions of these tutorials. Mr. Benjamin Archer'' °
' °
'''kindly checked out a nearly final version, bringing to my attention °
''' '''some ommisions, misspellings, and invalid terminology.'',r' °
X '''(PRESS ENTER to continue)''' °
*(1997 7 13 12 28 50 508) °
Fdo ÉFX 'do;T;E' 'Σ Expression driver' 'E╜''''' 'æ╜'' ''' 'T╜æ' °
'╕(^/'' ''=T)/0' °
'╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/2' °
'''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/2' °
'''This is not a valid APL2 expression''' 'æ╜''*''' '╕(''?''⌠╞1╟æ)/2' °
X 'E' '╕2' °
*(1997 7 13 12 28 50 508) °
Fdoif ÉFX 'U╢╜V╢ doif W╢;t╢' 'Σ Rule' '╕(^/~U╢╜V╢)/0' °
X '''U╢╜V╢ doif■ W╢'' ÉEA ''»V╢/W╢''' °
*(1997 9 9 12 50 14 444) °
Fendd ÉFX 'endd' 'Σ end of special feature' '20µ''²'' ╪ ╕(4<µÉLC)/0' °
X 'do' °
*(1997 8 14 12 37 28 464) °
Ferase ÉFX °
'erase;t;EXIT;GO;HELP;DISPLAY;REVIEW;TABLES;RANK;RULE;DISCLAIMER' °
'Σ Erase all global functions and variables' 't╜ÉNL 3' °
't╜(~t^.εlc,'' '')≡t' 't╜ÉEX(~t[;∞5]^.=''TEACH'')≡t' 't╜ÉNL 2' °
X 't╜ÉEX(~t^.εlc,'' '')≡t' °
*(1997 7 27 13 47 41 608) °
Fevaldd ÉFX 'u╜evaldd w;c;n' 'Σ Evaluate direct definition' 'u╜0' °
'n╜(w∞''Σ'')-1' 'c╜(((n╞w)⌠'':'')Γn╞w),Γ''ΣDD '',(n+1)╟w' °
'╕((1 label╞c)doif ''''''Invalid label'''''')/0' °
'╕((2=µc)doif ''u╜showdd 1╙c'')/0' °
'╕((3=ÉNC╞c)doif ''u╜⌡µÉ╜(╞c),'''' is already defined.'''''')/0' °
'╕((3=µc)doif ''u╜simdd c'')/0' 'c╜(Γ''α∙ aw'')replace■c' °
'u╜ε''u╜'',((''a''εεc[2 3 4])/''a ''),(╞c),'' w;t;b''' °
'u╜u(5πc)(''b╜(t╜'',(3πc),'')/'',addquote ''u╜'',4πc)' °
X 'u╜u,''╕(t doif b)/0''(''u╜'',2πc)' 'u╜╧ÉFX u' °
*(1997 7 25 13 27 52 564) °
Fexit ÉFX 'V exit W;T' 'Σ Exit if too many suspended functions' °
'╕(0⌠ÉNC ''V'')/L0 ╪ V╜10' 'L0:╕(V>µÉLC)/0' °
'''There are too many suspended functions''' '''Please enter '',W' °
X '╕' °
*(1997 7 26 12 33 39 536) °
Fget ÉFX 'U╜V get W;t;T;ÉPR' 'Σ Prompt for response from keyboard' °
'ÉPR╜T╜ÉAV[ÉIO+255] ╪ ╕(0⌠ÉNC ''V'')/L0 ╪ V╜1' 'L0:V╜V╧1' 'æ╜W ╪ t╜æ' °
'U╜(+/^\t=T)╟t' '╕(''╕''⌠╞U)/L1 ╪ ╕' 'L1:╕V/0' 't╜(U⌠'' '')ΓU' °
X 'U╜(µt),(ΓU),t' °
*(1997 7 28 13 33 8 424) °
Fglobals ÉFX 'globals' 'Σ Initialize useful global variables' °
'uc╜''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' °
'lc╜''abcdefghijklmnopqrstuvwxyz''' 'dig╜''1234567890''' °
X 'r╜ÉAV[13+ÉIO]' 'q╜''''''''' °
*(1997 7 3 12 47 6 368) °
Finitialize ÉFX 'initialize;T' 'Σ Initialize workspace' °
'''AT ALL TIMES, TO CONTINUE, PRESS RETURN!'',r' °
'''To see disclaimers enter:'',r,'' disclaimer''' 'do' 'erase' °
'globals' °
'''Make sure the CAP LOCK light on your keyboard (upper right) is ON!' °
X'' 'endd' °
*(1997 7 27 13 14 33 444) °
Flabel ÉFX 'u╜v label w' °
'Σ Return 1 if label w does not begin with a cap' °
'╕(0⌠ÉNC ''v'')/L0 ╪ v╜0' 'L0:v╜v╧1 ╪ w╜εw ╪ ╕v/L1 ╪ ╕(u╜0⌠ÉNC w)/0' °
X 'L1:╕(u╜~^/wεlc,uc,dig)/0' 'u╜w[1]εlc,dig' °
XClc 1 26 abcdefghijklmnopqrstuvwxyz °
*(1997 7 13 12 28 55 528) °
Fnon ÉFX 'non;T;RC;ET;R' 'Σ Ignore keyboard entry' 'æ╜'' ''' 'T╜æ' °
'╕(0=µ(T⌠'' '')/T)/0' '(RC ET R)╜ÉEC T' '╕(0=RC)/2' °
X '╕((1=RC)doif ''R'')/2' '╕2' °
*(1997 7 13 12 28 55 528) °
Fnotb ÉFX 'u╜notb w' 'Σ Remove trailing blanks' °
'╕((1<╧w)doif ''u╜notb■ w'')/0' '╕((1<µµw)doif ''u╜πnotb Γ[2]w'')/0' °
X 'u╜(1-(,'' ''⌠Φw)∞1)╟w' °
*(1997 7 27 12 55 6 496) °
Fproblems ÉFX 'problems' 'Σ Problems' °
'''That is all for this lesson. Remember, if you want to practice,''' °
'''and plan to use direct definitions, be sure to first enter GO.''' °
'''Direct definitions will then be accepted. To exit GO, enter EXIT.'' °
,r' °
'''To erase a previously defined DIRECT DEFINITION FUNCTION, enter'',r °
' ''' )ERASE functionname'',r' °
X '''WARNING! do not use )ERASE on other labels.'',r' °
XCq 0 ' °
XCr 0 °
*(1997 7 13 12 28 56 532) °
Freplace ÉFX 'u╜v replace u;i;r;s' 'Σ Replace elements in v in u' °
'i╜Γ∞µu' 's╜2πv╜(v⌠'' '')Γv' 'i╜⌡r╜i⌡■Γ[1]u°.=╞v' °
X 'u[(εi)/εr]╜s[(εi)/εi⌡■∞µs]' °
*(1997 7 13 12 28 56 532) °
Fround ÉFX 'U╜V round W' 'Σ Half adjust to V th decimal' °
X 'U╜(╛0.5+W⌡10*V)÷10*V' °
*(1997 6 1 12 52 40 512) °
Frule ÉFX 'V rule W' 'Σ Additional remarks on rules' °
'╕(2=ÉNC ''V'')/L ╪ V╜0' 'L:╕(~(╞W)ε∞8)/LA ╪ V rulea╞W ╪ ╕L0' °
'LA:╕(~(╞W)ε9 10 11)/LB ╪ V ruleb╞W ╪ ╕L0' °
'LB:╕((11+∞8)=╞W)/L12,L13,L14,L15,L16,L17,L18,L19' °
'''This rule number is undefined'' ╪ ╕' 'L12:╕(V>1)/L12X' °
''' RULE 12. A scalar function is one that will produce a result on' °
'' °
''' every scalar element of a vector or array. EXAMPLES:''' °
''' + - ⌡ ÷ * are numeric scalar functions. , µ ∞ are NOT'' °
' ''' scalar functions.'',r' '╕(0=V)/L0' °
'L12X:''This distinction will become clearer when we study more genera °
lized''' '''arrays.''' '╕L0' 'L13:╕(V>1)/L13X' °
''' RULE 13. Scalars that work with vectors also work with arrays.'' °
,r' '╕(0=V)/L0' °
'L13X:''No problem with simple scalars, but this rule may need modific °
ation''' °
'''when we deal with "rank zero arrays" that are not simple scalars.'' °
' '╕L0' 'L14:╕(V>1)/L14X' °
''' RULE 14. Arrays with the same shape can be processed with scalar °
''' '''functions.'',r' '╕(0=V)/L0' °
'L14X:''Arrays with distinct shapes can be processed as we shall see, °
but''' °
'''there will be restrictions, and we will use new operators.''' °
'╕L0' 'L15:╕(V>1)/L15X' °
''' RULE 15. The number of semicolons inside brackets must equal the °
''' ''' rank of the array MINUS 1.'',r' '╕(0=V)/L0' °
'L15X:''There is no further generalization to this rule.''' '╕L0' °
'L16:╕(V>1)/L16X' °
''' RULE 16. An axis specification can be a scalar, a vector or a''' °
''' null vector.'',r' '╕(0=V)/L0' °
'L16X:''Recall that null array is the ONLY bracket specification that' °
'' °
'''makes sense for a scalar. We will use that property when dealing wi °
th''' '''rank 0 arrays.''' '╕L0' 'L17:╕(V>1)/L17X' °
''' RULE 17. An axis specification can be any array containing''' °
''' valid integers.'',r' '╕(0=V)/L0' °
'L17X:''With this rule, you can generate an APL2 object of arbitrary'' °
' '''rank.''' '╕L0' 'L18:╕(V>1)/L18X' °
''' RULE 18. The shape of the result of axis specification is the''' °
''' catenation of the shapes of expressions inside brackets °
.''' °
''' If the expression for an axis is missing, the shape for °
''' ''' that axis is unchanged.'',r' '╕(0=V)/L0' °
'L18X:''This is merely the rule for implementation of rule 17.''' °
'╕L0' 'L19:╕(V>1)/L19X' °
''' RULE 19. Simple reduction of arrays removes its last dimension.' °
',r' '╕(0=V)/L0' 'L19X:''This is a generalization of rule 2.''' °
X 'L0:do' '╕(0=µW╜1╟W)/0' '╕L' °
*(1997 6 1 12 42 53 524) °
Frulea ÉFX 'V rulea W' 'Σ Additional remarks on rules' °
'╕(2=ÉNC ''V'')/L ╪ V╜0' 'L:╕(W=∞8)/L1,L2,L3,L4,L5,L6,L7,L8' °
'''For rules not described in this lesson, see workspace INDEX''' °
'╕0' 'L1:╕(V>1)/L1X' °
'''RULE 1. For arithmetic operations a vector must be matched with a'' °
' ''' scalar, or another vector of the same length.'',2µr' °
'╕(0=V)/0' °
'L1X:''This is the first of many rules dealing with the concept of''' °
'''CONFORMABILITY. This means that elements to be processed by APL2''' °
'''must in some ways match.''' '╕0' 'L2:╕(V>1)/L1X' °
'''RULE 2. The reduction operator places the function, (specified to'' °
' °
''' its left), between all pairs of the vector. It then evaluat °
es''' ''' the resulting expression.'',2µr' '╕(0=V)/0' °
'L2X:''There is more to this, but at this stage the rule is essentiall °
y''' '''correct.''' '╕0' 'L3:╕(V>1)/L1X' °
'''RULE 3. All APL2 expressions are evaluated from right to left'',2µr °
' '╕(0=V)/0' °
'L3X:''This rule is valid in APL, which is the earlier version of APL2 °
.''' °
'''For APL2, the rule has been restated in a totally different way.''' °
'''However until we introduce some concepts that are new to APL2, the' °
'' '''newer form reduces to RULE 3 as stated.''' '╕0' 'L4:╕(V>1)/L1X' °
'''RULE 4. Vectors in APL2 can have lengths of zero and one.'',2µr' °
'╕(0=V)/0' °
'L4X:''This is the first concept that begins to introduce the idea tha °
t no''' °
'''matter what the APL expression, if it is valid, it must produce a'' °
' °
'''valid APL2 object. (Shades of which came first, the chicken or the °
egg)''' '╕0' 'L5:╕(V>1)/L1X' °
'''RULE 5. The shape of a variable is a vector. The shape of a vector' °
'' ''' is a vector of length 1. The shape of a scalar is a''' °
''' vector of length zero.'',2µr' '╕(0=V)/0' °
'L5X:''The left argument of RESHAPE is by definition a vector. If APL2 °
is''' °
'''to be internally consistent, the inverse operation to RESHAPE (whic °
h''' '''is indeed SHAPE) MUST return the same type of object.''' '╕0' °
'L6:╕(V>1)/L1X' °
'''RULE 6. Reduction of a scalar returns that scalar REGARDLESS of''' °
''' the function to the left of the reduction operator.'',2µr' °
'╕(0=V)/0' °
'L6X:''Reduction BY DEFINITION reduces the RANK by one. The exception °
is''' °
'''for objects whose rank is zero. Since there is no such thing as''' °
'''negative rank, reduction is undefined for scalars, and therefore''' °
'''the result is to leave the APL2 object unchanged.''' '╕0' °
'L7:╕(V>1)/L1X' °
'''RULE 7. The reduction of a null vector produces the identity''' °
''' element of the function.'',2µr' '╕(0=V)/0' °
'L7X:''A null vector may be empty, but it still has rank equal to one. °
''' °
'''Therefore its reduction MUST be a scalar. Since there is no data''' °
'''in a null vector, the scalar must be such that it has no effect''' °
'''on the operation performed by the function: Zero added to anything' °
'' °
'''has no effect in addition; one multiplied by anything has no effect °
''' '''in multiplication; and so on.''' '╕0' 'L8:╕(V>1)/L1X' °
'''RULE 8. Parentheses can (and should) be used to modify the right''' °
''' to left rule to improve efficiency'',2µr' '╕(0=V)/0' °
'L8X:''Parentheses are used in APL2 for other purposes as well, but fo °
r''' °
'''now, we will use them just to modify the right to left rule (RULE 3 °
).''' °
'''If you place parentheses where they are not needed, APL2 will ignor °
Xe''' '''them.''' °
*(1997 6 1 12 49 37 488) °
Fruleb ÉFX 'V ruleb W' 'Σ Additional remarks on rules' °
'╕(2=ÉNC ''V'')/L ╪ V╜0' 'L:╕(9 10 11=╞W)/L9,L10,L11 ╪ ╕0' °
'L9:╕(V>1)/L9X' °
''' RULE 9: To produce a quote inside a character string, enter''' °
''' the quote twice.'',2µr' '╕(0=V)/0' °
'L9X:''This is used in most other programming languages.''' '╕0' °
'L10:╕(V>1)/L10X' °
''' RULE 10: In replication the left argument must contain only''' °
''' integers. The length of the left argument must be''' °
''' the same as the length of the right argument.'',2µr' °
'╕(0=V)/0' °
'L10X:''The left argument may also have negative integers. Only the nu °
mber''' °
'''of positive integers and zeros must add up to the length of the rig °
ht''' °
'''argument. Replicate with negative integers is not discussed here be °
cause''' °
'''the explanation requires concepts that have not yet been mentioned. °
'',r' 'show ''1 1 2 ²1 0 1 1 1 ²1 1 0 1 1/''''CALIFORNIAN''''''' '╕0' °
'L11:╕(V>1)/L11X' °
''' RULE 11: The expression inside square brackets must return''' °
''' positive integers in the range of the length of''' °
''' the indexed vector. If the expression is MISSING,''' °
''' the result is as if the brackets were missing.'',2µr' °
'╕(0=V)/0' °
'L11X:''The expression in brackets need not be a vector. It could be a °
n''' '''array as in the next example'',r' °
X 'show ''''''CALIFORNIA''''[3 3µ?9µ10]'' ''(∞4)[3 3µ∞4]''' °
*(1997 7 13 12 28 57 536) °
Fshow ÉFX '╢V show ╢W;╢T;╢B' 'Σ Display and execute ╢W' °
'╢T╜(0=ÉNC ''╢V'')doif ''╢V╜0''' °
'╕((0=╧╢W)doif ''show ╢W,'''' '''''')/0' °
'╕((1<╧╢W)doif ''╢V show ■╢W'')/0' ''' '',╢W' °
X '╕((╢V^'':''ε╢W)doif ''╢T╜evaldd ╢W'')/L0' '''ÉEM'' ÉEA ╢W' 'L0:do' °
*(1997 7 13 12 28 57 536) °
Fshowdd ÉFX 'u╜showdd w;a;b;c;r' °
'Σ Display a direct definition function' °
'╕((1=╧w)doif ''u╜showdd Γw'')/u╜0' °
'╕((3⌠ÉNC╞w)doif ''(ε╞w),'''' is not a function'''''')/0' °
'c╜Γ[2]ÉCR╞w' 'c╜notb(2╞c),(Γ''aw α∙'')replace■2╟c' °
'╕((~''ΣDD''╧3╞2πc)doif ''''''Not a direct definition function'''''')/ °
0' 'u╜1' 'b╜('' ''⌠╞c)Γ╞c' 'a╜'' ''' 'r╜2╟3πc' °
'╕((3=µc)doif ''a,(╞w),'''':'''',r,(3<µ2πc)/'''' Σ'''',3╟2πc'')/0' °
'a╜a,(╞w),'':'',(2╟5πc),'':''' 'b╜(+\r=''('')-+\r='')''' 'b╜b∞0' °
X 'a╜a,(²3╟(b-1)╞3╟r),'':'',2╟»(b+2)╟r' 'a,(3<µ2πc)/'' Σ'',3╟2πc' °
*(1997 7 13 12 28 57 536) °
Fshowfn ÉFX 'U╜V showfn W;F;N;T;ÉIO' 'Σ Simulate ╖W[É]' °
'T╜(0=ÉNC ''V'')doif ''V╜0''' 'ÉIO╜0' °
'U╜r,'' '',''╖'',W,''[É]'',(╞V)╞''╖''' 'N╜1╞µF╜ÉCR W' 'N╜«∞N' °
'N╜(N⌠'' '')ΓN' 'F╜(π''['',■N,■Γ''] ''),F' °
'T╜(1<µ,V)doif ''F╜F[1╟V;]'' ''U╜''''''''''' 'U╜²1╟U,r,,F,r' °
X 'U╜((-+/^\'' ''=ΦU)╟U),('' ╖'')[╞V],r' °
*(1997 7 13 12 28 58 540) °
Fsimdd ÉFX 'u╜simdd w;e' 'Σ Direct definition mode' 'u╜0' °
'╕((0⌠ÉNC╞w)doif ''''''Already defined'''''')/0' 'e╜''α''ε2πw' °
'w[2]╜Γ''u╜'',''α∙ aw'' replace 2πw' 'w╜w[1 3 2]' °
X 'w[1]╜Γε''u╜'',(e/''a ''),w[1],'' w''' 'u╜╧ÉFX w' °
*(1992 6 3 9 59 17 424) °
Ftab ÉFX 'U╜V tab W;T;A;B;C;D;E;F;G;M;ÉPW' 'Σ Tabulate list W' °
'T╜(0=ÉNC ''V'')doif ''V╜0''' 'M╜''Invalid data for tabulation''' °
'V╜4╞V' 'ÉPW╜130╛30⌐G╜V[2]+79⌡V[2]=0' °
'L1:╕((1<╧W)doif ''''''W╜∞0'''' ÉEA ''''W╜πW'''''')/L1' °
'╕(((0=µεW)δ2<µµW)doif ''U╜(~V╧4╞0)/M'')/0' °
'T╜(1≥µµU╜«W)doif ''U╜πW╜(U⌠'''' '''')ΓU''' °
'T╜(0<V[1])doif ''U╜(«(Φ1,╞µW)µ(V[3]µ'''' ''''),∞(╞µW)-V[3]),'''' '''' °
,U''' '╕(G<30)/0' 'T╜(F╜µεV[4])+C╜1╟B╜µA╜(V[3],0)╟U' °
'T╜⌐(1╞B)÷1⌐╛(ÉPW+F)÷T' 'U╜(E╜(V[3],C)╞U),[1](B╜T,1╟B)╞A' °
'''D╜εV[4]'' ÉEA ''D╜ÉAV[εV[4]+33⌡V[4]=0]''' 'L0:A╜(T,0)╟A' °
X '╕(0=1╞µA)/0' 'U╜U,(((T+V[3]),µD)µD),E,[1]B╞A' '╕L0' °
*(1997 7 13 12 28 59 544) °
Ftest ÉFX 'U╜V test W;P' °
'Σ Describe problem in W, (correct answer in V)' 'U╜2' 'L1:W' °
'É╜'' ''' 'P╜æ' '''╕L0'' ÉEA ''P╜»P''' '╕(V╧P)/0' °
X 'L0:╕(0=U╜U-1)/0' '''Incorrect. Try again''' '╕L1' °
XCuc 1 26 ABCDEFGHIJKLMNOPQRSTUVWXYZ °
*(1996 4 6 16 2 32 304) °
Fvec ÉFX 'U╜vec' 'Σ Draw 2d vector in plane' °
'U╜r,r,''2- +'',r '': °'',r,''1- °'',r' °
X 'U╜U,'' : °'',r,''0|....|....|'',r,'' 0 1 2'',r' °