I have written a module that it generates a 101x101x51 zero matrix at first. At every iteration, it generates a list by funct1 and based on this list, if list is empty it gives the previous result. If list is non-empty, it applies funct2 and get the same-sized matrix and add it to the previous one. funct2 uses BinCounts so it generates a matrix with scalar values in it..
The piece of code is :
resultfunction[minx_, miny_, minz_, maxx_, maxy_, maxz_] := Module[{points, i, j, k},
mtrx = Table[0, {101}, {101}, {51}];
Do[points = funct1[i, j, k];
If[points === Null, mtrx, mtrx = mtrx + funct2[0.2, points](*0.2 is a coefficient*)],
{i, minx, maxx}, {j, miny, maxy}, {k, minz, maxz}];
points =.;
mtrx]
The problem is that I am using a device with 32gb RAM, however it runs out of memory when I ran this module. I thought because of the fact that maybe it can be storing the points, at every iteration, I cleared up the points. However, the result is the same. When adding matrices, don't they use the same memory?
p.s. minx=miny=minz=1; maxx= 85, maxy=80, maxz=50
Any help is appreciated.
EDIT
funct1 is too long to write it here and it just return Null or a list whose dimensions are {length of the list,3}. funct2 is :
funct2[coefficient_, points_] :=
Block[{bsplinefunct, sampledpoints, results},
bsplinefunct = BSplineFunction@points;
sampledpoints = Thread[bsplinefunct[Range[0, 1, .0001]]];
results = funct3[20, 20, 5, 40, 40, 15, coefficient, {sampledpoints}];
{bsplinefunct, sampledpoints} =.;
results]
funct3 is :
funct3[minx_, miny_, minz_, maxx_, maxy_, maxz_, step_, points_] :=
Module[{matrix},
matrix = Total[Unitize[BinCounts[#, {minx, maxx + step, step}, {miny, maxy + step,
step}, {minz, maxz + step, step}]] & /@ points]]
funct1
andfunct2
. One possible guess is that some of these functions unpack (returned not packed, but unpacked array) - unpacked arrays can take up much more memory. It could also be that the huge memory consumption happens inside those functions. $\endgroup$If[points === Null, mtrx, mtrx = mtrx + funct2[0.2, points](*0.2 is a coefficient*)]
equivalent toIf[Not[points === Null], mtrx = mtrx + funct2[0.2, points](*0.2 is a coefficient*)]
. But I am not sure I can suggest anything relevant withoutfunct1
andfunct2
$\endgroup$$HistoryLength=0
and should check if everything is a packed array. $\endgroup$