Note that there are some explanatory texts on larger screens.

plurals
  1. POMost efficient way to calculate the exponential of each element of a matrix
    primarykey
    data
    text
    <p>I'm migrating from Matlab to C + GSL and I would like to know <strong>what's the most efficient way to calculate the matrix B for which:</strong></p> <pre><code>B[i][j] = exp(A[i][j]) </code></pre> <p>where i in [0, Ny] and j in [0, Nx].</p> <p>Notice that this is different from matrix exponential:</p> <pre><code>B = exp(A) </code></pre> <p>which can be accomplished with some unstable/unsupported code in GSL (linalg.h).</p> <p>I've just found the brute force solution (couple of 'for' loops), but <strong>is there any smarter way to do it?</strong></p> <p>EDIT</p> <h2>Results from the solution post of Drew Hall</h2> <p>All the results are from a 1024x1024 <code>for(for)</code> loop in which in each iteration two <code>double</code> values (a complex number) are assigned. <strong>The time is the averaged time over 100 executions</strong>.</p> <ul> <li>Results when taking into account the {Row,Column}-Major mode to store the matrix: <ul> <li>226.56 ms when looping over the row in the inner loop in Row-Major mode (case 1).</li> <li>223.22 ms when looping over the column in the inner loop in Row-Major mode (case 2).</li> <li>224.60 ms when using the <code>gsl_matrix_complex_set</code> function provided by GSL (case 3).</li> </ul></li> </ul> <p><strong>Source code for case 1</strong>:</p> <pre><code>for(i=0; i&lt;Nx; i++) { for(j=0; j&lt;Ny; j++) { /* Operations to obtain c_value (including exponentiation) */ matrix[2*(i*s_tda + j)] = GSL_REAL(c_value); matrix[2*(i*s_tda + j)+1] = GSL_IMAG(c_value); } } </code></pre> <p><strong>Source code for case 2</strong>:</p> <pre><code>for(i=0; i&lt;Nx; i++) { for(j=0; j&lt;Ny; j++) { /* Operations to obtain c_value (including exponentiation) */ matrix-&gt;data[2*(j*s_tda + i)] = GSL_REAL(c_value); matrix-&gt;data[2*(j*s_tda + i)+1] = GSL_IMAG(c_value); } } </code></pre> <p><strong>Source code for case 3</strong>:</p> <pre><code>for(i=0; i&lt;Nx; i++) { for(j=0; j&lt;Ny; j++) { /* Operations to obtain c_value (including exponentiation) */ gsl_matrix_complex_set(matrix, i, j, c_value); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload