Next: Arrays and Matrices in Up: Calculators Previous: Starting Calc

## Simple Calc Operations

• The normal mode brings Calc in a window that takes over the bottom portion of a frame.
• The Calc window itself is split in vertically:
--- Emacs Calculator Mode ---                   |Emacs Calc Mode v2.00...
2:  17.3                                        |    17.3
1:  -5                                          |    3
.                                           |    2
|    4
|  * 8
|  ->-5
|
--%%-Calc: 12 Deg       (Calculator)----All----- --%%-Emacs: *Calc Trail*

• The lefthand window is the stack window .
• The righthand window is the trail window . It is like a record printed on a paper tape.
• Calc commands are typed in the minibuffer: Calc puts you in the minibuffer automatically whenever you type something in the Calc window.
• The default notation is the Reverse Polish  (after Jan ukasiewicz).
• Example:
• Type 2<ret>2<ret>+
• Observe that as you have typed + the two 2s visible on the stack become replaced with a 4.
• Type Q to extract
In order to illustrate how to call various Calc function, I will use the following notation:
2<ret>2<ret>+
1: 4

The top line is what you type, in this case 2 followed by return then 2 and return again, and finally a +. As you you type it all in, at the end Calc will respond with 1: 4. The final answer is always the number 1 item on the stack, with other objects pushed down to positions, 2, 3, 4, and so on. Most often Calc functions take one or two top objects from the stack and replace them both with and answer. That way the stack doesn't grow too much.
• Some elementary functions:
Q
, e.g.,
2<ret>Q
1: 1.41421356237

S
, e.g.,
90<ret>S
1: 1

Observe that Calc measures angles in degrees, not in radians, by default. You can switch to radians  as follows:
mrP2<ret>/S
1: 1.

What happens here is as follows:
mr
switches the angle mode to radians
P
puts on the stack: this happens the moment you type P, no need to follow it with return
2<ret>
puts 2 on the stack, as before
/
divides by 2 leaving 1.5707963268 on the stack
S
evaluates
If you find it hard to remember the key-strokes, you can use a normal algebraic notation . First type:

This is the same as to type mr. Now type an apostrophy, followed by the formula:
'sin(pi/2)
1: sin(pi / 2)

Calc took your algebraic formula, and placed it on the stack without evaluating it! To get the number type:
N
1: 1.

Observe the difference:
M-x calc-degrees-mode
'sin(90)
1: 1

Here I have switched again to the degrees mode. Instead of typing M-x calc-degrees-mode you can accomplish the same by typing md. This time Calc not only evaluates immediately. It returns an integer 1 instead of a floating point number 1.. Can you explain why?
C
, e.g.,
90<ret>C
1: 0
'cos(90)
1: 0
'cos(pi/2)
1: cos(pi / 2)
M-x calc-eval-num
1: 0.999624216859
'cos(pi/2)
1: cos(pi / 2)
N
1: 0.999624216859

Capital N, as in Mathematica, forces a numerical evaluation  of a  symbolic expression. It is equivalent to calling Calc function calc-eval-num.
T
, e.g.,
45<ret>T
1: 1.
90<ret>T
1: tan(90)
Division by zero: 1.

In the second operation Calc flagged a division by zero. Indeed . But you can tell Calc  to accept infinities, by switching to the infinite mode. You do that by typing
mi

or
M-x calc-infinite-mode

Observe the following:
mi
'tan(90)
1: uinf

uinf means undirected infinity, in other  words, . You can also get either a positive or a negative infinity. Compare:
'ln(0)
1: -inf
'exp(inf)
1: inf
'gamma(-7)
1: uinf

E
, e.g.,
1<ret>E
1: 2.71828182846
'exp(1)
1: 2.71828182846
'exp(-inf)
1: 0

This is very impressive. This is more than Maxima can do!
L
, e.g.,
1<ret>L
1: 0
'ln(1)
1: 0

H
a prefix for  hyperbolic functions, e.g., HS returns . Try the following
1<ret>HS
1: 1.17520119364
'sinh(1)
1: 1.17520119364

I
a prefix for inverse  functions, e.g., IS returns . Try the following
90<ret>S
1: 1
IS
1: 90
'arcsin(1)
1: 90

!
factorial, e.g.,
15<ret>!
1: 1307674368000

In this case you can also do:
15!<ret>
1: 1307674368000

Do you remember my comment that . You can check it now as follows:
'gamma(16)
1: 1307674368000

^
exponentiation, e.g.,
2<ret>2<ret>^
1: 4
2<ret>3.7<ret>^
1: 12.9960383417
'2^3.7
1: 12.9960383417
'2^-3.7
1: 0.0769465258341

How can you input  a negative number in a stack mode? The moment you type a minus sign, Calc attempts a subtraction! You can use either n or _ for that. n toggles the sign of the last number on the stack. _ is used to enter a negative number, e.g.,
2<ret>_3.7<ret>^
1: 0.0769465258341
n
1: -0.0769465258341

• Some other supported functions:
• Base-10 logarithms, any base  logarithms, e.g.,
10<ret>HL
1: 1
'log10(10)
1: 1

As you see, the key-stroke combination for is hyperbolic logarithm'', i.e., HL. For a logarithm of any base use B, or 'log. This function takes two arguments off the stack, with the second one being the base:
15<ret>15<ret>B
1: 1
'log(15, 15)
1: 1
'log(100, 10)
1: 2
100<ret>10<ret>B
1: 2

• Euler  gamma, incomplete gammas P(a, x) and Q(a, x) = 1 - P(a, x), and . We have already met with above, when we have compared it with factorial. The incomplete gamma functions in Calc are called gammaP and gammaQ. They have key-stroke abbreviations, which are as follows:
fg
gamma,
fG
gammaP,
IfG
gammaQ,
HfG
gammag,
HIfG
gammaG,
When it comes to this level of key-stroke complexity my personal preference is to resort to an algebraic entry, and simply type 'gammaG instead of HIfG, but, the point is that if you use these functions very often in your work sooner or later you'll master the key-stroke entry. Here's a quick demo:
'gamma(5)
1: 24
'gammag(5, 7)
1: 19.8482014108
'gammaG(5, 7)
1: 4.15179858917
+
1: 24.
'gammaP(5, 7)
1: 0.827008392118
'gammaQ(5, 7)
1: 0.172991607882
+
1: 1.

• Euler  beta, and incomplete beta function: this function is defined by the following integral:

Consider 1/B(z, w) for integer values of z and w such that z + w = n + 1, and z = k+1 (then, of course, w = n - k. Then

In summary, as can be tought of as a continuous relative of a factorial, the Euler beta function can be thought of as a continuous relative of the Newton symbol . To illustrate this point let's evaluate the Euler beta function for z = 3 and w = 4. Then n = 3 + 4 - 1 = 6, and k = 3 - 1 = 2, and we should have

Here is how this works in Calc.
'(6 - 2) * choose(6, 2)<ret>
1: 60
'1/beta(3, 4)<ret>
1: 59.9999999999

You can also use the stack notation and key-strokes perform both calculations. Key-strokes for choose and beta are kc and fb respectively:
6<ret>2<ret>-6<ret>2<ret>kc*
1: 60
1<ret>3<ret>4<ret>fb/
1: 59.9999999999

Calc can evaluate the incomplete beta function too. Its definition is

and its Calc representation is betaI which is invoked by the fB key-stroke combination. We also have the un-normalised version:

represented by betaB and by the HfB keystroke combination. Here is an example:
'betaI(0.7, 3, 4)
1: 0.92953
'betaB(0.7, 3, 4)
1: 0.0154921666667
'betaB(0.7, 3, 4) / beta(3, 4)
1: 0.92953
'1/betaB(1, 3, 4)
1: 59.9999999999

The Euler beta function is associated with some trigonometric integrals. For example:

This formula works for any values of and , e.g.,

• The error function

and the complementary error function . These are encountered in one dimensional diffusion problems, e.g., in modeling of transistors and diodes. They are special cases of the incomplete gamma function:

They are also used to assess the significance of statistical correlations and when using Fisher's z-transformation, an operation that lets us quantify whether a change in some control variable significantly alters an existing correlation between two other variables.

Calc notation for the error functions is erf (fe) and erfc (Ife). Observe the following features of error functions:

M-x calc-infinite-mode
'erf(inf)
1: 1.
'erf(0)
1: 0
'erfc(inf)
1: 0.
'erfc(0)
1: 1
'erf(-1)
1: -0.842700792945
'erf(1)
1: 0.842700792945
'erfc(-1)
1: 1.84270079295
'2 - erfc(1)
1: 1.84270079295

• Bessel  functions of the first, i.e., J, and second, i.e., Y, kind. They are given by the following formulae:

where is any real number and z is any complex number. The Y Bessel functions are defined for non integer values of only, because for all integer values .

How to input  complex numbers into Calc? Simply type in a pair:

(2,7)
1: (2, 7)
(3,4)
1: (3, 4)
-
1: (-1, 3)

Note: when you input complex numbers in the stack mode, you must not type a space between the comma and the imaginary part. You do not have to type a return at the end of your input either. Calc will place the whole complex number on the stack, the moment you type the closing bracket.

Using algebraic notation you can do as follows:

'(2, 7) - (3, 4)
1: (-1, 3)

You can also input  complex numbers in a polar mode. To switch to the polar mode issue the command:

M-x calc-polar-mode

or type
mp

This is a toggle, so to get out of the polar mode, simply type mp again.

Back to the Bessel functions. They are invoked by typing 'besJ(n,x) or fj and 'besY(n,x) or fy. For example:

'besJ(2, 3.5)
1: 0.45862918
'besJ(2, (3.5, 0))
1: 0.45862918
'besJ(2, (3.5, 1))
1: (0.60582898, -0.1362245)

Observe the following relationships:
'(2 * 3 / 3.5) * besJ(3, 3.5) - besJ(2, 3.5)
1: 0.204405294287
'besJ(4, 3.5)
1: 0.20440529
'(2 * 1.5 / 3.5) * besY(1.5, 3.5) - besY(0.5, 3.5)
1: -0.173345324286
'besY(2.5, 3.5)
1: -0.17334532

As an exercise verify with Calc the following approximate formulae that hold in the regime

Bessel functions appear whenever standing oscillation or eigen-value (much the same) problems are solved in spherical coordinates.

• Random number  generator: Calc uses a sophisticated random number generator derived  from Emacs' built-in function random. You can see the latter in action as follows:
1.
switch to the *scratch* buffer by typing C-x b *scratch*
2.
in the *scratch* buffer type
(random)

3.
position your cursor just after the closing bracket and press C-j
4.
repeat several times
But Calc uses it in an elaborate fashion, testing for range, seeding it automatically, normalising, shuffling, etc. The simplest way to generate a random number between 0 and N (exclusive) is to put N on the stack and then type kr:
20<ret>kr
1: 6

To continue the generation or random numbers with the same top integer, type ka:
ka
1: 5
ka
1: 13
ka
1: 12
ka
1: 9

The key-binding kr stands for 'random(n), e.g.,
'random(20)
1: 18

If the number on the stack is a floating point number, kr will return a random floating point number between that number and 0:
1.73<ret>kr
1: 1.67106802028
ka
1: 0.206379750199
ka
1: 1.19060203945

If the number on the stack is 0, kr will return a random number with a Gaussian distribution with a mean of 0 and a standard deviation of 1:
0<ret>kr
1: 1.70864920311
ka
1: 1.07740857088
ka
1: 0.0891803626409
ka
1: 1.16205405156
ka
1: 1.20374162505

If the number on the stack is given in the form of a mean with an error, i.e., , then kr and ka generate random numbers with a Gaussian distribution around the mean, e.g., 7, and with the standard deviation as given by , e.g., 2, in this case:
7p2<ret>
1: 7 +/- 2
kr
1: 6.27794656568
ka
1: 7.28422903876
ka
1: 5.35137211447

The argument on the stack can be also an interval, e.g., [3.7..7.4] or [3.7..7.4). The former includes the upper limit, whereas the latter exludes it. For floating point integrals, they are divided into one million chunks and kr returns one of them at random. The borders are included or excluded as specified. For example:
[3.7..7.4]
1: [3.7 .. 7.4]
kr
1: 5.93551484212
ka
1: 5.65907211985
ka
1: 7.26496077254

For integer borders the result is an integer too:
[3..7]
1: [3 .. 7]
kr
1: 4
ka
1: 6
ka
1: 7
ka
1: 5
ka
1: 4

If the argument to kr is a vector, then the result is one element of the vector taken from it at random:
[7,3,5,11,24,2]
1: [7, 3, 5, 11, 24, 2]
kr
1: 24
ka
1: 7
ka
1: 5

You can also use kr with algebraic vectors:
M-x calc-algebraic-mode
[a,b,c,d,e,f,g,h]
1: [a, b, c, d, e, f, g, h]
kr
1: g
ka
1: c
ka
1: g
ka
1: a

Another useful random function is shuffle, which is invoked by the kh sequence. Try the following while still in the algebraic mode (remember that ma is a toggle):
'shuffle(5, [a,b,c,d,e,f,g,h,i,j])
1: [j, b, a, f, h]
4
1: 4
kh
1: [b, h, a, j]

• Combinatorics: In this area Calc provides a large number of utilities, some of which are not exactly combinatorics''.
• greatest  common divisor: the function is called gcd and its key-binding is kg. That's how it works:
'gcd(772435, 978985)
1: 5
772435<ret>978985<ret>kg
1: 5

• least  common multiple: the function here is lcm, and the key-binding is kl:
'lcm(26, 34)
1: 442
'gcd(26, 34)
1: 2
'2*442
1: 884
'26*34
1: 884

• A variant on gcd is the extended gcd. It returns  a vector of 3 numbers [g, a, b] such that

g = gcd (x, y) = a x + b y

Here's how it works:
'egcd(26, 34)
1: [2, 4, -3]
'4 * 26 - 3 * 34
1: 2

• Next we have a factorial, which we've already seen while  discussing the Euler gamma function, and the so called double factorial. A double factorial is a product of every second integer in a sequence that terminates with a given integer. For example:

You can enter the double factorial in the algebraic mode using !!, or in a stack mode using kd. You cannot use !! in in the stack mode. That would evaluate (n!)! which would usually be an insanely large number. The function name that stands for the double factorial is dfact:
'8!!
1: 384
'7!!
1: 105

• Newton  symbol : We have already seen the Newton symbol in action when discussing the Euler Beta function. So here we merely restate that the Calc function that corresponds to it is choose, and the corresponding key-binding is kc.
• Then we have a function that is somewhat incorrectly called the number of permutations function. It stands for

The naming is somewhat incorrect, because it is k! alone that tells us in how many ways we can rearrange a set of k objects. The meaning of the above is different. First means a number of ways in which we can select k elements from an n elements set, and then if you multiply it by k! you tell additionally in how many ways altogether those subsets can be rearranged.

Be it as it may, Calc function for that thing is perm and its key-binding is Hkc:

'choose(7,3)
1: 35
'perm(7,3)
1: 210
'3!*choose(7,3)
1: 210

• Bernoulli  number and polynomial : these are given by the following formulae:

Another way to calculate them is to expand the following expression on the left hand side and match it against the right hand side

With the exception of B1 all other odd Bernoulli numbers vanish, which is why in some books . Bernoulli numbers are used in numerical integration  (Euler-Maclaurin Summation Formula). Here's how they work in Calc:
'bern(0)
1: 1
'bern(1)
1: -1:2
'bern(2)
1: 1:6
'bern(3)
1: 0
'bern(4)
1: -1:30

Observe that Calc returns Bk using exact fraction notation. The key-binding for Bk is kb. If you type Hkb you get Bernoulli polynomial for that k:
8<ret>kb
1: -1:30
8<ret>'x<ret>Hkb
1: x^8 - 4 x^7 + 14:3 x^6 - 7:3 x^4 + 2:3 x^2  - 1:30

• Euler  number and   polynomial: these are given by

All odd Euler numbers vanish, which is why in some books, and in Calc they are renumbered as follows:

Euler numbers are generated in Calc by function euler, key-binding ke, whereas Hke computes the Euler polynomial:
8<ret>ke
1: 1385
6<ret>'x<ret>Hke
1: 0.999999999999 x^6 - 3. x^5 + 5. x^3 - 3.00000000003 x - 1.3e-11

• Stirling   number: there are two of those. The first, invoked by stir1 or ks, calculates the number of k-cycle permutations of n objects. For example, the number of 2-cycle permutations of 4 objects is 11:
'stir1(4,2)
1: 11

The second one, invoked by stir2 or Hks, calculates the number of ways to partition n objects into k non-empty sets, e.g., a 4 element set can be divided in 7 ways into two non-empty sets:
'stir2(4,2)
1: 7

• prime  test, next prime, previous prime, prime factorization: for example
7<ret>kp
1: 7 [Prime (guaranteed)]

Here the messages Prime (guaranteed) appears in the mini-buffer and in the print-out window. The function invoked by the key-stroke kb invokes a prime test. A test can be also carried out by calling prime:
'prime(7000235)
1: 0
'prime(7)
1: 1

where 0 stands for false and 1 stands for true. An integer can be factorized into primes by calling function prfac, key-binding kf:
'prfac(7000235)
1: [5, 11, 123277]

Successive primes can be searched for by calling nextprime, key-binding: kn, or prevprime, key-binding: Ikn:
'prevprime(127276)
1: 127271
'nextprime(127276)
1: 127277

• Euler  totient function: calculates the number of integers less than n, which are relatively prime to n, i.e., don't divide it:
'totient(16)
1: 8
'totient(17)
1: 16
'totient(384)
1: 128

• Moebius  function: invoked by moebius or by km tells us if its argument has any duplicate prime factors (in which case it returns 0) or if it is a product of k distinct prime factors, in which case -1k is returned:
'7*7<ret>
1: 49
km
0
'3*5*7<ret>
1: 105
km
1: -1

• Probability  distributions: Here Calc provides the upper and lower tail versions of continuous distributions, i.e., integrals from x to , or from to x. For discrete, e.g., binomial, distributions, sums replace integrals, with the upper sum being inclusive (of x) and the lower sum exclusive.
• binomial: The   probability that an event will occur k or more times out of n trials, if its probability of occurring in any given trial is p:

For example the probability of throwing tails in a single trial is 1/2. The probability of throwing tails more than 7 times in 10 trials is:
'utpb(7, 10, 0.5)
1: 0.171874999999
10<ret>0.5<ret>7<ret>kB
1: 0.171874999999

• : This   is our goodness of fit, which we have studied so much already. The usual notation for this function is

Compare our previous result with the following:
'utpc(15.31, 8)
1: 0.0533906926374

Remember that in our programs we have calculated

It is easy to see why, for a linear relationship, if the number of measurements is , then the number of degrees of freedom is . Recall that if we have two points only, the fitting procedure has no freedom at all. They determine a straight line uniquely.

There no key-binding for the distribution.

• the F distribution: this is a tricky   function, which is used to determine if two distributions have the same variances. The issue arises in measuring the same quantity under slightly changed conditions and then assessing to what extent the change has affected the measured quantity. The F function is given in terms of the incomplete beta function

where and are degrees of freedom corresponding to both measurements. F is the ratio of the observed dispersion of the first measurement to the observed dispersion of the second one. Q is the probability that
F would be as large as it is if the fist sample's distribution has a smaller variance than the second's.
The Calc function that corresponds to the F distribution is utpf(F,v1,v2).
• Gaussian distribution: Calc provides the upper   tail function here. That is
'utpn(7, 4, 3)
1: 0.158655253933

is the probability that
a normal distributed random variable for a Gaussian distribution with mean of 4 and standard deviation of 3 would exceed 7
• Poisson   distribution: it is given by

It describes a cumulative number of rare events for a large number of measurements. Beta decay, due to electro-weak interactions, is described by a Poisson distribution, for example.

In Calc the probability that n or more random Poisson events with a Poisson distribution of mean x will occur is given by 'utpp(k,x), for example:

'utpp(5, 10)
1: 0.031828057306

this, again, is the upper tail function.
• Student's t   distribution: this function is somewhat related to the F function, but whereas the F function was concerned with comparing variances of two distributions, the Student's function is concerned with comparing means of two distributions.

The function is given in terms of the Euler beta function and the following integral:

is the probability , for degrees of freedom, that t, which is related to the difference of means, would be smaller than the observed value if the means were in fact the same.
In other words, if for a given t and is close to 1, then the observed means are really different.

The Calc Student's t function corresponds to and is called is utpt(t,v). There's no key-binding for it.

Next: Arrays and Matrices in Up: Calculators Previous: Starting Calc
Zdzislaw Meglicki
2001-02-26