Live data from Hacker News

Translating math into code with examples in Java, Racket, Haskell, Python (2011)

matt.might.net

61–70 of 95 posts

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#61

Earlier quoted context omitted.

Most of the structuring (and, therefore, syntax) around an 'enterprise' application, is about how easy it is to 'replace'/'change' functions based on future needs (or known, but yet un-realized requirements). In mathematics, the driver for notation is: how succinctly can I express, what I want to express in this particular effort, yet allowing others to verify my work without mis-interpretation. I am not sure if thos…

> As I progress (perhaps, already at a sunset) of my career, I can see that lack of fluency in mathematics (both algebra and analysis) is a major impediment for transition into more highly compensated (and impactful) , yet, still technical roles for many of us, out there. Do you have an example off the top of your head? I'm assuming that by, "fluency in mathematics (both algebra and analysis)", you mean something bey…

> mean something beyond basic calculus and linear algebra.

"Fluency" in mathematics, in my experience, isn't about being able to perform more complex computation, that's what computers are for. The key to fluency in mathematics is increased efficiency at equational reasoning.

Granted, I work in data science, but I've personally seen that more and more problems I have are solved writing out a few equations and reasoning about the problem than writing code. It's not about knowing, for example, what SVD is, but seeing how a variety of common problems trivially map to it. Or figuring out how to transform a business problem into a probability problem so you can quickly estimate your success. Many times now I have spent a few afternoons writing equations that simplified what would have been very large and unnecessarily complex programs.

The mistake programmers make is thinking that math is just fancy computation, but mathematical reasoning is an entirely distinct way of reasoning about problems and computation is usually the least important part.

I used to also think that there was no reason for programmers to learn any math outside of enough calculus and linear algebra to compute basic things. But really understanding these areas and reasoning about them fluently (as well as other areas of mathematics) opens up many possibilities of problem solving that code it self does not.

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#62
post #4

I 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 s…

About variable naming in math. I sometimes also think that it would be good to define sensibly named variables and then use them in subsequent equations much more often than is usually done. Math equations are traditionally very large and complicated, even though they could be broken down to pieces and then assembled piece by piece.

I think one argument against this is that in math you often want to reach down a few abstraction levels and reorder terms, factor out things etc. which requires peeking into the "internals of boxes". You can't abstract to the point where you only have two or three variables in one equation because to show the next step, you would need to make the expanded formulas of those variables interact (cancellations etc.).

Another is that math is quite a different way of thinking. Most professions don't do as much of this input/output-based, requirements-based thinking with lots of ad-hoc on-the-fly introduced abstractions that is usual in programming. For example engineers, doctors etc. just work very differently and consider many different levels of abstraction at the same time. They consider it normal to sprinkle in temperatures, voltage values etc. in supposedly high-level discussions. Programming is a product of the mind so we have an easier time with it for sure, as our abstractions are more reliable (even if they leak to some degree, they leak much much more in other disciplines).

Now, math is also a product of the mind, but tradition is quite different. Of course math is very abstraction-heavy but the abstractions are usually not generated by the practitioner on-the-fly, on-demand, the way you just write a function or introduce a class in programming. Rather they are widely-known definitions and conventions, which are mostly only created by the people developing new theory. Of course you do define some notations and variables in math, but I find it not as pervasive as in programming.

The closest you can get to programming-like variables in math writing that still somewhat adheres to math tradition is to place full words into subscript positions (index). For example, you can write out N_columns (as a subscript) or S_upper or L_dual or f_transform. This is also something I see more in CS papers than in non-CS math content.

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#63
post #4

I 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 s…

A few personal observations:

1) A lot of frustration comes from bad expectations about how easy a math formulate should be to read. If I see 3 short lines of code, my gut tells me "this will be easy", and it's right. If I see 3 lines of math I feel like it should be easy but then get frustrated when 30 minutes later I'm still looking at the same 3 lines.

2) Once you understand a formula, I often decided I couldn't write it any better myself. It's most useful as a tool for thought and remembering, not necessarily as a tool for teaching.

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#64
post #28

> Mathematics is a purely functional language. It is not. E.g. x ∈ ℕ ∧ x > 2 ∧ x 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.

I'm not sure what point you're trying to make. You can certainly express those statements in both FP and imperative languages, and you can prove the equivalences you mention in FP languages like Agda, Coq, and F*.

An example of

> E.g. x ∈ ℕ ∧ x > 2 ∧ x

in Agda would be this, I think:

  _ : ∀ { x : ℕ } → x > 2 → x 

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#65
post #28

> Mathematics is a purely functional language. It is not. E.g. x ∈ ℕ ∧ x > 2 ∧ x 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.

Yes, well said. Math is not any form of programming, and programming is not math. Studying the relations between the two can be very helpful, and the difference itself is interesting. One may have specific reasons to us FP to cleanly decouple from certain kinds of side-effects, but there’s nothing un-mathy about mutability, and there are times when it’s a fine choice.

Can you provide an example of mutability in, say, high school or undergraduate math?

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#66
post #4

I 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 s…

About variable naming in math. I sometimes also think that it would be good to define sensibly named variables and then use them in subsequent equations much more often than is usually done. Math equations are traditionally very large and complicated, even though they could be broken down to pieces and then assembled piece by piece. I think one argument against this is that in math you often want to reach down a few…

You can use full words or even short multi-world phrases in math expressions. From a typesetting POV, write them in roman (upright) type, and enclose them in brackets whenever ambiguity arises (due to the possibility of separating spaces within a single variable, or the concurrent use of juxtaposition (of single letters) as a product or for other purposes within the same sorts of expression. This is very common in the applied sciences in general, and the convention is universally understood.

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#67

Earlier quoted context omitted.

Yes, well said. Math is not any form of programming, and programming is not math. Studying the relations between the two can be very helpful, and the difference itself is interesting. One may have specific reasons to us FP to cleanly decouple from certain kinds of side-effects, but there’s nothing un-mathy about mutability, and there are times when it’s a fine choice.

Can you provide an example of mutability in, say, high school or undergraduate math?

any sum with an index of summation requires mutability

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#68

Earlier quoted context omitted.

About variable naming in math. I sometimes also think that it would be good to define sensibly named variables and then use them in subsequent equations much more often than is usually done. Math equations are traditionally very large and complicated, even though they could be broken down to pieces and then assembled piece by piece. I think one argument against this is that in math you often want to reach down a few…

You can use full words or even short multi-world phrases in math expressions. From a typesetting POV, write them in roman (upright) type, and enclose them in brackets whenever ambiguity arises (due to the possibility of separating spaces within a single variable, or the concurrent use of juxtaposition (of single letters) as a product or for other purposes within the same sorts of expression. This is very common in th…

It may take some effort to realize this, but a mathematical formula is actually a picture, not text - more often than not it is intended to be seen - as a whole - rather then read, "word by word."

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#69
post #45
post #4

I 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 s…

I've come to the conclusion that my brain's "algorithm"y, rather than "equation"y. My scores on (especially) spatial reasoning tests are excellent so one would think I'd have a leg up in mathematics, but trying to read typical "mathy" (equation-heavy) writing makes me feel the way I assume dyslexics do, even when it's something I'm pretty familiar with . Algorithms, though? No problem. Programming in typical Algol-fa…

You're not alone. The Math symbols are easy once you learn them (at least the subset I know), but it was challenging getting there.

My field has a lot of dense math that makes the pyramids look simple (ok I exaggerate, but there is a lot going on). The only way I've been able to really grok it is by writing software to do it. Now that I know what's going on, the math notation is useful in that it explains the problem in a succint way. I think Calculus via Python with nothing but the stdlib and Matplotlib would be a better way for me to learn to be honest.

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#70

Earlier quoted context omitted.

Can you provide an example of mutability in, say, high school or undergraduate math?

any sum with an index of summation requires mutability

"requires" is a strong word. the only explicit definition ive seen given for n-ary summations and products products was recursive.
Post reply on HN