Prev: discretized Laplacian or linear inverse problem with extremely
Next: Kernels dying on parallel code
From: Benjamin Hell on 31 Jul 2010 02:40 Hi, let's say I have defined the following function: f[x_?Positive] = x Now I want to evaluate f with a variable t: f[t] As mathematica knows nothing about t, the output is f[t] instead of t. How can I tell mathematica, that t should be a positive number so that Positive[t] evaluates true and then f[t] evaluates to t? Of course this is just an example, which should present what I would like to know. Thanks in advance.
From: Leonid Shifrin on 1 Aug 2010 04:54 Hi Benjamin, You can use UpValues to accomplish your goal: In[1]:= Clear[f]; f[x_?Positive] = x Out[2]= x In[3]:= Clear[t]; t /: Positive[t] = True Out[4]= True In[5]:= f[t] Out[5]= t You can look up the definition and properties of UpValues in the documentation. The assignment operator used above is called TagSet, you may also want to look at UpSet, which is a bit more narrow (specifically for defining UpValues). Regards, Leonid On Sat, Jul 31, 2010 at 10:40 AM, Benjamin Hell <hell(a)exoneon.de> wrote: > Hi, > let's say I have defined the following function: > f[x_?Positive] = x > Now I want to evaluate f with a variable t: > f[t] > As mathematica knows nothing about t, the output is f[t] instead of t. > > How can I tell mathematica, that t should be a positive number so that > Positive[t] evaluates true and then f[t] evaluates to t? > Of course this is just an example, which should present what I would > like to know. > > Thanks in advance. > >
From: Murray Eisenberg on 1 Aug 2010 04:57 But once you put the restriction on the argument of f, you've told Mathematica not to carry out the evaluation of f unless the input supplied is actually positive. If now t is just a symbol, then it is not positive (and not negative, either) -- it's just a symbol. So why would you expect to be able to tell Mathematica that it's positive? In general, Mathematica variables don't really have types. If it's just the particular symbol t that you want to supply to f, then you could do this (I'm changing your function definition for clarity) -- not that you probably want SetDelayed (:=) instead of Set (=): f[x_?Positive] := x^2 f[t] = t^2; f[3] 9 f[t] t^2 t = -5; f[t] f[-5] f On 7/31/2010 2:40 AM, Benjamin Hell wrote: > > let's say I have defined the following function: > f[x_?Positive] = x > Now I want to evaluate f with a variable t: > f[t] > As mathematica knows nothing about t, the output is f[t] instead of t. > > How can I tell mathematica, that t should be a positive number so that > Positive[t] evaluates true and then f[t] evaluates to t? > Of course this is just an example, which should present what I would > like to know. -- Murray Eisenberg murray(a)math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305
From: Bill Rowe on 1 Aug 2010 04:58 On 7/31/10 at 2:40 AM, hell(a)exoneon.de (Benjamin Hell) wrote: >let's say I have defined the following function: f[x_?Positive] = x >Now I want to evaluate f with a variable t: f[t] As mathematica >knows nothing about t, the output is f[t] instead of t. >How can I tell mathematica, that t should be a positive number so >that Positive[t] evaluates true and then f[t] evaluates to t? Basically, you can't unless you assign a positive value to t. The pattern _?PositiveQ only matches positive numbers. A variable with no assigned values will not match this pattern. If you want your function to evaluate to a symbolic expression given variables with no assigned values as arguments, you cannot use a pattern that places restrictions on the arguments those variables are used for. Such restrictive patterns will always fail to match a variable with no assigned values. Consequently, Mathematica will always return the function unevaluated.
From: Christoph Lhotka on 2 Aug 2010 07:05 On 01/08/2010 10:58, Bill Rowe wrote: > On 7/31/10 at 2:40 AM, hell(a)exoneon.de (Benjamin Hell) wrote: > > >> let's say I have defined the following function: f[x_?Positive] = x >> Now I want to evaluate f with a variable t: f[t] As mathematica >> knows nothing about t, the output is f[t] instead of t. >> > >> How can I tell mathematica, that t should be a positive number so >> that Positive[t] evaluates true and then f[t] evaluates to t? >> > Basically, you can't unless you assign a positive value to t. > > The pattern _?PositiveQ only matches positive numbers. A > variable with no assigned values will not match this pattern. If > you want your function to evaluate to a symbolic expression > given variables with no assigned values as arguments, you cannot > use a pattern that places restrictions on the arguments those > variables are used for. Such restrictive patterns will always > fail to match a variable with no assigned values. Consequently, > Mathematica will always return the function unevaluated hello, I do not agree on that point, saying that one has to assign a value to t to make the function f work: f[t_] := If[MemberQ[{$Assumptions}, t > 0], t, -t] Assuming[t > 0, f[t]] which will return t, if t is assumed to be positive and -t otherwise. best regards, Christoph
|
Next
|
Last
Pages: 1 2 3 Prev: discretized Laplacian or linear inverse problem with extremely Next: Kernels dying on parallel code |