Khaled Barazi
Khaled Barazi

Reputation: 8741

Accelerate framework ios: Fastest pythagoren calculation

So I have 2 matrices: each is 100x100.

I am looking to calculate a 3rd matrix such that: M3[i]=sqrt(M1[i]^2 + M2[i]^2).

I can obviously do ForLoops but I am sure there is something faster.

I digged into the accelerate framework and got lost in Linpack world

Any help to get me on the right track...

Thanks

Upvotes: 1

Views: 598

Answers (2)

jackdev23
jackdev23

Reputation: 116

I think the fastest way is to use

vDSP_vpythg Vector Pythagoras; single precision.

Subtracts vector C from A and squares the differences, subtracts vector D from B and squares the differences, adds the two sets of squared differences, and then writes the square roots of the sums to vector E.

Obviously pass ZERO vector for C and D .

Upvotes: 0

Chris McGrath
Chris McGrath

Reputation: 1936

The Accelerate framework is a good idea.

You could use a function like vDSP_vsq to operate on one column of the matrix at a time, placing the result in the corresponding column of M3. You might have to make two resultant matrices (one which will be M1^2, the other M2^2), and then add them to form the correct M3 result matrix by adding the columns with a call to vDSP_vadd which, again, will be able to operate one column at a time.

There is sample code (showing how to add two vectors, etc) here at the Apple developer page.

Upvotes: 1

Related Questions