How do you write this in lisp



BTW, instead of sign(x-a) etc. you can use compare(x,a).  You may or may not
still want to handle infinities explicitly as well, depending on the exact
semantics you want (is inf in the open interval (2,inf) ?)

             -s

On Mon, Apr 5, 2010 at 17:22, Stavros Macrakis <macrakis at gmail.com> wrote:

> Richard,
>
> The slowest thing in this code is surely the comparisons, which will be
> about as expensive whether called from Lisp or from Maxima.
>
> If you use sign instead of is(>), you can roughly halve the number of
> comparisons. This also gets rid of the 'unknown' problem which forces you to
> write stuff like "if is(a>0)=true then...".
>
> Beyond that, rewriting in Lisp won't speed things up much.
>
> Something like this:
>
>
> simpbetween(x,a,b,[option])  :=
>      block([prederror:false,leftopen,rightopen,compxa,compxb],
>         if option=[] then leftopen:rightopen:false
>         else ( leftopen: member(option[1],[lopen,open]),
>            rightopen: member(option[1],[ropen,open])),
>         compxa: sign(x-a),
>         compxb: sign(x-b),
>         if
>       (a=minf or x=inf or member(compxa,if leftopen then '[pos] else
> '[pos,pz,zero]))
>                and
>       (b=inf or x=minf or member(compxb,if rightopen then '[neg] else
> '[neg,nz,zero]))
>         then 1
>         elseif
>        (((x # inf) and (a=inf))
>              or member(compxa,if leftopen then '[neg,nz,zero] else '[neg]))
>            or
>        (((x # minf) and (b=minf))
>              or member(compxb,if rightopen then '[pos,pz,zero] else
> '[pos]))
>         then 0
>         else apply('simpfuncall,append(['between,x,a,b],option))
>         )$
>
> (this doesn't include all your options, and might not handle infinities
> exactly as you do, but should see the general idea)
>
> Of course, every time you resimplify the expression, Maxima must reevaluate
> the conditionals, which may have changed value in the meantime.
>
> Does this help?
>
>             -s
>
> On Sun, Apr 4, 2010 at 22:29, Richard Hennessy <rich.hennessy at verizon.net>wrote:
>
>>  I have a simplifying function called between() which is pretty slow.
>> Could anyone be kind enough to show me how to write the same function in
>> Lisp so it is faster and still works in Maxima.  Compiling this does not
>> produce significant improvement.
>>
>> load("simplifying.lisp");
>> simpbetween(__x,__a,__b, [__option]):=
>> block(
>>  [prederror:false],
>>  if emptyp(__option) or first(__option) = 'fourier then
>>   if is(__x > __a) = true and is(__x < __b)=true then
>>    1
>>   elseif is(__x < __a)=true or is(__x > __b)=true then
>>    0
>>   elseif is(equal(__x,__a))=true or is(equal(__x,__b))=true then
>>    1/2
>>   elseif __b = 'inf then
>>   (
>>    (1 + signum(__x - __a))/2
>>   )
>>   elseif __a = 'minf then
>>   (
>>    (1 - signum(__x - __b))/2
>>   )
>>   elseif __a = 'minf and __b = 'inf then
>>    1
>>   elseif __a = 'minf and __b = 'minf then
>>    0
>>   elseif __a = 'inf and __b = 'inf then
>>    0
>>   else
>>    if emptyp(__option) then
>>     simpfuncall('between, __x, __a, __b)
>>    else
>>     simpfuncall('between, __x, __a, __b, first(__option))
>>  elseif first(__option) = 'closed then
>>   if is(__x >= __a) = true and is(__x <= __b)=true then
>>    1
>>   elseif is(__x < __a)=true or is(__x > __b)=true then
>>    0
>>   elseif __a = 'minf and __b = 'inf then
>>    1
>>   elseif __a = 'minf and __b = 'minf then
>>    0
>>   elseif __a = 'inf and __b = 'inf then
>>    0
>>   else
>>    simpfuncall('between, __x, __a, __b, first(__option))
>>  elseif first(__option) = 'open then
>>   if is(__x > __a) = true and is(__x < __b)=true then
>>    1
>>   elseif is(__x <= __a)=true or is(__x >= __b)=true then
>>    0
>>   elseif __a = 'minf and __b = 'inf then
>>    1
>>   elseif __a = 'minf and __b = 'minf then
>>    0
>>   elseif __a = 'inf and __b = 'inf then
>>    0
>>   else
>>    simpfuncall('between, __x, __a, __b, first(__option))
>>  elseif first(__option) = 'rclosed then
>>   if is(__x > __a) = true and is(__x <= __b)=true then
>>    1
>>   elseif is(__x <= __a)=true or is(__x > __b)=true then
>>    0
>>   elseif __a = 'minf and __b = 'inf then
>>    1
>>   elseif __a = 'minf and __b = 'minf then
>>    0
>>   elseif __a = 'inf and __b = 'inf then
>>    0
>>   else
>>    simpfuncall('between, __x, __a, __b, first(__option))
>>  elseif first(__option) = 'lclosed then
>>   if is(__x >= __a) = true and is(__x < __b)=true then
>>    1
>>   elseif is(__x < __a)=true or is(__x >= __b)=true then
>>    0
>>   elseif __a = 'minf and __b = 'inf then
>>    1
>>   elseif __a = 'minf and __b = 'minf then
>>    0
>>   elseif __a = 'inf and __b = 'inf then
>>    0
>>   else
>>    simpfuncall('between, __x, __a, __b, first(__option))
>>  else
>>   simpfuncall('between, __x, __a, __b, first(__option))
>> )$
>> simplifying(between, simpbetween)$
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Maxima mailing list
>> Maxima at math.utexas.edu
>> http://www.math.utexas.edu/mailman/listinfo/maxima
>>
>>
>