Skip to content

Instantly share code, notes, and snippets.

@waldyrious
Created February 15, 2018 19:06
Show Gist options
  • Save waldyrious/fe2fb40b30e9c0e32ae948606d5e2ef3 to your computer and use it in GitHub Desktop.
Save waldyrious/fe2fb40b30e9c0e32ae948606d5e2ef3 to your computer and use it in GitHub Desktop.
Comparison of Matlab/Octave .^2 with multiplication by the complex conjugate
a = rand(5);
b = rand(5);
c = a + 1i*b;
% -------------------------------------------------------------------------
% POW
tic();
c1 = real(c).^2 + imag(c).^2;
toc()
% -------------------------------------------------------------------------
% MUL BY COMPLEX CONJ
tic();
c2 = c .* conj(c);
toc()
% -------------------------------------------------------------------------
% Observations:
% - the results match (c1 == c2)
% - time with .^2 is consistently faster than using the complex conjugate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment