WEBVTT captioned by sachac NOTE Introduction 00:00:03.620 --> 00:00:08.799 Hello, my name is Christopher Howard and welcome to my talk. 00:00:08.800 --> 00:00:11.319 This is basically an introduction 00:00:11.320 --> 00:00:15.119 to the built-in Emacs calculator, 00:00:15.120 --> 00:00:18.319 properly known as Emacs Calc, 00:00:18.320 --> 00:00:21.439 particularly from the perspective of someone 00:00:21.440 --> 00:00:27.559 with a technical background such as engineering or electronics. 00:00:27.560 --> 00:00:32.879 I will say, though, my personal interest is not really 00:00:32.880 --> 00:00:37.839 in digital computing or digital calculators, 00:00:37.840 --> 00:00:42.519 but lately has been focused more on analog computing. 00:00:42.520 --> 00:00:46.799 I have, for example, been working to master 00:00:46.800 --> 00:00:50.839 the venerable slide rule, a mechanical computer 00:00:50.840 --> 00:00:57.319 that calculates multiplication powers and logarithms. 00:00:57.320 --> 00:01:02.199 Here's a picture of one. 00:01:02.200 --> 00:01:06.799 It's a physical tool that was used for hundreds of years 00:01:06.800 --> 00:01:08.999 for this sort of thing 00:01:09.000 --> 00:01:16.679 before the handheld calculator was made popular. 00:01:16.680 --> 00:01:18.639 And I also had a project that I did 00:01:18.640 --> 00:01:21.119 for a while to several months 00:01:21.120 --> 00:01:33.119 to build an electronic analog computer. 00:01:33.120 --> 00:01:38.679 A rudimentary attempt of mine, but it's functional, 00:01:38.680 --> 00:01:43.399 and it's basically a 1960s or 1970s style 00:01:43.400 --> 00:01:48.839 electronic analog computer built very much on a budget, 00:01:48.840 --> 00:01:52.559 but the box in the middle is the computer proper 00:01:52.560 --> 00:01:55.719 which has most of the components inside of it 00:01:55.720 --> 00:02:00.199 as well as the potentiometers for setting values, 00:02:00.200 --> 00:02:02.039 and an operation switch. 00:02:02.040 --> 00:02:04.399 There's a patch panel on the left 00:02:04.400 --> 00:02:07.119 for connecting the different integrators, 00:02:07.120 --> 00:02:11.319 amplifiers, multipliers, and so forth together. 00:02:11.320 --> 00:02:16.919 Then the output of the simulation is displayed 00:02:16.920 --> 00:02:19.799 on the oscilloscope on the right side, 00:02:19.800 --> 00:02:25.479 which is a digital oscilloscope. 00:02:25.480 --> 00:02:28.439 To be honest, I think that a talk about analog computing 00:02:28.440 --> 00:02:30.199 would be much more interesting 00:02:30.200 --> 00:02:32.039 than the talk that I'm about to give, 00:02:32.040 --> 00:02:36.639 but unfortunately that would be out of scope for EmacsConf. NOTE What is Calc? 00:02:36.640 --> 00:02:39.919 So instead I will talk about Emacs Calc, 00:02:39.920 --> 00:02:43.359 the digital calculator built into Emacs. 00:02:43.360 --> 00:02:47.519 Emacs Calc, while not being a replacement for software 00:02:47.520 --> 00:02:51.479 like GNU Octave, does have advanced calculator functionality 00:02:51.480 --> 00:02:55.039 that can be useful in engineering, electronics, 00:02:55.040 --> 00:03:00.759 or other technical applications. So I don't want to oversell it, 00:03:00.760 --> 00:03:06.479 but I think functionality-wise, Calc is somewhere in between 00:03:06.480 --> 00:03:12.239 what you'd expect of a decent scientific calculator 00:03:12.240 --> 00:03:23.939 and an advanced graphics calculator. 00:03:23.940 --> 00:03:28.839 So this talk I'll mention is not intended to be a tutorial 00:03:28.840 --> 00:03:33.839 but only a brief introduction to Calc. 00:03:33.840 --> 00:03:37.439 Please refer to the built-in Calc info manual 00:03:37.440 --> 00:03:46.739 for detailed instructions on how to complete operations. 00:03:46.740 --> 00:04:01.479 Turn off my volume here. 00:04:01.480 --> 00:04:05.719 The documentation for Emacs Calc is built-in, 00:04:05.720 --> 00:04:10.439 although on some distributions you may have to install 00:04:10.440 --> 00:04:24.479 the Emacs documentation separately for licensing reasons. 00:04:24.480 --> 00:04:28.599 Calc presents itself as a stack-based calculator 00:04:28.600 --> 00:04:31.599 where entries are dropped onto a stack 00:04:31.600 --> 00:04:36.739 and then an operation is performed on the stack entries. 00:04:36.740 --> 00:04:42.899 For example, I can drop 1.23 onto the stack, 00:04:42.900 --> 00:04:54.279 and then 8.56, and then multiply them together. NOTE calc-algebraic-entry 00:04:54.280 --> 00:05:01.559 It may present itself as a stack-based calculator, 00:05:01.560 --> 00:05:05.399 but indeed, Calc is also capable of accepting input 00:05:05.400 --> 00:05:07.739 in the more well-known algebraic format 00:05:07.740 --> 00:05:10.759 by using the calc-algebraic-entry command, 00:05:10.760 --> 00:05:14.999 which by default is bound to the apostrophe (') key. 00:05:15.000 --> 00:05:19.759 So you type the apostrophe key, enter the algebraic input, 00:05:19.760 --> 00:05:22.759 including parentheses as needed. 00:05:22.760 --> 00:05:28.199 For example, here's a calculation of the resonance frequency 00:05:28.200 --> 00:05:35.039 of a coil which has an inductance of 250 microhenries 00:05:35.040 --> 00:05:41.059 and 160 picofarads, taken from one of my electronics handbooks. 00:05:41.060 --> 00:05:50.019 The formula for that is 1 over 2 pi 00:05:50.020 --> 00:05:57.439 and then the square root of our inductance 00:05:57.440 --> 00:06:06.279 which is in this case 250 microfarads - excuse me, microhenries 00:06:06.280 --> 00:06:19.399 and then the capacitance is 160 picofarads. 00:06:19.400 --> 00:06:24.399 Small typo here. 00:06:24.400 --> 00:06:26.639 Now I need to evaluate that one more time, 00:06:26.640 --> 00:06:30.919 because pi is a symbol. 00:06:30.920 --> 00:06:37.398 I get about 800 kHz resonant frequency. NOTE calc-roll-down 00:06:37.399 --> 00:06:41.679 The command calc-roll-down, 00:06:41.680 --> 00:06:44.199 which by default is bound to the TAB key, 00:06:44.200 --> 00:06:47.919 will swap the top two stack entries, 00:06:47.920 --> 00:06:51.559 which is sometimes useful if you need to manipulate something 00:06:51.560 --> 00:06:56.999 that's further down the stack. 00:06:57.000 --> 00:07:02.039 So I can swap this around and say multiply by two 00:07:02.040 --> 00:07:05.479 and then put it back where it was. 00:07:05.480 --> 00:07:14.039 This command is also capable of rolling the entire stack. 00:07:14.040 --> 00:07:18.899 Say I want to shift them all around. 00:07:18.900 --> 00:07:21.399 This can be done by passing extra arguments 00:07:21.400 --> 00:07:23.559 to the calc-roll-down function. 00:07:23.560 --> 00:07:28.279 That's a little bit inconvenient to do manually, 00:07:28.280 --> 00:07:40.079 so in my init file, I defined here a key definition 00:07:40.080 --> 00:07:45.759 that passes in those arguments correctly. 00:07:45.760 --> 00:07:49.179 I attached this to shift-tab, 00:07:49.180 --> 00:07:52.319 so this way, I can roll the entire stack. 00:07:52.320 --> 00:07:56.159 Then I could change one entry here 00:07:56.160 --> 00:08:03.459 and then put it back where it was. 00:08:03.460 --> 00:08:07.759 So Calc does algebraic input. NOTE Advanced functions 00:08:07.760 --> 00:08:10.159 It also does advanced functions 00:08:10.160 --> 00:08:15.599 that you would expect any handheld scientific calculator, 00:08:15.600 --> 00:08:19.159 including trigonometric functions. 00:08:19.160 --> 00:08:25.319 For example, we can get the sine of a number. 00:08:25.320 --> 00:08:30.719 Now I'll mention here that Calc has multiple modes. 00:08:30.720 --> 00:08:32.319 Right now it's in degree mode. 00:08:32.320 --> 00:08:38.159 You can switch over to radian mode if you want. 00:08:38.160 --> 00:08:42.799 I'm going to put it back in degrees. 00:08:42.800 --> 00:08:49.799 Drop 12 degrees on the stack, and then get the sine of that. 00:08:49.800 --> 00:08:58.179 And then with the inverse sine function, I can put it back. NOTE Solving equations with calc-solve-for 00:08:58.180 --> 00:09:07.519 Calc also has the nifty ability to solve equations for you 00:09:07.520 --> 00:09:13.919 so long as the equation is not too complicated. 00:09:13.920 --> 00:09:19.959 This is using the calc-solve-for function. 00:09:19.960 --> 00:09:31.699 For example, we could enter in an equation algebraically, 00:09:31.700 --> 00:09:36.679 then run calc-solve-for, and we just have to tell it 00:09:36.680 --> 00:09:40.999 what variable we want to solve for. And there we go. 00:09:41.000 --> 00:09:43.199 We can do this manually as well 00:09:43.200 --> 00:09:54.719 just so you can see that we get the same result. NOTE Systems of equations 00:09:54.720 --> 00:09:57.959 Calc is also able to solve systems of equations. 00:09:57.960 --> 00:10:03.439 We can put more than one equation on the stack, 00:10:03.440 --> 00:10:08.959 and then solve for several variables. 00:10:08.960 --> 00:10:13.319 To give a technical example for this, 00:10:13.320 --> 00:10:30.659 I'll show you a resistor network scribble that I did recently. 00:10:30.660 --> 00:10:32.819 Hopefully you can see that. Basically, 00:10:32.820 --> 00:10:38.719 it's fairly simple, a pretty simple resistor network 00:10:38.720 --> 00:10:42.159 with 1 kilo ohm and 10 kilo ohm resistors, 00:10:42.160 --> 00:10:48.959 and using the loop methods, we are calculating the currents, 00:10:48.960 --> 00:10:52.759 the current in each loop, and then that current can be used 00:10:52.760 --> 00:10:58.839 to solve for the voltage of each individual resistor 00:10:58.840 --> 00:11:06.199 if we want to. So at the bottom there we have the equations 00:11:06.200 --> 00:11:11.519 that we come up with as we work through each loop. 00:11:11.520 --> 00:11:19.579 And I'm going to paste that into Calc. 00:11:19.580 --> 00:11:22.719 To save some time, I'm going to copy and paste that 00:11:22.720 --> 00:11:34.259 from my notes instead of typing it out. 00:11:34.260 --> 00:11:38.259 So we have two equations there on the stack 00:11:38.260 --> 00:11:44.719 in one stack entry. We run that calc-solve-for function again, 00:11:44.720 --> 00:11:49.899 and we tell it which variables we want to solve for. 00:11:49.900 --> 00:11:51.959 And voila! Those are our currents, 00:11:51.960 --> 00:11:55.719 which we can then use to get the voltages 00:11:55.720 --> 00:12:00.079 for the individual resistors. NOTE calc-find-root 00:12:00.080 --> 00:12:01.999 I'll just briefly mention 00:12:02.000 --> 00:12:05.839 that if Calc is not able to solve an equation 00:12:05.840 --> 00:12:07.779 with calc-solve-for, 00:12:07.780 --> 00:12:10.279 then you might be helped by another calc function 00:12:10.280 --> 00:12:11.559 called calc-find-root. 00:12:11.560 --> 00:12:14.439 This function basically does a manual search 00:12:14.440 --> 00:12:30.199 for a numerical solution to the equation. 00:12:30.200 --> 00:12:39.959 And there's the documentation page on that. NOTE Derivatives and integrals 00:12:39.960 --> 00:12:44.039 Calc can also solve or find derivatives of functions, 00:12:44.040 --> 00:12:47.579 at least the more straightforward functions. 00:12:47.580 --> 00:12:49.839 For a simple example, 00:12:49.840 --> 00:13:00.559 we can get the derivative of that 00:13:00.560 --> 00:13:11.979 with the derivative function. 00:13:11.980 --> 00:13:17.159 On the other hand, Calc is also capable of figuring out 00:13:17.160 --> 00:13:22.099 indefinite integrals. 00:13:22.100 --> 00:13:26.859 Say we put that function back on the stack, 00:13:26.860 --> 00:13:32.559 and this time, we call the integral function. 00:13:32.560 --> 00:13:35.079 There you go. Of course, you have to add 00:13:35.080 --> 00:13:39.819 your own constant of integration. 00:13:39.820 --> 00:13:43.399 For integrals that Calc cannot figure out symbolically, 00:13:43.400 --> 00:13:46.079 a numerical integration method is available 00:13:46.080 --> 00:13:59.998 through the calc-num-integral command, which is documented... 00:13:59.999 --> 00:14:17.539 The function documentation is available here, more or less. NOTE Programmable functions 00:14:17.540 --> 00:14:20.399 I definitely need to mention 00:14:20.400 --> 00:14:24.759 that Calc is capable of doing programmable functions. 00:14:24.760 --> 00:14:29.619 That is to say, you can program your own functions into Calc. 00:14:29.620 --> 00:14:32.239 There are three separate ways to do this. 00:14:32.240 --> 00:14:36.279 One is through a macro method 00:14:36.280 --> 00:14:41.539 similar to Emacs's usual keyboard macros. 00:14:41.540 --> 00:14:46.519 The second method is to transform an algebraic function 00:14:46.520 --> 00:14:50.859 into a stored function definition. 00:14:50.860 --> 00:14:54.059 And the third is to use Elisp directly. 00:14:54.060 --> 00:14:56.599 Personally, I find that the second method 00:14:56.600 --> 00:15:01.799 is the most practical, the most convenient and practical 00:15:01.800 --> 00:15:08.059 in my opinion. So I'll give a quick example of that. 00:15:08.060 --> 00:15:14.159 So I could... Let's say I wanted to have a function 00:15:14.160 --> 00:15:20.699 for calculating capacitive reactance. 00:15:20.700 --> 00:15:28.899 I'll define that in algebraic mode first. 00:15:28.900 --> 00:15:33.639 The function for that is 1 over 2 pi 00:15:33.640 --> 00:15:41.599 the frequency and the capacitance. 00:15:41.600 --> 00:15:44.959 Drop that on the stack. You see, it does automatically 00:15:44.960 --> 00:15:52.079 get simplified a little bit, but it's the same function. 00:15:52.080 --> 00:15:58.839 And then I press letters Z and F. Do that again. 00:15:58.840 --> 00:16:06.239 Z and F to start transforming that into a stored function. 00:16:06.240 --> 00:16:11.039 It asks me to select a user key, a single key press. 00:16:11.040 --> 00:16:15.479 I'll use the letter c. 00:16:15.480 --> 00:16:19.079 Then it's going to ask for a longer command name. 00:16:19.080 --> 00:16:24.639 I've actually defined this once before, so it prefilled in 00:16:24.640 --> 00:16:38.339 that command name. 00:16:38.340 --> 00:16:42.999 Then I need to enter which variables in the formula 00:16:43.000 --> 00:16:46.559 are actual arguments, rather than just symbols 00:16:46.560 --> 00:16:52.559 to be evaluated later. I prefer to put this in with frequency 00:16:52.560 --> 00:16:54.279 and the capacitance after that, 00:16:54.280 --> 00:16:57.799 but actually in this particular case, 00:16:57.800 --> 00:17:07.339 it doesn't matter at all to the mathematics. 00:17:07.340 --> 00:17:11.399 So, now all I have to do, that this is defined, 00:17:11.400 --> 00:17:15.199 is I can drop the frequency on the stack, 00:17:15.200 --> 00:17:24.399 which we'll say, for this example, will be 4.5 MHz, 00:17:24.400 --> 00:17:32.279 and then drop on the capacitance, which in this example 00:17:32.280 --> 00:17:40.319 will be 22 pF. 00:17:40.320 --> 00:17:42.439 Then I'll call the function that I just defined. 00:17:42.440 --> 00:17:45.239 I don't really like having to try to remember 00:17:45.240 --> 00:17:48.679 the short letters that I've come up with, 00:17:48.680 --> 00:17:57.839 so I'll just use the longer name. 00:17:57.840 --> 00:17:59.799 I need to evaluate one more time 00:17:59.800 --> 00:18:05.619 because the symbol pi is in there and not yet evaluated. 00:18:05.620 --> 00:18:07.539 And so if I've done that right, 00:18:07.540 --> 00:18:12.159 we have a capacitive reactance of about 1600 ohms. NOTE Plotting 00:18:12.160 --> 00:18:16.839 As the last feature that I'll mention here, 00:18:16.840 --> 00:18:24.059 Emacs Calc does have an interface with gnuplot, 00:18:24.060 --> 00:18:30.799 if you want to have Calc work as your graphing calculator. 00:18:30.800 --> 00:18:33.159 I do need to be honest and mention 00:18:33.160 --> 00:18:35.579 that I don't generally use it myself 00:18:35.580 --> 00:18:39.719 because there's another program in GNOME 00:18:39.720 --> 00:18:43.499 that I've found to be generally more convenient 00:18:43.500 --> 00:18:47.399 for the things that I want to graph quickly. 00:18:47.400 --> 00:18:53.399 But I think I can give you a simple example. 00:18:53.400 --> 00:19:00.339 So first, we need to drop a range on the stack. 00:19:00.340 --> 00:19:06.619 Let's say 0 to 10. 00:19:06.620 --> 00:19:11.639 And then we need to drop the function on the stack. 00:19:11.640 --> 00:19:17.839 And then I believe it's the letters g and f that graph this. 00:19:17.840 --> 00:19:22.319 Let's see. Yep, there we go. 00:19:22.320 --> 00:19:25.059 So there's our function and it looks nice. 00:19:25.060 --> 00:19:26.659 That was pretty easy. 00:19:26.660 --> 00:19:29.019 That's the fast way to do it. 00:19:29.020 --> 00:19:32.839 I will, as a disclaimer, mention that 00:19:32.840 --> 00:19:34.159 using this quick approach, 00:19:34.160 --> 00:19:38.759 that sometimes more complicated graphs 00:19:38.760 --> 00:19:39.999 will not turn out nicely, 00:19:40.000 --> 00:19:44.339 because by default, the resolution will be pretty low. 00:19:44.340 --> 00:19:48.119 That is to say it's... gnuplot is going to be 00:19:48.120 --> 00:19:49.899 skipping a lot of points 00:19:49.900 --> 00:19:52.039 and so you'll have to learn a bit more 00:19:52.040 --> 00:19:55.319 about how to use the interface, 00:19:55.320 --> 00:19:59.519 what parameters to pass if you want all your graphs 00:19:59.520 --> 00:20:03.699 to come out looking nice. 00:20:03.700 --> 00:20:08.799 So that covers all the features that I wanted to cover. NOTE Wish list 00:20:08.800 --> 00:20:13.279 I wanted to briefly mention a wish list of items 00:20:13.280 --> 00:20:16.679 that I'd like to see in Calc. 00:20:16.680 --> 00:20:23.639 One of them would be improper integrals. 00:20:23.640 --> 00:20:25.159 So that's like our definite integrals 00:20:25.160 --> 00:20:32.859 except for where a limit of integration is infinity. 00:20:32.860 --> 00:20:38.559 That's something that can be useful in a few applications. 00:20:38.560 --> 00:20:41.079 Something else that would be neat to have would be 00:20:41.080 --> 00:20:45.679 annotations for row entries. So for example 00:20:45.680 --> 00:20:48.819 if I was putting together a sum of numbers 00:20:48.820 --> 00:20:53.279 for, say, my monthly budget, 00:20:53.280 --> 00:20:57.479 let's say I was paying $2,000 for my rent 00:20:57.480 --> 00:21:03.831 and let's say $800 a month for my groceries, 00:21:03.832 --> 00:21:07.931 (a lot of kids to feed there) 00:21:07.932 --> 00:21:14.565 and then say another $60 for dining out, and so on, 00:21:14.566 --> 00:21:18.259 it would be nice if there was some way 00:21:18.260 --> 00:21:21.319 to put a little annotation next to each number 00:21:21.320 --> 00:21:23.399 so that you could remember 00:21:23.400 --> 00:21:27.039 what the meaning of that number was more easily. 00:21:27.040 --> 00:21:31.199 I actually looked into programming this into Calc myself, 00:21:31.200 --> 00:21:35.919 but discovered that it would require reprogramming 00:21:35.920 --> 00:21:41.839 quite a bit of Calc to make that work well 00:21:41.840 --> 00:21:43.479 across all calc functionality, 00:21:43.480 --> 00:21:46.939 and so, eventually, I gave up. 00:21:46.940 --> 00:21:51.139 But I'd still really like to have that feature. 00:21:51.140 --> 00:21:52.039 The final thing, though 00:21:52.040 --> 00:21:54.579 I think this would not necessarily belong in Calc, 00:21:54.580 --> 00:21:57.919 I think it would be cool if Emacs had some way 00:21:57.920 --> 00:22:00.599 to run numerical solutions 00:22:00.600 --> 00:22:02.599 for systems of differential equations, 00:22:02.600 --> 00:22:06.019 also known as a differential analyzer. 00:22:06.020 --> 00:22:09.279 So this would allow you to be able to set up simulation models 00:22:09.280 --> 00:22:11.679 involving systems of differential equations, 00:22:11.680 --> 00:22:14.879 for example, a spring mass system, or pressure temperature, 00:22:14.880 --> 00:22:18.039 or what have you, and then run the simulation 00:22:18.040 --> 00:22:22.119 using numerical approximation. 00:22:22.120 --> 00:22:24.079 Maybe it would be silly 00:22:24.080 --> 00:22:25.999 to actually put that in Calc itself, 00:22:26.000 --> 00:22:30.339 but a nice interface maybe to some other software, 00:22:30.340 --> 00:22:33.299 simple software that did that, 00:22:33.300 --> 00:22:35.779 an easy to use interface for that 00:22:35.780 --> 00:22:38.599 would be really great. NOTE Wrapping up 00:22:38.600 --> 00:22:41.800 So that's my entire talk. 00:22:41.801 --> 00:22:44.534 I'll just mention some information. 00:22:44.535 --> 00:22:48.365 If you want to learn more about me 00:22:48.366 --> 00:22:50.119 or things that I'm interested in, 00:22:50.120 --> 00:22:57.779 I do not any longer have a web presence. 00:22:57.780 --> 00:22:59.659 I don't have a website anymore, 00:22:59.660 --> 00:23:03.359 but I do have a Gemini capsule 00:23:03.360 --> 00:23:07.139 that I post to all the time. 00:23:07.140 --> 00:23:13.879 And if you can install, if you're willing to install the... 00:23:13.880 --> 00:23:19.079 Gemini browser known as Elpher 00:23:19.080 --> 00:23:23.698 into Emacs, which is available from ELPA, 00:23:23.699 --> 00:23:27.359 then you can browse directly to it 00:23:27.360 --> 00:23:31.439 and look around my Gemini capsule. 00:23:31.440 --> 00:23:35.920 Thank you very much.