Mysterious Emacs: Journey into the 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 programming lesson, it was urgent to translate 377 into the decimal system. We did not begin to lay it out in triples, to represent powers of eight, or simply not to pay attention to it and to miss this unimportant moment. We launched the calculator in the operating system, moved its window to the center of the screen and there we figured out everything. Yes, this is it - the number 255.

    Of course, there is no need to tell what happens in more complex cases. Sometimes it comes to a leaf in a notebook lying next to a computer just for such cases. Everyone has a parabola or something, crossing something else.

    First contact



    As you would expect, when using Emacs for months or just many years, brain Emacs occurs. Therefore, such cases are not in vain, and in the middle of the night the question arises in my head: “How can I translate 377 into Emacs?” Does he even know how? Well, at least 8 will raise to a power? ".

    The next morning, recharged with energy in the form of tea with a handful of chocolates, we open Emacs and begin his study.



    We start with operations



    It is clear that by entering Alt + x and calc, the first thing we see is a regular stack calculator, which, probably, were written by a hundred with these hands. 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 + - * / Then Oh! And here is the square of eight! And where is the square, there is the cube!

    5 7 +

    5 7 -

    5 7 *

    5 7 /




    8 8 *




    We open a help



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

    I won’t say that this time it all started with the instructions, because unlike many people, I don’t turn off the Emacs menu, but use it to remember keyboard shortcuts in all kinds of rare modes.

    So this time I started the review with the menu of the calculator.



    In order to open the help, you need to enter Ctrl + x + * + i. And to open the 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 in the process of other work.

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

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

    (calc-dispatch-help ARG)

    C-x* 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, i.e., a number.
    Z calc-user-invocation. Invoke Calc in the way you defined with `Z I' 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 (`C-x * *') to turn Calc on or off using the same
    Calc user interface as before (either C-x * C or C-x * K; initially C-x * C).



    Raise the bar



    It took about a day 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 works.

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

    Factorial 3000 (a kind of speed check) is calculated not without difficulty, but still it is calculated in fifteen seconds.

    Transposing a matrix, finding its determinant, or scalar multiplication of vectors is easy and whistling.

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

    Continue



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

    But it was not required. It turned out that it was enough just to introduce three equations, and then ask for their solution. Yes, just like in Tungsten.

    We find the first system that comes across. 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 3 vp vector, press a P and in response to the question "What variables to look for roots?" enter xyz.

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




    A vector with answers will appear on the stack, then we just press y and the vector with answers is inserted at the cursor position in the original buffer with equations. If we left the cursor at the end of the system of equations, then the answer would be inserted there.

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




    It's time to turn around



    Stack strength



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

    So the hours go on, until at one fine 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 a certain 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 and such a number."

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

    1: 5


    (Enter), then it will be copied and we will have Now this two is equal to the number of elements on the stack, it is the address of the first number five, and the number 1 is the address of the second number five. The team

    2: 5
    1: 5




    there is an argument that defaults to 1. When we click  , it copies so many elements from the top of the stack (the top is at the very bottom). Therefore, if we introduce Alt + 2 + , he will take two elements and copy them. If the argument is negative, then he takes an element with this address (if minus is removed). Therefore, if we now introduce Alt + - + 4 +

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


     , it will copy the item at address 4. Most basic operations work in the same way. If you need to remove three elements, then write Alt + 3 +

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


     where Is a backspace. If you need to delete the item with address 3, then write Alt + - + 3 + .

    If you read the help in even more detail, then to these operations you can find such peculiar operations from the antiworld, consisting of black matter. For example, for there is an operation Ctrl + j, for there is Alt +  , and for there is Alt +  .

    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 will not copy three elements, but an element with address 3, and if you enter a negative argument, it will copy not an element with address 3, but three elements.

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

    There are many ways



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

    For example, how can you enter a matrix?

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

    This is turning into this second that comes to mind - this is the use of internal means. The matrix is ​​three vectors. The third thing that comes to mind is the use of an internal and convenient means. And the fourth thing that comes to mind is “Can’t you just enter it?” The pack command has a symmetric one - unpack, so if you need to decompose the matrix into vectors or decompose a vector into numbers, then we just press vu.

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




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






    1 2 3 3 v p
    4 5 6 3 v p
    7 8 9 3 v p
    3 v p




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




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




    We attract external funds



    As you pick around in the calculator, you suddenly begin to pay attention to the surrounding buffers and the question gradually arises: “What means of ordinary Emacs can be attracted to when working in the calculator?”

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

    For example, enter the expression Then press F3 to start recording the macro. Then press ad to calculate the derivative. Then to the question “By what variable?” enter x. Then press F4 to end the macro recording. We cancel the calculation by pressing Ctrl + / or applying the internal cancellation of the calculator - the letter U. Having the original expression, press Alt + 3 + F4 and as a result we have the third derivative

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








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




    1: 60 x^2 + 24


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

    We leave to the outside world



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

    And then one fine moment 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, can a fox jump so many times right to the exact point if it is not computer? This thing needs to be fixed.

    We have the following text. Hover over 1024 and carefully press Ctrl + x + * + w. Then press Q, and then gently press Ctrl + x + * + w. And we get it. 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.

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






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




    But the same



    I remembered what I went for

    Ctrl + x + * + *
    8#377
    d 2
    d 8
    d 6
    d 0
    Ctrl + x + * + *


    And finally



    There was a time when I wrote a function in python. I am enclosing here a list of units that he can convert, in the calculator they are by uv.

    2016
    1
    1
    Alt + - + 14 v p
    255
    +




    List of units
    Calculator Units Table:

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

    Unit Type Definition Description

    m m 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

    s s 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

    g g 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

    J N 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 h c / 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

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

    K K K Degree Kelvin (base unit)
    dK K K Degree Kelvin
    degK K K Degree Kelvin
    dC C K Degree Celsius
    degC C K 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

    A A Ampere (base unit)
    C A s Coulomb
    Fdy ech Nav Faraday
    e ech Elementary charge
    ech 1.602176487 10^-19 C (*) Elementary charge
    V W / A Volt
    ohm V / A Ohm
    Ω ohm Ohm
    mho A / V Mho
    S A / V Siemens
    F C / 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'.


    Also popular now: