Could somebody teach me how to compile this code?
Do[uu = Outer[Times, u[[n1]], u[[m1]]];
Unm[[n1, m1]] = Flatten[uu].Flatten[data], {n1, n}, {m1, n}];
Do[uuu = Outer[Times, u[[All, s]], u[[All, t]]];
reimg[[s, t]] = Flatten[uuu].Flatten[Unm], {s, sz}, {t, sz}];
This part of code calculates image moment. data is image data with size sz*sz
. Unm
is a table of moment which I try to get. u
is a table size of n*sz
. usually n=sz
. My code runs hours to get the reconstruction image, reimg
. How could I compile this code? All of the arguments are tables of list, that is what confuse me the most. This code is upgrade from this Original code:
Do[Unm[[n1, m1]] =
Sum[Sum[u[[n1, s]] u[[m1, t]] data[[s, t]], {s, sz}], {t,
sz}], {n1, n}, {m1, n}];
Do[reimg[[s, t]] =
Sum[Sum[ Unm[[n1, m]] u[[n1, s]] u[[m, t]], {n1, n}], {m, n}], {s,
sz}, {t, sz}];
The first code is 2 times faster than the original one, but still not fast enough.Please Help!!!
I try to compile the Unm
part like this:
fc = Compile[{{list1, _Real, 1}, {list2, _Real, 1}}, Unm = list1.list2,
Parallelization -> True, RuntimeAttributes -> {Listable}];
Do[list1 = Flatten[Outer[Times, u[[n1]], u[[m1]]]]; list2 = Flatten[data];
Unm = fc[list1, list2], {n1, n}, {m1, n}];
It says:
Flatten::normal: "Nonatomic expression expected at position 1 in Flatten[1.38595258992021`].
What does it mean?
Based on answers below, here is the update code:
Unm = u.data.u\[Transpose];
reimg1 = u\[Transpose].Unm.u;
It's simple. It's elegant. It runs like lightning. Love it. Thanks for helping out!
Unm=Table[Sum[...],{n1,n},{m1,n}]
as opposed toDo[Unm ...]
and likewise for the secondDo
loop you've defined forreimg
. $\endgroup$Moments
and/orCentralMoments
? It looks like you are reproducing built in function. $\endgroup$Map[Flatten, Transpose /@ Outer[Times, u, u], {2}].data
is yourUnm
. Is that a) doing what you want, and b) faster? $\endgroup$