The multiplication of list of matrices - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-22T03:11:23Z https://mathematica.stackexchange.com/feeds/question/193512 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/193512 3 The multiplication of list of matrices Ghady https://mathematica.stackexchange.com/users/44040 2019-03-18T20:56:16Z 2019-03-19T08:00:42Z <p>I need to multiply 3 lists of matrices (b.a.b) as the following code </p> <pre><code>a = {{{-17.8227277373099`, -1.6565234964560602`}, {-1.6565234954649242`, 5.298073701591974`}}, {{-17.812203521929003`, -1.5013126607114478`}, {-1.5013126574896714`, 4.384050851253119`}}, {{-17.801677045750512`, -1.4055541329078751`}, {-1.405554138172727`, 3.869511752542245`}}}; b = {{{0.8409518416651456`, 0}, {0, 0.1274293000222242`}}, {{0.8409815693580924`, 0}, {0, 0.14187218616724442`}}, {{0.841011296000238`, 0}, {0, 0.15290209433231844`}}}; </code></pre> <p>I used the following:</p> <pre><code> mat = b.a.b </code></pre> <p>But, I think this way is not correct because I didn't get the result. Also, I make a test for the first matrices as the following code, that what I wanted to get for the whole matrices multiplication. </p> <pre><code> a1 = {{-17.8227277373099`, -1.6565234964560602`}, {-1.6565234954649242`, 5.298073701591974`}}; b1 = {{0.8409518416651456`, 0}, {0, 0.1274293000222242`}}; mat1 = b1.a1.b1 {{-12.6042, -0.177516}, {-0.177516, 0.0860313}} Thanks. </code></pre> https://mathematica.stackexchange.com/questions/193512/-/193515#193515 8 Answer by swish for The multiplication of list of matrices swish https://mathematica.stackexchange.com/users/2490 2019-03-18T21:21:42Z 2019-03-18T21:21:42Z <p>Using <code>MapThread</code> and <code>Dot</code>:</p> <pre><code>MapThread[Dot, {b, a, b}] </code></pre> https://mathematica.stackexchange.com/questions/193512/-/193518#193518 6 Answer by Henrik Schumacher for The multiplication of list of matrices Henrik Schumacher https://mathematica.stackexchange.com/users/38178 2019-03-18T23:36:09Z 2019-03-19T08:00:42Z <p>If you need it really fast, then use <code>Compile</code>:</p> <pre><code>n = 1000000; a = RandomReal[{-1, 1}, {n, 2, 2}]; b = RandomReal[{-1, 1}, {n, 2, 2}]; cf = Compile[{{a, _Real, 2}, {b, _Real, 2}}, b.a.b, RuntimeAttributes -&gt; Listable, Parallelization -&gt; True ]; MapThread[Dot, {b, a, b}]; // AbsoluteTiming // First cf[a, b]; // AbsoluteTiming // First </code></pre> <blockquote> <p>1.46343</p> <p>0.075722</p> </blockquote>