6 ms·
It's not C code. It calls into a mixture of C, CUDA, Fortran, and a slew of other things. Someone did the work of finding the best library for me, and integrati
by blagie 5y ago
It's not C code. It calls into a mixture of C, CUDA, Fortran, and a slew of other things. Someone did the work of finding the best library for me, and integrating them.
As for me, I write:
A * B
It multiplies two matrices. C can't do that. In C, I'd have some unreadable matrix64_multiply(a, b). Readability is a big deal. Math should look more-or-less like math. I can handle 2^4, or 2**4, but if you have mpow(2, 4) in the middle of a complex equation, the number of bugs goes way up.
I'd also need to allocate and free memory. Data wrangling is also a disaster in C. Format strings were a really good idea in the seventies, and were a huge step up from BASIC or Python. For 2022?
And for that A * B? If I change data types, things just work. This means I can make large algorithmic changes painlessly.
Oh, and I can develop interactively. ipython and jupyter are great calculators. Once the math is right, I can copy it into my program.
I won't even get started on things like help strings and documentation.
Or closures. Closures and modern functional programming are huge. Even in the days of C and C++, I'd rather do math in a Lisp (usually, Scheme).
I used to do numerics in C++, and in C before that. It's at least a 10x difference in programmer productivity stepping up to Python.
Your comment sounds like someone who has never done numerical stuff before, or at least not serious numerical stuff.
- tomrod 5y agoYet, Python (and most of her programmers including data scientists, of which I am one) stumble with typing. if 0.1 + 0.2 == 0.3: print('Data is handled as expected.') else: print('Ruh roh.') This fails on Python 3.10 because floats are not decimals, even if we really want them to be. So most folks ignore the complexity (due to naivety or convenience) or architect appropriately after seeing weird bugs. But the "Python is easiest and gets it right" notion that I'm often guilty of has some clear edge cases.
- dullcrisp 5y agoWhy would you want decimals for numeric computations though? Rationals might be useful for algebraic computations, but that’d be pretty niche. I’d think decimals would only be useful for presentation and maybe accountancy.
- tomrod 5y agoWell, for starters folks tend to code expecting 0.1+0.2=0.3, rather than abs(0.3-0.2-0.1) < tolerance_value Raw floats don't get you there unfortunately.
- gjm11 5y agoThey also expect 1/3 + 1/3 + 1/3 == 1. Decimals won't help with that.
- kbenson 5y agoThat's slightly different in that most programmers won't read 1/3 as "one third" but instead "one divided by three", and interpret that as three divisions added together, and the expectations are different. Seeing a constant written as a decimal invites people to think of them as decimals, rather than the actual internal representation, which is often "the float that most closely represents or approximates that decimal".
- dullcrisp 5y agoIf you want that you should use integers. This seems to be a misalignment of expectations rather than a fault in the language. Other people have posted other examples but it’s not possible to represent real numbers losslessly in finite space. Mathematicians use symbolic computation but that probably is not what you would want for numerics. I could see a language interpreting decimal input as a decimal value and forcing you to convert it to floating point explicitly just to be true to the textual representation of the number, but it would just be annoying to anyone who wants to use the language for real computation and people who don’t understand floating point would probably still complain. Edit: I’ll admit I have a pet peeve that people aren’t taught in school that decimal notation is a syntactic convenience and not an inherent property of numbers.
- dekhn 5y agohttps://docs.python.org/3/library/decimal.html https://docs.python.org/3/library/decimal.html
- tomrod 5y agoCorrect! Many python users don't know about this and similar libraries that assist with data types. Numpy has several as well.
- fnord123 5y agoThis is an issue for accountancy. Many numerical fields have data coming from noisy instruments so being lossy doesn't matter. In the same vein as why GPUs offer f16 typed values.
- d0mine 4y agoIt is not a Python thing, it is a floating-point thing. You need it if you want hardware support (CPU/GPU) for non-integer arithmetic in any language. Otherwise, you have decimal, fractions, sympy, etc modules depending on your needs. https://docs.python.org/3/tutorial/floatingpoint.html https://docs.python.org/3/tutorial/floatingpoint.html
- danuker 5y ago> the number of bugs goes way up In case you are forced to use the unreadable long-named unintuitively-syntaxed methods, add unit tests, and check that input-output pairs match with whatever formula you started with.
- arinlen 5y ago> It multiplies two matrices. C can't do that. In C, I'd have some unreadable matrix64_multiply(a, b). In C you get BLAS which provides functions like ?gemm, a BLAS level-3 function which is literally stands for general matrix-matrix product. Hardly cryptic. https://www.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-fortran/top/blas-and-sparse-blas-routines/blas-routines/blas-level-3-routines/gemm.html https://www.intel.com/content/www/us/en/develop/documentatio... Also, anyone doing any remotely serious number crunching/linear algebra work is well aware that you need to have control over which algorithms you use to run these primitive operations, and which data type you're using. > I used to do numerics in C++, and in C before that. It's at least a 10x difference in programmer productivity stepping up to Python. I'm rather skeptical of your claim. Eigen is the de-facto standard C++ linear algebra toolkit and it overloads operators for basic arithmetics. I'm not sure your appeal to authority is backed up with any relevant experience or authority. https://eigen.tuxfamily.org/dox-devel/group__TutorialMatrixArithmetic.html https://eigen.tuxfamily.org/dox-devel/group__TutorialMatrixA... I'm not sure your appeal to authority is backed up with any relevant experience or authority. It's ok if you like Python and numpy, but don't try to pass off your personal taste for anything with technical merit.
- patrick451 5y ago> > It multiplies two matrices. C can't do that. In C, I'd have some unreadable matrix64_multiply(a, b). > In C you get BLAS which provides functions like ?gemm, a BLAS level-3 function >which is literally stands for general matrix-matrix product. > Hardly cryptic. Seriously? The signature for dgemm is void cblas_dgemm(const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE TransA, const CBLAS_TRANSPOSE TransB, const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT K, const double alpha, const double *A, const CBLAS_INT lda, const double *B, const CBLAS_INT ldb, const double beta, double *C, const CBLAS_INT ldc) Maybe you have a loads of free time, but I don't want to memorize that function signature when I could just type A @ B.
- blagie 4y agoI don't actually mind memorizing these things if I'm doing this 60 hours per week. I used to be quite good at some of the C++ STL numerics libraries. I took the same perverse pride some folks here apparently do in knowing these things in- and-out. However, most of code is about being able to read it and modify it, not write it. Equations are sometimes hard enough when they look like equations. If you've got a call like that in your code, that's going to be radically harder to understand than A*B. That's not to mention all the clutter around it, of allocating and deallocating memory. One of the things C++ programmers don't typically understand are the types of boosts one gets from closures, garbage collection, and functional programming in general, especially for numerics. I recommend this book: https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Classical_Mechanics https://en.wikipedia.org/wiki/Structure_and_Interpretation_o... In it, you'll see code which: - The user writes a Lagrangian - The system symbolically computes the Lagrange equations (which are derivatives of the above) - This is compiled into native code - A numerical algorithm integrates that into a trajectory of motion - Which is then plotted All of this code is readable (equations are written in Lisp, but rendered in LaTeX). None of this is overly hard in a Lisp; this was 1-3 people hacking together, and not even central to what they were doing. It'd be neigh-impossible in C or C++. Those are the sorts of code structures which C/C++ programmers won't even think of, because their impossible to express. (Footnote: Recent versions of C++ introduced closures; I have not used them. People who have express they're not "real closures.")