[alexandria-devel] Some number patches
Tamas Papp
tkpapp at gmail.com
Wed Apr 25 07:32:40 UTC 2012
Hi Robert,
Robert Smith <quad at symbo1ics.com> writes:
> On Wed, Apr 25, 2012 at 12:30 AM, Tamas K Papp <tkpapp at gmail.com> wrote:
>> I don't really see the value of aliases for PLUSP and MINUSP, and I have
>> some doubts about NON-ZERO-P too.
>
> Not unreasonable.
>
> All of these show up in a lot of mathematical code I (and coworkers)
> write. I only argue for POSITIVEP, etc., because that is an extremely
> common term. If we need to make it more substantial than just aliasing
> PLUSP, then we can let POSITIVEP also return T for +0.0, similarly for
> NEGATIVEP and -0.0. I don't really like that, but at least it has
> utility.
CL already has FLOAT-SIGN.
> Having its own function is also useful for functional operations, like
> filtering. Real-world example: finding non-zero values in the diagonal
> of one of the matrices in a singular value decomposition and finding
> those indexes in the other factor lets us find the basis of the
> kernel/null space of the matrix.
First, I would never want test for something being (non)zero in those
cases (because of numerical error). I would test with something like
(let ((tolerance (expt epsilon (/ n))))
(lambda (x)
(<= (abs x) tolerance)))
where epsilon corresponds to the float type, and n is 2 or 3, depending
on the application. This is what is usually done in code that
calculates rank numerically.
Second, if you need a function, you can use (COMPLEMENT #'ZEROP).
> Quite simply, I'd say, the concept of non-zeroness as an almost atomic
> notion is pervasive in especially mathematics, which Lisp happens to
> be good at.
Sure, but you have to be careful when mapping abstract concepts to
floating point calculations.
Best,
Tamas
More information about the alexandria-devel
mailing list