Convolve two arrays in c++

This is a kind of strength-reduction, and modern compilers will do it for you automatically. Indeed, in C, arrays and pointers are so similar that there is little difference to begin with.

The only way this would make any difference would be if you accidentally stumbled onto something for which the compiler has an efficient idiom. Which may be worth a try, but it sounds unlikely.

BTW, why would it involve overwriting one of the arrays? There's no reason why you can't use three matrices with pointers:
*c++ = *a++ + *b++
nor why you couldn't overwrite one of the arrays in array notation:
a += b

Jiri Baum <[email protected]>
MAT LinuxPLC project --- --- Machine Automation Tools
Johan Bengtsson:
> - assembler can be used to speed up things. Really clever assebly code
> could probably improve things with a factor 2 to 10 in such cases

I still insist that a compiler is going to produce better assembly than pretty much anyone on this list :)

> - datatype, are you using floting point (slow) or integer math (fast)

Ah, that is one thing you have to do yourself. Good point.

Jiri Baum <[email protected]>
MAT LinuxPLC project --- --- Machine Automation Tools
> guess, your problem here is that arrays are not directly supported by
> c++, and 2-dim arrays are mimicked by using a "vector-of-vectors"
> representation.

No - it can be done that way, but if you simply say "int foo[2][2]" then you get an actual, honest-to-goodness two-dimensional array, usually laid out the way you expect (ie the same way as an int[4]).

> sure, doing the calculation directly on the allocated memory (using
> pointers) speeds things up

Which is how most compilers actually implement it.

> there are some free c-libraries for matrix operations and image
> manipula- tion available, and this would be the faster way if you are
> not an experienced c++ pro- grammer (look for linux / GNU scientific
> software).

Especially if the matrices are sparse or something - only store the non-zero elements.

> finally -- are you really sure that you want to do this element-wise
> addition of matrizes? depending on what you want to achieve, there
> might be other algorithmic ways (look into the vast image processing
> literature).

Indeed - for one thing, what are you going to do with A+B once you calculate it?

Jiri Baum <[email protected]>
MAT LinuxPLC project --- --- Machine Automation Tools

Johan Bengtsson

Not better than me <grin>
Point noted however, to really optimize things in assembler takes both time and skill - of course - and Jiri is right, todays compilers are good. It is still possible to improve small parts of the code however.

Note: by understanding what the compiler does you can probably improve things by helping it too, like my other note about indexing vs. moving a pointer, and still write it in C. This is not trivial either, but simpler.

/Johan Bengtsson

P&L, Innovation in training
Box 252, S-281 23 H{ssleholm SWEDEN
Tel: +46 451 49 460, Fax: +46 451 89 833
E-mail: [email protected]
Do you need education in the area of automation?
It´s an interesting topic in programing. I`ve tried such task using "memcpy" function in c++.
First at all, you MUST handle the matrices as arrays.

For example, lets try to insert an array into another, where its elements are float type.
You can copy elements like this:

void insertarray(
float *dest_array, ////where we copy
float *source_array , ///from we copy
long dest_position, ////start index
long copycount ///how many elements)

long copysize;
copysize = copycount*4;
/*remember that a type float has 4 byte*/
memcpy((void*)(&est_array[dest_position]), (void *)source_array, copysize);


But memcpy don't take avantage of the mmx procesors. I suspect that it make the work element by element, but it's much faster than a loop made by hand.