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) $
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Back to Backups
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- A New Version of Rust Hits the Streets
- Google's Abacus Project: It's All about Trust
- Secure Desktops with Qubes: Introduction
- Seeing Red and Getting Sleep
- Fancy Tricks for Changing Numeric Base
- Secure Desktops with Qubes: Installation
- Working with Command Arguments
- Linux Mint 18
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide