I am using C to do some scientific computing, and need to move around a lot of vectors.
I’ve defined some basic Multiply and Add functions of the form
add(int size, double *out, double *x, double *y)
But for complex operations the code quickly becomes long and hard to read.
Is it possible to define inline operators (V1 + V2) ? Or any general best practices that will make it easier to spot check code for math errors? Perhaps some #define shenanigans?
At first I thought this was a C++ question for some reason!
If you can use C++ you might be able to use an STL array (where the size is a template parameter, not a stored value).
It might look something like this:
And you can define operator overload for operator+. E.g:
so that:
so you use at most one temporary in any chain of operations.
The memory layout of a std::array should be the same as a native array, so you should be able to “inject” this code quite easily into your existing program (by (ab)using casts, typedefs or preprocessor) if you want to touch a minimal amount of existing code.