www.delorie.com/gnu/docs/calc/calc_21.html   search  
 
Buy the book!


GNU Emacs Calc 2.02 Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.2 Algebraic-Style Calculations

If you are not used to RPN notation, you may prefer to operate the Calculator in "algebraic mode," which is closer to the way non-RPN calculators work. In algebraic mode, you enter formulas in traditional 2+3 notation.

You don't really need any special "mode" to enter algebraic formulas. You can enter a formula at any time by pressing the apostrophe (') key. Answer the prompt with the desired formula, then press RET. The formula is evaluated and the result is pushed onto the RPN stack. If you don't want to think in RPN at all, you can enter your whole computation as a formula, read the result from the stack, then press DEL to delete it from the stack.

Try pressing the apostrophe key, then 2+3+4, then RET. The result should be the number 9.

Algebraic formulas use the operators `+', `-', `*', `/', and `^'. You can use parentheses to make the order of evaluation clear. In the absence of parentheses, `^' is evaluated first, then `*', then `/', then finally `+' and `-'. For example, the expression

 
2 + 3*4*5 / 6*7^8 - 9

is equivalent to

 
2 + ((3*4*5) / (6*(7^8)) - 9

or, in large mathematical notation,

 
    3 * 4 * 5
2 + --------- - 9
          8
     6 * 7

The result of this expression will be the number -6.99999826533.

Calc's order of evaluation is the same as for most computer languages, except that `*' binds more strongly than `/', as the above example shows. As in normal mathematical notation, the `*' symbol can often be omitted: `2 a' is the same as `2*a'.

Operators at the same level are evaluated from left to right, except that `^' is evaluated from right to left. Thus, `2-3-4' is equivalent to `(2-3)-4' or -5, whereas `2^3^4' is equivalent to `2^(3^4)' (a very large integer; try it!).

If you tire of typing the apostrophe all the time, there is an "algebraic mode" you can select in which Calc automatically senses when you are about to type an algebraic expression. To enter this mode, press the two letters m a. (An `Alg' indicator should appear in the Calc window's mode line.)

Press m a, then 2+3+4 with no apostrophe, then RET.

In algebraic mode, when you press any key that would normally begin entering a number (such as a digit, a decimal point, or the _ key), or if you press ( or [, Calc automatically begins an algebraic entry.

Functions which do not have operator symbols like `+' and `*' must be entered in formulas using function-call notation. For example, the function name corresponding to the square-root key Q is sqrt. To compute a square root in a formula, you would use the notation `sqrt(x)'.

Press the apostrophe, then type sqrt(5*2) - 3. The result should be 0.16227766017.

Note that if the formula begins with a function name, you need to use the apostrophe even if you are in algebraic mode. If you type arcsin out of the blue, the a r will be taken as an Algebraic Rewrite command, and the csin will be taken as the name of the rewrite rule to use!

Some people prefer to enter complex numbers and vectors in algebraic form because they find RPN entry with incomplete objects to be too distracting, even though they otherwise use Calc as an RPN calculator.

Still in algebraic mode, type:

 
1:  (2, 3)     2:  (2, 3)     1:  (8, -1)    2:  (8, -1)    1:  (9, -1)
    .          1:  (1, -2)        .          1:  1              .
                   .                             .

 (2,3) RET      (1,-2) RET        *              1 RET          +

Algebraic mode allows us to enter complex numbers without pressing an apostrophe first, but it also means we need to press RET after every entry, even for a simple number like 1.

(You can type C-u m a to enable a special "incomplete algebraic mode" in which the ( and [ keys use algebraic entry even though regular numeric keys still use RPN numeric entry. There is also a "total algebraic mode," started by typing m t, in which all normal keys begin algebraic entry. You must then use the META key to type Calc commands: M-m t to get back out of total algebraic mode, M-q to quit, etc. Total algebraic mode is not supported under Emacs 19.)

If you're still in algebraic mode, press m a again to turn it off.

Actual non-RPN calculators use a mixture of algebraic and RPN styles. In general, operators of two numbers (like + and *) use algebraic form, but operators of one number (like n and Q) use RPN form. Also, a non-RPN calculator allows you to see the intermediate results of a calculation as you go along. You can accomplish this in Calc by performing your calculation as a series of algebraic entries, using the $ sign to tie them together. In an algebraic formula, $ represents the number on the top of the stack. Here, we perform the calculation sqrt(2*4+1), which on a traditional calculator would be done by pressing 2 * 4 + 1 = and then the square-root key.

 
1:  8          1:  9          1:  3
    .              .              .

  ' 2*4 RET        $+1 RET        Q

Notice that we didn't need to press an apostrophe for the $+1, because the dollar sign always begins an algebraic entry.

(*) Exercise 1. How could you get the same effect as pressing Q but using an algebraic entry instead? How about if the Q key on your keyboard were broken? See section 1. (*)

The notations $$, $$$, and so on stand for higher stack entries. For example, ' $$+$ RET is just like typing +.

Algebraic formulas can include variables. To store in a variable, press s s, then type the variable name, then press RET. (There are actually two flavors of store command: s s stores a number in a variable but also leaves the number on the stack, while s t removes a number from the stack and stores it in the variable.) A variable name should consist of one or more letters or digits, beginning with a letter.

 
1:  17             .          1:  a + a^2    1:  306
    .                             .              .

    17          s t a RET      ' a+a^2 RET       =

The = key evaluates a formula by replacing all its variables by the values that were stored in them.

For RPN calculations, you can recall a variable's value on the stack either by entering its name as a formula and pressing =, or by using the s r command.

 
1:  17         2:  17         3:  17         2:  17         1:  306
    .          1:  17         2:  17         1:  289            .
                   .          1:  2              .
                                  .

  s r a RET     ' a RET =         2              ^              +

If you press a single digit for a variable name (as in s t 3, you get one of ten quick variables q0 through q9. They are "quick" simply because you don't have to type the letter q or the RET after their names. In fact, you can type simply s 3 as a shorthand for s s 3, and likewise for t 3 and r 3.

Any variables in an algebraic formula for which you have not stored values are left alone, even when you evaluate the formula.

 
1:  2 a + 2 b     1:  34 + 2 b
    .                 .

 ' 2a+2b RET          =

Calls to function names which are undefined in Calc are also left alone, as are calls for which the value is undefined.

 
1:  2 + log10(0) + log10(x) + log10(5, 6) + foo(3)
    .

 ' log10(100) + log10(0) + log10(x) + log10(5,6) + foo(3) RET

In this example, the first call to log10 works, but the other calls are not evaluated. In the second call, the logarithm is undefined for that value of the argument; in the third, the argument is symbolic, and in the fourth, there are too many arguments. In the fifth case, there is no function called foo. You will see a "Wrong number of arguments" message referring to `log10(5,6)'. Press the w ("why") key to see any other messages that may have arisen from the last calculation. In this case you will get "logarithm of zero," then "number expected: x". Calc automatically displays the first message only if the message is sufficiently important; for example, Calc considers "wrong number of arguments" and "logarithm of zero" to be important enough to report automatically, while a message like "number expected: x" will only show up if you explicitly press the w key.

(*) Exercise 2. Joe entered the formula `2 x y', stored 5 in x, pressed =, and got the expected result, `10 y'. He then tried the same for the formula `2 x (1+y)', expecting `10 (1+y)', but it didn't work. Why not? See section 2. (*)

(*) Exercise 3. What result would you expect 1 RET 0 / to give? What if you then type 0 *? See section 3. (*)

One interesting way to work with variables is to use the evaluates-to (`=>') operator. It works like this: Enter a formula algebraically in the usual way, but follow the formula with an `=>' symbol. (There is also an s = command which builds an `=>' formula using the stack.) On the stack, you will see two copies of the formula with an `=>' between them. The lefthand formula is exactly like you typed it; the righthand formula has been evaluated as if by typing =.

 
2:  2 + 3 => 5                     2:  2 + 3 => 5
1:  2 a + 2 b => 34 + 2 b          1:  2 a + 2 b => 20 + 2 b
    .                                  .

' 2+3 => RET  ' 2a+2b RET s =          10 s t a RET

Notice that the instant we stored a new value in a, all `=>' operators already on the stack that referred to a were updated to use the new value. With `=>', you can push a set of formulas on the stack, then change the variables experimentally to see the effects on the formulas' values.

You can also "unstore" a variable when you are through with it:

 
2:  2 + 5 => 5
1:  2 a + 2 b => 2 a + 2 b
    .

    s u a RET

We will encounter formulas involving variables and functions again when we discuss the algebra and calculus features of the Calculator.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003