home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
teach.zip
/
TEACH19A.ATF
< prev
next >
Wrap
Text File
|
1997-09-19
|
54KB
|
655 lines
XNÉIO 0 1 °
XNÉCT 0 1E²13 °
XCÉFC 1 6 .,*0_² °
XNÉRL 0 16807 °
XCÉPR 1 1 °
XCÉLX 1 5 TEACH °
*(1996 4 6 16 10 36 352) °
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' °
*(1997 9 14 12 30 44 504) °
XFDISCLAIMER ÉFX 'DISCLAIMER' 'Σ Copyright statement' 'disclaimer' °
*(1996 4 6 16 10 36 352) °
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 10 36 352) °
FEXIT ÉFX 'EXIT' 'Σ Exit from function' '''To log off type: )OFF''' °
X '╕' °
*(1996 4 6 16 10 36 352) °
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 19 11 38 4 388) °
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 cursor 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' °
'''REVIEW Review the previous lesson''' °
X '''V TIME W Measure execution time of W evaluated V times''' 'endd' °
*(1997 8 19 11 40 23 472) °
XFPASCAL ÉFX 'u╜PASCAL w' 'ΣDD' 'u╜(0,∞w)!w' °
XNRATES 1 6 0.04 0.05 0.06 0.07000000000000001 0.08 0.09 °
*(1997 7 18 13 38 26 476) °
FTEACH ÉFX 'TEACH' 'Σ Start lesson #19: APL2 by Zdenek V JIZBA' °
'exit ''TEACH''' 'initialize' °
'TEACH1 Σ Inconsistencies in mathematical notation' °
'TEACH2 Σ Simple and compound interest' °
'TEACH3 Σ Abacus, multiplication, division and logarithms' °
'TEACH4 Σ Calculating compound interest rate' °
'TEACH5 Σ Pascal''s triangle' 'TEACH6 Σ Binomial coefficients' °
X 'problems' °
*(1997 7 18 13 17 58 520) °
FTEACH1 ÉFX 'TEACH1' 'Σ APL2 and mathematics' °
''' APL2 AND MATHEMATICS'',r' °
'''In previous lessons we learned how to use APL2 to solve a variety o °
f''' °
'''fairly simple problems. Some of the problems were of a mathematical °
''' '''nature, but many were not. Ken Iverson, a mathematician, was''' °
'''primarily interested in establishing a discipline of mathematical'' °
' '''notation, when he invented the forerunner of APL2.'',r' °
'''In this and in the next three lessons we will learn more about the' °
'' °
'''mathematics of APL2. Even if you do not have extensive mathematical °
''' °
'''background, these lessons are well worth a careful study. We will t °
ry''' °
'''to present the material in as simple and straight forward way as''' °
'''possible. Even then, there may be some portions of the text that yo °
u''' °
'''might find difficult to follow. Please do not be discouraged. In th °
e''' °
'''long run, your efforts at understanding will eventually be rewarded °
.''' 'do' °
'''We begin by reviewing the power function that we studied in lesson °
3.''' °
'''Recall that the primitive power function is defined by an asterisk °
(*).''' °
'''Why should APL2 use a new symbol, one not taught in algebra?'',r' °
'''There are two answers to this question. The simple answer is that °
in''' °
'''the mathematics classroom, power is usually defined by placing the' °
'' °
'''"exponent" in the upper right hand corner of the number. Since''' °
'''most computer implementations do not support these "superscripts",' °
'' '''another solution had to be found.'',r' °
'''There is yet another, more fundamental reason. Most mathematical'' °
' °
'''notation has evolved without much regard to internal consistency.'' °
' °
'''Usually, when a mathematician first used a particular symbol to''' °
'''define a concept, that symbol tended to be accepted by the math''' °
'''community. Because of such historical accidents, some primitive''' °
'''functions are defined by special symbols to the right of the''' °
'''argument (a!), some to the left (~B), some on both sides (|A|),''' °
'''and some, such as power by the placement of one of the arguments.'' °
' 'do' °
'''When Ken Iverson was defining APL, he felt strongly that mathematic °
al''' °
'''notation should be consistent. If a function has only one argument °
,''' °
'''it''''s symbol should precede the argument. If a function has two' °
'' °
'''arguments, it''''s symbol should be placed between the arguments. °
Since''' °
'''the power function is not represented by a symbol, but rather is''' °
'''defined by position, he felt a symbol had to be invented.'',r' °
'show ''5*2''' °
'''The expression 5*2 is therefore equivalent to the more familiar''' °
'''notation we learn in school, where the 5 is followed by a smaller'' °
' °
'''superscript 2. To make things even more confusing, this is by no''' °
'''means a universally accepted notation. In some programming language °
s''' °
'''the * stands for the multiplication symbol. The power symbol may''' °
'''be represented by **, by ╞ or even by other symbols.''' 'do' °
'''(In this and subsequent lessons we may find it useful to fall back' °
'' '''to more "conventional" algebraic notation. However, lacking''' °
'''superscripts, we may represent the power of X as an implicit produc °
t.''' °
'''For example XX will represent X square and XXX will stand for X*3.) °
X''' 'endd' °
*(1997 7 18 13 21 32 432) °
FTEACH2 ÉFX 'TEACH2' 'Σ Compound interest' 'a╜ÉAV[38]' °
'''SIMPLE AND COMPOUND INTEREST'',r' °
'''One of the most common applications of the power function in modern °
''' °
'''society is in the calculation of interest. Money lending is an''' °
'''occupation that has been around since ancient times. If person A''' °
'''needs money, person B will lend it, but after some period of time,' °
'' °
'''B will demand back not only the amount that was borrowed, but also' °
'' '''some extra quantity for the favor.'',r' °
'''The rate of change per unit time is called INTEREST, and is usually °
''' '''described as PERCENT. This word "percent" derives from latin''' °
'''meaning a change for every 100. The expression 5'',a,'' means that °
for''' °
'''every 100 units, also called the PRINCIPAL, there is a change of 5' °
'' °
'''units of INTEREST. The calculation can be simplified if we divide'' °
' °
'''the 5 by 100, and then use the value of 0.05 as the RATE of change. °
''' '''We can express this formula as an equation'',r' °
'pause ''(PRINCIPAL + INTEREST) = PRINCIPAL ⌡ (1 + RATE)''' °
'''In this way, if we invest 134 dollars at 5'',a,'' in SIMPLE interes °
t,''' '''then at the end of the year we will have'',r' °
'show ''134⌡1.05''' °
'''If we keep the amount at the same rate for two years, then we will' °
'' '''end up with'',r' 'show ''(134⌡1.05)⌡1.05''' °
'''Of course this is the same as'',r' 'show ''134⌡1.05*2''' °
'''We can always recover the amount of simple interest from this''' °
'''formula'',r' °
'pause ''(PRINCIPAL+INTEREST)=PRINCIPAL⌡(1+RATE)*TIME''' °
'''By dividing both sides by PRINCIPAL, and by taking the root 1÷TIME' °
'' '''we can recover the simple interest'',r' °
'show ''(147.735÷134)*÷2''' °
'''The next question one might ask, is how much will we have after one °
''' '''year and six months?''' 'do' 'show ''134⌡1.05*1.5''' °
'''With APL2 this calculation is done easily, but before we had such'' °
' °
'''powerful computers, it would have been quite difficult to calculate °
''' °
'''1.05 raised to the power 1.5. The computation would be even more''' °
'''difficult if we credited the interest every day. This process is ca °
lled''' °
'''COMPOUNDING. COMPOUND INTEREST is now a very common form of money'' °
' °
'''lending. If you put your money in a money market fund, the interest °
''' °
'''may be credited to you every day. Supposing a year has 356 days,''' °
'''then your daily interest will be'',r' 'show ''0.05÷356''' °
'''Therefore this calculation wil be done 356 times in a year'',r' °
'show ''134⌡(1+0.05÷356)*356''' °
'''By this compounding, instead of earning 140.7, you get 140.87.''' °
'''So compound interest gives you a somewhat greater return than the'' °
' °
'''NOMINAL interest rate. How can we find out what is the EFFECTIVE''' °
'''simple interest rate that would produce 140.87 in one year?'',r' °
'show ''140.8698323÷134''' °
'''Now you try to calculate the SIMPLE interest, given that after''' °
X '''356 days compounded daily, 134 dollars grew to 140.87!''' 'endd' °
*(1997 7 18 13 25 11 364) °
FTEACH3 ÉFX 'TEACH3' 'Σ Invention of logarithms' °
''' LOGARITHMS'',r' °
'''In ancient times knowhow about multiplication of large numbers''' °
'''was very rare. (Try to multiply the Roman CXVII times XXIV!)''' °
'''Even with the invention of the arabic numerals, the process is''' °
'''time consuming'',r' ''' 4279''' ''' ⌡5167''' °
''' ²²²²²²''' '0 1 2 3Φ13 0«4 1µ4279°.⌡7 6 1 5' °
''' ²²²²²²²²²²²''' '13 0«4279⌡5167' 'do' °
'''A long division such as 4279÷5167 would have been even more difficu °
lt.''' °
'''Actually the ancients were quite ingenious. They solved these probl °
ems''' °
'''at least partially by inventing a variety of mechanical devices. Ma °
ny''' °
'''ancient civilizations seemed to have been familiar with such device °
s.''' °
'''A form of ABACUS has been used by millenia, as attested by the word °
s''' °
'''in many languages describing this device: the Turkish COULBA, the'' °
' °
'''Armenian CHOREB, the Chinese SUAN-PAN, the Japanese SOROBAN, the''' °
'''Russian SCHOTY, and many others. Even the South American Incas used °
''' '''a like device with grains of corn (or wheat) as the beads.'',r' °
'''An early form of the ABACUS was also used by the Greeks and by the' °
'' °
'''Romans. It was a piece of slate (or even bronze) with grooves. The' °
'' °
'''beads, called CALCULI were used as counters. The word itself comes' °
'' '''from "calc" meaning a pebble of limestone. Today, when we use''' °
'''the word "calculation" (or calculus) we are no longer aware that''' °
'''these words date back to days when people used limestone pebbles''' °
'''to do their taxes.''' 'do' °
'''In 1642 Blaise Pascal invented a mechanical device composed of whee °
ls''' °
'''and rachets, that was capable of multiplying numbers. This was the' °
'' °
'''first known calculating machine, but its use did not catch on until °
''' '''centuries later. Instead, a number of mathematicians of the''' °
'''Renaisance became aware of the properties of certain mathematical'' °
' °
'''formulas. In Denmark for example, astronomers developed tables to'' °
' °
'''simplify multiplication. These tables were based on trigonometric'' °
' '''formulas already known to Archimedes.''' 'do' °
'''In Scotland, John Napier, a mathematician published in 1614 a table °
''' °
'''of logarithms. The basic idea for this table arose from the followi °
ng''' °
'''simple concept: Given the sequence 2 4 8 16 32 64 ... we can draw'' °
' '''two straight lines:'',r' ''' 2 4 8 16 32 64''' °
''' |-----|-----|-----|-----|-----|-- and'',r' °
''' 1 2 3 4 5 6''' °
''' |-----|-----|-----|-----|-----|--'',r' °
'''The top line represents the natural numbers, while the bottom one'' °
' °
'''represents the corresponding powers of two. He reasoned that there' °
'' °
'''should be a way to convert numbers to a form, where multiplication' °
'' '''can be substituted by addition, and division substituted by''' °
'''subtraction.''' 'do' °
'''This can be done with a function caled LOGARITHM. Before we proceed °
''' °
'''to describe how APL2 handles this function, let us go back to the'' °
' °
'''example of Compound interest. Suppose we desire to calculate the''' °
'''simple interest, given that after 5 years, the sum of 134 dollars'' °
X' '''has grown to say 215 dollars.''' 'endd' °
*(1997 7 18 13 31 19 420) °
FTEACH4 ÉFX 'TEACH4' 'Σ The log function' °
'''To answer that question, we look at the theoretical equation for''' °
'''interest rate'',r' °
'pause ''(PRINCIPAL+INTEREST)=PRINCIPAL⌡(1+RATE)*TIME''' °
'''To solve for rate, we transform this equation by taking logarithms' °
',r' °
'pause '' (log PRINCIPAL+INTEREST)=(log PRINCIPAL)+TIME⌡(log 1+RATE)'' °
' '''By subtracting (log PRINCIPAL) from both sides, we get'',r' °
'pause ''(log PRINCIPAL+INTEREST)-(log PRINCIPAL)=TIME⌡(log 1+RATE)''' °
'''And now the left hand side can be re-written as'',r' °
'pause ''(log (PRINCIPAL+INTEREST)÷PRINCIPAL)=TIME⌡(log 1+RATE)''' °
'''Now dividing both sides by TIME, we get'',r' °
'pause ''(log (PRINCIPAL+INTEREST)÷PRINCIPAL)÷TIME=log 1+RATE''' °
'''At this point we no longer need the logarithms, so we revert back'' °
' '''to its DUAL function (let us call it "exp").'',r' °
'pause ''(exp (log (PRINCIPAL+INTEREST)÷PRINCIPAL)÷TIME)=1+RATE''' °
'''Subtracting 1 from this result should give us the actual rate.''' °
'do' °
'''The last equation seems simple enough, but how do we calculate''' °
'''logarithms?''' 'do' °
'''The answer is very simple. APL2 supports a primitive function''' °
'''called logarithm. Its symbol is ╡, and it exists both as a''' °
'''monadic and dyadic function. The logarithm function is defined''' °
'''with respect to a BASE. In its dyadic form, the left argument''' °
'''specifies the base of the logarithm. In its monadic form, the base' °
'' °
'''is a default value, namely the number "e", also called the NATURAL' °
'' '''NUMBER (2.718281828)'',r' °
'show ''10╡10 100 1000'' ''2╡1 2 4 8 16'' ''╡2 10 16''' °
'''The log function is the dual of the power function. Recall that a'' °
' °
'''DUAL function is the inverse of the first function. This is shown'' °
' '''in the following examples'',r' °
'show ''10*10╡10 100 1000'' ''2*2╡1 2 4 8 16'' ''*╡2 10 16''' °
'''Before we had computers, it was common to use tables of logarithms' °
'' °
'''for extensive multiplication. Many such tables were available,''' °
'''including logarithms of various TRANSCENDENTAL functions. The most' °
'' °
'''popular base was 10. With APL2 we can still take advantage of the'' °
' °
'''base 10 dyadic log function, but it is much simpler to use the mona °
dic''' '''(or natural) base'',r' 'show ''*(╡215÷134)÷5''' °
'''This last example shows that if we invest 134 dollars, and five''' °
'''years later collect 215 dollars, the annual simple interest rate''' °
'''is 9.91'',a' 'do' '''Recalling the interest rate formula,'',r' °
''' (log (PRINCIPAL+INTEREST)÷PRINCIPAL)=TIME⌡(log 1+RATE)'',r' °
'''we can estimate the time it takes to double your investment'',r' °
'show ''É╜RATES╜.01⌡3+∞6'' ''TIMES╜(╡2)÷╡1+RATES''' °
'''There is the well known "rule of 72"'',r' °
X 'show ''╛(.5+100⌡TIMES⌡RATES)''' 'endd' °
*(1997 7 18 13 35 10 400) °
FTEACH5 ÉFX 'TEACH5' 'Σ Pascal triangle' °
''' PASCAL''''S TRIANGLE'',r' °
'''In the interest rate formula, we raised the quantity 1+RATE to a''' °
'''power. Let us now take another look at this process, but we will''' °
'''do it in a more abstract way. We will abbreviate the RATE to just R °
.''' °
'''In the calculation of interest after two years, we raise (1 + R) to °
''' '''the power 2'',r' ''' 1 + R1''' °
''' ⌡1 + R2''' ''' ²²²²²²²²²²²''' °
''' R2 + R1R2''' ''' 1 + R1''' ''' ²²²²²²²²²²²²²²²''' °
''' 1+(R1+R2)+R1R2''' 'do' °
'''We see that after two years, the PRINCIPAL + INTEREST is made up''' °
'''of three parts:''' ''' 1. 1 (representing the principal)''' °
''' 2. R1+R2 the (simple) interest for year one plus year two''' °
''' 3. R1R2 the (compounded) 2d year interest on the 1st year intere °
st''' 'do' °
'''If we now multiply this result again by (1 + R) to get the third''' °
'''year interest we get'',r' ''' 1 + R1 + R2 +R1R2''' °
''' ⌡ 1 + R3''' °
''' ²²²²²²²²²²²²²²²²²²²²²²''' °
''' R3 + R1R3 + R2R3 + R1R2R3''' ''' 1 + R1 + R2 + R1R2''' °
''' ²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²''' °
''' 1 + (R1+R2+R3) + (R1R2+R1R3+R2R3) + R1R2R3''' 'do' °
'''There is a pattern here. We can look at the coefficients (i.e. the' °
'' '''number of entries in parentheses):'',r' °
''' POWER COEFFICIENTS''' ''' (1 + R) 1 1''' °
''' (1 + R)*2 1 2 1''' ''' (1 + R)*3 1 3 3 1''' °
''' (1 + R)*4 1 4 6 4 1'',r' °
'''We can extend this table in a simple way: We start with a 1 in a''' °
'''new row. Then we add the number above (1) to the number to its''' °
'''left (4) and place this sum (5) to the right of of the one. In the' °
'' °
'''same way we add 4+6 for the next number. Proceeding in this way,''' °
'''we generate the next row in the table: 1 5 10 10 5 1'',r' °
'''This pattern is now called the PASCAL TRIANGLE. Although Pascal''' °
'''did not discover this pattern (it was known to several ancient''' °
'''civilizations), he did find the formula for the general term.''' °
'do' °
'''Before we describe this formula, we will look at a derivation''' °
'''of Pascal''''s triangle from yet another point of view. Then we wil °
l''' °
'''introduce a new primitive function (monadic FACTORIAL, and dyadic'' °
' '''BINOMIAL).''' 'do' ''' SETS AND SUBSETS'',r' °
'''A SET is by definition a collection of objects. For example, the''' °
'''digits 1, 2 and 3 form a set of three. We will use the following''' °
'''notation to describe a set (with apology to sticklers in math notat °
ion).''' °
'''The elements of a set will be enclosed by curly brackets. Here is a °
n''' '''example of the set we will call FOUR'',r' '4 sset 4' °
'r,''A SUBSET is a set of elements that also belong to the set. Two or °
''' '''more subsets will be separated by commas in our notation'',r' °
'1 sset 4' °
'r,''The line above shows four subsets of FOUR, each containing only'' °
' '''one element.''' 'do' °
'''We will include the EMPTY set as a subset of FOUR too'',r' °
'0 sset 5' 'r,''Let us now look at all of the subsets of FOUR'',r' °
'(0 sset 4),(1 sset 4),(2 sset 4),(3 sset 4),4 sset 4' 'do' °
'''If we count the number of elements in each collection of subsets''' °
'''we get 1 4 6 4 1, a line in Pascal''''s triangle. What is more, the °
''' °
'''total number of subsets of set FOUR is 16 (or 2*4). You can verify' °
'' °
'''that in general, a set of N elements has 2*N subsets, and the numbe °
r''' °
'''of sets with K elements are given by the (1+K)th column of the Nth' °
'' '''row of Pascal''''s triangle.'',r' °
'''The formula we are seeking describes the number of different ways'' °
X' '''of taking K objects out of a collection of N objects.''' 'endd' °
*(1997 7 19 12 4 56 460) °
FTEACH6 ÉFX 'TEACH6' 'Σ Factorial, Gamma and binomial' °
''' FACTORIAL, GAMMA AND BINOMIAL'',r' °
'''Since the formula we are seeking was first developed in the study o °
f''' °
'''expanding powers of binomials, it is called the equation for BINOMI °
AL''' °
'''coefficients. To define it, we first must define another function'' °
' °
'''called the FACTORIAL. This function is merely the product of the''' °
'''first N integers'',r' 'show ''⌡/∞5'' ''!5''' °
'''In mathematical notation, factorial N is represented by N!. In''' °
'''APL2 we merely place the ! in front of the value.''' 'do' °
'''The argument to the factorial is an integer. One could also ask''' °
'''whether the argument to ! could be a number not an integer'',r' °
'show ''!0.5''' °
'''Obviously the answer is yes. This function is called the GAMMA''' °
'''function. It has many applications, but for the time being, we''' °
'''will leave it alone. However, we should mention that this function' °
'' °
'''cannot be represented by finite polynomials. There are many such''' °
'''functions, as we will see later. They are called TRANSCENDENTAL''' °
'''functions. Gamma and the log functions are both transcendental.''' °
'do' °
'''Coming back to the factorial; how does that function come about?''' °
'''Consider the set FOUR, that we discussed before. One could ask, how °
''' °
'''many different ways are there to order the elements of this set?''' °
'''If you think about it, we have 4 choices to pick the first element, °
''' °
'''then there are only three choices to pick the second element, then' °
'' '''two choices, an finally the fourth element is determined'',r' °
'''1234 1324 1342 1423 1432 2134 2143 ... all together 24 possible''' °
'''patterns, or'',r' 'show ''!4''' °
'''We can now derive the formula for the binomial coefficient. Given'' °
' '''the !N possible ways to rearrange N elements, if we are only''' °
'''interested in K of then, we do not care what the sequence is on''' °
'''the (N-K) remaining ones. Therefore we can divide (!N) by (!N-K).'' °
' °
'''Also, we do not care about the ordering, 12 is the same as 21, and' °
'' °
'''so on, so we can also divide the remaining patterns by !K. Therefor °
e''' °
'''the binomial formula for the number of ways to take K distinct''' °
'''subsets out of a collection of N objects is'',r' °
''' (!N)÷(!K)⌡!(N-K)'',r' °
'''The dyadic ! gives us this same result for, say K=3 N=8'',r' °
'show ''(!8)÷(!5)⌡!3'' ''3!8''' °
'''With the binomial primitive, we can easily define an algorithm to'' °
' '''calculate the Nth row of the Pascal triangle'',r' °
'1 show ''PASCAL:(0,∞∙)!∙'' ''PASCAL 1'' ''PASCAL 2'' ''PASCAL 10''' °
X 'endd' °
*(1997 8 19 11 37 32 496) °
FTIME ÉFX 'U╜V TIME W;T' °
'Σ Measure execution time of W evaluated V times' °
X 'U╜60 60 1000¥²3╞ÉTS' 'T╜»■VµΓW' 'U╜(,-U-60 60 1000¥²3╞ÉTS)÷V' °
NTIMES 1 6 17.6729876851297 14.20669908289046 11.89566104594187 °
X 10.24476835105871 9.006468342000588 8.043231726932046 °
XCa 0 % °
*(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 10 36 352) °
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 19 11 36 54 580) °
Ferase ÉFX 'erase;t;EXIT;GO;HELP;DISPLAY;BIND;REVIEW;TIME;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 10 36 352) °
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' °
*(1996 4 6 16 10 36 352) °
Fsset ÉFX 'U╜K sset N' 'Σ Generate subset of K items out of N' °
'╕((0=K)doif ''U╜ÉAV[124 126 33]'')/0' 'U╜²1Φ1+((-K)╞∞N)ÿ∞(!K)⌡K!N' °
'U╜«²1╟ε(Γ[1](^≡2<≡U)/U),■'',''' °
X 'U╜(1ΦÉAV[126 124],(U⌠'' '')/U),'' ''' °
*(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 10 36 352) °
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' °