First, some data:
er = RandomReal[1, {343, 3}];
ns = RandomReal[1, {343, 3996, 3}];
A compiled, auto-parallelized way, which is quite fast:
cf = Compile[{{v, _Real, 1}, {w, _Real, 1}},
v.w,
RuntimeAttributes -> {Listable}, Parallelization -> True
];
(result = cf[er, ns]) // Dimensions // AbsoluteTiming
(* {0.04556, {343, 3996}} *)
An array-based approach, which is somewhat faster than the OP's MapThread
code:
(result2 = MapThread[Dot, {er, #}] & /@ Transpose[ns, {2, 1, 3}] // Transpose) //
Dimensions // AbsoluteTiming
(* {0.147916, {343, 3996}} *)
result2 == result
(* True *)
OP's:
MapThread[Function[{x, y}, ((x.#) & /@ y)], {er, ns}] // Dimensions // AbsoluteTiming
(* {0.376943, {343, 3996}} *)
Marius's solutions:
Table[er[[i]].ns[[i, j]], {i, 343}, {j, 3996}] // Dimensions // AbsoluteTiming
(* {0.855036, {343, 3996}} *)
ans2 = Block[{erCopies, erT, nsT},
erCopies = ConstantArray[#, 3996] & /@ er;
erT = Transpose[erCopies, {2, 3, 1}];
nsT = Transpose[ns, {2, 3, 1}];
Plus @@ (erT*nsT)]; // AbsoluteTiming
(* {0.149258, Null} *)
For
; useDo
,Table
,While
etc. In this case I'd suggestParallelDo
(maybe withParallelEvaluate
; see here). $\endgroup$ – corey979 Oct 11 '16 at 18:19For
, I simply mentioned it because it is absolutely obvious with that notation what I want to do. I am trying to do it withMap
and others, it is written in the question though... $\endgroup$ – leosenko Oct 11 '16 at 18:21For
I automatically discourage from using it. $\endgroup$ – corey979 Oct 11 '16 at 18:23MapThread
"is not it"? What's wrong with it? It's correct, and the fastest among the methods here. $\endgroup$ – corey979 Oct 11 '16 at 19:10Transpose[er.Transpose[ns, {2, 3, 1}], {1, 1, 2}]
. $\endgroup$ – J. M.'s ennui♦ Mar 21 '18 at 2:47