Tip 70. Do not change the value of the variable ORIGIN

  

Fig. 70. Do not change the value of the variable ORIGIN

In Mathcad, like in any other programming environment (or rather – in computer environment), äà and even in day-to-day life there are a lot of defaults. A mother says to her son: «Alex, please go for some butter!» at that she passes over in silence that he has to take some money, bag and buy butter in the nearest shop (and so on and etc.).

The main default, touching with vectors and matrix (arrays) in Mathcad, is in the providential variable ORIGIN, which initial value equals zero. It means that initial («upper») element of a vector (left column and upper line of a matrix) has zero number. User has a right to break this default with help of two methods. In the first place it possible by the command Options... from menu Math to call the dialogue window Math Options with the laying Built-In Variables, where in the field Array Origin the user can change zero to another integer number (including negative numbers). In the second place it is possible to write ORIGIN := N in Mathcad-document, where N – integer number, negative or positive one. But it is better not to touch this variable at all trying to work with other ones. Generally speaking it is necessary to have two variables of the type ORIGIN in order to one of them keeps a number of a left column but the other – number of an upper line and of the first element of a vector. But if we use only one variable ORIGIN, then we’ll be able not to use this variable at all (see the name of the tip), or rather to forget about it at all.

As rule one changes the value of the variable ORIGIN from zero to one. In that way he stresses that the numeration of the vector’s elements begins usually from one but not from zero[1] in mathematics.

If when you solve the problem and a vector is used then the numeration of the elements begins from one and it is necessary not to use the variable ORIGIN, but you have to try to fill zero element up something.

Famous puzzle «Hanoi’s towers» is solved in the document in the fig. 70. There are three sticks marked a, b and c. n mixed disk are stringed on the stick a in imitation of child’s pyramid: the biggest disk in the bottom of the stick but the smallest on – at the top. It is necessary to move this pyramid from the stick a to the stick c using the following rules: 1) one disk is moved for one step; 2) the stick b is used for intermediate warehousing of disks; 3) it is impossible to put a big disk on a small one.

The solution of the problem for three disks is shown in the fig. 70: the function PD returns the line of textual constants that mark the beginning of the game, the order of transposition and the end of the game.

Features of the program:

1.      In general case the number of transpositions equals 2n-1 – this number is noted in the variable p. The vector V that keeps the order of transpositions of the disks is created in advance. We need that its initial element has had the first number and has kept the direction of the first transposition. For this it is necessary to break the default and to write ORIGIN := 1. But we shall not do it (the main point of the tip), but we’ll fill zero element of the vector V up additional information about the beginning of the game (“begin”). For «symmetry» we’ll fill the last (additional) element of the vector up the message about the end of transpositions (“end”).

2.      The problem about Hanoi’s towers is solved with help of recursive call of the function PD: the problem with n disks is reduced to the problem with two disks; the second of them is the disk that consists of n-1 disks. For its turn the problem with n-1 disks is reduced to the problem with two disks, the second of them is the disk that consists of n-2 and so on. This way it is possible to «go down» the problem with one disk, which is solved simply – a-c. Hence there is recursion in the program.

3.      The solution of the problem about Hanoi’s towers in Mathcad is suggested by Êîíñòàíòèíîì Ìàìàåâûì from Áàðíàóëà.



[1] To begin the calculation of a vector’s elements from one is European tradition, but from zero – American one. The other «mathematical» difference of the Old and New World is the marking of the imaginary unit: in our case it is i, but in USA – j.

«I have a comment about the TIP's footnote on the last line.  It's sometimes amusing to see that we don't always have the same idea of what happens in other parts of the world.  (1) In my experience, vector and matrix indices beginning with 1 are much more common than 0 in the U.S. although everyone understands that it's just an arbitrary choice. and (2) At least in the U.S., most engineers use j for sqrt(-1), while most physicists use i.»
Regards
, Stan