Math-Intensive Reports with GNU Emacs and Calc, Part 1
Calc is an elisp package originally written by Dave Gillespie. Lately, however, many others have contributed to its development in the classical manner: hackers working over the Internet. Once it has been downloaded and installed in your local emacsen directory (the place where elisp add-on packages live) and a few lines have been added to your .emacs configuration file, Calc becomes an invisible math miracle worker. In any Emacs buffers you choose, you can slip quickly and silently into and out of Calc mode (any of several such modes, really) with a simple keystroke or two.
Let's do a simple calculation of a right triangle. If the two short legs are denoted a and b, where (say)
$ a := 3.1 $
$ b := 4.1 $
then the length of the hypotenuse is
$ c := sqrt(a^2 + b^2) => 5.14003891036 $.
I didn't have to get my calculator out to do this. I did it right inside Emacs. Furthermore, this file is live. That means in order to update the calculation for an arbitrary right triangle, all I have to do is is change the values assigned to a, b or both, position the cursor inside the first formula and type M-# e (M for Meta, the Alt key on most PCs; the keystroke sequence therefore is Alt-# e, which is Alt-Shift-3 e). The Calc package loads up, and the value of c is immediately updated.
Now that I have the variables a, b and c defined, I can use their values over and over:
$ a + b + c => 12.3400389104 $ (the perimeter) $ a^2 + b^2 - c^2 => 0. $ (a check on the calculation)
I am going to provide you with a short and incomplete survey of Emacs' ability to perform mathematical and numerical evaluations like my examples, right in the editing buffer, by means of Calc.
I do not intend to explain in detail how to download, install or use Emacs (although the Resources section gives some pointers). Nor do I intend to waste words disparaging other editors (such as vi), nor browbeat their users about the superiority of Emacs. And I won't go into any real detail on how to start up the Calc package, nor will I delve deeply into the keystroke sequences needed to cause Calc to perform its magic. All that stuff is documented elsewhere; you simply need to make your decision to learn Emacs. What I am going to show you are some astounding (and generally unknown) capabilities of Emacs for performing mathematical analysis, courtesy of Calc.
To have the capability to write live ASCII math documents, you and your computer must have a few required pieces:
An environment that is capable of running Emacs;
Emacs itself, installed and configured to your taste;
The calc.el math plug-in (I suppose we could call it that) installed in your local emacsen directory; and
Some practice using Emacs.
Emacs is basically a UNIX program, but it has been written carefully so it may be ported to other operating systems. Versions are available for almost every computing platform in existence: all known Unices and UNIX workalikes (such as Linux and BSD); all versions of Windows since Windows 95; UNIX-like environments, such as the Cygwin tools, that run under Windows; and various other platforms and operating systems as well.
If you run UNIX or Linux, Emacs is probably already available on your system; type emacs at the command prompt and see if it pops up. If it doesn't, it is a simple download away; see the Resources.
If you are a Windows user, you will need to download and install the Windows version of Emacs, called NTEmacs. It is a standard Windows-style self-installing executable. If you are using the Cygwin tools under Windows, be aware that NTEmacs doesn't build under that UNIX-like package but does work well with it.
Next, you need the Calc package, known by the filename calc.el. Download and install it (see Resources), and you can then turn on the calculator any time you are editing by using the M-# keyboard command.
Here is another example, one that is somewhat close to home for me. I am going to calculate some cross-sectional area properties for a beam. Let's say the beam's cross section is rectangular, of height "height" and width "width". (Of course, I have obtained all the dimensional information from the preliminary engineering drawings.) I merely type the following Calc assignments into my editing buffer (that is to say, directly into the window that shows the article I am writing now):
$ height := 0.65 in $ $ width := 0.25 in $
(I am including units here; the dimensions are in inches.) Then I can start Calc and perform the following calculations. First, I need the cross sectional area of the beam. I enter
$ area := width height => $
Then I type M-# u, u for update, and I immediately see the ab ve line change to this:
$ area := width height => 0.1625 in^2 $
The answer, complete with derived units, pops up on my editing screen, to the right of the => symbol.
Later on, I'll need to use these results to calculate bending stresses. To do that, I first have to calculate the moment of inertia (MI) of the cross section. Without getting into the details, the moment of inertia is an important geometric property of a beam. (The following formula is correct only for a rectangular cross section.)
$ MI := width height^3 / 12. => 5.72135416667e-3 in^4 $
For many purposes, the section modulus Z is a more useful number. For this simple rectangular cross section, it is equal to the moment of inertia divided by half the section height:
$ Z := 2. MI / height => 0.0176041666666 in^3 $
I did this example in about one minute, entirely inside my editor's screen buffer. And I did not have to use any other program to perform these calculations---only Emacs and Calc, working smoothly together.
A few explanations are in order to help you read this and the following examples:
I am using the embedded mode of Calc, which evaluates the formulas in the buffer where they are written. (Other modes of operation are available that show you a calculator pop-up screen or a stack. I won't be using those modes because they are not so useful for reports.)
Formulas are bracketed by dollar signs to facilitate recognition by Calc (other syntactical conventions are also supported).
Multiplication is indicated by adjacency of the factors ("width height" is width times height). Whitespace must separate the factors.
=> is the evaluates-to operator; in embedded mode, the value of the expression on its left is inserted on its right.
:= is an assignment operator. The variable on its left receives the value on its right, and that variable can be used in future calculations.
|Designing Electronics with Linux||May 22, 2013|
|Dynamic DNS—an Object Lesson in Problem Solving||May 21, 2013|
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Have you tried Boxen? It's a
4 hours 58 min ago
- seo services in india
9 hours 30 min ago
- For KDE install kio-mtp
9 hours 30 min ago
- Evernote is much more...
11 hours 30 min ago
- Reply to comment | Linux Journal
20 hours 16 min ago
- Dynamic DNS
20 hours 50 min ago
- Reply to comment | Linux Journal
21 hours 48 min ago
- Reply to comment | Linux Journal
22 hours 39 min ago
- Not free anymore
1 day 2 hours ago
1 day 6 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?