Anda di halaman 1dari 10

From http://booktype-demo.sourcefabric.

org/algebra-an-algorithmic-treatment/

Page 1 of 10

Excerpts from Algebra: An Algorithmic Treatment


[From chapter 1, The Language of Mathematics]

A student using an APL or J computer in exploration is sometimes confronted with matters not treated until a later point in the text. For example, a beginning student entering the expression
2000*3000*4000

will receive the response


2.4e10

This response is expressed in exponential notation (meaning 2.4 times 10 to the power 10) which is not discussed until Section 5.17. The Index, the Summary of Notation, and Appendix B can be used to resolve such difficulties. The student may also be confronted with error messages, such as:
(2*3)%(4*5 |syntax error | (2*3)%(4*5 length=.9 lenght |value error: lenght 1 3 5+6 8 1 4 3 |length error | 1 3 5 +6 8 1 4 3

These result from omitting a right parenthesis, from misspelling, and from a mismatch of list lengths, respectively. These and other error messages and their causes are listed in Appendix B.

Expressions and Results Evaluation of the expression 2+3produces the result 5. Such a fact will be written in the following form:
2+3 5

and will be read aloud as "2 plus 3 makes 5". The following examples would be read in a similar way:
7+12 19 8*4 32

Where there is more than one function or verb to be executed, parentheses are used to indicate which is to be done first. Thus the expression
(2+3)*4

is evaluated by first performing the verb within the parentheses (that is, 2+3), and then multiplying the result by 4. The final result is therefore 20, as shown below:
(2+3)*4 20

The foregoing is read aloud as "quantity 2+3, times 4". The word "quantity" indicates that the first expression following it is to be executed first. That is, you are to find the result of 2+3 before attempting to execute the verb "times". The steps in the execution of an expression may be displayed on successive lines, substituting at each line the value of part of the expression above it as illustrated below:

DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf

4/19/2012 12:23:00 PM

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 2 of 10

|(2+3)*4 | 5 *4 20

The vertical line drawn to the left of the first two lines=. indicates that they are equivalent statements, either of which would produce the result 20 shown on the final line. The whole statement would be read aloud as "Quantity 2 plus 3 times 4 is equivalent to 5 times 4 which makes 20". The following examples would be read in a similar way as shown on the right: Quantity 2 plus 3 times quantity 5 plus 4 is equivalent to 5 times 9 which makes 45 Quantity 2 times 3 plus quantity 5 times 4, all times 2 is equivalent to quantity 6 plus 20 times 2 is equivalent to 26 times 2 which makes 52

|(2+3)*(5+4)

9 45

|((2*3)+(5*4))*2 |( 6 + 20 )*2 | 26 *2 52

The last example illustrates the difficulty of expressing in English the sequence of execution that is expressed so simply by parentheses in algebra, that is, when parentheses are "nested" within other parentheses even the use of the word "quantity" does not suffice and one resorts to expressions such as "all times 2". The main point is this: in learning any new language (such as algebra) it is important to re-express statements in a more familiar language (such as English); however certain things are so awkward to express in the old language that it becomes important to learn to "think" in the new language. The expression 2+3*4, written without parentheses, could be taken to mean either (2+3)*4 (which makes 20) or 2+(3*4) (which makes 14). To avoid such ambiguity, we make the following rule: when two or more verbs occur in succession with no parentheses between them, the rightmost verb is executed first. For example:
|2+3*4 |2+ 12 14 |1+2*3+4*5 |1+2*3+ 20 |1+2* 23 |1+ 46 47 |(1+2*3)+4*5 |(1+ 6 )+20 | 7 +20 27 #7-12

Names
Consider the following statements:
(1+3+5+7+9)*2 50 (1+3+5+7+9)*3
DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf 4/19/2012 12:23:00 PM

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 3 of 10

75 (1+3+5+7+9)*4 100

Since the expression 1+3+5+7+9 occurs again and again in the foregoing statements, it would be convenient to give some short name to the the result produced by the expression. This is done as follows.
it =. 1+3+5+7+9 it*2 50 it*3 75 it*4 100 it 25

The foregoing would be read aloud as follows:


The name it is assigned the value of the expression 1+3+5+7+9. it times 2 makes 50. it times 3 makes 75. it times 4 makes 100. it makes 25.

Unlike the case of an expression that returns a visible result on the next line, an assignment statement does not display a result. To display the value of a variable as it is assigned, we can use the same verb, denoted by ], which returns its argument unchanged. For example:
]it=.1+3+5+7+9 25

Names can be chosen at will. For example:


length =. 5 width =. 4 length*width 20 area=. length*width area 20 price =. 5 quantity =. 4 price*quantity 20

Mathematicians usually prefer to use short names like lor wor xor y, perhaps because this brings out the underlying structure or similarity of expressions which may deal with different names. Consider, for example, the following sequence:
x =. 5 y =. 4 x*y 20

If x is taken to mean length and y is taken to mean width, the result is the area of the corresponding rectangle; but if x is taken to mean price and y is taken to mean quantity, then the result is the total price.
DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf 4/19/2012 12:23:00 PM

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 4 of 10

This makes clear that there is some similarity between the calculation of the area from length and width, and the calculation of total price from price and quantity. The names used in algebra are also called variables, since they may vary in the sense that the same name may represent different values at different times. For example:
x=.3 x*x 9 x=.5 x*x 25

This ability to vary distinguishes a name like x from a symbol like 5 which always represents the same value, and is therefore called a constant. It is interesting to note that the variables in algebra correspond to the pronouns in English. For example, the sentence "close it" is meaningless until one knows to what "it" refers. This reference is usually made clear by a preceding sentence. For example, "See the door. Close it" is unambiguous because the first sentence makes clear that "it" refers to "the door". Similarly, in algebra the expression it+5 cannot be evaluated unless the value to which it refers is known. In algebra this reference is make clear in one way, by the assignment represented by the symbol =. . For example:
it=.3 it+5 8

The same name can stand for different values at different times just as the pronoun "it" can refer to different things at different times.
#13-18

Over Notation
It is often necessary to take the sum over a whole list of numbers. For example, if the list consists of the numbers 1 3 5 7 9 11, then their sum could be written as
1+3+5+7+9+11 36

It is more convenient to use the following notation:


+/1 3 5 7 9 11 36

The foregoing is read aloud as "Sum over 1 3 5 7 9 11" or as "Plus over 1 3 5 7 9 11". The convenience is much greater when using names, where we do not have the option of inserting + symbols. For example:
list=.1 3 5 7 9 11 +/list 36

The over notation can be used for other verbs as well as for addition. For example: READ AS Times over 1 2 3 makes 6
4/19/2012 12:23:00 PM

*/1 2 3 6

DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 5 of 10

*/1 2 3 4 24 +/1 2 3 4 10 (+/1 2 3 4)*6 60

Times over 1 2 3 4 makes 24 Plus over 1 2 3 4 makes 10 Quantity plus over 1 2 3 4 times 6 makes 60

n=.1 2 3 4 +/n 10

*/n 24

n assigned 1 2 3 4 plus over n makes 10 Times over n makes 24


#19-21

Lists
A sequence of numbers such as 3 5 7 11 is called a list. The numbers in the list are called the elements of the list. Thus the first element of the list 3 5 7 11 is the number 3, the second element is 5, the third element is 7, and the fourth is 11. The number of elements in the list is called the size of the list. Thus the size of the list 3 5 7 11 is 4. Any single quantity (such as 17) will now be referred to as an atom to distinguish it from a list. Lists can be added and multiplied as shown in the following examples:
3 5 7+1 2 3 4 7 10 1 2 3+3 2 1 4 4 4 1 2 3*3 2 1 3 4 3

READ AS List 3 5 7 plus list 1 2 3 makes 4 7 10 List 1 2 3 plus list 3 2 1 makes 4 4 4 List 1 2 3 times list 3 2 1 makes 3 4 3

From this it should be clear that when two lists are added the first element is added to the first element, the second element is added to the second, and so on. Multiplication is performed similarly. Like any other result, a list can be assigned a name. For example:
v=.1 2 3 4 w=.4 3 2 1 v+w 5 5 5 5

READ AS The name v is assigned 1 2 3 4 The name w is assigned 4 3 2 1

v plus w makes 5 5 5 5 v times w v*w 4 6 6 4 makes 4 6 6 4 v times v v*v 1 4 9 16 makes 1 4 9 16


The following examples may be read similarly:
DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf 4/19/2012 12:23:00 PM

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 6 of 10

n=.i.5 n 0 1 2 3 4 n*n 0 1 4 9 16 (i.6)*i.6 0 1 4 9 16

READ AS n is assigned first 5 integers

n
makes 0 1 2 3 4

n times n makes 0 1 4 9 16 Quantity the first 6 integers times quantity the first 6 integers 25 makes 0 1 4 9 16 25

Since the addition of two lists v and w means that the first element of v is to be added to the first element of w, the second element of v is to be added to the second element of w, and so on, then an expression such as
1 3 5+6 8 1 4 3

cannot be executed because the lists are not of the same size. However, expressions of the following form can be executed: READ AS 3 plus list 1 3 5 7 makes 4 6 8 10 list 1 2 3 4 5 plus 6 makes 7 8 9 10 11

3+1 3 5 7 4 6 8 10 1 2 3 4 5+6 7 8 9 10 11

In other words, if one of the quantities to be added is a single number, (i.e., an atom), it is added to each element of the list. The same holds for multiplication as follows: READ AS 3 times list 1 3 5 7 3*2 3 5 7 6 9 15 21 makes 6 9 15 21

3 times the first 5 integers makes 0 3 6 9 12 2 plus 3 times the first 5 integers 2+3*i.5 2 5 8 11 14 makes 2 5 8 11 14 1 plus 2 times the first 6 integers 1+2*i.6 1 3 5 7 9 11 makes 1 3 5 7 9 11 plus over 1 plus 2 times the first 6 integers +/1+2*i.6 36 makes 36 1 plus plus over 1 plus 2 times the first 6 integers 1++/1+2*i.6 37 makes 37
3*i.5 0 3 6 9 12 #26-28

Character atoms and lists are quoted, as follows:


'abcde' abcde #'abcde' 5 '' NB. list of length 0 #'' 0 'a' NB. single-character list a #'a' 1

DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf

4/19/2012 12:23:00 PM

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 7 of 10

Tables
We can also create tables with varying numbers of rows and columns, and apply a variety of verbs to them. For example:
]t=.i.3 4 0 1 2 3 4 5 6 7 8 9 10 11

When applied to an atom a, the i. verb returns a list of the the first a integers. However, when applied to a list of numbers l, i. returns an array whose shape is given by l, with the the first */l integers as elements. Applying i. to the list 3 4 returns a table of three rows and four columns, containing the integers from 0 to 11, as shown just above: The tally verb # applied to a list yields the count of its elements. When applied to a table, it yields the number of rows. There is also a shape verb $ which yields both dimensions of a table thus:
#t 3 $t 3 4

A more general way to create tables is with the reshape verb, $, applied to two objects. The left object is a list which specifies the shape of the result, and the right object supplies the elements of the table, which are reused as often as necessary to fill the table. For example: 4 0 4 3 2 2 5$4 4 4 4 4 4 4 4 4 4 3 4$i.5 1 2 3 0 1 2 4 0 1 $2 5$4 5

We can get the items from a table in a list using the ravel verb , which yields the list of elements of its argument.
,2 2$i.4 0 1 2 3

For any table t, it is the case that t is the same as ($t)$,t read quantity shape of t reshape list of elements of t.
0 1 t 0 1 2 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11 ,t 0 1 2 3 4 5 6 7 8 9 10 11 $t 3 4 ($t)$,t

A table can be treated as a list of rows, as in the expression #t. For example:
4 5 5 7 9 11 12 15 16 4 6+t 6 10 14 17

Here the elements of the list 4 5 6 are paired with the rows of the table t, and then added as in the case of an atom added to a list. Adding the elements of a list to the columns of a table will be explained below, under Verb Rank.
#29-31
DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf 4/19/2012 12:23:00 PM

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 8 of 10

Verbs with One or Two Objects


English distinguishes verbs which do not take an object, such as "go", and verbs that do take an object, such as "eat". Verbs of the first class are called intransitive, and verbs of the second class transitive. Algebra does not use intransitive verbs, but has transitive verbs with either one or two direct objects. We have no English terms to make this distinction, so we will use algebraic terms. A verb that takes one object is called a monadic verb, and a verb with two objects is called a dyadic verb. In the expression x+y, the + verb is dyadic, taking a left argument or object and a right argument or object. However, the plus over verb +/ takes only a right object. We have just seen that $ can stand for the monadic shape verb with one object, in the form $x, or the dyadic reshape verb with two objects x$y. We will use a number of symbols that can represent both monadic and dyadic verbs. For example:
#i.5 NB. tally verb 5 5#3 NB. copy copy 3 3 3 3 3 NB. negation _2 NB. makes negative 2 3-2 NB. subtraction 1 -2

NB. stands for nota bene, Latin for note well, and will be used to add comments to executable lines. It is not a verb, but rather punctuation. Everything on a line after NB. is ignored by the computer.
Note the difference between the negation verb - and the negative sign _ . Like the decimal point in the number 0.5, the _ sign is part of a number. We can apply the negation verb to a list of numbers, but we cannot separate _ from the single number it is part of. Thus:
-(3 1 _5 3) _3 _1 5 _3 _(3 1 5 3) |syntax error | _(3 1 5 3)

We will see much more of the _ sign in numbers starting in Chapter 3, Negative Numbers. #32-34

Verb Rank
Some verbs inherently act on atoms to produce atoms. This includes arithmetic verbs such as +, -, and *, and many others that we will meet later in this book. Such verbs can act on lists and tables element-by-element to produce lists and tables of the same size and shape. The index verb i. can act on an atom to create a list, or on a list to create a table or higher-dimensional array. Still other verbs act on lists to create atoms, such as +/ , -:and the like, or in other ways. For example:
2+3 NB. Atomic object, atomic result, dyadic rank 0 0 5 ]l=.i.5 NB. Atomic object, list result, monadic rank 0 0 1 2 3 4 +/l NB. Object of any dimension, monadic rank _ 10 ]t=.i.3 4 NB. List object, table result, monadic rank 1 0 1 2 3 4 5 6 7 8 9 10 11 +/t NB. Add the three rows of the table 12 15 18 21 ,t NB. Object is whole table, result is list, rank _ 0 1 2 3 4 5 6 7 8 9 10 11 t-:,t NB. Objects are whole table and list, result is atomic 0

Verbs that have both monadic and dyadic cases have three ranks, one for the single argument in the monadic case, and two more, for the left and right arguments, in the dyadic case. The most common ranks are 0 (act on atoms), 1 (act on lists), and _ (act on entire object, regardless of dimension). There are some important
DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf 4/19/2012 12:23:00 PM

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 9 of 10

verbs of rank 2, acting on tables or matrices, that we will meet later on. We can use the basic adverb b. to get a list of the three ranks of any verb. (Basic can provide other information about a verb that we will not need until Chapter 19.) For example:
+b.0 0 0 0 +/b.0 _ _ _ ,b.0 _ _ _ #b.0 _ 1 _

There are times when we want a verb to act along the rows of a table rather than the columns. We do this with the rank adverb, " . For example:
+/"1 t NB. Take the sum within each row 6 22 38

We can also specify that a verb should act on tables within three-dimensional arrays (rank 2) or on whole arrays, no matter what their dimension (rank _, or infinity). We will encounter other uses of the rank adverb later on. #35-37

Infinities
In addition to _ there is a number __, read negative infinity. Both are convenient for some purposes, but do not obey many of the rules of other numbers. Many functions applied to either of them return _ or __ unchanged, while many others fail to yield a result at all, giving Not a Number errors (NaN) instead. Some verbs act on infinities in their normal manner, and there are special cases where finite results are defined. For example:
_+_2 0 2 _ _ _ |NaN error | _ __+_2 0 2 __ __ __ %_ -_ __ _+_ _ _+__ |NaN error | _ +__ _%_

We will not need to do arithmetic on infinities in this book.


#38

Boxed Arrays
The box verb, represented by the symbol < , puts any object into a container which acts as an atom. For example:

We can get at the contents of a boxed object using the open verb, > . For example: <3 3 <i.5 0 1 2 3 4
DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf 4/19/2012 12:23:00 PM

From http://booktype-demo.sourcefabric.org/algebra-an-algorithmic-treatment/

Page 10 of 10

<i.2 3 0 1 2 3 4 5 Often we will want to open a boxed object, do something with it, and put the result back into the box. For example, using the table t above:
]b=.<t 0 1 2 12 15 18 21 3 4 5 6 7 8 9 10 11 <+/>t

We can do this with the under adverb, &., which among other things can apply a verb to the contents of a boxed array, and box the results. In general, for a single boxed object and a verb v, v&.>a is equivalent to a . Continuing the example above:
+/&.>b 12 15 18 21

Operating in individual boxed arrays is not very interesting. But it is often convenient to be able to do the same operations on several objects of different shapes. We can create boxed lists of disparate objects using the link verb ; and then apply other verbs to the objects within the boxes using the phrase under open, to which we can give the name each. For example:
]l=.3;(i.3);(i.3 4) 30 1 20 1 2 3 4 5 6 7 8 9 10 11 each=.&.> # each l 133

The link verb always boxes its left argument, and boxes its right argument if it is not boxed. This is convenient for adding boxed items to an existing boxed list. For example:
5;<3 53 (<5);3 3 5 35 7;b 735 ]b=.3;5

Opening a boxed array of lists or tables of different dimensions results in a table or higher-dimensional array where the rows or planes are padded with 0s or space characters to equal shapes. For example:
>(i.3);i.5 0 1 2 0 0 0 1 2 3 4 >(i.3);i.5 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 2 3 0 4 5 0 6 7 0 8 9 0 >'abc';'edcba' abc edcba #39-41

Multiple Assignment
It often happens that we want to assign several names at once. Boxed nouns give us a convenient way to do that, as follows:
x=.'a';'b';'c' (x)=.1 4 2 a 1 b 4 c 2 (x)=.(i.5);'abcde';3 c 3 a 0 1 2 3 4 b abcde

DHM: Excerpts from Algebra - An Algorithmic Treatment.pdf

4/19/2012 12:23:00 PM

Anda mungkin juga menyukai