This is Info file calc.info, produced by Makeinfo-1.55 from the input file calc.texinfo. This file documents Calc, the GNU Emacs calculator. Copyright (C) 1990, 1991 Free Software Foundation, Inc. Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the section entitled "GNU General Public License" is included exactly as in the original, and provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that the section entitled "GNU General Public License" may be included in a translation approved by the author instead of in the original English. File: calc.info, Node: Modes Tutorial, Prev: Undo Tutorial, Up: Basic Tutorial Mode-Setting Commands --------------------- Calc has many types of "modes" that affect the way it interprets your commands or the way it displays data. We have already seen one mode, namely algebraic mode. There are many others, too; we'll try some of the most common ones here. Perhaps the most fundamental mode in Calc is the current "precision". Notice the `12' on the Calc window's mode line: --%%-Calc: 12 Deg (Calculator)----All------ Most of the symbols there are Emacs things you don't need to worry about, but the `12' and the `Deg' are mode indicators. The `12' means that calculations should always be carried to 12 significant figures. That is why, when we type `1 RET 7 /', we get `0.142857142857' with exactly 12 digits, not counting leading and trailing zeros. You can set the precision to anything you like by pressing `p', then entering a suitable number. Try pressing `p 30 RET', then doing `1 RET 7 /' again: 1: 0.142857142857 2: 0.142857142857142857142857142857 . Although the precision can be set arbitrarily high, Calc always has to have *some* value for the current precision. After all, the true value `1/7' is an infinitely repeating decimal; Calc has to stop somewhere. Of course, calculations are slower the more digits you request. Press `p 12' now to set the precision back down to the default. Calculations always use the current precision. For example, even though we have a 30-digit value for `1/7' on the stack, if we use it in a calculation in 12-digit mode it will be rounded down to 12 digits before it is used. Try it; press RET to duplicate the number, then `1 +'. Notice that the RET key didn't round the number, because it doesn't do any calculation. But the instant we pressed `+', the number was rounded down. 1: 0.142857142857 2: 0.142857142857142857142857142857 3: 1.14285714286 . In fact, since we added a digit on the left, we had to lose one digit on the right from even the 12-digit value of `1/7'. How did we get more than 12 digits when we computed `2^3^4'? The answer is that Calc makes a distinction between "integers" and "floating-point" numbers, or "floats". An integer is a number that does not contain a decimal point. There is no such thing as an "infinitely repeating fraction integer," so Calc doesn't have to limit itself. If you asked for `2^10000' (don't try this!), you would have to wait a long time but you would eventually get an exact answer. If you ask for `2.^10000', you will quickly get an answer which is correct only to 12 places. The decimal point tells Calc that it should use floating-point arithmetic to get the answer, not exact integer arithmetic. You can use the `F' (`calc-floor') command to convert a floating-point value to an integer, and `c f' (`calc-float') to convert an integer to floating-point form. Let's try entering that last calculation: 1: 2. 2: 2. 1: 1.99506311689e3010 . 1: 10000 . . 2.0 RET 10000 RET ^ Notice the letter `e' in there. It represents "times ten to the power of," and is used by Calc automatically whenever writing the number out fully would introduce more extra zeros than you probably want to see. You can enter numbers in this notation, too. 1: 2. 2: 2. 1: 1.99506311678e3010 . 1: 10000. . . 2.0 RET 1e4 RET ^ Hey, the answer is different! Look closely at the middle columns of the two examples. In the first, the stack contained the exact integer `10000', but in the second it contained a floating-point value with a decimal point. When you raise a number to an integer power, Calc uses repeated squaring and multiplication to get the answer. When you use a floating-point power, Calc uses logarithms and exponentials. As you can see, a slight error crept in during one of these methods. Which one should we trust? Let's raise the precision a bit and find out: . 1: 2. 2: 2. 1: 1.995063116880828e3010 . 1: 10000. . . p 16 RET 2. RET 1e4 ^ p 12 RET Presumably, it doesn't matter whether we do this higher-precision calculation using an integer or floating-point power, since we have added enough "guard digits" to trust the first 12 digits no matter what. And the verdict is... Integer powers were more accurate; in fact, the result was only off by one unit in the last place. Calc does many of its internal calculations to a slightly higher precision, but it doesn't always bump the precision up enough. In each case, Calc added about two digits of precision during its calculation and then rounded back down to 12 digits afterward. In one case, it was enough; in the the other, it wasn't. If you really need X digits of precision, it never hurts to do the calculation with a few extra guard digits. What if we want guard digits but don't want to look at them? We can set the "float format". Calc supports four major formats for floating-point numbers, called "normal", "fixed-point", "scientific notation", and "engineering notation". You get them by pressing `d n', `d f', `d s', and `d e', respectively. In each case, you can supply a numeric prefix argument which says how many digits should be displayed. As an example, let's put a few numbers onto the stack and try some different display modes. First, use `M-0 DEL' to clear the stack, then enter the four numbers shown here: 4: 12345 4: 12345 4: 12345 4: 12345 4: 12345 3: 12345. 3: 12300. 3: 1.2345e4 3: 1.23e4 3: 12345.000 2: 123.45 2: 123. 2: 1.2345e2 2: 1.23e2 2: 123.450 1: 12.345 1: 12.3 1: 1.2345e1 1: 1.23e1 1: 12.345 . . . . . d n M-3 d n d s M-3 d s M-3 d f Notice that when we typed `M-3 d n', the numbers were rounded down to three significant digits, but then when we typed `d s' all five significant figures reappeared. The float format does not affect how numbers are stored, it only affects how they are displayed. Only the current precision governs the actual rounding of numbers in the Calculator's memory. Engineering notation, not shown here, is like scientific notation except the exponent (the power-of-ten part) is always adjusted to be a multiple of three (as in "kilo," "micro," etc.). As a result there will be one, two, or three digits before the decimal point. Whenever you change a display-related mode, Calc redraws everything in the stack. This may be slow if there are many things on the stack, so Calc allows you to type shift-`H' before any mode command to prevent it from updating the stack. Anything Calc displays after the mode-changing command will appear in the new format. 4: 12345 4: 12345 4: 12345 4: 12345 4: 12345 3: 12345.000 3: 12345.000 3: 12345.000 3: 1.2345e4 3: 12345. 2: 123.450 2: 123.450 2: 1.2345e1 2: 1.2345e1 2: 123.45 1: 12.345 1: 1.2345e1 1: 1.2345e2 1: 1.2345e2 1: 12.345 . . . . . H d s DEL U TAB d SPC d n Here the `H d s' command changes to scientific notation but without updating the screen. Deleting the top stack entry and undoing it back causes it to show up in the new format; swapping the top two stack entries reformats both entries. The `d SPC' command refreshes the whole stack. The `d n' command changes back to the normal float format; since it doesn't have an `H' prefix, it also updates all the stack entries to be in `d n' format. Notice that the integer `12345' was not affected by any of the float formats. Integers are integers, and are always displayed exactly. Large integers have their own problems. Let's look back at the result of `2^3^4'. 2417851639229258349412352 Quick--how many digits does this have? Try typing `d g': 2,417,851,639,229,258,349,412,352 Now how many digits does this have? It's much easier to tell! We can actually group digits into clumps of any size. Some people prefer `M-5 d g': 24178,51639,22925,83494,12352 Let's see what happens to floating-point numbers when they are grouped. First, type `p 25 RET' to make sure we have enough precision to get ourselves into trouble. Now, type `1e13 /': 24,17851,63922.9258349412352 The integer part is grouped but the fractional part isn't. Now try `M-- M-5 d g' (that's meta-minus-sign, meta-five): 24,17851,63922.92583,49412,352 If you find it hard to tell the decimal point from the commas, try changing the grouping character to a space with `d , SPC': 24 17851 63922.92583 49412 352 Type `d , ,' to restore the normal grouping character, then `d g' again to turn grouping off. Also, press `p 12' to restore the default precision. Press `U' enough times to get the original big integer back. (Notice that `U' does not undo each mode-setting command; if you want to undo a mode-setting command, you have to do it yourself.) Now, type `d r 16 RET': 16#200000000000000000000 The number is now displayed in "hexadecimal", or "base-16" form. Suddenly it looks pretty simple; this should be no surprise, since we got this number by computing a power of two, and 16 is a power of 2. In fact, we can use `d r 2 RET' to see it in actual binary form: 2#1000000000000000000000000000000000000000000000000000000 ... We don't have enough space here to show all the zeros! They won't fit on a typical screen, either, so you will have to use horizontal scrolling to see them all. Press `<' and `>' to scroll the stack window left and right by half its width. Another way to view something large is to press ``' (back-quote) to edit the top of stack in a separate window. (Press `M-# M-#' when you are done.) You can enter non-decimal numbers using the `#' symbol, too. Let's see what the hexadecimal number `5FE' looks like in binary. Type `16#5FE' (the letters can be typed in upper or lower case; they will always appear in upper case). It will also help to turn grouping on with `d g': 2#101,1111,1110 Notice that `d g' groups by fours by default if the display radix is binary or hexadecimal, but by threes if it is decimal, octal, or any other radix. Now let's see that number in decimal; type `d r 10': 1,534 Numbers are not *stored* with any particular radix attached. They're just numbers; they can be entered in any radix, and are always displayed in whatever radix you've chosen with `d r'. The current radix applies to integers, fractions, and floats. (*) *Exercise 1.* Your friend Joe tried to enter one-third as `3#0.1' in `d r 3' mode with a precision of 12. He got `3#0.0222222...' (with 25 2's) in the display. When he multiplied that by three, he got `3#0.222222...' instead of the expected `3#1'. Next, Joe entered `3#0.2' and, to his great relief, saw `3#0.2' on the screen. But when he typed `2 /', he got `3#0.10000001' (some zeros omitted). What's going on here? *Note 1: Modes Answer 1. (*) (*) *Exercise 2.* Scientific notation works in non-decimal modes in the natural way (the exponent is a power of the radix instead of a power of ten, although the exponent itself is always written in decimal). Thus `8#1.23e3 = 8#1230.0'. Suppose we have the hexadecimal number `f.e8f' times 16 to the 15th power: We write `16#f.e8fe15'. What is wrong with this picture? What could we write instead that would work better? *Note 2: Modes Answer 2. (*) The `m' prefix key has another set of modes, relating to the way Calc interprets your inputs and does computations. Whereas `d'-prefix modes generally affect the way things look, `m'-prefix modes affect the way they are actually computed. The most popular `m'-prefix mode is the "angular mode". Notice the `Deg' indicator in the mode line. This means that if you use a command that interprets a number as an angle, it will assume the angle is measured in degrees. For example, 1: 45 1: 0.707106781187 1: 0.500000000001 1: 0.5 . . . . 45 S 2 ^ c 1 The shift-`S' command computes the sine of an angle. The sine of 45 degrees is `sqrt(2)/2'; squaring this yields `2/4 = 0.5'. However, there has been a slight roundoff error because the representation of `sqrt(2)/2' wasn't exact. The `c 1' command is a handy way to clean up numbers in this case; it temporarily reduces the precision by one digit while it re-rounds the number on the top of the stack. (*) *Exercise 3.* Your friend Joe computed the sine of 45 degrees as shown above, then, hoping to avoid an inexact result, he increased the precision to 16 digits before squaring. What happened? *Note 3: Modes Answer 3. (*) To do this calculation in radians, we would type `m r' first. (The indicator changes to `Rad'.) 45 degrees corresponds to `pi/4' radians. To get `pi', press the `P' key. (Once again, this is a shifted capital `P'. Remember, unshifted `p' sets the precision.) 1: 3.14159265359 1: 0.785398163398 1: 0.707106781187 . . . P 4 / m r S Likewise, inverse trigonometric functions generate results in either radians or degrees, depending on the current angular mode. 1: 0.707106781187 1: 0.785398163398 1: 45. . . . .5 Q m r I S m d U I S Here we compute the Inverse Sine of `sqrt(0.5)', first in radians, then in degrees. Use `c d' and `c r' to convert a number from radians to degrees and vice-versa. 1: 45 1: 0.785398163397 1: 45. . . . 45 c r c d Another interesting mode is "fraction mode". Normally, dividing two integers produces a floating-point result if the quotient can't be expressed as an exact integer. Fraction mode causes integer division to produce a fraction, i.e., a rational number, instead. 2: 12 1: 1.33333333333 1: 4:3 1: 9 . . . 12 RET 9 / m f U / m f In the first case, we get an approximate floating-point result. In the second case, we get an exact fractional result (four-thirds). You can enter a fraction at any time using `:' notation. (Calc uses `:' instead of `/' as the fraction separator because `/' is already used to divide the top two stack elements.) Calculations involving fractions will always produce exact fractional results; fraction mode only says what to do when dividing two integers. (*) *Exercise 4.* If fractional arithmetic is exact, why would you ever use floating-point numbers instead? *Note 4: Modes Answer 4. (*) Typing `m f' doesn't change any existing values in the stack. In the above example, we had to Undo the division and do it over again when we changed to fraction mode. But if you use the evaluates-to operator you can get commands like `m f' to recompute for you. 1: 12 / 9 => 1.33333333333 1: 12 / 9 => 1.333 1: 12 / 9 => 4:3 . . . ' 12/9 => RET p 4 RET m f In this example, the righthand side of the `=>' operator on the stack is recomputed when we change the precision, then again when we change to fraction mode. All `=>' expressions on the stack are recomputed every time you change any mode that might affect their values. File: calc.info, Node: Arithmetic Tutorial, Next: Vector/Matrix Tutorial, Prev: Basic Tutorial, Up: Tutorial Arithmetic Tutorial =================== In this section, we explore the arithmetic and scientific functions available in the Calculator. The standard arithmetic commands are `+', `-', `*', `/', and `^'. Each normally takes two numbers from the top of the stack and pushes back a result. The `n' and `&' keys perform change-sign and reciprocal operations, respectively. 1: 5 1: 0.2 1: 5. 1: -5. 1: 5. . . . . . 5 & & n n You can apply a "binary operator" like `+' across any number of stack entries by giving it a numeric prefix. You can also apply it pairwise to several stack elements along with the top one if you use a negative prefix. 3: 2 1: 9 3: 2 4: 2 3: 12 2: 3 . 2: 3 3: 3 2: 13 1: 4 1: 4 2: 4 1: 14 . . 1: 10 . . 2 RET 3 RET 4 M-3 + U 10 M-- M-3 + You can apply a "unary operator" like `&' to the top N stack entries with a numeric prefix, too. 3: 2 3: 0.5 3: 0.5 2: 3 2: 0.333333333333 2: 3. 1: 4 1: 0.25 1: 4. . . . 2 RET 3 RET 4 M-3 & M-2 & Notice that the results here are left in floating-point form. We can convert them back to integers by pressing `F', the "floor" function. This function rounds down to the next lower integer. There is also `R', which rounds to the nearest integer. 7: 2. 7: 2 7: 2 6: 2.4 6: 2 6: 2 5: 2.5 5: 2 5: 3 4: 2.6 4: 2 4: 3 3: -2. 3: -2 3: -2 2: -2.4 2: -3 2: -2 1: -2.6 1: -3 1: -3 . . . M-7 F U M-7 R Since dividing-and-flooring (i.e., "integer quotient") is such a common operation, Calc provides a special command for that purpose, the backslash `\'. Another common arithmetic operator is `%', which computes the remainder that would arise from a `\' operation, i.e., the "modulo" of two numbers. For example, 2: 1234 1: 12 2: 1234 1: 34 1: 100 . 1: 100 . . . 1234 RET 100 \ U % These commands actually work for any real numbers, not just integers. 2: 3.1415 1: 3 2: 3.1415 1: 0.1415 1: 1 . 1: 1 . . . 3.1415 RET 1 \ U % (*) *Exercise 1.* The `\' command would appear to be a frill, since you could always do the same thing with `/ F'. Think of a situation where this is not true--`/ F' would be inadequate. Now think of a way you could get around the problem if Calc didn't provide a `\' command. *Note 1: Arithmetic Answer 1. (*) We've already seen the `Q' (square root) and `S' (sine) commands. Other commands along those lines are `C' (cosine), `T' (tangent), `E' (`e^x') and `L' (natural logarithm). These can be modified by the `I' (inverse) and `H' (hyperbolic) prefix keys. Let's compute the sine and cosine of an angle, and verify the identity `sin(x)^2 + cos(x)^2 = 1'. We'll arbitrarily pick -64 degrees as a good value for `x'. With the angular mode set to degrees (type `m d'), do: 2: -64 2: -64 2: -0.89879 2: -0.89879 1: 1. 1: -64 1: -0.89879 1: -64 1: 0.43837 . . . . . 64 n RET RET S TAB C f h (For brevity, we're showing only five digits of the results here. You can of course do these calculations to any precision you like.) Remember, `f h' is the `calc-hypot', or square-root of sum of squares, command. Another identity is `tan(x) = sin(x) / cos(x)'. 2: -0.89879 1: -2.0503 1: -64. 1: 0.43837 . . . U / I T A physical interpretation of this calculation is that if you move `0.89879' units downward and `0.43837' units to the right, your direction of motion is -64 degrees from horizontal. Suppose we move in the opposite direction, up and to the left: 2: -0.89879 2: 0.89879 1: -2.0503 1: -64. 1: 0.43837 1: -0.43837 . . . . U U M-2 n / I T How can the angle be the same? The answer is that the `/' operation loses information about the signs of its inputs. Because the quotient is negative, we know exactly one of the inputs was negative, but we can't tell which one. There is an `f T' [`arctan2'] function which computes the inverse tangent of the quotient of a pair of numbers. Since you feed it the two original numbers, it has enough information to give you a full 360-degree answer. 2: 0.89879 1: 116. 3: 116. 2: 116. 1: 180. 1: -0.43837 . 2: -0.89879 1: -64. . . 1: 0.43837 . . U U f T M-RET M-2 n f T - The resulting angles differ by 180 degrees; in other words, they point in opposite directions, just as we would expect. The META-RET we used in the third step is the "last-arguments" command. It is sort of like Undo, except that it restores the arguments of the last command to the stack without removing the command's result. It is useful in situations like this one, where we need to do several operations on the same inputs. We could have accomplished the same thing by using `M-2 RET' to duplicate the top two stack elements right after the `U U', then a pair of `M-TAB' commands to cycle the 116 up around the duplicates. A similar identity is supposed to hold for hyperbolic sines and cosines, except that it is the *difference* `cosh(x)^2 - sinh(x)^2' that always equals one. Let's try to verify this identity. 2: -64 2: -64 2: -64 2: 9.7192e54 2: 9.7192e54 1: -64 1: -3.1175e27 1: 9.7192e54 1: -64 1: 9.7192e54 . . . . . 64 n RET RET H C 2 ^ TAB H S 2 ^ Something's obviously wrong, because when we subtract these numbers the answer will clearly be zero! But if you think about it, if these numbers *did* differ by one, it would be in the 55th decimal place. The difference we seek has been lost entirely to roundoff error. We could verify this hypothesis by doing the actual calculation with, say, 60 decimal places of precision. This will be slow, but not enormously so. Try it if you wish; sure enough, the answer is 0.99999, reasonably close to 1. Of course, a more reasonable way to verify the identity is to use a more reasonable value for `x'! Some Calculator commands use the Hyperbolic prefix for other purposes. The logarithm and exponential functions, for example, work to the base `e' normally but use base-10 instead if you use the Hyperbolic prefix. 1: 1000 1: 6.9077 1: 1000 1: 3 . . . . 1000 L U H L First, we mistakenly compute a natural logarithm. Then we undo and compute a common logarithm instead. The `B' key computes a general base-B logarithm for any value of B. 2: 1000 1: 3 1: 1000. 2: 1000. 1: 6.9077 1: 10 . . 1: 2.71828 . . . 1000 RET 10 B H E H P B Here we first use `B' to compute the base-10 logarithm, then use the "hyperbolic" exponential as a cheap hack to recover the number 1000, then use `B' again to compute the natural logarithm. Note that `P' with the hyperbolic prefix pushes the constant `e' onto the stack. You may have noticed that both times we took the base-10 logarithm of 1000, we got an exact integer result. Calc always tries to give an exact rational result for calculations involving rational numbers where possible. But when we used `H E', the result was a floating-point number for no apparent reason. In fact, if we had computed `10 RET 3 ^' we *would* have gotten an exact integer 1000. But the `H E' command is rigged to generate a floating-point result all of the time so that `1000 H E' will not waste time computing a thousand-digit integer when all you probably wanted was `1e1000'. (*) *Exercise 2.* Find a pair of integer inputs to the `B' command for which Calc could find an exact rational result but doesn't. *Note 2: Arithmetic Answer 2. (*) The Calculator also has a set of functions relating to combinatorics and statistics. You may be familiar with the "factorial" function, which computes the product of all the integers up to a given number. 1: 100 1: 93326215443... 1: 100. 1: 9.3326e157 . . . . 100 ! U c f ! Recall, the `c f' command converts the integer or fraction at the top of the stack to floating-point format. If you take the factorial of a floating-point number, you get a floating-point result accurate to the current precision. But if you give `!' an exact integer, you get an exact integer result (158 digits long in this case). If you take the factorial of a non-integer, Calc uses a generalized factorial function defined in terms of Euler's Gamma function `gamma(n)' (which is itself available as the `f g' command). 3: 4. 3: 24. 1: 5.5 1: 52.342777847 2: 4.5 2: 52.3427777847 . . 1: 5. 1: 120. . . M-3 ! M-0 DEL 5.5 f g Here we verify the identity `N! = gamma(N+1)'. The binomial coefficient N-choose-M is defined by `n! / m! (n-m)!' for all reals `n' and `m'. The intermediate results in this formula can become quite large even if the final result is small; the `k c' command computes a binomial coefficient in a way that avoids large intermediate values. The `k' prefix key defines several common functions out of combinatorics and number theory. Here we compute the binomial coefficient 30-choose-20, then determine its prime factorization. 2: 30 1: 30045015 1: [3, 3, 5, 7, 11, 13, 23, 29] 1: 20 . . . 30 RET 20 k c k f You can verify these prime factors by using `v u' to "unpack" this vector into 8 separate stack entries, then `M-8 *' to multiply them back together. The result is the original number, 30045015. Suppose a program you are writing needs a hash table with at least 10000 entries. It's best to use a prime number as the actual size of a hash table. Calc can compute the next prime number after 10000: 1: 10000 1: 10007 1: 9973 . . . 10000 k n I k n Just for kicks we've also computed the next prime *less* than 10000. *Note Financial Functions::, for a description of the Calculator commands that deal with business and financial calculations (functions like `pv', `rate', and `sln'). *Note Binary Functions::, to read about the commands for operating on binary numbers (like `and', `xor', and `lsh'). File: calc.info, Node: Vector/Matrix Tutorial, Next: Types Tutorial, Prev: Arithmetic Tutorial, Up: Tutorial Vector/Matrix Tutorial ====================== A "vector" is a list of numbers or other Calc data objects. Calc provides a large set of commands that operate on vectors. Some are familiar operations from vector analysis. Others simply treat a vector as a list of objects. * Menu: * Vector Analysis Tutorial:: * Matrix Tutorial:: * List Tutorial:: File: calc.info, Node: Vector Analysis Tutorial, Next: Matrix Tutorial, Prev: Vector/Matrix Tutorial, Up: Vector/Matrix Tutorial Vector Analysis --------------- If you add two vectors, the result is a vector of the sums of the elements, taken pairwise. 1: [1, 2, 3] 2: [1, 2, 3] 1: [8, 8, 3] . 1: [7, 6, 0] . . [1,2,3] s 1 [7 6 0] s 2 + Note that we can separate the vector elements with either commas or spaces. This is true whether we are using incomplete vectors or algebraic entry. The `s 1' and `s 2' commands save these vectors so we can easily reuse them later. If you multiply two vectors, the result is the sum of the products of the elements taken pairwise. This is called the "dot product" of the vectors. 2: [1, 2, 3] 1: 19 1: [7, 6, 0] . . r 1 r 2 * The dot product of two vectors is equal to the product of their lengths times the cosine of the angle between them. (Here the vector is interpreted as a line from the origin `(0,0,0)' to the specified point in three-dimensional space.) The `A' (absolute value) command can be used to compute the length of a vector. 3: 19 3: 19 1: 0.550782 1: 56.579 2: [1, 2, 3] 2: 3.741657 . . 1: [7, 6, 0] 1: 9.219544 . . M-RET M-2 A * / I C First we recall the arguments to the dot product command, then we compute the absolute values of the top two stack entries to obtain the lengths of the vectors, then we divide the dot product by the product of the lengths to get the cosine of the angle. The inverse cosine finds that the angle between the vectors is about 56 degrees. The "cross product" of two vectors is a vector whose length is the product of the lengths of the inputs times the sine of the angle between them, and whose direction is perpendicular to both input vectors. Unlike the dot product, the cross product is defined only for three-dimensional vectors. Let's double-check our computation of the angle using the cross product. 2: [1, 2, 3] 3: [-18, 21, -8] 1: [-0.52, 0.61, -0.23] 1: 56.579 1: [7, 6, 0] 2: [1, 2, 3] . . . 1: [7, 6, 0] . r 1 r 2 V C s 3 M-RET M-2 A * / A I S First we recall the original vectors and compute their cross product, which we also store for later reference. Now we divide the vector by the product of the lengths of the original vectors. The length of this vector should be the sine of the angle; sure enough, it is! Vector-related commands generally begin with the `v' prefix key. Some are uppercase letters and some are lowercase. To make it easier to type these commands, the shift-`V' prefix key acts the same as the `v' key. (*Note General Mode Commands::, for a way to make all prefix keys have this property.) If we take the dot product of two perpendicular vectors we expect to get zero, since the cosine of 90 degrees is zero. Let's check that the cross product is indeed perpendicular to both inputs: 2: [1, 2, 3] 1: 0 2: [7, 6, 0] 1: 0 1: [-18, 21, -8] . 1: [-18, 21, -8] . . . r 1 r 3 * DEL r 2 r 3 * (*) *Exercise 1.* Given a vector on the top of the stack, what keystrokes would you use to "normalize" the vector, i.e., to reduce its length to one without changing its direction? *Note 1: Vector Answer 1. (*) (*) *Exercise 2.* Suppose a certain particle can be at any of several positions along a ruler. You have a list of those positions in the form of a vector, and another list of the probabilities for the particle to be at the corresponding positions. Find the average position of the particle. *Note 2: Vector Answer 2. (*) File: calc.info, Node: Matrix Tutorial, Next: List Tutorial, Prev: Vector Analysis Tutorial, Up: Vector/Matrix Tutorial Matrices -------- A "matrix" is just a vector of vectors, all the same length. This means you can enter a matrix using nested brackets. You can also use the semicolon character to enter a matrix. We'll show both methods here: 1: [ [ 1, 2, 3 ] 1: [ [ 1, 2, 3 ] [ 4, 5, 6 ] ] [ 4, 5, 6 ] ] . . [[1 2 3] [4 5 6]] ' [1 2 3; 4 5 6] RET We'll be using this matrix again, so type `s 4' to save it now. Note that semicolons work with incomplete vectors, but they work better in algebraic entry. That's why we use the apostrophe in the second example. When two matrices are multiplied, the lefthand matrix must have the same number of columns as the righthand matrix has rows. Row `i', column `j' of the result is effectively the dot product of row `i' of the left matrix by column `j' of the right matrix. If we try to duplicate this matrix and multiply it by itself, the dimensions are wrong and the multiplication cannot take place: 1: [ [ 1, 2, 3 ] * [ [ 1, 2, 3 ] [ 4, 5, 6 ] ] [ 4, 5, 6 ] ] . RET * Though rather hard to read, this is a formula which shows the product of two matrices. The `*' function, having invalid arguments, has been left in symbolic form. We can multiply the matrices if we "transpose" one of them first. 2: [ [ 1, 2, 3 ] 1: [ [ 14, 32 ] 1: [ [ 17, 22, 27 ] [ 4, 5, 6 ] ] [ 32, 77 ] ] [ 22, 29, 36 ] 1: [ [ 1, 4 ] . [ 27, 36, 45 ] ] [ 2, 5 ] . [ 3, 6 ] ] . U v t * U TAB * Matrix multiplication is not commutative; indeed, switching the order of the operands can even change the dimensions of the result matrix, as happened here! If you multiply a plain vector by a matrix, it is treated as a single row or column depending on which side of the matrix it is on. The result is a plain vector which should also be interpreted as a row or column as appropriate. 2: [ [ 1, 2, 3 ] 1: [14, 32] [ 4, 5, 6 ] ] . 1: [1, 2, 3] . r 4 r 1 * Multiplying in the other order wouldn't work because the number of rows in the matrix is different from the number of elements in the vector. (*) *Exercise 1.* Use `*' to sum along the rows of the above 2x3 matrix to get `[6, 15]'. Now use `*' to sum along the columns to get `[5, 7, 9]'. *Note 1: Matrix Answer 1. (*) An "identity matrix" is a square matrix with ones along the diagonal and zeros elsewhere. It has the property that multiplication by an identity matrix, on the left or on the right, always produces the original matrix. 1: [ [ 1, 2, 3 ] 2: [ [ 1, 2, 3 ] 1: [ [ 1, 2, 3 ] [ 4, 5, 6 ] ] [ 4, 5, 6 ] ] [ 4, 5, 6 ] ] . 1: [ [ 1, 0, 0 ] . [ 0, 1, 0 ] [ 0, 0, 1 ] ] . r 4 v i 3 RET * If a matrix is square, it is often possible to find its "inverse", that is, a matrix which, when multiplied by the original matrix, yields an identity matrix. The `&' (reciprocal) key also computes the inverse of a matrix. 1: [ [ 1, 2, 3 ] 1: [ [ -2.4, 1.2, -0.2 ] [ 4, 5, 6 ] [ 2.8, -1.4, 0.4 ] [ 7, 6, 0 ] ] [ -0.73333, 0.53333, -0.2 ] ] . . r 4 r 2 | s 5 & The vertical bar `|' "concatenates" numbers, vectors, and matrices together. Here we have used it to add a new row onto our matrix to make it square. We can multiply these two matrices in either order to get an identity. 1: [ [ 1., 0., 0. ] 1: [ [ 1., 0., 0. ] [ 0., 1., 0. ] [ 0., 1., 0. ] [ 0., 0., 1. ] ] [ 0., 0., 1. ] ] . . M-RET * U TAB * Matrix inverses are related to systems of linear equations in algebra. Suppose we had the following set of equations: a + 2b + 3c = 6 4a + 5b + 6c = 2 7a + 6b = 3 This can be cast into the matrix equation, [ [ 1, 2, 3 ] [ [ a ] [ [ 6 ] [ 4, 5, 6 ] * [ b ] = [ 2 ] [ 7, 6, 0 ] ] [ c ] ] [ 3 ] ] We can solve this system of equations by multiplying both sides by the inverse of the matrix. Calc can do this all in one step: 2: [6, 2, 3] 1: [-12.6, 15.2, -3.93333] 1: [ [ 1, 2, 3 ] . [ 4, 5, 6 ] [ 7, 6, 0 ] ] . [6,2,3] r 5 / The result is the `[a, b, c]' vector that solves the equations. (Dividing by a square matrix is equivalent to multiplying by its inverse.) Let's verify this solution: 2: [ [ 1, 2, 3 ] 1: [6., 2., 3.] [ 4, 5, 6 ] . [ 7, 6, 0 ] ] 1: [-12.6, 15.2, -3.93333] . r 5 TAB * Note that we had to be careful about the order in which we multiplied the matrix and vector. If we multiplied in the other order, Calc would assume the vector was a row vector in order to make the dimensions come out right, and the answer would be incorrect. If you don't feel safe letting Calc take either interpretation of your vectors, use explicit Nx1 or 1xN matrices instead. In this case, you would enter the original column vector as `[[6], [2], [3]]' or `[6; 2; 3]'. (*) *Exercise 2.* Algebraic entry allows you to make vectors and matrices that include variables. Solve the following system of equations to get expressions for `x' and `y' in terms of `a' and `b'. x + a y = 6 x + b y = 10 *Note 2: Matrix Answer 2. (*) (*) *Exercise 3.* A system of equations is "over-determined" if it has more equations than variables. It is often the case that there are no values for the variables that will satisfy all the equations at once, but it is still useful to find a set of values which "nearly" satisfy all the equations. In terms of matrix equations, you can't solve `A X = B' directly because the matrix `A' is not square for an over-determined system. Matrix inversion works only for square matrices. One common trick is to multiply both sides on the left by the transpose of `A': `trn(A)*A*X = trn(A)*B'. Now `trn(A)*A' is a square matrix so a solution is possible. It turns out that the `X' vector you compute in this way will be a "least-squares" solution, which can be regarded as the "closest" solution to the set of equations. Use Calc to solve the following over-determined system: a + 2b + 3c = 6 4a + 5b + 6c = 2 7a + 6b = 3 2a + 4b + 6c = 11 *Note 3: Matrix Answer 3. (*)