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. :)
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With