# @fastmath for matrices?

**URL:** https://discourse.julialang.org/t/fastmath-for-matrices/5484
**Category:** Numerics
**Tags:** question, linearalgebra, fast-math
**Created:** [August 21, 2017, 3:52pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484 "2017-08-21T15:52:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [August 21, 2017, 3:52pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484/1 "2017-08-21T15:52:28Z")

</div>

I wonder if there is a package that can figure out the fastest way to multiply out a few matrices by each other using dynamic programming or any other approach. For example, if I am doing `A*B*c` where `A` is 100x100, `B` is 100x100 and `c` is 100x1, then it is more efficient to multiply B and c first then left multiply the result by `A`, O(n^2), than to multiply A and B first then right multiply the result by c, O(n^3). Also since the title is @fastmath for matrices, probably considering the type compatibility of matrices would be an interesting twist over the traditional complexity minimization problem. So I wonder if anyone shares my thoughts that this is actually important to make Julia orders of magnitude faster in evaluating matrix multiplications than other languages which don’t automatically optimize running time complexity?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 21, 2017, 3:54pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484/2 "2017-08-21T15:54:58Z")

</div>

SugarBLAS.jl?

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [August 21, 2017, 4:04pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484/3 "2017-08-21T16:04:56Z")

</div>

This looks very useful, I might actually use it in my work, but it is not what I am looking for! I was talking about a macro that basically figures out where to put some parentheses in `A*B*c` as such `A*(B*c)`.

---

<div class="post-metadata">

### Author: ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)
#### Post date: [August 21, 2017, 4:05pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484/4 "2017-08-21T16:05:23Z")

</div>

[LinearOperators.jl](https://github.com/JuliaSmoothOptimizers/LinearOperators.jl) will perform the product in the order you want, but it requires that you convert your matrices to linear operators (which are really just thin wrappers).

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [August 21, 2017, 4:21pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484/5 "2017-08-21T16:21:09Z")

</div>

Again looks very useful, but I am not sure if it approaches the same problem I was talking about. From a quick look, it seems like LinearOperators.jl does lazy evaluation of linear operators and some embedding of easy-to-embed operators such as transposes. But I will need to take a further look at the source to see if it is actually indirectly doing these parentheses insertions or not when all operators come from matrices. I would also prefer a solution that does not exit the world of matrices and vectors to have access to all BLAS and LAPACK functions which may or may not be supported by this new type `LinearOperator`.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 21, 2017, 4:26pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484/6 "2017-08-21T16:26:44Z")

</div>

> **[GitHub - AustinPrivett/MatrixChainMultiply.jl: Find the fastest way to...](https://github.com/AustinPrivett/MatrixChainMultiply.jl)**
>
> Find the fastest way to multiply a chain of matrices and do it. - GitHub - AustinPrivett/MatrixChainMultiply.jl: Find the fastest way to multiply a chain of matrices and do it.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [August 21, 2017, 4:30pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484/7 "2017-08-21T16:30:50Z")

</div>

Perfect, just what I was looking for!
