11 ms·
Translating math into code with examples in Java, Racket, Haskell, Python (2011)
- adamnemecek 7y agoJulia is notably missing. Packages like this one are dope https://github.com/korsbo/Latexify.jl https://github.com/korsbo/Latexify.jl Or this https://github.com/qojulia/QuantumOptics.jl https://github.com/qojulia/QuantumOptics.jl Or this https://www.juliahomotopycontinuation.org/ https://www.juliahomotopycontinuation.org/
- eggy 7y agoI like Julia for all of the libraries, the Lisp underneath, however, from the article: "Many of the encodings are as immutable, purely functional data structures (even in imperative languages), a topic unfortunately omitted from many computer science curricula." Julia and similar PLs don't express math like APL, J[1], Haskell[2], Scheme[3] or even Clojure can with immutable structures and function composition to name a couple. Sure you can write it in Julia, but I don't think the article is about creating math output in the Latexify.jl example, but how to code these math structures where certain languages can express them out of the box in an easier manner. [1] https://www.jsoftware.com/books/pdf/ https://www.jsoftware.com/books/pdf/ [2] https://www.amazon.com/Haskell-Logic-Programming-Second-Computing/dp/0954300696 https://www.amazon.com/Haskell-Logic-Programming-Second-Comp... [3] https://mitpress.mit.edu/sites/default/files/titles/content/sicm_edition_2/book.html https://mitpress.mit.edu/sites/default/files/titles/content/...
- adamnemecek 7y agoIdk why apl or haskell would be any better.
- new4thaccount 7y agoI'm not as opinionated on Haskell, but APL is both a programming language AND executable math notation. It's just the notation is different than what most are already used to.
- ddragon 7y agoCan you explain it better? I do agree with Haskell due to laziness by default and arrow types for example, but against Scheme and Clojure the only aspect seels like it would be the more strict rebinding/shadowing rules (which doesn't limit what you can express, just that you can also express non-mathematical expressions). Primitive types (like pretty much all languages) are immutable and structs in Julia are immutable by default (which includes pretty much every math type like complex numbers), and all math operators are non mutating (in fact mutating operators should have a ! in the name). Function composition is just as simple in this scenario: f(x) = x + 2 g(x) = 2x h = g ∘ f h(3) == 10
- eggy 7y ago@ddragon your points are well taken. I will look further into Julia. I still think when I wrote "out of the box" with APL in mind as far as expressing math, or J for that matter if you learn the ASCII symbols. I've seen other examples since your post on Julia that show similar math expressiveness, but they seem to rely on libraries or the syntactic sugar provided by the libraries. I haven't tried your example, but it looks like vanilla Julia. My familiarity with Matlab makes Julia an easy choice for me too. Here is a simple example in J: +/ % # The above is an average function in J. It is a fork, where you fold right over the input (+/), and divde (%) by the tally (#) or count of items, so that (+/ % #) 4.5 3 2 12 produces 5.375 as the average of the input vector. Notice the array-based language deals with singular quantities or scalars, vectors, and multi-dimensional arrays as fundamentals of the PL. You could also define it as: average =: +/ % # for those who make the readability argument, however, you learn math symbols and read math papers full of them, and not lengthy verbal descriptions of these math formulas (average is equal to the "sum all of the input elements, and divde by the count or tally of input items"). The fact that a highly-functional APL program can fit on one screen negates the argument that you need readability, so that when you pick up or someone else picks up, your 110K lines of JavaScript code they can make a change. You may have to do a refresh read in APL and J, but because it is terse, you can deal it pretty quickly. Well-commented code in the 100k to 1 million lines of code scale will never allow one person to see the whole picture. The work by Aaron Hsu in APL is amazing. Here is a link to the slides from a talk he gave. It's a bit lengthy but I found it very interesting and appropriate to this topic: https://sway.office.com/b1pRwmzuGjqB30On https://sway.office.com/b1pRwmzuGjqB30On The YouTube link to the talk is here: https://www.youtube.com/watch?v=9xCJ3BCIudI https://www.youtube.com/watch?v=9xCJ3BCIudI
- Abishek_Muthian 7y agoNice article, I was wondering why Julia wasn't featured considering its support for math syntax. I wondered whether this could have been an old article, but there is no date in the article. I checked the RSS feed and couldn't find the article in it. I cross-checked the oldest article in the feed with the index of articles on the site, this article seems to be written before Dec 2014. This makes sense for why Julia isn't included.
- codelemur 7y agoThanks for doing the sleuthing on that, I (and surely others) had been wondering about the same :).
- Abishek_Muthian 7y agoI see that there are other comment questioning the same, I saw that only after posting this else I would have added this as comment to it.
- throwawaymath 7y agoThat's also supported by the fact that examples are given using Python 2, not Python 3. Not a hard and fast rule, but it's a helpful heuristic.
- mark_l_watson 7y agoWell, we are fortunate that the author covered four languages. That said, seeing the same in Julia and also Common Lisp would be great. I am coming up on my 36 year anniversary for using Common Lisp, so even though I really love Haskell and Racket, and I am enjoying learning Julia, I would vote for adding CL. This might be a good community project - the article on github and allow people to do pull requests adding their favorite language.
- Mathnerd314 7y agoDon't forget the Internet Archive! The first version is from Nov. 14: https://web.archive.org/web/20111114185849/http://matt.might.net/articles/discrete-math-and-code/ https://web.archive.org/web/20111114185849/http://matt.might... The article index from Nov. 8 does not include the article: https://web.archive.org/web/20111108003034/http://matt.might.net/articles/ https://web.archive.org/web/20111108003034/http://matt.might... So we can say with confidence that this should have a [2011] tag and was published sometime Nov 8 - Nov 14 in 2011.
- dreen 7y agoI dream of material that would explain some advanced (for a regular person understanding of the word) math concepts using Python. Maybe it's because I've programmed more in my life than did math, but the "language" we use to write it is absolutely crazy: one letter variables everywhere, no structure, thousands of custom notations etc. I'm not criticising it, I realise it exists this way for a reason and if I wasn't so lazy I would have learnt it in school. But with my 31-year old brain oriented for looking at a sequential flow of code, it's quite difficult to learn math now.
- everyone 7y agoAgreed. I think the internet needs a 'math as code' wiki site. Math is so easy! and its a joy to work with when programming, but the way its written, the notation, is often baffling to me.
- jciochon 7y agoMaybe you can start from a different angle. It might sound odd, but try taking a simple concept that you know, say calculating a restaurant tip or something and write it out in English + LaTeX. Build on this until you can do some more complicated problems, and soon I would bet you’ll read math as something a bit closer to code in your head. I had to do this a lot for classes in college, and forcing yourself into exposition is the math equivalent of rubber ducky programming. LaTeX will give you a feel somewhere between math and code that might feel a bit more natural.
- 87zuhjkas 7y agoAgreed, i'm not sure whether one letter (immutable) greek variables are better than proper named variables. Another consideration: A formal rule based math syntax could be checked by a computer (theorem prover).
- kbenson 7y agoWhere widely understood, I consider a Greek variable to be vastly superior. The problem is how well it's understood. Delta is an example of a succinct variable which has a well defined meaning which to encapsulate in a named variable either requires a long name or leaving out some nuance (delta isn't 'change' which is ambiguous in English, but the difference between two measurable things.) Keep in mind we use special single letter variables all the time, sometimes multiple for the same concept. Multiply, divide, add, subtract. You learned them all in early education along with everyone else, and now you have a universal set of symbols to express arithmetic concepts. Would we be better off if every occurrence of '+' was replaced with 'sum'? I don't think so, but that doesn't necessarily mean that loading up with single letter variables that people aren't familiar with is better. It's entirely to do with how familiar the target audience is with them. A library targeted for use by scientists might see benefit to more of them, while one targeted towards being accissible to many people might not.
- eggy 7y agoI would think Clojure vs. Java would be used given immutability. I like both Haskell and Racket for math, especially this article on geometric algebra with Haskell: https://crypto.stanford.edu/~blynn/haskell/ga.html https://crypto.stanford.edu/~blynn/haskell/ga.html I am personally learning Shen[1] because Lisp, optional typing (great type system), and the formal work done within it, and for it in proving, verification and other related endeavors. It seems to tick the Haskell, Lisp, Prolog and logic boxes for me. Its portability is great[2], however, it is still young and building libraries. I am thinking on trying to mimic the DL work done by Dragan Djuric[3] for Clojure in Shen. We'll see how that goes... [1] http://www.shenlanguage.org/ http://www.shenlanguage.org/ [2] https://shen-language.github.io/ https://shen-language.github.io/ [3] https://dragan.rocks/ https://dragan.rocks/
- throwawaymath 7y ago> Explicit sequences tend to be wrapped in angle-brackets, so that: s = <s1, s2, ... sn> (Please note that in LaTeX, one should use \langle and \rangle for angle brackets--not less than and greater than.) The article is pretty good overall, but I find this an little odd. I don't think I've ever seen sequences denoted with angle brackets. You usually see those used for denoting an inner product. I've only ever seen sequences denoted with parentheses, like (a_n) = (a_1, a_2, ..., a_n). It's kind of humorous to me that we can sort of fingerprint the textbooks/lecturers people learned from by the idiosyncrasies of their notation.
- new4thaccount 7y agoThis is great, but I'd love to see more Mathy languages added like APL, Mathematica, and Matlab/Octave.
- pron 7y ago> Mathematics is a purely functional language. It is not. E.g. x ∈ ℕ ∧ x > 2 ∧ x < 5 ∧ x % 2 = 0 implies x = 4. This is not something pure functional languages can express directly. In math we make common use of relations, of which functions are a special case, but not the common one and certainly not the only one. But even when we do work with functions, a functional PL does not suffice. E.g. writing f(t) = t + 1 ∧ x ∈ ℕ ∧ f(x) = 3 is equivalent to writing x = 2. Also, we commonly employ objects that are not computable. This is just one of the many mathy myths that plague the FP community. Yes, there are some similarities between FP and mathematical notation, but FP and imperative are much closer to one another than either is to mathematics.
- orbifold 7y agoNot to mention that whole fields of mathematics deal with numerical algorithms: Computational algebraic geometry, number theory, numerical mathematics etc.
- j88439h84 7y agoI don't know much about this area. What about "x ∈ ℕ ∧ x > 2 ∧ x < 5 ∧ x % 2 = 0 implies x = 4." can't be expressed in common languages? Don't lots of solver systems have support for sets, and deriving facts from statements about them?
- Recursing 7y agoJust in case somebody might benefit from finding out how easy it is in python with z3 >>> from z3 import Int, solve >>> x = Int('x') >>> solve([x > 2, x < 5, x % 2 == 0]) [x = 4]
- taeric 7y agoThe argument here is that you used a program to solve an equation. You did not write your program in that way. Indeed, you are using operator overloading/fancy syntax to make this work. If anything, this seems to support the original claim.
- 7y ago
- yters 7y agoNot sure what the point of translating math into code is. Just because we represent something with code doesn't mean it is computable. It's also technically incorrect to say math is translated into code. The mathematical symbols are being swapped with codey things, but the underlying semantics are quite different. For example, we might say the symbol 'oo' is infinity, but infinity itself is something that cannot be embedded in a finite program. So, we might be coerce a parser to realize that 1/oo=0 with a background ruleset, but the identity itself was derived by our minds accessing the mathematical world of forms that is completely inaccessible to finite computational mechanisms. It seems like this effort is going to increase a lot of overhead to do math, and obscures the role of thinking through the math conceptually. There seems to be a Principia Mathematica 2.0 idea in the programming world that we just need to reduce mathematics to code and it'll become easily accessible and usable. I'm not convinced that will be the case.
- throwawaymath 7y agoYou and I read the article very differently. Based on your comment, it sounds like you interpreted the article as implicit advocation for the idea that mathematics should be represented in a programmatic (i.e. computable) way. I didn't pick up any ideology in my reading. My interpretation of the article is that the author wanted to provide tips on how to implement mathematics, for two reasons: 1. People have to implement nontrivial mathematics from time to time, and 2. Most mathematics doesn't concern itself with obstacles to implementation, because it doesn't have to.
- yters 7y agoThat's true, and numerical analysis can be quite helpful for mathematical insight. However, I've never found the math to code part to be tricky. It's mostly an after thought compared to the conceptualization itself. So the emphasis on coding math suggests a reversal of priorities. In my experience, focusing on the coding part becomes very inefficient, especially with combinatorial problems. No matter how tricky I get with the implementation, a combinatorial explosion is still a combinatorial explosion. Coming up with a neat analytic solution ends up being super efficient in comparison.
- misterdoubt 7y agoDespite the title, this article does a real disservice to Python. For the purpose of constructing a generator, the author wrote: > In an object-oriented setting like Python or Java, streams can be constructed from an interface Yikes. itertools was introduced in, let's see... 2003?
- scardine 7y agoPython has both generator functions and generator expressions. A generator function is like this: def int_generator(): i = 0 while True: yield i i += 1 A generator expression looks like a list comprehension (a Python construct based on the set-builder notation in Math) and is used in situations where you don't really need to materialize the list - for example when you are just iterating over it in a `for` loop: for n, square in ((i, i * i) for i in int_generator()): print(f"{n}² = {square}") time.sleep(1)
- plibither8 7y agoPast discussions: * https://news.ycombinator.com/item?id=9022676 https://news.ycombinator.com/item?id=9022676 * https://news.ycombinator.com/item?id=3231525 https://news.ycombinator.com/item?id=3231525
- beagle3 7y agoAPL is an executable mathematical notation - in fact, it was designed by Iverson for describing algorithms on paper (in 1958 IIrC), and then the team working on it implemented it in software, because ... it was a well specified way to describe algorithms. I highly recommend reading [0], which contains the simplest (IMO) description of the simplex algorithm, which is both mathematical and executable. If you find that inspiring, [1] is also worth a read. [0] https://www.jsoftware.com/papers/DFSP.htm https://www.jsoftware.com/papers/DFSP.htm [1] http://www.eecg.toronto.edu/~jzhu/csc326/readings/iverson.pdf http://www.eecg.toronto.edu/~jzhu/csc326/readings/iverson.pd...