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
*(a, b, c, xs...) should have methods for matrices and vectors #12065
Comments
As a generalization of this, we could use the classic dynamic programming On Wednesday, July 8, 2015, Andreas Noack notifications@github.com wrote:
|
Matrix multiplications with floats are not associative, so technically |
Everyone knows that floating point multiplication is not associative, but the reason why it rarely matters in practice is because the error in nonassociativity is bounded essentially by two ulps. See #12375 (comment) for a reference to TAoCP. |
I'm new to Julia and took this as an exercise to get used to it. I created a simple package that uses the dynamic programming algorithm mentioned by Stefan. It think it works quite well and gives huge speedups, when the default ordering is suboptimal. Maybe you find it useful! |
Hi @LMescheder, this looks cool. I don't see your package in https://github.com/JuliaLang/METADATA.jl - have you registered it yet? |
Yes, very cool! I would note that the left-to-right multiplication result is not necessarily more correct, in fact, I would guess that the method that uses fewer operations would tend to be more accurate, but I'm sure that people like @alanedelman, @stevengj, @andreasnoack and @jiahao could say better than I. |
Well, oestensibly the reason for our special parsing of |
You can always force a particular association with parentheses. |
Yes, that seems to be what is done in practice, rather than being left ambiguous for the reader to deduce from parsing rules. |
What I really like about the idea is, that it is very reasonable to interpret the absence of parentheses as "I don't care about association". In fact, I think this interpretation is far more natural than the usual "act as though I wanted multiplication from the left" interpretation (even though most programming languages and frameworks interpret it in the second way). |
However, it should be noted, that the dynamic programming algorithm's running time is cubic and the required space quadratic in the number of factors. So there should be some heuristic preventing the algorithm from running if someone e.g. wants to multiply 10000 2x2 matrices... |
I guess nobody will explicitly write the multiplication of 10000 matrices as |
Excluding sqaure matrices seems like good conservative heuristic. Have you ever looked at the Hu and Shing (1981) algorithm under the More Efficient Algorithms section on Wikipedia? Apparantly it's |
@andreasnoack Thanks for the hint, no I was not aware of it. In their paper (part 1 and part2) they state that empirically the algorithm runs faster than the dynamic programming algorithm for 7 or more factors. Probably the best approach would be to implement both and select the algorithm depending on the problem size. There also seems to be a O(n) heuristic algorithm whose solution is never worse than 1.155 times the optimal solution... In their paper they also cite some other heuristic approaches with different error ratios. |
Thanks for summarizing the paper. I've never read it. Seems like dynamic programming with an upper limit to the number of factors considered is a good solution. Later on, someone might want to add the fancy version for long chains. |
What about
? |
+1 for something like @StefanKarpinski's option 2, or even doing the naive algorithm for |
What's wrong with |
It unduly stresses the type system since we dispatch on the type of all |
dup of #3223? |
This addresses the simplest part of #12065 (optimizing * for optimal matrix order), by adding some methods for * with 3 arguments, where this can be done more efficiently than working left-to-right. Co-authored-by: Daniel Karrasch <daniel.karrasch@posteo.de>
Closed by #37898 |
This addresses the simplest part of JuliaLang#12065 (optimizing * for optimal matrix order), by adding some methods for * with 3 arguments, where this can be done more efficiently than working left-to-right. Co-authored-by: Daniel Karrasch <daniel.karrasch@posteo.de>
This addresses the simplest part of JuliaLang#12065 (optimizing * for optimal matrix order), by adding some methods for * with 3 arguments, where this can be done more efficiently than working left-to-right. Co-authored-by: Daniel Karrasch <daniel.karrasch@posteo.de>
There are two problems right now. First of all, it would be great to have
A*B*x=A*(B*x)
instead of(A*B)*x
because the former isΘ(n^2)
-ish whereas the latter isΘ(n^3)
-ish. The second problem is that the optimized transposed multiplication methods not called for e.g.A'*B*C
. Right now,A
is transposed before the multiplication so typically(A'*B)*C
is much faster thanA'*B*C
.The text was updated successfully, but these errors were encountered: