Math-Intensive Reports with GNU Emacs and Calc, Part 2
In part 1 of this article, I explained some of the evolving and complicated requirements of math calculations in certain types of reporting. As part 1 outlined, Emacs and the Calc application offer many types of utility for the requirements of math-intensive report generation. Part 2 of this two-part series will delve a bit deeper into the mathematical operations you can do with Emacs, further demonstrating why this editor is so valuable.
Let's look at some vector calculations. Suppose we want to calculate the bending moment induced at a point in space by a force vector acting through another point. If, at the location given by the radius vector
$ R := [3, -11, -29] $
in a given coordinate system, a force
$ F := [50, 60, -30] $
is acting, then the moment M induced at the origin by F acting through R is
$ M := cross(R, F) => [2070, -1360, 730] $
The dot product is denoted by
$ R F => 360 $
The unit vector triad typically is denoted in vector mechanics by (i, j, k), where i = [1, 0, 0] and so on. However, in Calc, i is predefined as the imaginary unit
$ i => (0, 1) $
$ i^2 => -1 $
Therefore, to avoid any possible confusion, we will use I, J and K as the unit vectors. These variables are not predefined in Calc, but they are absurdly easy to define:
$ I := [1, 0, 0] $
$ J := [0, 1, 0] $
$ K := [0, 0, 1] $
We could then, for example, resolve the moment vector M, calculated in the preceding section, into components parallel to the I, J and K vectors. (When both arguments are three-vectors, multiplication produces the dot product.)
$ M I => 2070 $
$ M J => -1360 $
$ M K => 730 $
Check for yourself that the resultant of these three components equals the given value of M.
Calc also is good at matrix operations. Suppose A is a 4-by-4 matrix. Before starting, I am going to set some Calc modes explicitly:
% [calc-mode: matrix: scalar] % [calc-mode: matrix-brackets: (O C)]
In Calc, some minor annoyances exist when using matrices. To define A, I entered it in like this:
A := [1, 2, 3, 4; 6, 5, 4, 3; 7, 12, 3, 4; 5, 6, 3, 2]
This is a simple input format used by other software (Matlab and Octave, for instance). However, when I use the command M-# u to invoke the assignment operation, Calc reformats it like this:
$ [ 1, 2, 3, 4; A := 6, 5, 4, 3; 7, 12, 3, 4; 5, 6, 3, 2 ] $
Note that the A := part has been moved down a line. I wouldn't mind it doing this, except it seems that later re-evaluation of this assignment can hit a stumbling block when Calc re-reads what it rewrote (the second form). I suspect it's probably my own inexperience using Calc that causes this, but I digress. We have made the assignment; we can print it out like this:
$ [ 1, 2, 3, 4; A => 6, 5, 4, 3; 7, 12, 3, 4; 5, 6, 3, 2 ] $
Now, let B be a 4-by-1 vector. To enter it conveniently, we can type it as a 1-by-4 but transpose it by giving it as an argument to the trn (transpose) command, like this:
$ [ 4 ; B := trn([4, 6, 9, -2]) => 6 ; 9 ; -2 ] $
Now, let C be the product A*B, a 4-by-1 vector:
$ [ 35 ; C := A B => 84 ; 119; 79 ] $
Thus, we can calculate the inverse of A. First, however, I will set the display precision to four digits so it will fit on the page better. Not to worry, the full internal precision always is carried in calculations.
% [calc-mode: float-format: (float 4)]
The function inv actually calculates the inverse:
$ [ -0.2857, 0.7143, 0.1667, -0.8333; Ainv := inv(A) => 0.07143, -0.4286, 1.135e-14, 0.5 ; 0.2857, -0.7143, -0.5, 1.5 ; 0.07143, 0.5714, 0.3333, -1.167 ] $
We can verify that Ainv is indeed the inverse of A, for their product will equal the unit matrix:
$ [ 1.0000, 0., 0., -1e-11; A Ainv => 6e-12, 1.000, -1e-12, -1e-11; -4e-12, 1e-11, 1., -1e-11; 1e-12, 0., 1e-12, 1.0000 ] $
Well, it's pretty closely. The difference between this result and a perfect unit matrix is due to the roundoff error intrinsic to floating point machine arithmetic.
Complex numbers can arise as the result of operations on real or integer numbers. For example, the imaginary unit is, in a simple way, defined as the square root of -1:
$ sqrt(-1) => (0, 1) $
Note, this is one way Calc can display i. It is in rectangular vector format, denoted as (realpart, imaginarypart). However, as I already mentioned, i also is a predefined variable in Calc, and it behaves as we might expect:
$ i => (0, 1) $ $ i i => -1 $
We may use preexisting real numbers to construct complex values, as in this example:
$ rp := 2.7 $ $ ip := -3.2 $
Both of these values are real, but we can construct a complex number out of them easily:
$ c := rp + ip i => (2.7, -3.2) $
If we have complex values to work with, we can perform general complex mathematical evaluations. For example, let z be a complex number:
$ z := (2, 3) $
Let me push up a notch Calc's precision for displaying the result:
% [calc-mode: float-format: (float 6)]
Now, we may map the complex point z to the complex point w using a functional mapping:
$ w := 1 / (1 - z^2) => (0.0333333, 0.0666667) $
|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|
- Designing Electronics with Linux
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Validate an E-Mail Address with PHP, the Right Way
- Tech Tip: Really Simple HTTP Server with Python
- Build a Skype Server for Your Home Phone System
- Why Python?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Reply to comment | Linux Journal
24 min 31 sec ago
- Reply to comment | Linux Journal
1 hour 14 min ago
- Not free anymore
5 hours 16 min ago
9 hours 3 min ago
- Reply to comment | Linux Journal
9 hours 11 min ago
- Understanding the Linux Kernel
11 hours 26 min ago
13 hours 56 min ago
- Kernel Problem
23 hours 59 min ago
- BASH script to log IPs on public web server
1 day 4 hours ago
1 day 8 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?