home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
teachapl.zip
/
TEACH.ZIP
/
TEACH22A.ATF
< prev
next >
Wrap
Text File
|
1997-09-19
|
53KB
|
645 lines
XNÉIO 0 1 °
XNÉCT 0 1E²13 °
XCÉFC 1 6 .,*0_² °
XNÉRL 0 530511967 °
XCÉPR 1 1 °
XCÉLX 1 5 TEACH °
*(1996 4 6 16 11 29 328) °
FBIND ÉFX 'BIND' 'Σ Binding rules of APL2' °
''' 1. Brackets bind to the left''' °
''' 2. Left arrow binds to the left''' °
''' 3. Dyadic operator binds to the right''' °
''' 4. Strand notation binding''' °
''' 5. Operator binds to its left''' °
''' 6. Function binds to its left''' °
''' 7. Function binds to its right''' °
X ''' 8. Left arrow binds to its right'',r' °
*(1996 4 6 16 11 56 436) °
XFDEGREES ÉFX 'U╜DEGREES W' 'U╜180⌡W÷Ω1' °
*(1997 9 14 12 30 44 504) °
XFDISCLAIMER ÉFX 'DISCLAIMER' 'Σ Copyright statement' 'disclaimer' °
*(1996 4 6 16 11 56 436) °
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 11 56 436) °
FEXIT ÉFX 'EXIT' 'Σ Exit from function' '''To log off type: )OFF''' °
X '╕' °
*(1996 4 6 16 11 56 436) °
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' °
*(1996 4 6 16 11 56 436) °
FGRAD ÉFX 'U╜GRAD A0;AI;SS0;SS1' °
'Σ Gradient method of function minimization (R and X are global)' °
'SS0╜R SS A0 FUN X' 'AI╜3 3µA0' 'U╜SS0-R SS(AI+0.01⌡AI⌡ID 3)FUN X' °
X 'U╜A0 MIN U÷A0' °
*(1996 4 6 16 11 56 436) °
FGRADIENT ÉFX 'GRADIENT;D' 'Σ Explanation of gradient minimization' °
'''The basic idea is to find a direction of search, such that the''' °
'''value of SS will decrease the fastest''' '0 2 showfn ''GRAD''' °
'''In line [2] of function GRAD, we first compute the sum of squares'' °
' °
'''associated with A0. Next we want to find the rate of change (slope) °
''' °
'''of function SS(A) in each of the components (directions) of A0. To' °
'' '''do that we prepare a square array of vectors A0''' °
'0 3 showfn ''GRAD''' 'do' °
'''The array AI+0.2⌡AI⌡ID 3 adds to each vector of AI a small incremen °
t''' °
'''in each of its components. Finding the difference between SS0 and t °
he''' °
'''sum of squares of each of these incremental vectors gives the rate °
of''' '''change in each of the component directions''' °
'0 4 showfn ''GRAD''' °
'''Finally to get the direction of descent, we divide vector U by A0.' °
'' '''This vector is then used as right argument to function MIN''' °
'0 5 showfn ''GRAD''' 'do' '''MINIMIZING SS GIVEN A DIRECTION'',r' °
'''By its nature a sum of squares of deviations from an mean value''' °
'''would be expected to behave in a way similar to the function Y=X*2' °
'' °
'''which is a parabola. The estimate of minimum value of function SS(A °
)''' °
'''can be made based on that assumption. That is the objective of''' °
'''function MIN.''' 'do' '1 showfn ''MIN''' °
'''The left argument to MIN is the current value of the constant, and' °
'' °
'''the right argument is the direction of search. Let us take a closer °
''' '''look at the algorithm of MIN''' 'do' '0 2 showfn ''MIN''' °
'''Since SS0 is required here, it is calculated, but only if not known °
''' '''(GRAD may not be the only place where MIN will be applied.)''' °
'0 3 showfn ''MIN''' °
'''Since we cannot assume that SS(V+W) will be smaller than SS(V),''' °
'''the variable T (in units of W) is initialized to ²1 and +1''' 'do' °
'0 4 showfn ''MIN''' °
'''To insure that only small increments of W (relative to V) are appli °
ed,''' °
'''the length of W is normalized to be 1÷100 of the length of V'',r' °
'0 5 showfn ''MIN''' °
'''Line [5] calculates SS for offsets at ²1 0 and +1''' 'do' °
'''Before we look at the remaining code, let us review some simple''' °
'''algebra. We postulate a quadratic function of the form'',r' °
''' Y ╜ a + bX + cXX'',r' °
'''The minimum (or maximum) can be found by taking derivative and by'' °
' '''setting Y'''' to zero'',r' 'do' °
''' 0= b + 2cX(min) or X(min) = -b÷2c'',r' °
'''Since we know this function for X= ²1 0 1, the problem of finding'' °
' '''X(min) is reduced to the solution of two equations'',r' °
''' 1╙SS2 ╜ 2╙SS2 - b + c'',r' °
''' 3╙SS2 ╜ 2╙SS2 + b + c'',r' °
'''From these we find that the value of X(min) is given by'',r' °
''' (-/SS2[1 3])÷D╜(+/SS2[1 3])-2⌡SS2[2]''' 'do' °
'''If D>0 then we are dealing with a maximum. In that case we merely'' °
' °
'''set X(min) to an offset. Lines [6 7] of MIN deal with these cases'' °
,r' '0 6 7 showfn ''MIN''' 'do' '0 8 showfn ''MIN''' °
'''Since we cannot be sure that SS(A) will actually behave as a parabo °
la''' °
'''we merely add the value of X(min) for which SS(A) should be smalles °
t''' '''to T, and compute the associated value of SS'',r' °
'0 9 showfn ''MIN''' °
'''Finally we select the constants associated with the smallest value' °
X'' '''of SS1''' 'do' 'endd' °
*(1997 8 21 13 29 57 580) °
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╜5),'' 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' °
'''BIND List the binding rules''' °
'''GRADIENT To discuss function minimization using a gradient''' °
X 'endd' °
*(1996 4 6 16 11 56 436) °
XFID ÉFX 'u╜ID w' 'ΣDD' 'u╜(2µw)µ1,wµ0' °
*(1996 4 6 16 11 56 436) °
FMIN ÉFX 'U╜V MIN W;SS1;T' °
'Σ Find minimum given direction W and constants V (global R, X and SS0 °
)' 'T╜(0=ÉNC ''SS0'')doif ''SS0╜R SS V FUN X''' 'T╜²1 0 1' °
'W╜0.01⌡W⌡((+/V*2)÷+/W*2)*0.5' °
'SS1╜²1ΦSS0,R SS((2 3µV)+1 ²1°.⌡W)FUN X' °
'╕((0≥D╜+/SS1[1 3]-2⌡SS1[2])doif ''T╜T,2⌡╞(SS1=╛/SS1)/²1 0 1'')/L0' °
'T╜T,(-/SS1[1 3])÷D' 'L0:SS1╜SS1,R SS(V+T[4])FUN X' °
X 'U╜V+W⌡(SS1=╛/SS1)/T' °
*(1996 4 6 16 11 56 436) °
XFRADIANS ÉFX 'U╜RADIANS W' 'U╜ΩW÷180' °
*(1997 7 24 13 6 36 412) °
FTEACH ÉFX 'TEACH' 'Σ Start lesson #22: APL2 by Zdenek V JIZBA' °
'exit ''TEACH''' 'initialize' 'TEACH1 Σ Matrix divide' °
'TEACH2 Σ The grocery problem revisited' °
'TEACH3 Σ Fitting observed data to a polynomial' °
'TEACH4 Σ Fitting obseved data to other equations' °
X 'TEACH5 Σ Function minimization' 'problems' °
*(1997 7 23 13 48 46 616) °
FTEACH1 ÉFX 'TEACH1;ROWS;COLS;BILL;BUY;PRICE;COST;INB;GRNO' °
'Σ Solving linear equations with dyadic domino' °
''' MATRIX DIVIDE'',r' 'BUY╜3 3µ2 1 1 1 2 2 3 2 1' °
'PRICE╜0.3 0.4 0.5' 'COST╜BUY+.⌡PRICE' °
'''In the previous lesson we learned about matrices, geometrical vecto °
rs''' °
'''(as contrasted with APL2 vectors, -- a more general concept), and w °
e''' °
'''studied the APL2 primitive function called MATRIX INVERSE.'',r' °
'''As an example of the use of matrix inverse we solved the grocery''' °
'''price of fruit problem: Given the purchase of fruit by three''' °
'''customers, find the unit cost of each fruit. We defined a matrix''' °
'''BUY and two vectors COST and PRICE'',r' °
'show ''COST BUY PRICE''' °
'''In this problem, PRICE is the unknown'',r' °
'ROWS╜πΓ■''CUSTOMER 1'' ''CUSTOMER 2'' ''CUSTOMER 3''' °
'COLS╜'''' ''APPLES'' ''ORANGES'' ''BANANAS'' '' COST''' °
'BILL╜3 1µΓ[2]3 6µ(ÉAV[33 37],''0.59'')«COST' 'COLS,[1]ROWS,BUY,BILL' °
'do' °
'''To solve this problem, we first found the inverse of matrix BUY, th °
en''' '''we multiplied this inverse by COST'',r' °
'show ''INB╜ÆBUY'' ''INB+.⌡COST''' °
'''In the dyadic version of the DOMINO primitive, this procedure can b °
e''' '''accomplished in a single step'',r' 'show ''COSTÆBUY''' °
'''This dyadic function is called MATRIX DIVIDE, since it is a''' °
'''generalization of division into the matrix domain. The right argume °
nt''' °
'''to matrix divide should be a matrix. The left argument can be a vec °
tor''' °
'''or a matrix. Suppose that in the grocery problem, we compare the co °
sts''' '''of the same items from two or more groceries'',r' °
'show ''É╜GRNO╜3 3µ1.5 2.1 2.2 1.3 1.85 1.9 1.5 2.1 2.25'' ''(φGRNO)ÆB °
UY''' °
'''The columns of the result return the unit costs of each item.'',r' °
'BILL╜(φ3 3µ1.5 2.1 2.2 1.3 1.85 1.9 1.5 2.1 2.25)ÆBUY' °
'('''' ''STORE 1'' ''STORE 2'' ''STORE 3''),[1](3 1µ1╟COLS),BILL' °
'do' °
'''Although matrix BUY is a square one, this is not a requirement, as' °
'' °
'''we will see shortly. For matrix divide to work, however there are'' °
' '''some limitations:'',r' ''' A. Right argument'',r' °
''' 1. If it is a vector, there is no matrix divide. The result °
is''' °
''' related to the monadic version and the following identity °
''' ''' holds:'',r' °
''' V1+.⌡ÆV2 ╜ ╕ V1ÆV2 or''' °
''' (φM)+.⌡ÆV ╜ ╕ MÆV2'',r' °
''' 2. If the right argument is a matrix, the number of columns' °
'' °
''' must not exceed the number of rows. Also no column may be °
''' °
''' the same as the matrix product of the remaining columns.' °
'' ''' This requirement is called LINEAR INDEPENDENCE.''' °
'do' ''' B. Left argument'',r' °
''' 1. If it is a vector, its length must match the number of''' °
''' rows in the right argument.'',r' °
''' 2. If the left argument is an array, the number of rows''' °
''' must be the same as the number of rows of the right''' °
''' argument.'',r' °
''' C. If both arguments are simple scalars, Æ is the same as ÷''' °
'do' °
'''Next let us describe the meaning of XÆY when Y is not a square''' °
X '''matrix.''' 'endd' °
*(1997 7 23 13 54 59 692) °
FTEACH2 ÉFX 'TEACH2;D;BUY;PRICE;COST' 'Σ Least squares' 'D╜ÉAV[37]' °
''' FITTING BY LEAST SQUARES'',r' °
'BUY╜4 3µ2 1 1 1 2 2 3 2 1 2 2 2' 'PRICE╜0.3 0.4 0.5' °
'COST╜BUY+.⌡PRICE' °
'''Suppose that we add a fourth customer to our list. This one bought' °
'' '''two apples, two oranges, and two bananas, paying $2.40'',r' °
'show ''BUY'' ''COST''' °
'''We can still use the same expression to determine the PRICE of each °
''' '''unit'',r' 'show ''COSTÆBUY''' °
'''Adding an extra customer really did not do anything. The new data'' °
' °
'''is redundant. But suppose that the price of the fruit is not on a'' °
' '''unit basis, but rather on a per/pound basis. If the fourth''' °
'''customer selected somewhat smaller than average fruit, the cost''' °
'''may have been $2.35 instead of $2.40. Will the same expression''' °
'''work, and if yes, what does it mean?'',r' °
'show ''COST[4]╜2.35'' ''8 2«.005+COSTÆBUY''' °
'''(Why did we insert the expression ''''8 2«.005+''''?)''' 'do' °
'''The numbers we now get represent the AVERAGE cost of each''' °
'''fruit. To explain precisely the underlying mathematics of the''' °
'''calculation would take us too far into a subject called''' °
'''MATHEMATICAL STATISTICS. We can however explain the basic idea.''' °
'do' °
'''Let us assume that the grocery store would like to receive on the'' °
' °
'''average the amounts of 0.30 0.40 and 0.50 each for one apple, one'' °
' °
'''orange and one banana. To do that, they find the average weight of' °
'' °
'''these fruit, and price the per/pound accordingly. When we select''' °
'''specific fruit, we find that the deviation from the average can be' °
'' '''positive or negative'',r' 'show ''PRICE-COSTÆBUY''' °
'''We would like to treat the negative numbers the same as the positiv °
e''' °
'''numbers. There are several ways to do that, but as Karl Gauss showe °
d''' °
'''in 1795, the best way is to square these differences and add them u °
p'',r' 'show ''+/(PRICE-COSTÆBUY)*2''' °
'''This quantity, called the SUM OF SUARES (often abbreviated SS)''' °
'''should be minimized for a best estimate. This is indeed what''' °
'''the expression COSTÆBUY actually does!''' 'do' °
'''The method of LEAST SQUARES is a very general one. It is the''' °
'''foundation of a great deal of applied statistics. We will not''' °
'''deal with this extensive subject here, except to point out that''' °
'''APL2 is an ideal language for solving problems of a statistical''' °
'''nature. We will give more examples including some techniques for''' °
X '''the solution of very difficult problems.''' 'endd' °
*(1997 7 24 12 47 9 464) °
FTEACH3 ÉFX 'TEACH3;A;X;Y;A1;YOBS1;YOBS2;SS1N;SS1R;YTH' °
'Σ Fitting curves' '''FITTING CURVES'',r' °
'''Let us suppose that a scientist has reason to believe that every''' °
'''time a value is set for variable X, the value for variable Y''' °
'''should be determined by a polynomial of the form'',r' °
''' Y = A + (B⌡X) + (C⌡XX)'',r' °
'''(As before we write XX to represent X*2)'',r' °
'''Furthermore, let us suppose that unknown to the scientist, the''' °
'''value of constants A, B and C is 4 2 ²0.3''' 'do' °
'''The polynomial equation can be written as an APL2 expression'',r' °
''' A[1]+X⌡A[2]+X⌡A[3] where'',r' °
'''Why is the expression "a+X⌡b+X⌡c" equivalent to "a+(bX)+(cXX)"?'',r °
' 'show ''A╜4 2 ².3''' °
'''The belief that there is a specific relation between X and Y is cal °
led''' °
'''a HYPOTHESIS. To demonstrate the validity of the hypothesis, the''' °
'''scientist must do the following:'',r' °
''' 1. Take a number of measurements of X and Y''' °
''' 2. Find the "best" constants A, B and C''' °
''' 3. Show that the polynomial is "a good fit".''' 'do' °
'''Since the values of X can be selected before an experiment, let us' °
'' °
'''assume that this choice is without error. (If we cannot select X''' °
'''without error, the theory gets much more complex.)'',r' °
'show ''X╜∞10''' °
'''We can now calculate the values of Y that would be predicted by''' °
'''theory given vector A'',r' 'show ''Y╜A[1]+X⌡A[2]+X⌡A[3]'' ''Y''' °
'do' °
'''We can verify that these values are correct, by recovering A from'' °
' '''X, Y and the equation'',r' 'show ''YÆ1,X°.*1 2''' °
'''In a typical experiment, the scientist obtains Y by making measurem °
ents.''' °
'''We can simulate this process by adding a random error to the values °
''' °
'''of vector Y. We will call the vector of observed Y''''s YOBS1'',r' °
'show ''YOBS1╜Y+.01⌡²10+?10µ19'' ''YOBS1''' °
'''The results can be used to estimate vector A. We will call this vec °
tor''' '''A1 because it is only an ESTIMATE of A'',r' °
'show ''É╜A1╜YOBS1Æ1,X°.*1 2''' °
'''If the errors of measurement are greater, our estimate of A will''' °
'''deviate more from the theoretical values'',r' °
'show ''YOBS2╜Y+.05⌡²10+?10µ19'' ''YOBS2''' °
'show ''YOBS2Æ1,X°.*1 2''' °
'''At this point we have taken two of the three steps: We have made''' °
'''the observations, and we have estimated the PARAMETERS (namely the' °
'' °
'''vector A). We still need to determine to what extent the hypothesis °
''' '''is correct. Here is a brief outline of that process:''' 'do' °
'''The basic idea is to show that the hypothesis explains the outcome' °
'' °
'''of the experiment better than no hypothesis, (also called the NULL' °
'' °
'''HYPOTHESIS). If we have no theory, then the SUM OF SQUARES of the'' °
' °
'''observations is defined as the SUM OF DEVIATIONS FROM THE AVERAGE.' °
'' '''We will call this quantity SS1N'',r' °
'show ''SS1N╜+/(YOBS1-(+/YOBS1)÷µYOBS1)*2''' °
'''On the other hand given the estimated values of vector A, we can''' °
'''also obtain the theoretical values of Y'',r' °
'show ''YTH╜(1,X°.*1 2)+.⌡A1''' °
'''and from these obtain the RESIDUAL SUM OF SQUARES'',r' °
'show ''SS1R╜+/(YOBS1-YTH)*2''' °
'''Comparing the values of SS1N and SS1R (and applying a number of''' °
'''special equations), it is possible to determine the LIKELIHOOD''' °
'''that the hypothesis is correct. For YOBS1 there is not much doubt'' °
' '''that the polynomial makes a good prediction'',r' °
'show ''SS1N'' ''SS1R''' °
'''(You might also try these calculations for the estimate YOBS2)''' °
X 'endd' °
*(1997 7 24 12 53 46 636) °
FTEACH4 ÉFX 'TEACH4;A;G;R;R1' 'Σ Fitting trigonomrtric functions' °
''' FITTING TRIGONOMETRIC FUNCTIONS'',r' °
'''Suppose now that instead of a polynomial, the equation that the''' °
'''scientist desires to fit involves trigonometric functions'',r' °
''' R = a + b(cosα) + c(cos2α)'',r' °
'''Here again we will set vector A to contain the values a,b and c, an °
d''' '''set'',r' 'show ''A╜1 2 3''' °
'''Then this equation can represented by the following expression'',r' °
''' R╜A[1]+(2ΩG°.⌡1 2)+.⌡A[2 3]'',r' °
'''where G is the vector of angles expressed in radians.''' 'do' °
'''In the theory of least squares, this is treated exactly the same''' °
'''as the problem of fruit in the grocery store. We merely treat the'' °
' °
'''cosines as unknown variables. You may think of them as if we made'' °
' '''the following transformations'',r' °
''' X╜cosα and Y╜cos2α.'',r' '''Given angles G'',r' °
'show ''G╜RADIANS 0,10⌡∞9''' '''We find the values for R'',r' °
'show ''É╜R╜A[1]+(2ΩG°.⌡1 2)+.⌡A[2 3]''' °
'''and given observations R1'',r' 'show ''R1╜R+.001⌡(?10µ19)-10''' °
'''we get an estimate of A, say A1'',r' 'show ''R1Æ1,2ΩG°.⌡1 2''' °
'''A more difficult problem arises if the theory leads us to the''' °
'''following equation'',r' ''' R ╜ a + cos(b+α) - cos(c+2α)'',r' °
'''The theory of least squares can still be applied, but now the''' °
'''equations no longer fall in the domain of linear algebra. The''' °
'''problem is still soluble but requires a different strategy.''' °
'''Often, tailor made algorithms have to be optimised for each''' °
X '''individual problem.''' 'endd' °
*(1997 7 24 13 2 10 292) °
FTEACH5 ÉFX 'TEACH5;A;A0;A1;D;R;X;FN;FUN;SS;LOOP' °
'Σ Function minimization' ''' FUNCTION MINIMIZATION'',r' °
'''There are general techniques for the solution of this type of''' °
'''problem, but these may not be efficient, and in many cases not''' °
'''even reach the true least squares solution. Just to illustrate''' °
'''how these techniques work, we will attempt to find the constants''' °
'''a, b and c in the following equation.'',r' °
''' R ╜ cos(a+x) + cos(b+2x) + cos(c+3x)'',r' °
'''We will use the method of gradients, also known as the method of''' °
'''STEEPEST DESCENT. We begin by defining vector A to contain the''' °
'''values of a, b and c'',r' 'show ''A╜1 2 3''' °
'''This time when translating the equation into an APL2 expression,''' °
'''we define a function that calculates R, given A and X. We also must °
''' °
'''define the function in such a way that it will calculate R for more °
''' '''than one set of A''''s.'',r' °
'show ''FN╜''''R╜A FUN X'''' ''''Σ cos(1╙A+X)+cos(2╙A+2⌡X)+cos(3╙A+3⌡X °
)''''''' °
'show ''FN╜FN,Γ''''╕((2=µµA)doif ''''''''R╜φπ(Γ[2]A) FUN■ ΓX'''''''')/ °
0''''''' 'show ''ÉFX FN,Γ''''R╜+/2ΩA+[2]X°.⌡∞3''''''' °
'1 showfn ''FUN''' 'do' °
'''Next, for convenience, we also define a function to calculate the'' °
' °
'''sum of squares with α╜R and ∙╜A FUN X where A may be an array'',r' °
'1 show ''SS:+≡((φ(Φµ∙)µα)-∙)*2''' °
'''To set up the problem, we define X, and observations R'',r' °
'show ''X╜RADIANS 0,10⌡∞9''' 'show ''R╜(A FUN X)+.001⌡(?10µ19)-10''' °
'''The basic idea for the general solution is to start with an initial °
''' °
'''estimate of A, find the associated SS, and then try to migrate A''' °
'''such that SS is constantly reduced. The expectation is that with''' °
'''enough iterations, the values of A will approach the least squares' °
'' '''solution.''' 'do' °
'''Let us begin with an initial value of A0'',r' °
'show ''A0╜1.5 1.5 2.5'' ''A-A0''' °
'''Function GRAD takes as the right argument this estimate, and return °
s''' '''the new value of A with reduced value of SS'',r' °
'1 showfn ''GRAD''' °
'''For more details on the gradient approach, type GRADIENT'',r' °
'show ''R SS A0 FUN X'' ''A1╜GRAD A0'' ''R SS A1 FUN X''' °
'''One iteration seldom reaches the minimum. We can define function''' °
'''LOOP to iterate more than once, and see the rate of convergence''' °
'''with the gradient technique'',r' °
'show ''FN╜''''LOOP W;S;SS0'''' ''''Σ minimization with gradient'''' ' °
'''S╜0''''''' °
'show ''FN╜FN,''''L0:A1╜GRAD A1'''' ''''²1╞S╜S,SS0╜R SS A1 FUN X'''''' °
' 'show ''ÉFX FN╜FN,''''╕(=/²2╞S)/0'''' ''''╕(0<W╜W-1)/L0''''''' °
'1 showfn ''LOOP''' 'do' °
'''Note that exit from LOOP ocurrs if the new value of S is unchanged' °
',r' 'show ''LOOP 10''' °
'''The result will not necessarily converge to the least squares''' °
'''solution, because it may get caught in what is known as a LOCAL''' °
'''MINIMUM. There are ways of avoiding such local minima. We can''' °
'''check whether the estimate A0 is approaching the true value of A'', °
r' 'show ''A'' ''A0'' ''A-A0'' ''A-A1''' °
'''In this example, we may not be in a local minimum, but in a valley. °
''' °
'''Think of a river gorge. The steepest slopes point down to the''' °
'''river, but the direction of global minimum is at right angles to''' °
'''this slope. We can ammend LOOP by adding an extra search in the''' °
'''direction A1-A0'',r' °
'show ''ÉFX (²1╟FN),''''A1╜A MIN A1-A0'''' ''''²1╞S,R SS A1 FUN X'''', °
²1╞FN''' '1 showfn ''LOOP''' 'do' '''And try again'',r' °
'show ''A1╜A0'' ''LOOP 10''' '''This time the search converges.''' °
X 'show ''A1'' ''A-A1''' 'endd' °
*(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]' °
*(1996 4 6 16 11 56 436) °
XFaq ÉFX 'U╜aq W' 'U╜Γaddquote W' °
*(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 21 13 30 22 444) °
Ferase ÉFX °
'erase;t;EXIT;GO;BIND;HELP;DISPLAY;ID;DEGREES;RADIANS;MIN;GRADIENT;GRA °
D;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' 't╜ÉNL 4' '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' °
*(1996 4 6 16 11 57 440) °
Fpause ÉFX 'V pause W;T' °
'Σ Pause, then print W V spaces right and return' °
X 'T╜(0=ÉNC ''V'')doif ''V╜6''' 'do' '(Vµ'' ''),W' 'do' °
*(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 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 °
*(1997 8 21 13 15 13 348) °
Fupdate ÉFX 'update' °
'''This version of APL2 lessons has been updated in the summer of 1997 °
''' °
'''Any subsequent updates will be announced to the APL news group.''' °
'''You may also e-mail me at jizba@kaiwan.com for additional details.' °
'' '''Any constructive comments will be welcome.'',r' °
'''For an update of these lessons, mail a self addressed stamped''' °
'''floppy disk mailer with two HD 3.5 inch blank floppies to:'',r' °
''' Zdenek V. Jizba''' °
''' 1341 Rebecca Dr.''' °
X ''' La Habra, CA 90631''' °
*(1996 4 6 16 11 57 440) °
Fvec ÉFX 'U╜V vec W;T' 'Σ Draw 2d vector in plane' °
'T╜(0=ÉNC ''V'')doif ''V╜''''''''''' °
'U╜r,'' '',W[3],r,''2- +'',W[2],r '': °'',r,''1- °'',r' °
X 'U╜U,'' : °'',r,''0|....|....|.'',V,,'' '',W[1],r,'' 0 1 2'',r' °