What is the simplest way to constraint a built-in

2019-04-06 11:00发布

问题:

For example, for a built-in function in Mathematica, f, originally f[1] gives {1,2,3}, but I want to let Mathematica gives only {1,3}. A simple method for rewritting f is desired. I don't want to define a new function or totally rewrite f or just dealing with original f's outputs. I want to rewite f.

Thanks. :)

回答1:

You can use the Villegas-Gayley trick for this.

For the Sin function:

Unprotect[Sin];
Sin[args___]/;!TrueQ[$insideSin]:=
   Block[{$insideSin=True,result},
      If[OddQ[result=Sin[args]],result]
   ];
Protect[Sin];
{Sin[Pi],Sin[Pi/2]}

==> {Null,1}


回答2:

I prefer a method which is functional and reminds me of decorators in Python. http://wiki.python.org/moin/PythonDecorators

First we create the decorator:

OddOnly[x_] := If[OddQ[x], x, Null];

It can then be used as a prefix:

OddOnly@
 Sin[Pi]

Null (* Doesn't actually give a result *)

OddOnly@
 Sin[Pi/2]

1


回答3:

A variation of Searke's method that I prefer is:

OddOnly[s_Symbol] := 
 Composition[If[OddQ@#, #, ##&[]] &, s]

This automatically removes results that are not odd, and it is applied to the function itself, which I find more convenient.

Examples:

OddOnly[Sin] /@ {2, Pi, Pi/2}

(*  Out[]= {1}  *)

Array[OddOnly[Binomial], {5, 5}]

(*  Out[]= {{1}, {1}, {3, 3, 1}, {1}, {5, 5, 1}}  *)


回答4:

Could apply a rule of the form

whatever /. _Integer?EvenQ :>Sequence[]

Daniel Lichtblau