28 ms·
Concise algorithms written in Julia
- cs702 5y agoBeautiful indeed, even though the screenshots generated by Carbon do not look right on Firefox 89.0.2 64-bit on Ubuntu 20.04.
- chairmanwow1 5y agoI'm not sure I understand the premise here. One letter variable and function names do not strike me as beautiful, but needlessly obtuse.
- spywaregorilla 5y agoThey seem appropriate if you want your code to read like the math they're representing.
- kps 5y ago“I used to be a mathematics professor. At that time I found there were a certain number of students who could not learn mathematics. I then was charged with ¨the job of making it easy for businessmen to use our computers. I found it was not a question of whether they could learn mathematics or not, but whether they would. […] They said, ‘Throw those symbols out — I do not know what they mean, I have not time to learn symbols.’ I suggest a reply to those who would like data processing people to use mathematical symbols that they make the first attempt to teach those symbols to vice-presidents or a colonel or admiral. I assure you that I tried it.” — Grace Hopper tl;dr: COBOL won.
- nerdponx 5y agoFor business and business people. Julia's target audience isn't "business" or "business people", although there's no reason you couldn't use it for that purpose.
- ska 5y agoHopper was making a bit of a different point - you need to tailor your communication to your audience. In this case, if the developers involved are assumed to know the math already, concise is probably for the good. If you are trying to teach the algorithms to a mathematically unsophisticated audience, it's a different story. If I'm describing the same concept to a) a group of undergrad students, b) a group of mathematicians and grad students c) a group of software developers or d) a room full of execs - I'm going to do it differently in each case, including notation and emphasis.
- kps 5y agoYes, that was my point, or at least part of it: COBOL style isn't suitable for mathematical programming. The other part is that it was never intended for programmers at all; it was for the ‘vice-president or a colonel or admiral’ who wanted the illusion of being able to understand their underlings' work. I think it's unfortunate that it took over general-purpose computing during the dot-com boom; prolixity engenders clarity about as much in code as in human language.
- spywaregorilla 5y agoJulia is not for businessmen incapable of learning mathematics.
- 0-_-0 5y agoThese variable names are hard to read for those who could actually learn from this, but known to those who are already familiar with the algorithms and have little more to learn about them. This is a common problem you run into when you start learning about a new area.
- Jolter 5y agoExactly. Each of these algorithms’ beauty comes from understanding them. And reading the original paper is probably the best way to understand and appreciate them. I guess this site is trying to sell Julia a bit, by showing how close it looks to mathematical pseudo-code.
- coliveira 5y agoAnyone who is interested in these algorithms know what these single letters mean. This is very readable.
- drran 5y agoNo, it's not. Readability assumes that code is easy to read and easy to understand. This code is easy to read for those, who understood the algorithm already, but is hard to read for those who has no understanding. It's the famous "write-only" problem of the Perl language, which uses symbols heavily too.
- patrick451 5y agoCode that mirrors a paper is the easiest to understand. Verbose, "descriptive" variable names just put more friction between you and the math. You are unlikely to reverse engineer the algorithm from the code unless you were already in a position to understand the terse mathy code to begin with. Just go read the danged paper.
- TheTrotters 5y agoI disagree. Virtually no one who worked even a little with these algorithms has used math symbols in their code. For better or for worse it messes up people's pattern recognition.
- drran 5y agoThey are no more obtuse than one letter options for command line tools. The author of function just forgot to write a man page.
- jstx1 5y ago- one-letter variable names - Greek letters - glyphs with obscure latex codes The last one in particular - how do you read this in your mind if you don't already know the name of the character? - "Hmm, that's a curly handwritten A". It's so disruptive to the thought process of the reader of your code if they don't already know the notation. I really think that this is the opposite of beautiful. Code is about communicating with other people too. I also think that mixing code and math notation is liked by a vocal minority of Julia users but in the long term is harming the adoption of the language by beginners.
- dragonwriter 5y agoI also find the combination of Greek letters where they would be in mathematical notation without subscripts where they would be in the same notation has a bit of a uncanny valley effect. I don’t think that Julia having the capacity to use this style is bad, but I don’t think these examples are “beautiful and concise”, more “baroque and tasteless”.
- oscardssmith 5y agoNote that there is an effort to fix this. A group of Julia developers have written a proposal to give Unicode proper subscript and superscript modifiers.
- nerdponx 5y agoWriting is about knowing your audience. If you're a practitioner and not much of a programmer, it might be faster and easier to read the "math notation" version, which will be familiar to you, than the version a programmer might write, which might be unfamiliar and might require your brain to scan everything "from scratch".
- saalweachter 5y agoThe Greek letters don't bother me so much as the ones where I am not quite sure if it's an ASCII x, v or w that is just being displayed with a different font, or a letter is not technically an ASCII x, v or w.
- taeric 5y agoThe image for Huffman coding has, I assume, a bug.
- ubavic 5y agoAlso Gradient descent, Two-layer neural network, Newton's method, Gaussian process, Quine. Did author of this repo checked even once rendered images?
- qart 5y agoThere's a bug somewhere, but I'm not sure where. Opening the image in a new tab renders it legibly. On the main page, it is a garbled mess. This looks fine in a tab by itself. https://raw.githubusercontent.com/mossr/BeautifulAlgorithms.jl/master/img/svg/quine.svg https://raw.githubusercontent.com/mossr/BeautifulAlgorithms....
- taeric 5y agoGood catch!
- papito 5y agoIt's like Perl, only horrible /s
- fgdelcueto 5y agoThey are indeed concise and beautiful, but some of the implementations I checked are quite naïve. They're great for academic purposes and for some simple problems they will work just fine, but I would advise against using them for real-world problems. For instance, for some of the regression algorithms the pseudo-inverse of the matrix is used to solve the normal equations. Again, for small, well-behaved problems it is OK, but for larger scale, less well-behaved problems, it is actually dangerous (numerical instabilities and other issues). There are better, faster, more stable approaches. (PhD in Applied Math here) BTW, I have been using Julia for work for several years now and I think it is a fantastic language for scientific computing. For those who use Matlab/Numpy regularly, you should definitely check it out.
- andi999 5y agoSo what are these better faster and more stable approaches?
- FabHK 5y agoInstead of 𝛉 = pinv(𝐗)*y write 𝛉 = y\𝐗 It will automatically dispatch to the most appropriate algorithm depending on the type of X (Cholesky, QR, LU/Gauss, …)
- jhgb 5y agoThe former could easily be corrected by a compiler macro, should Julia eventually support that.
- bee_rider 5y agoI don't know about the pseudoinverse in particular, but at least in the case of normal inversion -- you almost never want to invert a matrix, but sometimes you do, so it should be possible to sensibly express the idea of 'invert' I think.
- 5y ago
- FabHK 5y agoNot a Julia expert, but these snippets don’t strike me as very idiomatic or algorithmically sound [1]. What they seem to do, instead, is to demonstrate that you can write code in Julia that is quite close to the mathematical notation. (Whether you should do so is another question). [1] for example, adding a row of ones to a matrix by using array comprehension: 𝐗 = [j==0 ? 1.0 : X[i][j] for i in 1:m, j in 0:n] or solving a linear system by multiplying with the (pseudo-)inverse: 𝛉 = pinv(𝐗)\*y
- thom 5y agoWell, now I can at least understand what it feels like to look at Lisp and hate all the parentheses, so I'm trying to temper my immediate repulsion a little. But no, absolutely not. I would code a pre-commit hook to reject these on the first attempt, and send you your P45 on the second, sorry.
- cestith 5y agoI understand your frustration looking at symbols which may not be straightforward without a certain background or a glossary of terms. However, do you realize anyone outside the UK similarly likely would need to look up what a P45 form is? It's like a USian saying "pink slip" rather than any Anglophone just saying "termination papers". We all like our own shorthand symbols and tend toward discomfort when we're the out group.
- thom 5y agoThat comment was obvious hyperbole on an internet message board, not production code, and even then it's a terrible analogy because I actually typed it, I didn't use some inscrutable emoji that you then had to cut and paste. Also the idea that "people in the know" write code like this is absurd - I've worked with mathematicians for more than a decade in languages that support unicode names, and it would never occur to any of them to do idiotic stuff like this. This is terrible practice and always will be.
- generalizations 5y agoBy the way, on the subject of single-character variables, is there a list somewhere of the single-letter math symbols that julia supports? (e.g. multiply, divide, sigma, etc)
- JustFinishedBSG 5y agoBasically all of unicode, using latex commands to autocomplete because wtf remembers that \mathcal{P} is U+1D4AB https://docs.julialang.org/en/v1/manual/unicode-input/ https://docs.julialang.org/en/v1/manual/unicode-input/
- kps 5y agoSadly you can't use ↦ for anonymous functions.
- andi999 5y ago"A bunch of algorithms, never used." - E.Hemingway
- gugagore 5y agoI do think Julia is uniquely poised to provide a new kind of trade-off between concision, performance, understandability, etc. because of multiple dispatch, and great syntax for things like broadcasting. Sometimes a practical algorithm does not look like a textbook algorithm, because they're different algorithms, but I think the main reason that there is such a discrepancy is: 1. math notation is many times quite ambiguous 2. there are a bunch of performance optimizations that end up being unsightly I am not claiming it's easy work, but I think it's nice to try to find abstractions so that at least somewhere, the algorithm looks simple. I don't, however, think that these code examples really make that shine at all. There are also choices I disagree with. For example, there is (paraphrasing) `map(vⱼ -> g(x, vⱼ), V)`. the `j` subscript is meaningless to me, and is an awkward combination of "point free" / "index free" notation with a higher-order function, and a more traditional spelling. Math texts love to do things like V = [v_1, ... , v_n]. That doesn't make sense to do in a programming language. instead of writing v_j, you just do V[j].
- gugagore 5y agoto make a related point, I think that notions of programming (e.g. scope, types, awareness of Hungarian notation) should influence how we write math more. I do not think the goal can be to make code look like "math" completely because there is so, so much punning in how most people write math.
- mbauman 5y agoMy favorite examples show the reference algorithm alongside the code (which cites it). The goal should be that the code provides enough similarity to the reference that it's easy to transition between the two and see past those necessary differences. For example: https://twitter.com/tymwol/status/1305416942967283712 https://twitter.com/tymwol/status/1305416942967283712
- infogulch 5y agoImagine a world where papers are written as a Julia Jupyter Notebook (or other nb format) that compiles to both a pdf suitable for publishing and also to a library that is directly consumable. The phrase "I'm using an algorithm I found in this paper" would become a much more meaningful statement.
- aimor 5y agoOne of the things I like about code is that it's usually very easy to read. There are problems with characters like 1 l I 0 O i j etc that are addressed by font choices. But even with those problems I find it much easier to read the ASCII letters than math notation with subscripts and superscripts, Greek letters and script letters, bold and doubled, and so on. People might deny having the same problem, but I've seen it hundreds of times where a mistake is made because two symbols were conflated or a subscript was overlooked, or a symbol with multiple uses in different contexts was assumed to be one thing when really it was another. That said it's still neat to see code using common symbols with what you'd find in textbooks.
- la_fayette 5y agoHm, looks like math formulas... You could use any programming language and use one letter UTF-8 variable names and try to fit everything in one line...
- DNF2 5y agoObviously, these code samples are meant only for aesthetic display, not for their pedagogical value. The criticisms being leveled by some here could be valid if their target had a different purpose. As it is, they miss the purpose and their mark.
- jdeaton 5y agoGross.
- todd8 5y agoMy first degree was in math. I've frequently discovered that I don't understand the notation in math papers. What does the paper mean by the harpoon (↼ arrow with half its arrowhead) and how is it different than the waved arrow (⬿ arrow with a wavy shaft)? Each discipline has its own conventions, is the backslash a set difference operator or is it matrix division. Is alpha, α, an angle in some trig equation or is it the minimum assured score value in alpha-beta pruning? It takes a bit of time to dive into a math paper. Even the universally understood symbol for integration (∫) can mean different things. Is it Riemann integration or is it Lebesgue integration? Is it symbolic or is it a numerical approximation. It depends upon context, and the context is communicated between mathematicians by the subject of the course, the preceding results in or paper, or just a few asides by a professor giving a lecture. Computer scientists (I've been one for roughly 50 years) introduce their own academic notations. Is circled plus, ⊕, a boolean exclusive-or or is it bitwise exclusive-or. Take a look at Knuth Vol 4A, it's chock full of mathematical notations embedded in algorithms. He uses superscripts that are themselves superscripts, how are those supposed to be entered with our text editors?. What about sequence notations like 1, 4, 9, 16, ... we might suppose that it is just the integer squares, but the On-line Encyclopedia of Integer Sequences lists 187 other possibilities. Is the compiler supposed to guess what this is? Well, if mathematicians use these concise notations, why shouldn't programmers? I believe it is because mathematicians don't want or need to take the time and space needed to spell out these operators, variables, and functions in their papers. It's not necessary for mathematicians. Other specialists in their field can figure out what the symbols mean while reading their papers. Their students can understand what a blackboard capital F (𝔽), likely a field in a class on abstract algebra. Programmers are doing something different. Their programs are going to be a lot longer than most math papers or lecture expositions. The programs have to deal with data, networks, business rules, hardware limits, etc. And everything in a program must be unambiguous and precise. Programs are large and can be edited many times by many people. For these reasons, I'm inclined to favor programing in plain language with plain ascii. See: Knuth, The Art of Computer Programming Vol 4A, Combinatorial Algorithms The on-line encyclopedia of integer sequences, https://oeis.org https://oeis.org
- Y_Y 5y agoMy background is also in maths and I am keenly interested in, and frustrated by, notation as it appears in various fields. There's a time and place for fancy, dense notation, and I don't think it's here. Subjectively I found the use of unusual math unicode symbols to be gratuitous in this repo.
- rmah 5y agoI think it's very interesting how some people are repulsed by the use of mathematical symbols in the sample code. While others love it. The vast majority of the code I write is not mathy. But I do write a fair bit of mathy code because of the field I operate in. Because of this, I'd side with the ones that hate it for some of these algos: the various sorts, for example. But for others, the use of mathematical symbols brings clarity, IMO. Assuming you already know the algo. I've had to translate mathematical notation from a set of papers into code before. When I first picked up Julia, I re-did something I had previously done in Python/pandas/numpy. I used mathy notation... and julia code, ended up both MUCH clearer and quite a bit faster. I call that a win in my book.
- TheTrotters 5y agoIn a vacuum real math symbols look better but they're virtually never used in practice and they mess up people's pattern recognition. It's uncanny valley but for code.
- enriquto 5y ago> they mess up people's pattern recognition you have described perfectly the problem that I have when reading n0code written by programmers who are not mathematicians... my mind wants to read variables with multiple letters as products of each letter. I find multi-letter variable names extremely old fasioned, as when math was written explicitly in latin before the advent of algeraic notation. EDIT: an illustrative tweet of my concern: https://mobile.twitter.com/fermatslibrary/status/1410945173954174976 https://mobile.twitter.com/fermatslibrary/status/14109451739...
- kurige 5y ago> I find multi-letter variable names extremely old fasioned Sometimes I read things here on Hacker News that throw me so hard I leave the site for a month or two. Congratulations, this time it's your fault. Goodbye.
- 5y ago