From octave-sources-request at bevo dot che dot wisc dot edu Fri Jul 14 21:28:19 2000 Subject: Re: matlab-like functions From: pkienzle at kienzle dot powernet dot co dot uk (Paul Kienzle) To: tpikonen at pcu dot helsinki dot fi Cc: octave-sources at bevo dot che dot wisc dot edu Date: Fri, 14 Jul 2000 17:19:47 +0100 (BST) From: Teemu Ikonen >Hi, > >I downloaded yor matcompat collection today, good work! > >Checking out my own matlab-compatibility functions, I found that I have a >slightly better mod function which handles the case mod(x,0) like matlab, >ie. returns x, not NaN. I attached the file to this mail. Slightly worse I would say. Mod(x,0) is NaN even if matlab says otherwise. Okay, lim_{y->0+} mod(x,y) = 0, but that is still not what matlab is returning. The question is, do we really want to take a 50-80% hit in execution time to support a documented non-feature, even if the non-feature is not used? If any(y==0), mod can be up to a 600% slower. Note that your function do not support one argument being a scalar and the other being a vector. The following code fixes this, as well as being somewhat faster. Paul Kienzle pkienzle at kienzle dot powernet dot co dot uk function r=mod(x,y) if nargin != 2, usage("r=mod(x,y)"); endif pzoi = prefer_zero_one_indexing; unwind_protect prefer_zero_one_indexing = 1; nz = y != 0.0; if all(all(nz)) r = x - floor(x./y).*y; elseif is_scalar(y) r = x; elseif is_scalar(x) r = x*ones(size(y)); y = y(nz); r(nz) = x - floor(x./y).*y; else r = x; x = x(nz); y = y(nz); r(nz) = x - floor(x./y).*y; endif unwind_protect_cleanup prefer_zero_one_indexing = pzoi; end_unwind_protect endfunction ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------