[HN Gopher] Concise algorithms written in Julia
___________________________________________________________________
Concise algorithms written in Julia
Author : thetwentyone
Score : 164 points
Date : 2021-07-09 14:24 UTC (8 hours ago)
(HTM) web link (github.com)
(TXT) w3m dump (github.com)
| FabHK wrote:
| Not 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: X = [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: theta = pinv(X)\*y
| thom wrote:
| Well, 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.
| gugagore wrote:
| I 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 wrote:
| to 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.
| infogulch wrote:
| Imagine 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.
| gugagore wrote:
| That is a great world. But considering that we still do not
| really know how to write e.g. generic code that operates on
| both immutable and mutable arrays, I think Julia is not there
| yet.
|
| The other big thing missing in Julia that is IMO important
| here is more control over evaluation, like allowing lazy
| evaluation. You can build some of these features on top of
| Julia (e.g. https://github.com/RelationalAI-oss/Salsa.jl).
|
| Math is usually not written with computation in mind, so some
| algorithm might e.g. re-evaluate f(x) multiple times.
| Compiler optimizations can help in some cases, as can a
| "dynamic" solution like memoization/caching. But again, I
| don't think Julia is "there" yet.
| StefanKarpinski wrote:
| I don't really get the first issue. If an algorithm doesn't
| need to mutate a matrix then it will work for a mutable or
| immutable matrix. If it needs to mutate a matrix then how
| could it work for an immutable matrix? If you want a
| polyalgorithm that uses a mutating algorithm on mutable
| matrices and a non-mutating algorithm on immutable
| matrices, then you can just do that (and multiple dispatch
| makes it easy). What additional features are needed? Is
| there some language that "does this right"?
| pjmlp wrote:
| That was the premise of literate programming, but it did not
| took the world by storm.
|
| https://en.wikipedia.org/wiki/Literate_programming
| mbauman wrote:
| My 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
| aimor wrote:
| One 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 wrote:
| Hm, 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...
| generalizations wrote:
| By 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 wrote:
| Basically 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/
| kps wrote:
| Sadly you can't use - for anonymous functions.
| cs702 wrote:
| Beautiful indeed, even though the screenshots generated by Carbon
| do not look right on Firefox 89.0.2 64-bit on Ubuntu 20.04.
| todd8 wrote:
| My 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, a, 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
| (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
| Y_Y wrote:
| My 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.
| alfiedotwtf wrote:
| > 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.
|
| Mathematicians don't need to fight compilers, only other
| mathematicians. If Mathematicians needed to convince a compiler
| their equations, I'm sure they'd be forced to be fully
| explanatory and less handwavey at the margins.
| rmah wrote:
| I 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 wrote:
| In 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 wrote:
| > 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.twi
| tter.com/fermatslibrary/status/14109451739...
| jdeaton wrote:
| Gross.
| DNF2 wrote:
| Obviously, 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.
| jstx1 wrote:
| - 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 wrote:
| I 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 wrote:
| Note 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 wrote:
| Writing 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".
| 8note wrote:
| In mechanical engineering class, there was squiggly, and double
| squiggly. I don't remember which Greek letters those correspond
| to, but it's easy enough.
|
| Its not like anyone could draw them properly by hand either
| mbauman wrote:
| It's all about matching the language of the literature in your
| domain. If you don't work in a domain that uses those
| conventions, you shouldn't use them. If you do, you and your
| colleagues should recognize it.
|
| If you're branching out into other code, you can always ask
| Julia itself what a particular character is -- just paste it
| into a help prompt at the REPL: help?> A
| "A" can be typed by \scrA<tab> julia> 'A'
| 'A': Unicode U+1D49C (category Lu: Letter, uppercase)
| discmonkey wrote:
| Gradient descent is a pretty common algorithm, and I've seen
| it implemented in everything from JavaScript to c++. I have
| to say in this case the notation is significantly more
| distracting than helpful imo. Or to put it another way, the
| set of people working in the ml field, is both larger and
| more diverse than the set of people working purely in math.
| Everyone in the population of people who can both program and
| understand this type of notation would also be able to
| understand the less terse but more readable utf8 based text,
| the inverse is not true.
| generalizations wrote:
| I think that means this code in particular is not catering
| to the lowest common denominator of people using the
| algorithm, but is instead aimed to be easy to use by those
| already fluent in the domain. It's important for both types
| of implementations to exist, and as you say, there's lots
| of other implementations available for those not familiar
| with the mathematical notation.
| icedistilled wrote:
| >is not catering to the lowest common denominator of
| people using the algorithm
|
| I don't think an expert in some field who just happens
| not to be an expert in some other field's jargon and
| pictographs should be branded "a lowest common
| denominator of people", this terminology is both
| judgmental and elitist in all the bad ways.
| jstx1 wrote:
| > I think that means this code in particular is not
| catering to the lowest common denominator of people using
| the algorithm
|
| Maybe the title should have been 'Algorithms that look
| concise and beautiful to people who already know them'.
| deeviant wrote:
| Is it easier to use? Or just it just look pretty to a
| subset of people?
| generalizations wrote:
| It's easier.
|
| There's a reason we use 'y = mx + b' instead of 'range =
| slope * domain + intercept' when we're doing math
| calculations. It's nice to use the long form when we're
| learning what the symbols are, but the symbols enhance
| readability when you're doing math, once you've become
| familiar with the meaning of the terms.
| TheTrotters wrote:
| Anyone who has even basic fluency with these algorithms
| has ~never worked with code that uses these symbols and
| will likely find them distracting in this context.
|
| I've seen plenty of function use, say, `theta` but I have
| never worked with th.
| gnarbarian wrote:
| I put emoji in my JavaScript all the time.
| generalizations wrote:
| It's also a matter of knowing your audience. All due respect:
| but this looks like code intended for people familiar with the
| relevant math, for whom such variables are very familiar.
|
| Personally, those 'strange letters' make it more readable,
| because that's how the mathematics are written.
| contravariant wrote:
| Though it's worth keeping in mind that the mathematics is
| usually accompanied by an article that at a minimum (tersely)
| tells you what the symbols are.
| ModernMech wrote:
| Exactly, this is the thing people always seem to forget.
| When I write a paper with mathematical symbols like this, I
| always include a glossary for the reader. It's even
| required in some venues.
|
| People like to pretend that mathematical notations are
| standardized and obvious to practitioners, but they're just
| not. In my physics undergrad days we made a joke shirt with
| all the usages of the symbol "k" as it appeared in our
| physics texts. Is it the constant in Coulomb's law? Is it a
| spring constant? The Boltzman proportionality factor? Is it
| kinetic energy? Is it a kilo? A kelvin? I think we were
| able to cover the front and back of the shirt with the
| overloaded usages.
| contravariant wrote:
| Yeah things can get a little wild sometimes with all
| possible interpretations. I'm pretty sure there are a few
| equations that use 'i' as both the imaginary unit and an
| index at the same time.
|
| I've noticed physicists can be a bit fast and loose with
| their definitions, somehow you're assumed to be up to do
| date on what concept is given the symbol "R" in that
| particular context. Though they do tend to agree with
| each other which concept should have which symbol.
| spywaregorilla wrote:
| Sounds like something one could expect from a docstring
| counters wrote:
| Bingo. And it goes vice-versa; because software engineering
| standards are so lax (read: non-existent) in most academic
| disciplines and domains, on the rare occasion where you get
| accompanying code to an academic work it may be impossible
| to actually much of the source to the original work itself.
|
| During my dissertation I had to collect implementations of
| about a dozen parameterizations of a process I was
| studying, with the intent of coupling them/incorporating
| them into a set of climate modeling experiments. All of the
| implementations were in Fortran (which in and of itself
| wasn't a problem, albeit it was annoying re-writing or re-
| factoring some F77 code to play more nicely with the more
| modern Fortran codebase in which our climate model was
| written). Of these codes I collected, the majority of them
| had bugs which restricted my ability to reproduce the
| reported results from the original work they accompanied.
| One or two of them had significant flaws in them - answer-
| changing bugs which, when corrected, potentially would
| yield different results from those reported in their
| original papers, and might actually lead to different
| conclusions.
|
| One of the nice things about the Jupyter ecosystem of tools
| and Julia is the ability to more closely replicate
| equations from the original paper. It seems dumb, but in my
| anecdotal experience I've noticed fewer significant bugs
| when colleagues have used these features. I've also noticed
| a propensity for scientists and researchers to write
| clearer, better-documented code - if your next line of code
| is going to directly mimic Equation 13 in your paper, why
| wouldn't you go ahead and write a comment saying so?
| res0nat0r wrote:
| Do folks normally write code using the Greek alphabet? I
| totally understand reading/writing math papers in latex,
| or on paper using the traditional mathematical symbols,
| but does anyone really want to edit their Julia code in
| vim and type \scrA<tab> for a variable name? Seems
| extremely annoying and convoluted vs naming your
| variables alpha or sigma, etc.
| mbauman wrote:
| All the time. Incidentally, I got these unicode a and s
| by copy-pasting from tab completion in an open Julia
| REPL.
|
| https://juliahub.com/ui/Search?q=a&type=code&w=true
|
| https://juliahub.com/ui/Search?q=s&type=code&w=true
| leephillips wrote:
| But I can type a with two keystrokes. "alpha" is five.
| res0nat0r wrote:
| Looking up escape codes or cutting and pasting non ascii
| characters takes much longer than typing 5 letters.
| leephillips wrote:
| > But I can type a with two keystrokes.
| res0nat0r wrote:
| Learning that for all the mathematical symbols, still too
| time consuming.
| FabHK wrote:
| On the other hand, those are precisely the terms you will
| encounter in the literature.
|
| What is easier: matching (mu, Sigma) to (mu, Sigma), or to (mu,
| Sigma)?
| gugagore wrote:
| I personally use Unicode liberally in my Julia code.
|
| Let me say there is a third alternative: (mean, cov) (except
| you'll shadow some functions...).
| 127 wrote:
| Exactly. Making it harder to read, write and understand on
| purpose, unless you're a part of a specific small exclusive
| club. There's absolutely no reason.
| gspr wrote:
| No. There's a reason: it communicates extra information very
| cheaply and in-band with the code for people who are
| intimately familiar with the mathematics involved. This can
| be better than comments or documentation in some situations
| (and also doesn't preclude also having those).
|
| It's becoming a pet peeve of mine how HN conflates a solid,
| classical mathematical education with some kind of
| gatekeeping club. Anyone is welcome to join. Anyone is
| welcome _not_ to use this language if they wish. But please
| don 't dictate how us mathematicians are supposed to
| communicate.
| TheTrotters wrote:
| > it communicates extra information very cheaply and in-
| band with the code for people who are intimately familiar
| with the mathematics involved.
|
| I think it's just the opposite. People familiar with the
| relevant math have, most likely, worked with this stuff in
| other programming languages and never used math symbols in
| their code.
|
| I'm used to seeing th or a in text and theta or alpha in
| code. The combination of Greek letters and for loops looks
| very weird.
| 127 wrote:
| Yes, I agree completely. It communicates information very
| cheaply and in-band to very few people who are intimately
| familiar with the mathematics involved. And nobody else.
|
| Calling this mathematical education makes me doubt your
| implicit claim of pedagogical ability.
|
| I think a lot of math could be taught with a lot less
| overhead and in more understandable fashion. It's just that
| the natural incentives seem to drive people to unnecessary
| overhead and barriers for those who actually want to learn.
|
| Lot of the language and symbols involved could be
| streamlined. But like QWERTY keyboard and C++, you would
| first have to turn this huge hulking ship of legacy cruft
| into that direction.
| gspr wrote:
| > I think a lot of math could be taught with a lot less
| overhead and in more understandable fashion. It's just
| that the natural incentives seem to drive people to
| unnecessary overhead and barriers for those who actually
| want to learn.
|
| Ok. Have at it, I guess.
|
| > Lot of the language and symbols involved could be
| streamlined. But like QWERTY keyboard and C++, you would
| first have to turn this huge hulking ship of legacy cruft
| into that direction.
|
| This screams a fundamental misunderstanding of
| mathematics. The symbols need context to make any sense
| whatsoever. To a reader with intimate knowledge of the
| context, the symbols convey lots of information very
| efficiently. That doesn't mean that the symbols are
| understandable without context, or even that they could
| be made understandable without context.
| jstx1 wrote:
| It's not about math notation on its own or about
| mathematical education. I'm very comfortable with
| mathematical notation in textbooks and papers and I still
| think that using it in code is a mistake.
| neolog wrote:
| Can you explain why?
| leephillips wrote:
| I think these practices make sense for math-oriented code. In
| particular, I don't understand the last objection. When you
| read math, do you need to describe the letters and symbols with
| English phrases in your head, like "Calligraphic L...right
| arrow with double staff...bold italic a with squiggle on
| top...."?
| jstx1 wrote:
| > When you read math, do you need to describe the letters and
| symbols with English phrases in your head..?
|
| I don't need to only if I already know what they mean. How
| does your brain process a character that you can't pronounce
| and whose meaning you're trying to understand from the code
| you're currently reading? Because my brain will need to
| assign _something_ to it in order to use it.
| saalweachter wrote:
| The 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.
| JustFinishedBSG wrote:
| That's more of an artifact of the font being badly designed
| though.
|
| Math requires _excellent_ fonts if you don 't want your
| reader to want to murder you by attempting to decipher the
| difference between L, calligraphic L, script L, italic L...
| saalweachter wrote:
| Math's fondness for using multiple fonts with semantic
| meaning is going to backfire when the next generation's
| math genius uses comic sans for the new math they invent.
| enriquto wrote:
| > the next generation's math genius uses comic sans for
| the new math they invent.
|
| The way you put it, it almost sounds as if that wouldn'be
| great!
| kmill wrote:
| Hmm, I'm tempted to use it for mathscr... It probably
| would even be more legible.
| chairmanwow1 wrote:
| I'm not sure I understand the premise here. One letter variable
| and function names do not strike me as beautiful, but needlessly
| obtuse.
| coliveira wrote:
| Anyone who is interested in these algorithms know what these
| single letters mean. This is very readable.
| drran wrote:
| No, 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.
| TheTrotters wrote:
| I 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 wrote:
| They are no more obtuse than one letter options for command
| line tools. The author of function just forgot to write a man
| page.
| spywaregorilla wrote:
| They seem appropriate if you want your code to read like the
| math they're representing.
| kps wrote:
| "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 wrote:
| For 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 wrote:
| Hopper 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 wrote:
| Yes, 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 wrote:
| Julia is not for businessmen incapable of learning
| mathematics.
| 0-_-0 wrote:
| These 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 wrote:
| Exactly. 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.
| andi999 wrote:
| "A bunch of algorithms, never used." - E.Hemingway
| taeric wrote:
| The image for Huffman coding has, I assume, a bug.
| ubavic wrote:
| Also Gradient descent, Two-layer neural network, Newton's
| method, Gaussian process, Quine.
|
| Did author of this repo checked even once rendered images?
| qart wrote:
| There'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.githubusercon
| tent.com/mossr/BeautifulAlgorithms....
| taeric wrote:
| Good catch!
| papito wrote:
| It's like Perl, only horrible /s
| fgdelcueto wrote:
| They are indeed concise and beautiful, but some of the
| implementations I checked are quite naive. 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 wrote:
| So what are these better faster and more stable approaches?
| fgdelcueto wrote:
| It depends a lot of the problem. For a general least squares
| solution of the Ax=b problem, is your A matrix dense, sparse,
| structured? Do you need to solve it very accurately or just
| approximately? Are you going to be using this over and over
| with the same A but different b's? In that case, you may
| profit from a one time Cholesky factorization that can be
| used over and over very quickly. Is your linear operator A
| explicit (as in you can actually inspect the matrix) or is it
| a linear operator function in which you can only see the
| result of A(x) (and its adjoint A_adj(y)) ? In this case, you
| cannot use common factorizations (QR, cholesky, svd) but will
| have to use iterative methods (ie. Krylov space methods).
|
| There's a whole branch in applied math that's called
| numerical linear algebra and deals with all these kinds of
| situations. One of the most important results is that you
| should almost never do an explicit computation of the inverse
| of a matrix. It is expensive and can be highly unstable. If
| you ever see a A\b or pinv(A)*b in somebody's code, it should
| raise a big red flag.
| FabHK wrote:
| Instead of theta = pinv(X)*y
|
| write theta = y\X
|
| It will automatically dispatch to the most appropriate
| algorithm depending on the type of X (Cholesky, QR, LU/Gauss,
| ...)
| jhgb wrote:
| The former could easily be corrected by a compiler macro,
| should Julia eventually support that.
| bee_rider wrote:
| I 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.
| jhgb wrote:
| True, but the specific combination of inversion and
| multiplication, with a well-known meaning, could be
| transformed. Standalone inversion would still stay an
| inversion. Algebraic identities are definitely one thing
| you might want to exploit with advanced compiler macros.
| bee_rider wrote:
| I guess the only concern I'd have is, is it possible that
| someone knows what they are doing (so, something is
| special about their matrix) and wants to do an in-place
| inversion, multiply, and then use their inverted matrix
| for something else. I'm don't know Julia, though, so I'm
| not sure if this would be a weird thing to do. (is invert
| even in-place?)
| dgb23 wrote:
| As a complete layman the second one, given your
| explanation, seems to be more generic and declarative. Is
| that a correct assumption?
| syockit wrote:
| Yes, it basically means "solve Xtheta = y for theta", for
| which inverting X is one of the many methods that can be
| used to solve it. MATLAB also has the same backslash
| operator to mean the same thing.
| superdimwit wrote:
| QR, Cholesky
| the_svd_doctor wrote:
| QR and Cholesky factorization (QR is the most versatile and
| is always stable, so usually a better first choice)
___________________________________________________________________
(page generated 2021-07-09 23:01 UTC)