📜 ⬆️ ⬇️

Emacs Mysterious: Journey to Calculator



Nothing is so hidden from us as that which lies on the surface.
Sun Tzu and Zhuge Liang (free translation)

Foreword


')
This happened in one of the videos on YouTube. When viewing a programmer’s lesson, it was necessary to urgently translate 377 into the decimal system. We did not lay it out on triples of bits, represent degrees of the eight, or just ignore it and skip this unimportant moment. We launched a calculator in the operating system, pushed its window to the center of the screen and found out everything there. Yes, this is it - the number 255.

Of course, you do not need to tell what happens in more complex cases. Sometimes it comes to a leaf in a notebook, lying next to the computer just for such cases. Everybody has a parabola or something like that intersecting something about that.

First contact



As you would expect, using Emacs for months or just for many years comes Emacs of the brain. Therefore, such cases do not pass in vain and the question arises in the middle of the night: “How can I translate 377 in Emacs? Does he even know how? Well, at least 8 will raise a degree? ".

The next morning, charged with energy in the form of tea with a handful of chocolates, we open Emacs and begin its research.



We start with operations



It is clear that by entering Alt + x and calc, the first thing we see is the usual stack calculator, which was written, probably, by a hundred of these hands here. And I would not like to repeat this path a hundred and first time, finding that he does not know how.

At random we first do + - * /

5 <RET> 7 <RET> +

5 <RET> 7 <RET> -

5 <RET> 7 <RET> *

5 <RET> 7 <RET> /


Then

8 <RET> 8 <RET> *


ABOUT! And here is the square of the eight! And where is the square, there is a cube!

We open a help



Of course, everyone knows that a Russian person (USSR, imperial, etc.) will not start with instructions, but first he will try to break everything, then fix it, and then read the instruction in search of the missing secrets.

I will not say that this time everything started with instructions, since, unlike many others, I don’t turn off the Emacs menu, but I use it to recall keyboard shortcuts in all rare modes.

So this time I started the review from the calculator menu



In order to open the help, you need to enter Ctrl + x + * + i. And to open a tutorial, you need to enter Ctrl + x + * + t.

They can be opened in different ways, but, as practice has shown, the combination Ctrl + x + * + <key> is used very often and is made specifically for convenient control of the calculator during other work.

Therefore, you can open the calculator using Ctrl + x + * + c, and you can start the calculator in the current buffer using Ctrl + x + * + e.

Key Description
calc-dispatch-help is an interactive compiled Lisp function.

(calc-dispatch-help ARG)

Cx* is a prefix key sequence; follow it with one of these letters:

For turning Calc on and off:
C calc. Start the Calculator in a window at the bottom of the screen.
O calc-other-window. Start the Calculator but don't select its window.
B calc-big-or-small. Control whether to use the full Emacs screen for Calc.
Q quick-calc. Use the Calculator in the minibuffer.
K calc-keypad. Start the Calculator in keypad mode (X window system only).
E calc-embedded. Use the Calculator on a formula in this editing buffer.
J calc-embedded-select. Like E, but select appropriate half of => or :=.
W calc-embedded-word. Like E, but activate a single word, ie, a number.
Z calc-user-invocation. Invoke Calc in the way you defined with `ZI' cmd.
X calc-quit. Turn Calc off.

For moving data into and out of Calc:
G calc-grab-region. Grab the region defined by mark and point into Calc.
R calc-grab-rectangle. Grab the rectangle defined by mark, point into Calc.
: calc-grab-sum-down. Grab a rectangle and sum the columns.
_ calc-grab-sum-across. Grab a rectangle and sum the rows.
Y calc-copy-to-buffer. Copy a value from the stack into the editing buffer.

For use with Embedded mode:
A calc-embedded-activate. Find and activate all :='s and =>'s in buffer.
D calc-embedded-duplicate. Make a copy of this formula and select it.
F calc-embedded-new-formula. Insert a new formula at current point.
N calc-embedded-next. Advance cursor to next known formula in buffer.
P calc-embedded-previous. Advance cursor to previous known formula.
U calc-embedded-update-formula. Re-evaluate formula at point.
` calc-embedded-edit. Use calc-edit to edit formula at point.

Documentation:
I calc-info. Read the Calculator manual in the Emacs Info system.
T calc-tutorial. Run the Calculator Tutorial using the Emacs Info system.
S calc-summary. Read the Summary from the Calculator manual in Info.

Miscellaneous:
L calc-load-everything. Load all parts of the Calculator into memory.
M read-kbd-macro. Read a region of keystroke names as a keyboard macro.
0 (zero) calc-reset. Reset Calc stack and modes to default state.

Press `*' twice (`Cx * *') to turn Calc on or off using the same
Calc user interface as before (either Cx * C or Cx * K; initially Cx * C).



Raise the bar



About a day went to check all the options from the menu. Among them there are such as finding the next prime number, so this could not be simply missed, I wanted to make sure that it really worked.

The greatest common divisor (GCD) can now not be written, here it just is. You must enter two numbers and press kg, where k means combinatorics, and g - gcd (Greatest Common Divisor).

The factorial 3000 (a kind of speed check) is not easily calculated, but it is still calculated in fifteen seconds.

The transposition of the matrix, finding its determinant or scalar multiplication of vectors is performed easily and with a whistle.

For a minute, I forgot what I came for, performing operations one after another, not believing my own eyes. And for what did I come?

We continue



On the second day, it became interesting whether he calculated the system of equations by the Cramer method. Once there are determinants, then the Kramer solution can be found. Previously, I wrote this many times in the editor, even the skills have already developed on the calculation of the determinants in the editor.

But this was not required. It turned out that it is enough just to enter three equations, and then request their solution. Yes, just like in Wolfram.

Find the first available system

2y + x + z = -1
-z - y + 3x = -1
-2x + 3z + 2y = 5


You just need to select it Ctrl + x + h, copy Alt + w, go to the calculator Ctrl + x + * + *, paste it Ctrl + y, pack the equations into the 3 vp vector, press a P and in response to the question “By what search for roots? ”enter xy z.

A vector with answers appears in the stack, then we simply press y and the vector with answers is inserted at the cursor position in the original buffer with equations.

[[ 0., -2., 3. ]] 2y + x + z = -1
-z - y + 3x = -1
-2x + 3z + 2y = 5


If we left the cursor at the end of the system of equations, the answer would be inserted there.

It's time to turn around



Stack power



When using the calculator for some time, it becomes inconvenient because values ​​start to accumulate on the stack. At the same time, you do not know whether it is possible to remove them, since there are the necessary ones among them, therefore any experiment may result in loss.

So the clock goes on, until in one wonderful moment you decide to open the help and spend time reading it. Fortunately, all combinations are selected as conveniently as possible, so that there is even some symmetry of actions present and maintained.

These numbers with colons in the stack at the beginning of each line are like addresses. In many commands, entering a positive argument means “a few pieces,” and entering a negative argument means “such a number”.

For example, we have the number five on the stack.

1: 5


If we press <RET> (Enter), it will be copied and we will have

2: 5
1: 5


This two is equal to the number of elements in the stack, it is also the address of the first number five, and the number 1 is the address of the second number five.

The <RET> command has an argument that defaults to 1. When we press <RET>, it copies as many items from the top of the stack (the top is at the bottom). Therefore, if we enter Alt + 2 + <RET>, it will take two elements and copy them.

4: 5
3: 5
2: 5
1: 5


If the argument is negative, then it takes an element with such an address (if the minus is removed). Therefore, if we now enter Alt + - + 4 + <RET>, it will copy the element at 4

5: 5
4: 5
3: 5
2: 5
1: 5


Most of the basic operations work in the same way. If you need to delete three elements, then we write Alt + 3 + <DEL>, where <DEL> is the backspace. If you need to delete an element with the address 3, then we write Alt + - + 3 + <DEL>.

If you read the help in more detail, then these operations can be found such original operations from the anti-world, consisting of black matter. For example, for <RET> there is a Ctrl + j operation, for <DEL> there is Alt + <DEL>, and for <TAB> there is Alt + <TAB>.

The meaning of these operations is exactly the same, only the argument changes in sign. Therefore, if you enter Alt + 3 + Ctrl + j, then it does not copy the three elements, but the element with the address 3, and if you enter a negative argument, it will not copy the element with the address 3, but three elements.

If you want to clear the entire stack, then you need to press Alt + 0 + <DEL>.

There are many ways



At first it seems that everything can be done only in one way, which you just need to learn. But over time, there are many ways to achieve the same result.

For example, how can I enter a matrix?

The very first thing that comes to mind is to write it in a buffer and then just copy it.

This

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]


It turns into this

1: [ [ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ] ]


The second thing that comes to mind is the use of an internal remedy.

Matrix is ​​three vectors

1 <RET> 2 <RET> 3 <RET> 3 vp
4 <RET> 5 <RET> 6 <RET> 3 vp
7 <RET> 8 <RET> 9 <RET> 3 vp
3 vp


The third thing that comes to mind is the use of an internal and convenient means.

1 <RET> 2 <RET> 3 <RET> 4 <RET> 5 <RET> 6 <RET> 7 <RET> 8 <RET> 9 <RET>
[3, 3]
vp


And the fourth thing that comes to mind is “Can't you just enter it?”

[ 1 <SPC> 2 <SPC> 3 ; 4 <SPC> 5 <SPC> 6 ; 7 <SPC> 8 <SPC> 9 ] ]


The pack command has a symmetrical one - unpack, so if you need to decompose the matrix into vectors or decompose the matrix into numbers, then we just press v u.

We attract external funds



As you tinker with the calculator, you suddenly begin to pay attention to the surrounding buffers and the question gradually arises: “And what means of the usual Emacs can be attracted when working in the calculator?”

At first, we simply find the n-th derivative of a polynomial by writing a macro and repeating it n times through the macro argument.

For example, enter the expression

' x^5 + 4 x^3 + 2 x + 1 <RET>


Then press F3 to start recording the macro. Then press ad to calculate the derivative. Then to the question "For which variable?" We enter x. Then press F4 to finish recording the macro.

Do a cancellation of the calculation by pressing Ctrl +/ or applying the internal cancellation of the calculator - the letter U.

Having an initial expression

1: x^5 + 4 x^3 + 2 x + 1


Press Alt + 3 + F4 and as a result we have the third derivative

1: 60 x^2 + 24


If we press Ctrl + x + z, and then z z, then we will gradually get zero.

We leave to the outside world



After some time, the calculator closes and we return to our regular work, our usual entertainment.

And then at one point we read, in Emacs, of course, the story of a fox who jumped-jumped over a lazy dog ​​and jumped 1024 times. Wait, you say, unless a fox can jump right in exactly the same time, if it is not computer? It is necessary to fix this thing.

We have the following text

The quick brown fox has jumped over the lazy dog 1024 times.


Hover the cursor at 1024 and carefully press Ctrl + x + * + w. Next, press Q, and then just as carefully press Ctrl + x + * + w.

And we get

The quick brown fox has jumped over the lazy dog 32 times.


Now this is more like the truth. That is, in order to calculate something, the calculator does not have to be opened at all; you just need to press some buttons.

But the very thing



I remembered what I went for

Ctrl + x + * + *
8#377 <RET>
d 2
d 8
d 6
d 0
Ctrl + x + * + *


And finally



There was a time when I wrote a function on python

2016
1
1
Alt + - + 14 vp
255
+


I enclose here a list of units that he can convert, in the calculator, they are in u v.

List of units
Calculator Units Table:

(All definitions are exact unless marked with an asterisk (*).)

Unit Type Definition Description

mm Meter (base unit)
in 2.54 cm Inch
ft 12 in Foot
yd 3 ft Yard
mi 5280 ft Mile
au 149597870691 m (*) Astronomical Unit
lyr c yr Light Year
pc 3.0856775854 10^16 m (*) Parsec (**)
nmi 1852 m Nautical Mile
fath 6 ft Fathom
fur 660 ft Furlong
mu 1 um Micron
mil (1/1000) in Mil
point (1/72) in Point (PostScript convention)
Ang 10^-10 m Angstrom
mfi mi + ft + in Miles + feet + inches
texpt (100/7227) in Point (TeX convention) (**)
texpc 12 texpt Pica (TeX convention) (**)
texbp point Big point (TeX convention) (**)
texdd (1238/1157) texpt Didot point (TeX convention) (**)
texcc 12 texdd Cicero (TeX convention) (**)
texsp (1/65536) texpt Scaled TeX point (TeX convention) (**)

hect 10000 m^2 Hectare
a 100 m^2 Are
acre (1/640) mi^2 Acre
b 10^-28 m^2 Barn

L 10^-3 m^3 Liter
l L Liter
gal 4 qt US Gallon
qt 2 pt Quart
pt 2 cup Pint (**)
cup 8 ozfl Cup
ozfl 2 tbsp Fluid Ounce
floz 2 tbsp Fluid Ounce
tbsp 3 tsp Tablespoon
tsp 4.92892159375 ml Teaspoon
vol tsp+tbsp+ozfl+cup+pt+qt+gal Gallons + ... + teaspoons
galC galUK Canadian Gallon
galUK 4.54609 L UK Gallon

ss Second (base unit)
sec s Second
min 60 s Minute
hr 60 min Hour
day 24 hr Day
wk 7 day Week
hms wk + day + hr + min + s Hours, minutes, seconds
yr 365.25 day Year (Julian)
Hz 1 / s Hertz

mph mi / hr Miles per hour
kph km / hr Kilometers per hour
knot nmi / hr Knot
c 299792458 m / s Speed of light

ga 9.80665 m / s^2 "g" acceleration

gg Gram (base unit)
lb 16 oz Pound (mass)
oz 28.349523125 g Ounce (mass)
ton 2000 lb Ton
tpo ton + lb + oz Tons + pounds + ounces (mass)
t 1000 kg Metric ton
tonUK 1016.0469088 kg UK ton
lbt 12 ozt Troy pound
ozt 31.10347680 g Troy ounce
ct 0.2 g Carat
u 1.660538782 10^-27 kg (*) Unified atomic mass

N m kg / s^2 Newton
dyn 10^-5 N Dyne
gf ga g Gram (force)
lbf ga lb Pound (force)
kip 1000 lbf Kilopound (force)
pdl 0.138254954376 N Poundal

JN m Joule
erg 10^-7 J Erg
cal 4.1868 J International Table Calorie
calth 4.184 J Thermochemical Calorie
Cal 1000 cal Large Calorie
Btu 1055.05585262 J International Table Btu
eV ech V Electron volt
ev eV Electron volt
therm 105506000 J EEC therm
invcm hc / cm Energy in inverse centimeters
Kayser invcm Kayser (inverse centimeter energy)
men 100 / invcm Inverse energy in meters
Hzen h Hz Energy in Hertz
Ken k K Energy in Kelvins
Wh W hr Watt hour
Ws W s Watt second

WJ / s Watt
hp 550 ft lbf / s Horsepower
hpm 75 m kgf / s Metric Horsepower

KKK Degree Kelvin (base unit)
dK KK Degree Kelvin
degK KK Degree Kelvin
dC CK Degree Celsius
degC CK Degree Celsius
dF F (5/9) K Degree Fahrenheit
degF F (5/9) K Degree Fahrenheit

Pa N / m^2 Pascal
bar 10^5 Pa Bar
atm 101325 Pa Standard atmosphere
Torr (1/760) atm Torr
mHg 1000 Torr Meter of mercury
inHg 25.4 mmHg Inch of mercury
inH2O 2.490889 10^2 Pa (*) Inch of water
psi lbf / in^2 Pounds per square inch

P (1/10) Pa s Poise
St 10^-4 m^2 / s Stokes

AA Ampere (base unit)
CA s Coulomb
Fdy ech Nav Faraday
e ech Elementary charge
ech 1.602176487 10^-19 C (*) Elementary charge
VW / A Volt
ohm V / A Ohm
Ω ohm Ohm
mho A / V Mho
SA / V Siemens
FC / V Farad
H Wb / A Henry
T Wb / m^2 Tesla
Gs 10^-4 T Gauss
Wb V s Weber

cd cd Candela (base unit)
sb 10000 cd / m^2 Stilb
lm cd sr Lumen
lx lm / m^2 Lux
ph 10000 lx Phot
fc lm / ft^2 Footcandle
lam 10000 lm / m^2 Lambert
flam (1 / pi) cd / ft^2 Footlambert

Bq 1 / s Becquerel
Ci 37 10^9 Bq Curie
Gy J / kg Gray
Sv Gy Sievert
R 258 10^-6 C / kg Roentgen
rd (1/100) Gy Rad
rem rd Rem

mol mol Mole (base unit)

rad rad Radian (base unit)
circ 2 pi rad Full circle
rev circ Full revolution
deg circ / 360 Degree
arcmin deg / 60 Arc minute
arcsec arcmin / 60 Arc second
grad circ / 400 Grade
rpm rev / min Revolutions per minute

sr sr Steradian (base unit)

h 6.62606896 10^-34 J s (*) Planck's constant
hbar h / (2 pi) Planck's constant
mu0 4 pi 10^-7 H / m Permeability of vacuum
ÎĽ0 mu0 Permeability of vacuum
eps0 1 / (mu0 c^2) Permittivity of vacuum
ε0 eps0 Permittivity of vacuum
G 6.67428 10^-11 m^3/(kg s^2) (*) Gravitational constant
Nav 6.02214179 10^23 / mol (*) Avogadro's constant
me 9.10938215 10^-31 kg (*) Electron rest mass
mp 1.672621637 10^-27 kg (*) Proton rest mass
mn 1.674927211 10^-27 kg (*) Neutron rest mass
mmu 1.88353130 10^-28 kg (*) Muon rest mass
mÎĽ 1.88353130 10^-28 kg (*) Muon rest mass
Ryd 10973731.568527 /m (*) Rydberg's constant
k 1.3806504 10^-23 J/K (*) Boltzmann's constant
alpha 7.2973525376 10^-3 (*) Fine structure constant
α 7.2973525376 10^-3 (*) Fine structure constant
muB 927.400915 10^-26 J/T (*) Bohr magneton
muN 5.05078324 10^-27 J/T (*) Nuclear magneton
mue -928.476377 10^-26 J/T (*) Electron magnetic moment
mup 1.410606662 10^-26 J/T (*) Proton magnetic moment
R0 8.314472 J/(mol K) (*) Molar gas constant
V0 22.710981 10^-3 m^3/mol (*) Standard volume of ideal gas

Np Np Neper (base unit)
dB (ln(10) / 20) Np decibel

Unit Prefix Table:

Y 10^24 Yotta
Z 10^21 Zetta
E 10^18 Exa
P 10^15 Peta
T 10^12 Tera
G 10^9 Giga
M 10^6 Mega
k K 10^3 Kilo
h H 10^2 Hecto
D 10^1 Deka
d 10^-1 Deci
c 10^-2 Centi
m 10^-3 Milli
u ÎĽ 10^-6 Micro
n 10^-9 Nano
p 10^-12 Pico
f 10^-15 Femto
a 10^-18 Atto
z 10^-21 zepto
y 10^-24 yocto

(**) When in TeX or LaTeX display mode, the TeX specific unit
names will not use the `tex' prefix; the unit name for a
TeX point will be `pt' instead of `texpt', for example.
To avoid conflicts, the unit names for pint and parsec will
be `pint' and `parsec' instead of `pt' and `pc'.

Source: https://habr.com/ru/post/279853/


All Articles