开发者

Maple 13: how to turn true into 1 and false into 0?

开发者 https://www.devze.com 2023-01-22 19:10 出处:网络
generaly what I need is type transformation instructions to be capable of for example multipliiing on true like5 * true and get5 of s开发者_Python百科ay x * false and get 0.

generaly what I need is type transformation instructions to be capable of for example multipliiing on true like 5 * true and get5 of s开发者_Python百科ay x * false and get 0.

How to do such thing?


You can do it with:

subs([false=0, true=1], expr);


There are several ways to get such an effect,and which you choose may depend on more particulars of what you intend on doing with it.

The simplest is to use 2-argument eval (or subs, since evaluation should occur due to automatic simplification for a product involving exact 1 or 0).

> eval( 5*true, [true=1,false=0]);
                           5
> eval( x*false, [true=1,false=0]);
                           0

And of course you can create a procedure to handle that evaluation,

> T := expr -> eval(expr,[true=1,false=0]):

> T( 5*true );
                           5
> T( x*false );
                           0

You could also try using a module to export (and thus redefine at the "top-level" of interactive use) an enhanced *.

nb. A more careful version of this exported procedure * below would only replace 'true' and 'false' if occurring as entire multiplicands, and not do replacement throughout all of the expression. (A scalar expression can have unevaluated function calls in it, with 'true' and 'false' appearing in, say, optional arguments. Ideally, these should be left alone.)

> M:=module() option package; export `*`;
>  `*`:=proc(ee::seq(anything))
>         :-`*`(op(eval([ee],[true=1,false=0])));
>       end proc;
> end module:

> with(M):

> 5*true;
                           5
> x*false;
                           0

> a*b*c;
                         a b c

> eval( %, b=false ); # this doesn't play along
                       a false c

Notice how the substitution of 'false' in that last result didn't produce 0. It's because the result of the a*b*c (for unknown a,b, and c) is in terms of the global :-* and not the new *. So when b=false is substituted there's no call to the new *. It may be possible to work around that too, although the resulting display is not so nice (and the work-around likely "breaks" something else you might intend for all this),

> M:=module() option package; export `*`;
>   `*`:=proc(ee::seq(anything))
>          local res;
>          res:=:-`*`(op(eval([ee],[true=1,false=0])));
>          if type(res,:-`*`) then
>            'procname'(op(res));
>          else
>            res;
>          end if;
>        end proc;
> end module:

> with(M):

> 5*true;
                           5
> x*false;
                           0

> a*b*c;
                     `*`(`*`(a, b), c)

> eval( %, b=false );
                           0

In that last example above, the object that looks like *(*(a, b), c) is actually in terms of the unevaluated function calls to the new *. Hence when b=false is substituted there is a call to the new * and the desired result can obtain. (Hopefully this doesn't lead to an infinite recursion in a case I've overlooked.)

0

精彩评论

暂无评论...
验证码 换一张
取 消