Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real part of complex dot product #22261

Closed
antoine-levitt opened this issue Jun 7, 2017 · 3 comments
Closed

Real part of complex dot product #22261

antoine-levitt opened this issue Jun 7, 2017 · 3 comments
Labels
complex Complex numbers linear algebra Linear algebra

Comments

@antoine-levitt
Copy link
Contributor

A common enough operation when dealing with complex vectors is taking the real part of the inner product, ie dotr(a,b) = re(dot(a,b)) = sum_i re(ai) re(bi) + im(ai) im(bi). This turns up when evaluating Taylor series of complex-to-real maps, when evaluating quadratic forms (z* A z where A is Hermitian), etc. It is not trivial enough that one can easily define a one-liner that computes this in an optimal way (ie not real(dot(a,b))). Would it be a good idea to add a "dotr" function to base Julia or is this considered namespace pollution?

@ararslan ararslan added complex Complex numbers linear algebra Linear algebra labels Jun 7, 2017
@andreasnoack
Copy link
Member

andreasnoack commented Jun 7, 2017

I feel that this falls under our usual "modulizing Julia" discussion. I think it would be ok to export this from the LinAlg module but it is probably not a sufficiently common operation to become a Base exported function.

Update: The comment above was a bit too brief. I don't think it makes much sense in the current situation to add exports to LinAlg that are not exported from Base. In a possible future world where we don't export a lot of linear algebra symbols from Base by default, I think it would make sense to be more liberal about adding exports to LinAlg.

@stevengj
Copy link
Member

stevengj commented Jun 7, 2017

This should work for Vector, at least:

dotr{T}(a::Vector{Complex{T}}, b::Vector{Complex{T}}) = dot(reinterpret(T, a), reinterpret(T, b))

@antoine-levitt
Copy link
Contributor Author

If anybody is interested I did some benchmarking on this. On a single thread, the reinterpret version is consistently slower than real(dot()), even for large-ish arrays (which is surprising to me). A dot product of length 2N is faster for small arrays (<~= 1000), but at that point switching to static arrays is much more beneficial anyway. So the benefit in introducing a realdot is pretty marginal.

However, OpenBLAS does not multithread the complex dot product, and so even the reinterpret version is better for large arrays. I guess this should rather be fixed in OpenBLAS (opened an issue here: OpenMathLib/OpenBLAS#2221). I'm closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers linear algebra Linear algebra
Projects
None yet
Development

No branches or pull requests

4 participants