notes from /dev/null

by Charles Choi 최민수


01 May 2024

Mathing in Emacs with Casual

Emacs has long had a computer algebra system (CAS) packaged within it via Calc, but for many is unused because of its user interface design. Casual aims to provide a much easier CAS user experience and this post aims to illustrate this by example. With Casual you can focus more on math and less on tooling.

Let’s start with the polynomial expression f(x) = x3 - 4 x2 - 11 x + 30.

With f(x) we will perform the following tasks:

  1. Factor and expand f(x).
  2. Determine the root values of f(x).
  3. Find the derivative d(f(x))/dx. Integrate d(f(x))/dx and compare with f(x).
  4. Plot both f(x) and d(f(x))/dx.
  5. Determine minimum and maximum values of f(x) and d(f(x))/dx.

Factor and Expand f(x)

First start Calc and use algebraic entry (') to enter the following expression on the stack.

'x^3 - 4 x^2 - 11 x + 30

Let’s store this expression in the variable named f.

To do this invoke the Casual Main Menu (typically C-o) as shown below.

Under the Stack section, press z for the Variables menu.

Press s to store the expression. You will be prompted to name the variable to store. Enter f.

Enter C-g to go back to the main menu. Press a to invoke the Algebra menu as shown below.

With this menu raised, let’s factor the expression f(x) with respect to x by pressing f. The following result is put on the stack.

1
1:  (x + 3) (x - 2) (x - 5)

To return to the previous form, let’s expand this expression by pressing e.

1
1:  x^3 - 4 x^2 - 11 x + 30

Determine the root values of f(x)

To determine (or solve) for the root values of f(x), we can use the commands in the Symbolic Solutions menu navigated to via (a) Algebra ➡︎ (s) Symbolic as shown below.

Choosing s will provide a solution for f(x), but not all its solutions. To get all solutions, use p. You will be prompted in the mini-buffer for a variable to solve for. Enter x. The solution is returned as a vector with each element being a value of x that satisfies f(x) = 0.

1
1:  [5, 2, -3]

This result is consistent with the factored representation of f(x) determined above.

Derivation and Integration

To demonstrate derivation and integration, we will recall the saved variable f with the keypress sequence (z) Variables ➡︎ (r) Recall in the Algebra menu (or alternately, from the Casual Main Menu). Enter f in the prompt.

From the Algebra menu, press (d) Derivative and when prompted, enter x.

The result will be:

1
1:  3 x^2 - 8 x - 11

Store this in variable dfx.

To get the integral of dfx, from the Algebra menu press (i) Integral and when prompted, enter x.

The result will be:

1
1:  x^3 - 4 x^2 - 11 x

Note that the original constant of 30 in f(x) is lost due to derivation.

Plot f(x) and d(f(x))/dx

Casual can help you visualize these expressions. Given the solution vector, let’s plot both f(x) and d(f(x)) over the interval x[-4..6] which includes all solutions for x.

First we will plot f(x). Recall the variable f onto the stack. From either the Casual Algebra or Main menu, press g to raise the Graphics menu.

Press e to add a 2D equation to the plot. You will be prompted for an interval. Enter “[-4..6]”. Next you will be prompted for a title. Enter “f(x)”. A plot of the function should be displayed in a separate Gnuplot window. By default, Calc uses 15 points to draw a curve. We can make the curve smoother by choosing n for # Data Points and entering 50.

We can style the plot further by pressing S. This presents the following menu:

Press k to show the key (a.k.a. legend). Other options like setting the labels for the x and y axis are available from this menu.

Go back to the Casual Main menu and recall the function dfx. Plot dfx using the same interval and enter d(f(x))/dx for its title.

The resulting plot should look as shown below:

img

Find Minimum and Maximum

Looking at the plot can see there is a minimum for the d(f(x))/dx between [0..3]. We can find this minimum value from the Algebra menu: (n) Numeric ➡︎ (m) Find Minimum. Make sure that the expression dfx is at the top of the stack. You will be prompted for the interval and variable ([0..3], x). Calc will compute this value to be [1.33333, -16.3333], where the first element is x and the second f(x).

A similar exercise can be done to find the maximum value of f(x). Recall the expression f to the top of the stack. Use the interval [-2..0]. The computed result should be [-1., 36.].

Saving Work

You can save the variables you have defined by going back to the Variables menu and entering p to persist them. Both f and dfx will be available across restarts of Emacs.

Closing Thoughts

If you enjoy math as I do, hopefully this post will raise awareness of the CAS features of Calc and the ease of which Casual can provide access to them. A benefit to being an Emacs user is to know that these capabilities are only a few keystrokes away.

emacs

Past Articles

18
APR
2024

Writing Better Elisp Docstrings

Emacs has a lot of tools to help you write better docstrings. And if a tool you want doesn’t exist, Emacs can let you make your own.

read more
10
APR
2024

Exporting UTF-8 Smart Quotes from Org Mode

This post shows a way to use UTF-8 characters for smart quotes when exporting to Markdown or HTML from Org.

read more
24
MAR
2024

Announcing Casual - An opinionated porcelain for Emacs Calc

Announcing the first public release of Casual - an opinionated Transient-based porcelain to support the casual usage of Emacs Calc.

read more
18
FEB
2024

cc-isearch-menu now on MELPA

My first Emacs package, cc-isearch-menu is now available on MELPA. It is a Transient menu for isearch.

read more
15
FEB
2024

Computing Truth Tables in Org

Truth tables are a great way to design and verify combinational logic. That said, they are a PITA to make. With some Emacs Elisp and Org, making truth tables becomes effortless, leaving you to focus on solving your logic problem.

read more
31
JAN
2024

Administering MacPorts

Some learnings I've made using MacPorts for the past two decades.

read more
16
JAN
2024

Enhancing Dired Sorting With Transient

Out of the box, Dired sorting is rather underwhelming. This post remedies that with Transient and a context menu.

read more
10
JAN
2024

Highlighting with isearch

isearch highlighting is a really great feature that can actually be usable given a Transient menu. This post shows you how.

read more
18
DEC
2023

Improving Emacs isearch Usability with Transient

Emacs isearch has many powerful features that are largely unusable because of poor keybindings. A Transient menu can change all that.

read more

Page 1 / 11   >

 

AboutMastodonInstagramGitHub

Feeds & TagsGet Captee for macOS

Powered by Pelican