From help-octave-request at bevo dot che dot wisc dot edu Fri Jan 23 19:08:43 2004 Subject: Patch for discrete_inv.m From: Glenn Golden To: help-octave at bevo dot che dot wisc dot edu Date: Fri, 23 Jan 2004 18:08:12 -0700 The following patch to scripts/statistics/distributions/discrete_inv.m significantly reduces its memory usage for large datasets. It does the following: 1. Elimininates the variable 'ind', which is the same size as the incoming dataset, and was never being used. 2. Clears variable 'p' (also same size as the incoming dataset) as soon as it's finished using it. 3. Substitutes a loop for a vector-parallel operation which was very memory intensive. This a space-time tradeoff in favor of space. The interpreter may implicitly accomplish both of 1 and 2, I don't know, so those changes may not have any effect. The third one is the primary one. The diff is against discrete_inv.m 1.5, as supplied in 2.1.50. (I'm not posting this to the bugs-octave list because I'm not sure that it's considered a bug: In some cases, the space-time tradeoff made in this patch may be the opposite of what is desired, e.g., if the routine is being called many times with relatively small datasets and large CDF fraction vectors.) Glenn Golden ========================== begin patchfile =========================== *** ORIGdiscrete_inv.m Fri Jan 23 17:15:25 2004 --- discrete_inv.m Fri Jan 23 17:33:10 2004 *************** *** 46,53 **** n = r * c; x = reshape (x, 1, n); m = length (v); ! [v, ind] = sort (v); s = reshape (cumsum (p / sum (p)), m, 1); inv = NaN * ones (n, 1); if (any (k = find (x == 0))) --- 46,54 ---- n = r * c; x = reshape (x, 1, n); m = length (v); ! v = sort (v); s = reshape (cumsum (p / sum (p)), m, 1); + clear p; # Free some memory, p may be large inv = NaN * ones (n, 1); if (any (k = find (x == 0))) *************** *** 56,66 **** if (any (k = find (x == 1))) inv(k) = v(m) * ones (1, length (k)); endif if (any (k = find ((x > 0) & (x < 1)))) n = length (k); ! ## --FIXME-- ! ## This does not work! ! inv(k) = v(sum ((ones (m, 1) * x(k)) > (s * ones (1, n))) + 1); endif inv = reshape (inv, r, c); --- 57,72 ---- if (any (k = find (x == 1))) inv(k) = v(m) * ones (1, length (k)); endif + if (any (k = find ((x > 0) & (x < 1)))) n = length (k); ! # ! # The following loop is a space/time tradeoff in favor of space, since ! # the dataset may be large. ! # ! for q = 1:n ! inv(q) = v(sum (x(q) > s) + 1); ! endfor endif inv = reshape (inv, r, c); ========================== end patchfile =========================== ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------