3 ms·
I well understand R's operators, but why on earth is that relevant? math: S = ( H β − r )^T * ( H V H^T )^{-1} * ( H β − r ) python, ugly mess S = (H.
by x0x0 12y ago
I well understand R's operators, but why on earth is that relevant?
math:
S = ( H β − r )^T * ( H V H^T )^{-1} * ( H β − r )
python, ugly mess
S = (H.dot(beta) - r).T.dot(inv(H.dot(V).dot(H.T))).dot(H.dot(beta) - r)
python, better: (although @ is an ugly matrix operator)
S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)
the latter is an order of magnitude easier to understand, and looks just like the math. Having one layer of indirection: math to code, is far better than two: math to code to obfuscated code because you won't create infix operators.
Edit: examples stolen from the matrix operator pep
- j2kun 12y agoThe problem is that you want to do everything in one line. The proper way to do this would be to save things like H.dot(beta) - r in a separate variable and compute it just once. Moreover, if it's hidden in a library then why does the user care if it's ugly? It's the library designer's job to test it and make sure it's right.
- x0x0 12y agoNo, I simply want my math to look like math instead of a bunch of code that, after careful reading and some notes, implements some math. It's not a library function, it's the code I write. R, matlab, julia all allow users to write something that looks very close to the actual math. Python doesn't see that as a priority.