Math-Intensive Reports with GNU Emacs and Calc, Part 2

Using Calc for vector, matrix and complex operations.
Recognition of Formulas

Calc has the burden of recognizing the formulas you type into the editing buffer. It can recognize formulas in several formats. The simplest style is to put a formula by itself, preceded and followed by empty lines. This is the easiest to type but has the disadvantage that the layout may not be maintained. That is, if you choose to indent the formulas a bit (which makes them much easier to read, in my opinion), you will lose that indentation when you recalculate the report.

For general-purpose use, the style I prefer is to bracket each formula with a pair of dollar signs. Calc will find and (re)calculate such formulas but won't mess with your indentation. (This is the style I have used in this article.)

Using Calc With TeX and LaTeX

Calc also can recognize and generate markup with the TeX typesetting language. I use LaTeX, but I haven't used it extensively with Calc (yet), so my examples will be simple ones.

Calc can reformat its equations into a TeX markup style when you select a TeX display mode. For example, the Calc assignment

x := 3.2

is rendered in TeX mode as

x \gets 3.2

The TeX command \gets doesn't exist in native TeX. The intent is for you to define your own \gets command using a macro definition in your TeX document. It would look something like this:

\def\gets\text-that-typesets-an-assignment-operator

More complex math formulas will be rewritten in TeX mode using all the considerable number of TeX math operators. The math will come out gorgeous, as only TeX can make it.

Apparently, however, "some assembly is required" to work in this mode. I cannot say much more than this because I simply haven't much explored this aspect of Calc.

Of more interest to me, however, is that LaTeX environments are maintained within Calc. Consider the following markup for a simple LaTeX document. It repeats our first example:

     \documentclass{article}
     \begin{document}
     Let's do a simple calculation of a right triangle.  If the two
     short legs are denoted a and b, where (say)
     \begin{verbatim}
     a := 3.1
     \end{verbatim}
     and
     \begin{verbatim}
     b := 4.1
     \end{verbatim}
     then the length of the hypotenuse is
     \begin{verbatim}
     c := sqrt(a^2 + b^2) => 5.14003891036
     \end{verbatim}
     I didn't have to get my calculator out to do this.  I did it
     right inside Emacs.  Furthermore, this file is live.
     \end{document}

In LaTeX, a verbatim environment is enclosed between the \begin{verbatim} and \end{verbatim} tags. When typeset, the text between the tags will be set in a constant-width Courier typeface---not too pretty, but serviceable. The important thing to understand is that LaTeX environments are recognized as math delimiters by Calc. This means that I can recalculate my LaTeX document as often as I need to and never (well, hardly ever) have to do any manual editing of the numerical results. It always is up to date and it always looks great.

Although this is not the place to explain it, Emacs also has a language mode for TeX and LaTeX editing. It's another elisp package called AucTeX. Try it, you'll love it; it also works with Calc.

Conclusion

Emacs benefits greatly from Calc, and your calculation reports will be executed faster and more accurately than you ever imagined they could be. The interaction between Calc and Emacs is almost as seamless (if I may be forgiven for using that awfully overused computer word) as it gets. Using Emacs with Calc is one of the most friction-free ways I have ever found to write engineering analysis reports and keep them current in the face of constantly ongoing design changes.

I know of no other free general-purpose text editor that can perform mathematical evaluation. Commercial packages are available, sure, but you have to pay plenty for them. Moreover, many products in the lower price ranges, such as Mathcad, are only available for Windows. The more expensive packages (Mathematica, Matlab) generally are available on both Windows and UNIX platforms but cost upwards of a thousand dollars. In contrast, Emacs and Calc are entirely free for the downloading.

What about other free software for doing calculations? I've had some experience with Octave, and I must say that I like it very much. Although it can be used interactively, it is not an editor like Emacs is, and in my opinion it is best for batch computation. After Emacs+Calc, it is the tool I would choose for computations that require an algorithmic approach (with lots of loops and logical testing of results). The only problem I had with it as a publication tool is that your input file requires #-style comment lines for documentation, á là C. To get its output in a form that is suitable for a report with interspersed commentary, you need to perform some downstream editing with tools like sed or awk. Not at all difficult on UNIX but not easy on Windows, and the additional processing requires more steps than the combination of Emacs+Calc. I prefer the latter, because I find it handles about eighty or ninety percent of my routine calculations.

A related category of free software is the literate programming approach ascribed to Professor Don Knuth. This originated as a way to embed sections of functioning code within a running commentary on its design. This procedure allows the code to be extracted from the report and rearranged as needed for compilation. I'm not expert in this area, but I am convinced that Octave, for one, would benefit a lot from being used in a literate manner from within the body of a report. Where the literate programming approach fails, for my purposes at least, is that it seems not to have been considered as a way to compute mathematical expressions and numbers for immediate insertions into the report itself.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2

Anonymous's picture

The author seemingly does not have worked extensively with calc. calc-2.02f does not work too well with Emacs-21. There are a lot of problems with symbolic simplifications, and some quirks with regard to input, scrolling and display.

For that reason, Emacs-21 users should, if at all, get calc directly from the Emacs CVS archive at Savannah.gnu.org where it is maintained as part of the next new-feature release of Emacs (probably 21.4). That version works quite better.

It is a pity that calc's author Dave Gillespie has stopped working on calc while still having a significantly faster (calc-2 does not even use Emacs' floating point variables!) and more extensive version of calc up his sleeves and already in use privately. If anybody manages to wheedle out those floppy disks or whatever else he has been storing this work on...

Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2

Anonymous's picture

You're correct, in that at the time I wrote the article I was quite inexperienced with Calc. Yes, I had noticed some problems on the symbolic side when using 2.02f with Emacs-21 (my setup). I thought it might be me, but you have confirmed the existence of a general problem. Thanks! And I also thank you for the pointer to the CVS version--and for the intelligence about Dave Gillespie's newer version. I, too, hope he takes steps to release it to the public.

But my article's emphasis was mostly on straight-ahead numerical evaluation, as that accounts for 95% or more of my typical usage. My setup works quite well for that, and I would advise anybody whose patterns of usage resembles mine not to be put off too much by the issues you quite correctly raise in your letter.

Thanks for the comments.

Charles Hethcoat, author.

Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2

Anonymous's picture

The auther writes:

"The only problem I had with it as a publication tool is that your input file requires #-style comment lines for documentation,

Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2

Anonymous's picture

Hi, everyone. I didn't realize until today that my article was already on-line! I appreciate the nice feedback.

Concerning the syntax of Calc: I repeat, I'm still fairly new to it, but my reading of the manual leads me to believe that it's probably configurable within elisp, but I haven't tried to figure out how. For my purposes, the `=>' is fine. In any event, you certainly can post-process these marks to taste within or without Emacs.

The only thing to keep in mind is whether you may need to re-process the file with Calc later on. (Remember my discussion of design changes?) You don't want to leave your master file damaged with respect to Calc's ability to read and process the formulas.

So if you create a filter to fix up the syntax, try to create the corresponding "unfilter" as well. Better still, create a processing pipeline to first /copy/ your file to another name and work on that one. Now your original remains untouched. When the need arises to update your original, just rerun your pipeline.

Charles Hethcoat

Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2

Anonymous's picture

You're right, Emacs certainly can auto-convert those comments. One would need to use something like:

M-x replace-regexp ^# RET RET

to remove all #s at the starts of words, and then

M-x replace-regexp ^[^$] RET # SPACE RET

to put them back again. (This assumes all lines start either with a Calc $ or a comment #, which may be incorrect - I haven't used Calc myself.)

But the author was referring to Octave, not to Emacs+Calc. He wrote:

"you need to perform some downstream editing with tools like sed or awk. Not at all difficult on UNIX but not easy on Windows, and the additional processing requires more steps than the combination of Emacs+Calc."

So the fact that it needs processing that could be easily done in Emacs may be precisely his point...

calc math syntax constraint

Anonymous's picture

Thanks a lot. Very interesting article.

One question. Is there a way to get free of the calc package syntax constraint? I mean that in my report, i want to use the '=' caracter for assignments as well as for 'evaluates to' operations. For example, I want to see things like:

c = sqrt( a^2 + b^2 ) = 5.14003891036

and NOT:

c := sqrt(a^2 + b^2) => 5.14003891036

Is it possible, or are we stuck with one and only one syntax?

Re: calc math syntax constraint

Anonymous's picture

Ok, done a bit of reading and the following should work

M-x replace-regexp RET:=|=>RET=RET

Do what you need to do to print the file then Ctrl-_ will set things back to how they were .

Hope that's of help

Euan

Re: calc math syntax constraint

Anonymous's picture

BAH! Bloody stripped the backslash!

M-x replace-regexp RET :=BACKSLASH|=>RET=RET

Re: calc math syntax constraint

Anonymous's picture

One possible solution is to run a regex-replace on the document prior to printing. I'm fairly new to Emacs so not too sure of the syntax for regex. In Vi it would look something like this:

1,$ s/=>|:=/=/g

Which tranlates to search the whole file for => or := and replace with =.

Once you've published it you can then hit Ctrl_ to undo the regex.

If anyone's read this and knows how to do it in Emacs regex then please post :-)

Re: calc math syntax constraint

Anonymous's picture

oops, regex missing a backslash. Should read:

1,$ s/=>|:=/=/g

Sorry,

Euan

Re: calc math syntax constraint

Anonymous's picture

OK, for reasons unknown the backlsash gets stripped when I post. Here we go once more:

1,$ s/=>BACKSLASH|:=/=/g

>sigh<

Re: calc math syntax constraint

Anonymous's picture

Not that i know of... This syntax is used as a programming language one, rather than a symbolic one. And, as such, it has been elaborated since the early days of Pascal...

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState