14 ms·
Julia as a CLI Calculator
- dgb23 6y agoThis 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.
- dunefox 6y agoYou should, it's a fun, useful language. It's starting to replace python for my use cases.
- jweir 6y agoI used it as my CLI calculator, just like the articles title. I used to use Ruby for this, but Julia is more concise but still readable. Concise enough I have written useful applications that can fit in a tweet.
- enriquto 6y agoI 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).
- Sukera 6y agoOne way to alleviate the slowish startup is to use PackageCompiler.jl (discussed at the end of the article https://krasjet.com/rnd.wlk/julia/#packagecompiler.jl https://krasjet.com/rnd.wlk/julia/#packagecompiler.jl). I like that there's more than just the standard calculator operations available at the REPL, but I can see why you wouldn't use it if you don't need it and bc satisfies your needs.
- BubRoss 6y agoI remember years ago when caching was about to be integrated in the next version and be here in a few weeks. Now it has been half a decade of people complaining about how slow it is for his to keep recompiling the same thing over and over.
- Sukera 6y agoI've been using julia for about three years now and from my interactions with devs and the issues on github, caching more code has only been considered in the past year because before then, the priorities were in getting the language stable, to 1.0, and on other lower-hanging performance fruit. "Just cache more" turns out to be a harder problem than it seems at first sight, if by the very nature of the language loading a package can invalidate your caches. It has to be done with much caution, in order to reduce those invalidations as much as possible, which is why it takes time to figure it out.
- cbkeller 6y agoYeah, Julia has had some form of precompilation/caching for ages now, but sorting out all invalidations appears to be highly nontrivial [1] and devs have had more fundamental priorities pre-1.0. There have been some substantial improvements to latency in the meanwhile nonetheless, and PackageCompiler.jl is a viable option as well. [1] https://github.com/JuliaLang/www.julialang.org/pull/794 https://github.com/JuliaLang/www.julialang.org/pull/794
- BubRoss 6y agoJust saying caching is difficult doesn't gel with my experience of what seems like low hanging fruit. I realize a lot of Julia ends up being templates, but when just one line of importing the same plotting package always takes 30 seconds or so, I get suspicious of the idea that all the usability warts are super difficult to solve. This has always been a huge complaint for Julia and it seems like one of those things that happens to new languages. People are excited, there are a few big rough edges that drive people away, they get ignored or underprioritized for WAY too long, and a giant window of opportunity is missed. I think of D with weeding out garbage collection, go with generics, D with tools and infrastructure, Zig with its refusal to parse carriage returns or tabs, Rust with compilation times, everyone with a lack of an easy road to a GUI, Jai not being released at all... I think instead of getting lots of areas to a decent stage, languages end up trying to be the best in a single area and placate their hardcore users, when things like C++ succeed by not having huge blindsides and pitfalls, even if it ends up a little rougher in many areas.
- dunefox 6y agoSimply loading the REPL takes half a second for me. I use it all the time as a simple calculator.
- enriquto 6y ago> Simply loading the REPL takes half a second for me. I juse it all the time as a simple calculator. Half a second is precisely what is meant by "excruciatingly slow". If I need a simple calculator, I expect to be able to use it to compute intermediary results in a shell loop. If my loop has one thousand iterations, you are spending about 10 minutes just initializing the interpreter. This is absurd. Of course, you can rewrite your whole loop upside down, so that the interpreter is called outside the loop. But this kind of defeats the goal of considering julia to be a general-purpose calculator, orthogonal with its usage patterns.
- 4gotunameagain 6y agoI think the focus is on interactive use, there are surely better options for scripts and one liners
- dunefox 6y agoWell, we have different ideas of calculators then.
- grandinj 6y agothat sounds like a case where it would be best to pipe stuff into julia and read the output. or hack up a daemon-process thing where you can start it up outside the loop, then call it inside the loop
- Sukera 6y agoThe strength of using the julia REPL as a "calculator" is not in doing somewhat trivial calculations like dividing two numbers or quickly multiplying two things, and that's fine. That's not a goal of the REPL and julia is indeed overkill for those kinds of things. However, if you're mathematically inclined and sketch out a few matmuls like in the OP article, julia does have the benefit of making those kinds of interactions more bearable from a syntactic point of view. That's where it shines. And like I've posted above, if you use certain packages frequently and restart the REPL frequently, why not use PackageCompiler to compile your hot paths into your binary, like described in the article (admittedly at the end)? That way, even the first call in a new session will be fast because it has already been compiled. On top of that, the core devs are very much aware that this is a problem and it's actively being worked on.
- eismcc 6y agoMaybe a daemon model would be helpful here. REPL client could just boot instantly and forward operations to an already running Julia daemon
- eigenspace 6y agoBesides the other suggestions, you can do things like start julia using the --compile=min or -O0 command line flags to reduce / eliminate compilation overhead and make it startup quicker. The beta version of version 1.5.0 starts up in 0.25 seconds with --compile=min and I believe it's the same on 1.4.0
- leephillips 6y agoI suspect that the author starts the julia REPL up in the morning and leaves is running all day, so startup time would not be important.
- junke 6y agoI have a Lisp process that has been running for weeks now (window manager + devel. stuff), and any time I need to compute something I can go the REPL. I suppose Snail could be used in a same way (https://github.com/gcv/julia-snail https://github.com/gcv/julia-snail)
- socialdemocrat 6y agoHow long does it take to start on your computer? If I have run it before. It takes less than 0.25 seconds. I use a several years old Mac. On none of my computers do I experience startup times which has any practical relevance to being productive. The only real problem I have experienced in Julia is using plotting libraries and those you can include in your Julia image with PackageCompiler.
- pavon 6y agoFor me on Debian stable running version 1.0 takes about a second or two to start the repl the first time, less than a second on subsequent runs. On Windows 10 running version 1.3, it takes 10s of seconds every time. Also often times on windows it will load but never display anything until I press a key, so that timing is with me spamming the enter key.
- fsloth 6y agoOh, 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).
- nsajko 6y agoA 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://en.wikipedia.org/wiki/FriCAS https://fricas.github.io https://fricas.github.io https://github.com/fricas/fricas https://github.com/fricas/fricas http://www.euclideanspace.com/prog/scratchpad/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.
- baggepinnen 6y agoThis is unlikely to be a problem for the vast majority of people using julia as a calculator
- nsajko 6y agoThe problem is that you won't know when your results are wrong because of low input precision. And when you do know, things will be a bit more complicated.
- eigenspace 6y agoYou can do julia> big"1.461920290375737576933544899379e+31" 1.461920290375737576933544899379e+31 julia> sin(big"1.461920290375737576933544899379e+31") 0.6864670207863400975666631018263839509022548965872940746039593018855528710432756 to succinctly parse arbitrary precision literals.
- johnmyleswhite 6y agoIt seems like it would be pretty easy to make a REPL mode that did the big conversions automatically. Not sufficient to protect a naive user, but arbitrary precision floating point is never safe.
- knight17 6y agoSince 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/ 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.
- majewsky 6y agoMy favorite calculator of all time is units(1). For example, to calculate how long it takes to download a 20 MiB file over a 56 kbit dialup connection: $ alias units='units --verbose' # in your shell rc $ units Currency exchange rates from FloatRates (USD base) on 2019-06-05 3460 units, 109 prefixes, 109 nonlinear units You have: 56 kibibit/second You want: hour/20 mebibyte reciprocal conversion 1 / (56 kibibit/second) = 0.8126984126984126977077949 hour/20 mebibyte 1 / (56 kibibit/second) = (1 / 1.23046875) hour/20 mebibyte Note that if you are on macOS, you want to install a more recent units via homebrew. The one shipping with the base system is positively ancient.
- drunkpotato 6y agoI use insect almost daily ( https://github.com/sharkdp/insect https://github.com/sharkdp/insect ). It’s a command line calculator, not sure how well it would integrate with comint on emacs. Unit conversions built-in. I don’t know whether it has an RPN mode. The other thing I do frequently is fire up a python3 repl for quick hits. I just did it yesterday for a complex mass file rename I couldn’t figure out how to do with shell variable replacement. With python it was easy with glob and os.rename.
- dTal 6y agoQalculate is the best desktop calculator I've used. It has a command line version, qalc. Qalculate takes unit-aware calculation to the next level by including an algebraic solver. You don't have to rearrange everything to put your unknown on one side - you can just chuck 'x' into your equality somewhere and it'll work it out. For example, to verify the what-if xkcd assertion that an A10's main gun will propel Randall's car up to 7mph in 3 seconds: (60/s) × (x s) × (395 grams) × (1010 m/s) = (3500 lbs)(70 mph) ≈ x = 2.0754319 s 2 seconds! Maybe Randall's car is heavier than 3500 lbs. How much heavier? (60/s) × (3 s) × (395 grams) × (1010 m/s) = (x lb)(70 mph) ≈ x = 5059.1879 More like a Dodge Durango. Unless he's accounting for friction. You see how nice it is to be able to swap around the unknowns in an equality. Other great features are a large number of built-in constants, an astounding variety of built-in functions - it's practically a CAS - and a command history, which is where I pulled the above example from. It also has a plugin for Plasma's krunner, so you can do quick calculations right there in the dropdown. To summarize: Qalculate is the shit.
- krzyk 6y agoI use python CLI for exactly the same :)
- Hamuko 6y agoI used to use Python REPL for that until I switched to fish shell. Now I just run $ math "999.999 / 3" 333.333 Granted, I don't really do any complex calculations in my terminal.
- shreyasminocha 6y ago> Granted, I don't really do any complex calculations in my terminal. Same. I use insect — https://github.com/sharkdp/insect https://github.com/sharkdp/insect
- cmrdporcupine 6y agoI have been using python like this for over two decades. In fact these days I almost never write Python programs themselves, not liking the language much at all anymore. But I still habitually fire up Python to do calculations. I may switch to Julia, though. I really prefer the language. Wish I could use it at work.
- stellalo 6y agoPython is great, but if you quickly want to play with matrices and vectors, then it’s a no-go
- RivieraKid 6y agoI 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)`
- leephillips 6y ago“OOP” is an anemic special case of the multiple dispatch that Julia provides. So if you don’t know any better you can program in that style using Julia. “str.parse(Int).abs is more readable” No, but it makes sense if you also think that OOP makes sense. You can index based on anything you want.
- RivieraKid 6y agoNo it's not. OOP is regularly used for new projects by experienced programmers. Julia doesn't have interfaces for example. I know that I can set indexing to be 0-based. Not a good solution.
- cbkeller 6y agoRegardless of what is a subset of what, I certainly agree with the OP that OOP feels "anemic" in comparison to the dispatch-based paradigm of Julia. I can't tell you how many times I've seen disparate packages written by different authors with no explicit effort towards interoperability "just work" together in Julia (e.g., [1]), something I have never seen to a comparable degree in any other language. [1] https://www.youtube.com/watch?v=kc9HwsxE1OY https://www.youtube.com/watch?v=kc9HwsxE1OY
- bryanrasmussen 6y agofrom EasyJ https://www.jsoftware.com/books/pdf/easyj.pdf 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
- oefrha 6y agoI 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).
- cytzol 6y agoI know it's a very general question, but can I ask what sort of calculations you perform using Mathematica? Every time it has a new release, I download the demo and play around with it, but I never get as far as using it to actually compute anything that other CLI calculators couldn't do.
- oefrha 6y agoI’m a physicist so all kinds of numerical and symbolic calculations, e.g. very complicated integrals, differential equations, integral equations, and so on. I also work on math problems either seriously (way back when I was an undergrad I worked on some number theoretical problems where computational insight is very important) or for fun (think Project Euler style problems). If you just need to a calculator to do everyday arithmetic or unit conversions, or even simple physics calculations with units, it’s serious overkill... I certainly hardly ever need it when I’m wearing my programmer hat. I guess to answer is that the boundary between a calculator and a full blown computational environment is very blurry to me.
- jointpdf 6y agoMathematica is free on Raspberry Pi, so someone could put together an amazing physical calculator: - https://www.wolfram.com/raspberry-pi/ https://www.wolfram.com/raspberry-pi/ - https://blog.wolfram.com/2019/07/11/mathematica-12-available-on-the-new-raspberry-pi-4/ https://blog.wolfram.com/2019/07/11/mathematica-12-available...
- jaquers 6y agoThe 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...
- huy-nguyen 6y agoProbably derived from tufte.css https://edwardtufte.github.io/tufte-css/ https://edwardtufte.github.io/tufte-css/.
- jaquers 6y agoHere's the typeface https://typeof.net/Iosevka/ https://typeof.net/Iosevka/ What struck me was the [ t a b ] stuff as well as the Latex reference, which looks to be embedded img in src, so there must be some sort of preprocessor?
- krjt 6y agoThe styling is done from scratch. For more information, see the colophon [1] and karasu [2]. [1]: https://krasjet.com/colophon/ https://krasjet.com/colophon/ [2]: https://github.com/Krasjet/karasu https://github.com/Krasjet/karasu
- jaquers 6y agoNice job!
- gen220 6y agoI have a tangential question related to styling (and I agree the styling for this site is wonderful). When designing your personal/corporate website/blog, how much weight do you attribute to users whose browsers refuse to load remote fonts (by preference or otherwise)? I ask because I've noticed recently that some websites (notably, many google sites) are unusable, because they depend on remotely-loaded icons, which are replaced with unreadably-stretched alt-text with unintended form factors. On this blog, for example, the default preformatted-font didn't seem to be monospaced, and the result is that the "julia" name is rendered incorrectly. All this is meant to ask, do you ever test your designs with browser-default fonts? If not/so, is this conscious? Why? To be clear, I intend for this to be a survey, not an inquisition. I'm trying to understand this trend better. For my own answer. At my place of work, in spite of an avowed obsession with accessibility, our designs are regularly sabotaged by browser-default fonts and poorly-rendered alt-text.
- cormullion 6y agoThis 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 ¯\\_(ツ)_/¯ ...
- ohsonice 6y agoI'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!
- dan-robertson 6y agoMy 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.
- rscho 6y agoJulia is nice for scientific serious work, but for cli calc J (jsoftware.com) really rocks!
- seltzered_ 6y agoI wonder if it'd be possible to have a blend between this and https://calca.io https://calca.io where the repl output appears (and change dynamically) as specialized comments within a markdown-friendly format.
- 29athrowaway 6y agoMy personal choice for this is Jupyter and Sagemath.
- wbhart 6y agoSageMath takes 3s to load for me (sometimes a lot more if my systems administrator has updated it recently, which seems to happen a lot). I therefore use Pari/GP for my personal choice. I like the symbolic nature of Pari/GP over Sage or Julia, even though I spend nearly all of my life programming in Julia, and love the language a lot. However, for computations of trivial things that I want to run really fast, I put them in a function in Julia (so that they are jit-compiled), since nothing will come close to that, by some orders of magnitude.
- starpilot 6y agoOne 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.
- cycomanic 6y agoOne 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.
- dklend122 6y agoYou can use the @. macro to dot an entire block. I personally like the dot syntax. It's ergonomic, and provides cool stuff like this: https://julialang.org/blog/2017/01/moredots/ https://julialang.org/blog/2017/01/moredots/ I don't know of an equivalent functionality in any other language.
- cycomanic 6y agoThe @. syntax is definetly an improvement. I don't have an issue with the principle of the dot syntax just with the symbol. The examples in your link perfectly illustrate the issue. If this was a line in a long code base maybe even with mixed element-wise and matrix operations it would be very easy to overlook a missing dot. It would be even worse if the constants were floats. It is just such a terrible choice of symbol for this operation, because it is so easy to overlook. Everyone I know always looks for missing dots if they encounter a some nonstraight forward bug in matlab (I don't know any Julia users).
- teleforce 6y agoPerhaps you should look at D language offering, it has awesome UFCS feature [1] that some other languages are copying now and a REPL. For multi-dimensional array matrix it provides the fastest computation as well, even compared to Chapel and Julia [2]. [1]https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs https://tour.dlang.org/tour/en/gems/uniform-function-call-sy... [2]https://dlang.org/blog/2020/06/03/a-look-at-chapel-d-and-julia-using-kernel-matrix-calculations/ https://dlang.org/blog/2020/06/03/a-look-at-chapel-d-and-jul...
- plashchynski 6y agoI'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!
- DreamScatter 6y agoNice article! I'm the creator of the Grassmann.jl package, which was mentioned in the article :)