Improvement to askinteger for review/comment



Am Sonntag, den 11.10.2009, 15:00 +1100 schrieb Billinghurst, David
(RTATECH):
> The askinteger function does not know that sqrt(3) is not an integer.
> 
> For a rational numbers N, Maxima simplifies (most?) expressions of form 
> sqrt(N^2) to abs(N).  The patch below assumes that this simplification
> has occured
> and an expression of the form sqrt(x) is not an integer if
> numberp(x)=true.  
> Are there cases when this is unsafe?  One possibility I considered is
> for (very) large x.
> 
> So some questions:
>  - is this patch correct?
>  - if not, what additional tests/constrains are needed?
>  - are there any simple generalizations?      
> 
> 	David
> 
> Index: askp.lisp
> ===================================================================
> RCS file: /cvsroot/maxima/maxima/src/askp.lisp,v
> retrieving revision 1.13
> diff -u -6 -r1.13 askp.lisp
> --- askp.lisp 10 Feb 2009 05:42:06 -0000 1.13
> +++ askp.lisp 11 Oct 2009 03:32:00 -0000
> @@ -65,12 +65,17 @@
>  (defun ask-integerp (x)
>    (let (integer-ans)
>      (if (and (mplusp x) (integerp (cadr x))) (setq x (addn (cddr x)
> t)))
>      (if (and (mtimesp x) (equal (cadr x) -1)) (setq x (muln (cddr x)
> t)))
>      (cond ((or (maxima-integerp x) (memalike x integerl)) '$yes)
>     ((or ($numberp x) (nonintegerp x) (memalike x nonintegerl)) '$no)
> +          ;; Assumes a simplified sqrt of a number is never an integer.
> +          ((and (mexptp x) 
> +  ($numberp (second x)) 
> +  (alike1 (third x) '((rat) 1 2))) 
> +    '$no)
>     ((eq (setq integer-ans (ask-prop x 'integer nil)) '$yes)
>      (ask-declare x '$integer) '$yes)
>     ((eq integer-ans '$no)
>      (ask-declare x '$noninteger) '$no)
>     (t '$unknown))))

I do not like the functions $askinteger and $asksign. Very often these
functions ask questions which are nonsense. Therefore I try to avoid
these functions completely. I prefer the functions $sign and
maxima-integerp and try to write code which give a more general result
or a noun form, if a fact is not known. 

It is a bit strange that a user has to call the function $featurep to
get the functionality of maxima-integerp, e.g. featurep(sqrt(3),integer)
-> false (Should we extend integerp, oddp and evenp to use
maxima-integerp, mevenp and moddp?).

When we extend the functionality of askinteger, we double functionality
we already have in maxima-integerp.

Perhaps, it is the best not to try to improve the code of askinteger,
but to use the function maxima-integerp at more places.

But for this case the improvement of the code of askinteger will work.

Dieter Kaiser