Vector vol. 23 Nos. 1&2 Contents Editorial Stephen Taylor 2 Sustaining Members News



Download 0.53 Mb.
Page11/31
Date28.01.2017
Size0.53 Mb.
#9422
1   ...   7   8   9   10   11   12   13   14   ...   31

Indexing


Returning to our vector of numbers: Val← 22 37 41 19 54 11 34

In order to extract the fourth item, we write Val[4]

In other languages one uses parentheses instead of square brackets; this is not very different.

What is new is that one can extract several items in one instruction.

Val
22 37 41 19 54 11 34

Val [2 4 7 1 4] ⍝ note extracting the same item twice


37 19 34 22 19

And, of course, in the same way one may modify one or more items of Val designated by their indices, providing as many values are specified as there are items to modify, or a single value for all (modified items in bold type):

Val[3 5 1] ← 300 77 111

Val
111 37 300 19 77 11 34

It is often necessary to extract the first items from a list of values, for example the first 5. Nothing could be easier:

Val[1 2 3 4 5]


111 37 300 19 77

But if one needs to extract the first 500 items from a long list, typing the integers from 1 to 500 is naturally impossible.

This is why APL has been given the symbol ⍳ (iota), which produces the set of the first n integers.

Thus, instead of writing 1 2 3 4 5 6 7 8, it is sufficient to write ⍳8. And to extract the first 500 terms of a large vector, one may write Big[⍳500] .


Calculating without Writing Programs


The twenty salaries of a business are divided into three hierarchical categories, denoted simply 1 2 3.

One assigns to two variables the salaries and the categories of these salaries; a part shown here:

Salaries ← 4225 1619 3706 2240 2076 1389 3916 3918 4939 2735
Categories ← 3 1 3 2 2 1 3 3 3 2

Do they never want to upgrade these salaries? (What has our poor world come to!)

A rumour reaches us about their plans: they want a different percentage increase for each category, according to the following scale:

Category Required Upgrade
1 8%
2 5%
3 2%

How much is that going to cost the business?

We create a variable containing the above three rates, recalling that we can divide three numbers by a single number:

Rates ← 8 5 2 ÷ 100

Rates
0.08 0.05 0.02

Then, as the first salary is in category 3, the rate which applies to it is:

Rates[3]
0.02

It follows that the first five salaries, being in categories 3 1 3 2 2 respectively, require the following upgrades:

Rates[3 1 3 2 2]
0.02 0.08 0.02 0.05 0.05

More generally, the rates applied to our twenty salaries are obtained like this:

Rates[Categories]
0.02 0.08 0.02 0.05 0.05 0.08 0.02 0.02 0.02 0.05 0.05 …

Having the 20 rates it suffices to multiply by the 20 salaries to obtain the individual up-grades:

Salaries × Rates[Categories]
84.5 129.52 74.12 112 103.8 111.12 78.32 78.36 98.78 …

Finally, by adding them all, one will know how much it will cost the business:

+/ Salaries × Rates[Categories]
2177.41

One notes that:

• this method remains valid whatever the number of salaries or categories.

• the result has been obtained without writing any program.

• and this expression can be read as the simplest possible English: The sum of Salaries multiplied by Rates according to Categories.

This example shows clearly that there are ways of reasoning other than those which have dominated information processing for 40 years but they are, alas, still extremely rare. This difference and originality, introduced by APL, are major features. They typify the open and welcoming intellectual spirit of the people who practise it.


Our Binary Friends


APL makes much use of binary data. It is most often created by means of relational functions such as = or > .

Salaries > 3000


1 0 1 0 0 0 1 1 1 0 1 1 0 0 1 1 0 0 0 0

Actual > Forecast


0 0 1 1 1 0
1 0 0 1 0 0
1 1 1 1 1 0
0 0 0 1 0 1

One sees the favourable results instantly. It is a prime novelty of APL that it is the only computer language we know of which has the six relational functions, represented in their conventional mathematical form: < ≤ = ≥ > ≠ .

For sure, other languages manage somehow but it seems to us, at the beginning of the 21st century, not totally unreasonable to ask that the inequality ≥ should not be represented as => and that ≠ should not be represented by the diabolical <>!

Naturally one can operate on this binary data using all the functions of Boolean algebra and, moreover, the symbols used are those familiar, throughout the world, to mathematicians of all nationalities:

Function and is well known ∧ (denoted AND in many languages).

Function or is well known ∨ (denoted OR in these languages).

Thus, if I am looking for people in category 3 whose salary is less than 4000 euros, I can write:

(Categories=3) ∧ (Salaries<4000)


0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1

In fact APL offers all the functions of Boolean algebra, including some functions like NOR and NAND (Exclusive OR and Exclusive AND) not familiar to managers but very useful in electronic automation.

Incidentally, Exclusive OR (sometimes called XOR) can be simply ≠ because either symbol acts like Exclusive OR (either not both):

0 0 1 1 ≠ 0 1 0 1


0 1 1 0

Finally: these binary vectors can be used as we have described but also for novel purposes, as good tools for denumerating and selecting.


Denumeration


Having found which salaries are less than 2500 euros by means of the following expression:

Salaries<2500


0 1 0 1 1 1 0 0 0 0 0 0 1 1 0 0 1 0 1 0

it is easy to add all the 1s and 0s to calculate how many people earn less than 2500 euros:

+/Salaries<2500
8

Selection


One can also use the binary vector as a “mask” to select from other data those items corresponding to the binary 1s:

1 1 0 1 0 0 1/23 55 17 46 81 82 83


23 55 46 83

The procedure is identical for text data:

1 1 0 1 0 0 1/'Bernard'
Bend

This operation, called compression, is particularly useful for extracting from a variable the items conforming to a given criterion. For example, to display the salaries in Category 2, one writes:

(Categories=2)/Salaries
2240 2076 2735 3278 1339 3319

Powerful, isn’t it?



Directory: issues
issues -> Protecting the rights of the child in the context of migration
issues -> Submission for the Office of the High Commissioner for Human Rights (ohchr) report to the General Assembly on the protection of migrants (res 68/179) June 2014
issues -> Human rights and access to water
issues -> October/November 2015 Teacher's Guide Table of Contents
issues -> Suhakam’s input for the office of the high commissioner for human rights (ohchr)’s study on children’s right to health – human rights council resolution 19/37
issues -> Office of the United Nations High Commissioner
issues -> The right of persons with disabilities to social protection
issues -> Human rights of persons with disabilities
issues -> Study related to discrimination against women in law and in practice in political and public life, including during times of political transitions
issues -> Super bowl boosts tv set sales millennials most likely to buy

Download 0.53 Mb.

Share with your friends:
1   ...   7   8   9   10   11   12   13   14   ...   31




The database is protected by copyright ©ininet.org 2024
send message

    Main page