TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Math Symbols Explained with Python

130 pointsby amitnessabout 5 years ago

19 comments

exmadscientistabout 5 years ago
This is not a very good list. Several items are between &quot;misleading&quot; and &quot;wrong&quot;:<p>1. &quot;Pipe&quot; should be called &quot;bar&quot; (IMO)<p>2. &quot;Vector norm&quot; is a much deeper idea than just &quot;2-norm&quot; and if you limit yourself to the 2-norm, you will run into problems<p>3. &quot;Set membership&quot; is not called &quot;epsilon&quot;!<p>4. I wouldn&#x27;t describe functions as operating on &quot;pools&quot;, but that&#x27;s just me. (&quot;Pools&quot; seems to imply vector arguments, or at least set arguments, which is not how functions are usually thought of operating, though they are defined that way.)<p>5. The description of R^2 as &quot;2-D array&quot; is wrong.<p>6. I&#x27;ve never seen that notation for elementwise multiplication, though that doesn&#x27;t mean much.<p>7. I&#x27;ve rarely seen that notation for dot products. Usually the center dot is much more common.<p>8. Hat is... nonstandard. It has many, many meanings.
评论 #22639108 未加载
评论 #22639539 未加载
评论 #22640248 未加载
reikonomushaabout 5 years ago
It is faulty to think of the math notation and code being equivalent. The math states something much more general, and often much deeper, than some mundane for-loop. I’d have titled this “Python Recipes to Calculate Values of Simple Vector Math Expressions”.<p>Often times the symbols may not even be concrete numbers. Often the “real” number may not even be representable on a computer. Often a symbol <i>does</i> represent a concrete number but indirectly defined by some set of rules (e.g., x is the smallest eigenvalue of M; or y is the greatest value less than the supremum of f) that may require a whole library of code to calculate.<p>The mathematical notation is “meant” to be flexible and manipulated, not to be interpreted (necessarily) as a rigid computational process. It should also be noted that while some notation comes from convention, a lot of it is improvised and contextual!
评论 #22638646 未加载
评论 #22640014 未加载
评论 #22643501 未加载
评论 #22640657 未加载
评论 #22638954 未加载
lonelappdeabout 5 years ago
I do wonder, at there people writing ML programs in Python without learning basic math notation?<p>It&#x27;s hard to imagine someone learning important fundamt math concepts without using math notation, so anyone who benefits from this article should probably take a detour through a math book before continuing their Python ML, or else risk having beautiful code that implements nonsensical math.
评论 #22638436 未加载
评论 #22638587 未加载
评论 #22638672 未加载
评论 #22640041 未加载
canjobearabout 5 years ago
The article explains \hat{} as meaning a vector of unit length but in my experience in ML it nearly always designates an estimate (including in the first expression in this article).
评论 #22644370 未加载
评论 #22638285 未加载
评论 #22638650 未加载
boublepopabout 5 years ago
It’s interesting where the “technical limitations” set in on computers. We can’t have symbolic math accurately represented because “how would you even do that with keyboard input, and how would you encode it?!” Yet we have poop emojis because that’s an absolute essential part of written communication these days.
Davidbrczabout 5 years ago
Im always in awe when such basic maths makes the front page of Hacker news.
评论 #22638787 未加载
评论 #22638411 未加载
评论 #22638813 未加载
bitforgerabout 5 years ago
I see many comments criticizing the post for only implementing simple mathematics. This is true.<p>However, the approach of understanding math through code is still very helpful, I think. Personally, implementing things that are fuzzy mathematically provides immense clarity once I write them down.<p>For example, a simple monty hall simulator[1]. Or implementing matrix multiplication multiple ways to understand why each is equivalent[2], and why multiplying A(BC) can sometimes be faster than (AB)C[3].<p>I am not sure why this helps me. It may be because I was &quot;raised&quot; as a coder, and so that is how my brain works. But I also think that implementing something in code is very close to constructivist mathematics, in spirit. You cannot prove anything if you cannot construct (&#x2F;implement) it.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;mitchellgordon95&#x2F;implementing-paradoxes&#x2F;blob&#x2F;master&#x2F;monty_hall.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mitchellgordon95&#x2F;implementing-paradoxes&#x2F;b...</a> [2] <a href="https:&#x2F;&#x2F;github.com&#x2F;mitchellgordon95&#x2F;implementing-paradoxes&#x2F;blob&#x2F;master&#x2F;matmul.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mitchellgordon95&#x2F;implementing-paradoxes&#x2F;b...</a> [3] <a href="https:&#x2F;&#x2F;github.com&#x2F;mitchellgordon95&#x2F;implementing-paradoxes&#x2F;blob&#x2F;master&#x2F;matmul_assoc.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mitchellgordon95&#x2F;implementing-paradoxes&#x2F;b...</a>
carapaceabout 5 years ago
If you liked this...<p><a href="https:&#x2F;&#x2F;mitpress.mit.edu&#x2F;sites&#x2F;default&#x2F;files&#x2F;titles&#x2F;content&#x2F;sicm_edition_2&#x2F;book.html" rel="nofollow">https:&#x2F;&#x2F;mitpress.mit.edu&#x2F;sites&#x2F;default&#x2F;files&#x2F;titles&#x2F;content&#x2F;...</a><p>&quot;Structure and Interpretation of Classical Mechanics&quot; by Gerald Jay Sussman and Jack Wisdom<p>&gt; There has been a remarkable revival of interest in classical mechanics in recent years. We now know that there is much more to classical mechanics than previously suspected. The behavior of classical systems is surprisingly rich; derivation of the equations of motion, the focus of traditional presentations of mechanics, is just the beginning. Classical systems display a complicated array of phenomena such as nonlinear resonances, chaotic behavior, and transitions to chaos.<p>&gt; Traditional treatments of mechanics concentrate most of their effort on the extremely small class of symbolically tractable dynamical systems. We concentrate on developing general methods for studying the behavior of systems, whether or not they have a symbolic solution. Typical systems exhibit behavior that is qualitatively different from the solvable systems and surprisingly complicated. We focus on the phenomena of motion, and we make extensive use of computer simulation to explore this motion.<p>They are basically making the computer do the work, with emphasis on unambiguous, computable notation.<p>(It could be considered a companion to SICP.)
westurnerabout 5 years ago
Average of a <i>finite</i> series: There&#x27;s a statistics module in Python 3.4+:<p><pre><code> X = [1, 2, 3] from statistics import mean, fmean mean(X) # may or may not be preferable to sum(X) &#x2F; len(X) </code></pre> <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;statistics.html#statistics.fmean" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;statistics.html#statistics...</a><p>Product of a terminating iterable:<p><pre><code> import operator from functools import reduce # from itertools import accumulate reduce(operator.mul, X) </code></pre> Vector norm:<p><pre><code> from numpy import linalg as LA LA.norm(X) </code></pre> <a href="https:&#x2F;&#x2F;docs.scipy.org&#x2F;doc&#x2F;numpy&#x2F;reference&#x2F;generated&#x2F;numpy.linalg.norm.html" rel="nofollow">https:&#x2F;&#x2F;docs.scipy.org&#x2F;doc&#x2F;numpy&#x2F;reference&#x2F;generated&#x2F;numpy.l...</a><p>Function domains and ranges can be specified and checked at compile-time with type annotations or at runtime with type()&#x2F;isinstance() or with something like pycontracts or icontracts for checking preconditions and postconditions.<p>Dot product:<p><pre><code> Y = [4, 5, 6] np.dot(X, Y) </code></pre> <a href="https:&#x2F;&#x2F;docs.scipy.org&#x2F;doc&#x2F;numpy&#x2F;reference&#x2F;generated&#x2F;numpy.dot.html" rel="nofollow">https:&#x2F;&#x2F;docs.scipy.org&#x2F;doc&#x2F;numpy&#x2F;reference&#x2F;generated&#x2F;numpy.d...</a><p>Unit vector:<p><pre><code> X &#x2F; np.linalg.norm(X)</code></pre>
kstrauserabout 5 years ago
Seeing `for i in range(len(lst)): lst[i]...` gives me hives. That&#x27;s cool if you&#x27;re wanting to be super explicit about how the indexing works, but in this page it goes on to say &quot;or you can just write sum(lst)&quot; without worrying about the indexing.<p>I would write the explanations like:<p><pre><code> result = 1 x = [1, 2, 3, 4, 5] for number in x: result = result * number print(result) </code></pre> which in my opinion is much closer to the way a mathematician would think about the process.
jpeanutsabout 5 years ago
Maths notation can be wonderfully concise and precise, so it worth thinking about following it closely when programming. One of my favorite examples of this is the numpy `einsum` call [1]. It implements Einstein summation convention [2] - thereby making working with the many dimensions of high-rank tensors feasible.<p>E.g. this (Latex):<p>$C_{ml} = A_{ijkl} B_{ijkm}$<p>becomes (in Python):<p>C = einsum(&#x27;ijkl,ijkm-&gt;ml&#x27;, A, B)<p>[1] <a href="https:&#x2F;&#x2F;docs.scipy.org&#x2F;doc&#x2F;numpy&#x2F;reference&#x2F;generated&#x2F;numpy.einsum.html" rel="nofollow">https:&#x2F;&#x2F;docs.scipy.org&#x2F;doc&#x2F;numpy&#x2F;reference&#x2F;generated&#x2F;numpy.e...</a> [2] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Einstein_notation" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Einstein_notation</a>
jay754about 5 years ago
Solid post. I think someone who&#x27;s just starting out with ML or even basic mathematically notations would find this very helpful. Especially someone knows programming, but struggles with mathematically jargon
lonelappdeabout 5 years ago
For arrays, the math uses ordinal convention (1-based) but the Python uses offset convention (0-based). That&#x27;s OK, but the article should explicitly mention that to avoid confusion.
staredabout 5 years ago
Shameless plug, but I worked for some time on &quot;Thinking in Tensors, Writing in PyTorch&quot; pretty much focusing on turning LaTeX math into executable code.<p>E.g. Gradient Descent: formula, code, and visualization:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;stared&#x2F;thinking-in-tensors-writing-in-pytorch&#x2F;blob&#x2F;master&#x2F;2%20Gradient%20Descent.ipynb" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;stared&#x2F;thinking-in-tensors-writing-in-pyt...</a>
gugagoreabout 5 years ago
I think it is confusing not to address that math notation often (including the one used here) uses 1-based indexing, while Python uses 0-based indexing.
fatso784about 5 years ago
Given that FORTRAN was designed as basically rote translations from discrete math notation, it is deeply ironic that we now need to explain things in the opposite direction.
m4r35n357about 5 years ago
TFA doesn&#x27;t even mention what it is _really_ using (numpy), virtually none of this is pure Python.
hwcabout 5 years ago
math.sqrt(x[0]<i></i>2 + x[1]<i></i>2 + x[2]<i></i>2)<p>can be simplified to:<p>math.sqrt(sum(v<i></i>2 for v in x))
_ZeD_about 5 years ago
&gt;&gt;&gt; Average: $$\frac{1}{N}\sum_{n=1}^N x_i$$<p>since when 1&#x2F;N means &quot;len(n)&quot;?
评论 #22638205 未加载