Julia as a CLI Calculator

krjt | 198 points

This seems to be a very neat intro into Julia and a practical use case. I’ve been postponing to learn the language for a while now, but from what I heard/seen it seems to be extremely well designed.

dgb23 | 4 years ago

I love the julia language. I would love to use julia as a command line calculator. Unfortunately, starting the interpreter is excruciatingly slow, an order of magnitude slower than octave or python (and several orders of magnitude slower than bc or dc).

enriquto | 4 years ago

The typography in this post is beautiful (as well as the content), anybody know where this comes from? Did the author do it by hand or is it something off the shelf?

Asking for a friend...

jaquers | 4 years ago

I use Mathematica as my advanced calculator. It’s a functional language with an extremely vast and consistent mathematical and non-mathematical library, top notch symbolic capabilities (SymPy is just child’s play in comparison), top notch numerical capabilities, and very decent performance. You certainly don’t need to write your own rotation function — well, 2D rotation is trivial, but what if you want 3D rotation around a vector? Mathematica’s got you covered. Got a command line mode too, see MathKernel.

The downside: it’s not free (as in beer or speech).

oefrha | 4 years ago

I've been using Julia as my CLI calculator for a few years now - cool to see this post! While teaching an online Linear Algebra class, I would use Julia to construct examples and quickly solve them. The syntax sugar of all the functional idioms make it very fun and easy to express more complicated ideas. This is a cool introduction- I was not aware (or had forgotten) about function chaining. Really curious about using more of Latexify as well :)

On an unrelated note, (as a mathematician) I have started using Maple a lot more as my analytic computational helper. Never really gave it the time of day in years past - it is quite powerful and intuitive. Funny note: I recently copied and pasted output of a Maple command into my Julia code and it was syntactically correct!

ohsonice | 4 years ago

Since this is a calculator topic, I'd like request your input on which calculators you use.

I use the Emacs calculator (M-x calc) and XCalc (http://www.tordivel.no/xcalc/) as I like to use RPN calculators. The Windows 10 calculator is painful and slow. XCalc has a mini mode, where it will sit as a small, single line without distracting you too much. I like that feature very much.

One thing with Emacs is that I forget the shortcuts for non-frequent calculations that I'd have to go and search for it.

knight17 | 4 years ago

A problem with Julia is that it does not parse input numbers with arbitrary precision by default. Example:

  julia> sin(1.461920290375737576933544899379e+31)
  -0.9468766486679395

  julia> sin(parse(BigFloat, "1.461920290375737576933544899379e+31"))
  0.6864670207863400975666631018263839509022548965872940746039593018855528710432756
Fricas is better in this regard, some links:

https://en.wikipedia.org/wiki/FriCAS

https://fricas.github.io

https://github.com/fricas/fricas

http://www.euclideanspace.com/prog/scratchpad/fricas/

EDIT: some info about FriCAS: it's implementation is currently based on Common Lisp, on top of which a language called "Spad" (short for Scratchpad) is implemented. Fricas is a fork of the Axiom computer mathematics system, which in turn is a continuation of Scratchpad. EDIT: and, at least with SBCL, it can use GMP for bignum.

I only skimmed the article, but I believe Fricas supports all uses of Julia mentioned in the article, and more, like advanced symbolic integration.

nsajko | 4 years ago

Oh, brilliant! This is exactly what I've been looking for. Didn't realize the syntax was so succint. So far I've preferred excel for my multidimensional calc needs but this fits my needs exactly (need to do simple back-of-the-envelope vector calculations time to time).

fsloth | 4 years ago

I really wish there was a language based on Swift that would include most of the nice language, REPL and library characteristics of Julia (and perhaps few from Dart). That would get really close to a "perfect language" for me.

Overall I like Julia - and use it as a CLI calculator as well - but there are several things that prevent it from being a good general purpose language:

- OOP support is "meh".

- str.parse(Int).abs is more readable than abs(parse(Int, str)). And this is a very simple case, the readability difference is greater in more complex expressions.

- 1-based indexing.

- Lack of good support for nullability, think of "if let", "guard let", "??", "object?.property".

- There are some more, this is just from the top of my head.

EDIT: A better example of why Julia's syntax is less readable:

`images.first.resize(100, 100).crop(25, 25, 75, 75)`

versus

`crop(resize(first(images), 100, 100), 25, 25, 75, 75)`

RivieraKid | 4 years ago

One of my main gripes about matlab and that Julia unfortunately adopted is the dot syntax. Any other symbol would have worked, but the dot is just way too easy to overlook.

I think it is because it was initially developed for people primarily working with matrices.

Most of the things I do is actually based around multidimensional arrays so when I do operations I almost always want element-wise operations. Unfortunately you often don't even realise you do the wrong operation, just the result is wrong. So I have spend lots of time debugging, until I realised I had a missing dot somewhere, and then spending again significant time trying to find where the dot is missing.

I therefore always found the complaint about np.dot(x,y) being too verbose interesting, because I like that, it makes it explicit and is difficult to overlook.

cycomanic | 4 years ago

I've been using Julia REPL as s scientific calculator for a few months. I'm impressed how wonderful it is. Thank you for the article!

plashchynski | 4 years ago

One thing I miss is RPN-string input. Unlike normal RPN where you load the "stack," for RPN string you can enter

> 2 3 + 4 *

as one line, press enter, and get 20. This lets you take advantage of the no-parens nature of RPN while viewing the expression on one line rather than flipping through the stack. Only Windows PowerToy scientific calculator had this mode, that I know of.

starpilot | 4 years ago

This is a great article - interesting, well-written and beautifully presented!

A minor quibble in the typography - the use of small caps for every initial letter is slightly catching my eye every time I see it. Perhaps I don't see enough similar typography on the web to make it feel more natural ¯\\_(ツ)_/¯ ...

cormullion | 4 years ago

I use python CLI for exactly the same :)

krzyk | 4 years ago

Nice article! I'm the creator of the Grassmann.jl package, which was mentioned in the article :)

DreamScatter | 4 years ago

I wonder if it'd be possible to have a blend between this and https://calca.io where the repl output appears (and change dynamically) as specialized comments within a markdown-friendly format.

seltzered_ | 4 years ago

Julia is nice for scientific serious work, but for cli calc J (jsoftware.com) really rocks!

rscho | 4 years ago

from EasyJ https://www.jsoftware.com/books/pdf/easyj.pdf

J in its simplest use acts just like a desk calculator.

If you are working at a terminal, type the following input, pressing the “enter” key after each line :

   3+4 
7

   5*20 
100
bryanrasmussen | 4 years ago

My personal choice for this is Jupyter and Sagemath.

29athrowaway | 4 years ago

My main calculator is gnu calc which is the calculator built into emails. It tends to hit various ceilings where it can’t go much further but it has lots of nice features:

1. The data I want to work with tends to be in my text editor so it makes sense to have the calculator there too. Macros can be used to fix up formatting to input numbers

2. Speed of input makes it so much nicer to use. If I look at the examples in the article, here is a comparison. Julia:

  7 / 4
  x = ans + 2 im 
  exp(x)
  A = [...]
  A * [...]
  [a^2 for a \in A]
Emacs:

  7 RET 4 /
  (0,2) + s t x (but I probably wouldn’t use a variable)
  s r x E (if I used the stack this is just “E”)
  [(same but RET or , not SPC)] s t A
  s r A [...] *
  s r A V M I Q (that’s vector map inverse square root)
These examples make emacs look bad because I’ve replicated saving things to variables it actually I’d just use the stack. I guess in Julia you can press the up arrow. There are other examples like 1 |> exp |> sin is just 1 E S in Calc (and those operations broadcast over vectors automatically, although I think emasculated does the right thing and doesn’t broadcast exp over matrices).

Skipping the massive tangent of non-calculator things about Julia, we move on to a discussion of plotting. There isn’t a Unicode-art plotting mode in emacs because we have had graphical terminals for over 30 years. In emacs you plot with g f and can then modify your plot with other commands. In just about every language you need to call a function called plot so you’re already losing on characters before you’ve even started. If plotting is fast and easy then exploration is easy.

3. The calculator has lots of useful features that fit well together. Plotting is built in. As are unit conversions, a bunch of symbolic algebra (simplification, differentiation, basic integration, curve fitting, solving equations, numeric integration/solutions, and a super nice facility for editing expressions). There’s a bunch of other functions for stats, vectors (both linalg and things like map/reduce), number theory, physics, etc. There are language modes for things like latex output too. Most stuff works on a bunch of number types (bigints, rationals, arbitrary precision floats, infinity, intervals thereof, complex numbers, error forms, symbolic expressions, dates)

4. I want to reiterate the point about speed. The thing I care about being fast is telling the computer what to do. It takes basically 0 time to do most mathematical operations so latency is dominated by input. So even though emacs uses interpreted emacs lisp and doesn’t even use hardware floating point numbers, it still feels faster than Julia with its optimising JIT. If I want to do something with a large amount of data then I can either wait a few seconds or realise that I’ve gone beyond calculator work and use something better suited.

I think Julia is a great language and it has great advantages over emacs for numerical computing. A big one is that the language and type system is much more naturally extensible for this sort of thing. But I don’t feel that it makes a particularly good calculator. The main reason to think that it is a good calculator is that the alternatives (bash/bc/python/matlab) are so bad.

dan-robertson | 4 years ago