What is the simplest way to constraint a built-in function's output in Mathematica? say let Sin returns only odd numbers?

有些话、适合烂在心里 提交于 2019-12-09 17:00:38

问题


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



来源:https://stackoverflow.com/questions/6073396/what-is-the-simplest-way-to-constraint-a-built-in-functions-output-in-mathemati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!