[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[seul-sci] [Fwd: GNU Emacs Calc]

Greetings all,

I'm about to post Linux in Science report #7, and in it I refer to this
email from Robert Chassell about his emacs graphing extension, GNU Emacs
Calc. His email contains more info than my entry in the report, and since
the project has no web page to refer people to, I'm forwarding the mail in
toto to the list.


-------- Original Message --------
Subject: GNU Emacs Calc
Date: Mon, 8 Jan 2001 13:37:11 -0500 (EST)
From: "Robert J. Chassell" <bob@rattlesnake.com>
Reply-To: bob@rattlesnake.com
To: pete@seul.org

In your wonderful GNU/Linux in Science report you wrote:

    One of the most important tasks for scientists is to create clear,
    understandable graphs to explain or present results.

Did you pick up the note on GNU Emacs Calc mode that I sent
dloss@seul.org a week ago?

Basically, I wrote about its advantages for a graphing program,
however, Calc does a great deal of what an ordinary scientist needs or
does:  least squares linear regressions, matrix operations,
statistical operations, converting easily typed mathematical to
formats used for printed papers, and so on.

Here is a copy of the message I sent to dloss@seul.org:

------- Start of forwarded message -------
Date: Tue, 2 Jan 2001
To: dloss@seul.org

GNU Emacs Calc mode runs gnuplot.  You can create output for an ascii
terminal or for an X window or for a printer.

The Calc manual has good lots of documentation.  For example,

    The easiest graphics command is `g f' (`calc-graph-fast').  

    This command takes two vectors of equal length from the stack.
    The vector at the top of the stack represents the "y" values of
    the various data points.  The vector in the second-to-top position
    represents the corresponding "x" values.

    This command runs GNUPLOT (if it has not already been started by
    previous graphing commands) and displays the set of data points.
    The points will be connected by lines, and there will also be some
    kind of symbol to indicate the points themselves.

I use gnuplot and Calc mode to compute least squares linear
regressions and then plot the actual data and the best fit curve.

Emacs Calc is a standard part of Emacs, but because of its size is not
in most standard Emacs distributions (at 832 kilobytes, people used to
think it huge... :)

You can get it via ftp from


Mirror sites, and an Emacs expression so you can see the directory
using ange-ftp:

        (dired "/ftp@aeneas.mit.edu:/pub/gnu" nil)
        (dired "/ftp@ftp.cs.columbia.edu:/archives/gnu/prep" nil)
        (dired "/ftp@ftp.digex.net:/pub/gnu/" nil)

Besides graphing, arithmetic, matrixes operations, and so on, Calc
simplifies algebraic expressions, does least squares linear
regressions, transcendentals, differentiation, and integration.

The Calc manual is in Texinfo format.  This means you can read it on
line in Emacs, or in HTML on a Web site, or print it out as a (rather
big) nicely typeset book.

(Actually, the manual is in two parts: a tutorial and a reference
manual; you can print it as either one or as two books.)

Here are some notes

How to show mathematical expressions in different formats

The `d N' (`calc-normal-language') command selects the usual
notation for Calc formulas

  `d B' (`calc-big-language') 

  `d C' (`calc-c-language') 

  `d T' (`calc-tex-language')

For example:

    (a+1)/b + c^2

     a + 1    2
     ----- + c

and to

    (a + 1) / b + c ** 2

in place of `sqrt((a+1)/b + c^2)'.

Here is example for solving a quadratic equation of the form for the
value of x, given a, b, and c:

    ax^2 + bx + c = 0

These following are all the same, but interconverted from one to
another display mode using Emacs Calc.

    Emacs Calc `Big' mode:

                      |  2
            (-b) +/- \| b  - 4 ac
        x = ----------------------
                     2 a

    Emacs Calc `Normal' mode:

        x = (-b +/- sqrt(b^2 - 4 ac) / 2 a)

    Emacs Calc `TeX' mode:

        x = {-b \pm \sqrt{b^2 - 4 ac} \over 2 a}

Working in an email buffer:
- ---------------------------

Suppose you have this expression in your mail buffer:


In the mail buffer in which I am composing the message, I type `M-# d'
(i.e., Press alt key and # key at the same time, then press the d key)

This duplicates the expression.  I then type: 

    M-# e

to turn on Embedded Calc mode, and then type `m f' to keep the
fraction from being automatically converted to a float, and then type
`d B', which converts the duplicated expression to this:

                 |        2  3
             4   |   10 pi  r
    0.6666 - --  | -------------- - 1
             15  |  2
                \| D  G m epsilon

which I find easier to read.  (Also, I may type `d o' to specify what
format to use for fractions, and `m s' to prevent early evaluation of
symbolic forms.  Just type them to see what happens.)

Actually,  after typing `m f' and `m B' the first time, each time I
duplicate an expression like:


the duplicate is inserted in my email buffer in `big' mode.

         Q      1/4
     x*(1 - e)

(This is not as easy to read as the preceding expression, but is easier than
the `flat' format.)

How to operate on each of a column of numbers

21 April 1993,
Edward J. Hartnett asks:

   ... what would I do
   to perform a simple mathematical manipulation on each, specifically I
   want to multiply each element by 174 and pop another vector back into
   my original document, so if I had:


   I would be able to get 

           1       174
           2       348
           3       522

Use the Calc mapping and yank commands; see File: calc.info, 
Node: Mapping, and Node: Yanking Into Buffers, for more information.

You can record the sequence of commands in a keyboard macro; or use
the `generate.el' library from the archives to create a elisp function
that does the job, based on your keystrokes.  The advantage of an
elisp function is that you can edit it easily.

   Commands         Comments

    M-# g       grab the column as a vector and insert in *Calc* buffer

    174         insert number you want to multiply each element by

    V M *       Vector action, Mapped over each element, Multiplication

    v u         unpack vector so each element is on the stack separately
                    See File: calc.info,  Node: Packing and Unpacking

This results in separate items in the stack, each of which is an
element of the original column, multiplied by 174.  The *Calc* buffer
looks like this:

        3:  174
        2:  348
        1:  522

You can yank the elements out of the *Calc* buffer in two ways (with
my calc-version 2.02).

If point in your working buffer is in the left most (i.e., first
column) `C-u 3 M-# y' yanks out the top three elements of the stack,
producing a column like this:


On the other hand, if point in your working buffer is not in the left
most (i.e., not in the first column) `C-u 3 M-# y' yanks out the top
three elements of the stack, and the line numbers, too:

   3:  174
2:  348
1:  522

I don't understand why this is so and my documentation does not
explain this.  I don't know how this works in other versions of Calc.
In any case, you want the column without the line numbers.  

You can copy your column to where ever you want it using Emacs
rectangle commands.

I have bound `kill-rectangle' to `C-c k' and `yank-rectangle' to
`yank-rectangle', which makes it very easy to move a rectangle from
one place to another.  Place point and mark around the column, type
`M-x kill-rectangle', move mark to the upper left hand corner of where
you want to insert the rectangle and type `M-x yank-rectangle'.

Two other useful commands:

    V [         turn off brackets in vector in *Calc* buffer
                    See File: calc.info,  Node: Vector and Matrix Formats

    V ,         turn off commas in vector in *Calc* buffer

How to show  "fixed-point" notation with two digits after the decimal point  


        M-2 d f

    To show "normal" notation with three significant digits, type

        M-3 d n

    "scientific notation"   d s 

    "engineering notation"  d e 

    To set precision:

        p 12   

    12 is the default precision.     Display format does not effect

    To measure angles in degrees:  m d   To measure angles in radians:   m r

    To convert degrees to radians: c r   To convert radians to  degrees: c

    To convert to "hexadecimal", or "base-16" form,

        d r 16 <RET>

    Use `d r 2 <RET>' to see it in binary
    Use `d r 10<RET>' to see it in base 10

Best wishes.

- -- 
    Robert J. Chassell                  bob@rattlesnake.com
    Rattlesnake Enterprises             http://www.rattlesnake.com
------- End of forwarded message -------