as they code I notice nested for loops that could have been one matrix multiplication.
This seems like an odd choice for your primary example.
Is the primary concern that a sufficiently smart compiler could take your matrix multiplication and turn it into a vectorized instruction?
Is it only applicable in certain languages then? E.g. do JVM languages typically enable vectorized instruction optimizations?
Is the primary concern that a single matrix multiplication is more maintainable than nested for loops?
Is it only applicable in certain domains then (e.g. machine learning)? Most of my data isn’t modelled as matrices, so would I need some nested for loops anyway to populate a matrix to enable this refactoring?
Is it perhaps worth writing a (short?) top level post with an worked out example of the refactoring you have in mind, and why matrix multiplication would be better than nested for loops?
It’s not my one trick, of course, but it illustrates my usefulness. It’s more maintainable not just because it is shorter but also because it has decades of theory behind it. Drawing the connection unlocks inspiration from entire branches of math. And the speedups from standing on the shoulders of giants go far beyond the constant factors from vectorized instructions.
I’m not disagreeing with you (I don’t think?), but just pointing out a mistaken lesson someone new to programming might draw from your comment.
it’s more maintainable not just because it is shorter but also because it has decades of theory behind it
Being shorter is way down the list of things you should consider when you’re thinking about how to make your code more maintainable. In fact, longer code is often more maintainable. As a trivial example, imagine a variable called x and the alternate name for it: seconds_since_midnight
I’m not sure there is a good rule of thumb wherein you say “shorter/longer is more maintainable”.
What about after the maddening 8-hour debug session where you finally realise they were storing a number representing seconds since noon in that variable?
I had similar thought, but then most of the languages I use don’t support matrix operations directly in the language anyway. Great believer in “tried and true” numerical analysis libraries though.
This seems like an odd choice for your primary example.
Is the primary concern that a sufficiently smart compiler could take your matrix multiplication and turn it into a vectorized instruction?
Is it only applicable in certain languages then? E.g. do JVM languages typically enable vectorized instruction optimizations?
Is the primary concern that a single matrix multiplication is more maintainable than nested for loops?
Is it only applicable in certain domains then (e.g. machine learning)? Most of my data isn’t modelled as matrices, so would I need some nested for loops anyway to populate a matrix to enable this refactoring?
Is it perhaps worth writing a (short?) top level post with an worked out example of the refactoring you have in mind, and why matrix multiplication would be better than nested for loops?
It’s not my one trick, of course, but it illustrates my usefulness. It’s more maintainable not just because it is shorter but also because it has decades of theory behind it. Drawing the connection unlocks inspiration from entire branches of math. And the speedups from standing on the shoulders of giants go far beyond the constant factors from vectorized instructions.
I’m not disagreeing with you (I don’t think?), but just pointing out a mistaken lesson someone new to programming might draw from your comment.
Being shorter is way down the list of things you should consider when you’re thinking about how to make your code more maintainable. In fact, longer code is often more maintainable. As a trivial example, imagine a variable called
x
and the alternate name for it:seconds_since_midnight
I’m not sure there is a good rule of thumb wherein you say “shorter/longer is more maintainable”.
What about after the maddening 8-hour debug session where you finally realise they were storing a number representing seconds since noon in that variable?
It’s good to keep in mind that whomever wrote the code is probably a freakin’ liar!
I had similar thought, but then most of the languages I use don’t support matrix operations directly in the language anyway. Great believer in “tried and true” numerical analysis libraries though.