Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #16641
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx01.iad01.newshosting.com!newshosting.com!newspump.sol.net!posts.news.twtelecom.net!nnrp3.twtelecom.net!not-for-mail |
|---|---|
| From | Szabolcs Horvát <szhorvat@gmail.com> |
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Do we need a When function? |
| Date | Tue, 11 Mar 2014 07:17:50 +0000 (UTC) |
| Sender | steve@smc.vnet.net |
| Approved | Steven M. Christensen <steve@smc.vnet.net>, Moderator |
| Message-ID | <lfmdau$n7p$1@smc.vnet.net> (permalink) |
| References | <20140308074231.0A4506A0B@smc.vnet.net> <lfjtkc$hhc$1@smc.vnet.net> |
| Lines | 73 |
| Organization | Time-Warner Telecom |
| NNTP-Posting-Date | 11 Mar 2014 07:23:30 GMT |
| NNTP-Posting-Host | 3aa84a63.news.twtelecom.net |
| X-Trace | DXC=V3adFU?iLOTaZ\a6[n:\J_C_A=>8kQj6];[h;PUXBgbT27hNd_eWRHTEFiONJ7[GoVHC54TVm_59Q |
| X-Complaints-To | abuse@twtelecom.net |
| X-Received-Bytes | 3213 |
| X-Received-Body-CRC | 2904875026 |
| Xref | csiph.com comp.soft-sys.math.mathematica:16641 |
Show key headers only | View raw
On 2014-3-10, 4:37 , Itai Seggev wrote:
> On Sat, Mar 08, 2014 at 02:42:31AM -0500, David Bailey wrote:
>> Dear All,
>>
>> Recently I wanted to create an expression that only evaluated when x was
>> a number. Obviously, one way to do that is to write something like:
>>
>> If[x<1000000000000,f[x]]
>>
>> This will stay unevaluated until x is assigned to a number - as required
>> - but it is very ugly because it makes it hard to understand what is
>> going on.
>
> Why isn't this simply
>
> If[NumericQ[x], f[x]] (*are NumberQ, if want an actual number type*)
>
> But I'm further confused by your statement that it stays unevlauate until x is
> assigned a number. That's not true, it will evaluate to Null unless x is
> numeric prior to the start of evaluation.
>
>> More generally, it would be nice to write an expression that will only
>> evaluate when an expression is true. I solved that by writing a function
>> When:
>>
>> SetAttributes[When, HoldAll];
>> When[cond_, val_] := val /; cond
>>
>> However, my point is that this construction is sufficiently useful that
>> it should be built in to Mathematica, and my solution might not be
>> obvious to all users.
>>
>> Am I missing a simpler solution?
>>
>> Do others agree that this is an omission in the language?
>
> I'm not sure what your use case is. F[x] will of course stay unevluated until
> x has some value which F knows what to do with...
>
Hello Itai,
What (I believe) David means is that
If[NumericQ[x], f[x]]
immediately evaluates to Null if the symbol x has no assigned value.
When[NumericQ[x], f[x]] does not evaluate in this case, so later you can
substitute in a number for x and have f[x] evaluate correctly in case
f[x] only works with numbers, not symbols.
Then you could do something like
FindRoot[
When[NumericQ[a], First@FindMaximum[Exp[-a x] x^2, {x, 1}] - 0.5],
{a, 1}
]
without having to go to the trouble of defining
f[a_?NumericQ] := First@FindMaximum[Exp[-a x] x^2, {x, 1}]
first, and keeping in mind that the symbol f is defined (or Clear[]ing
it afterwards). This is potentially convenient when f would really be a
one-time use function.
Szabolcs
P.S. Personally I am not convinced that When[] is necessary, but I felt
the need to clarify since most responders seem to have missed the point
of the suggestion.
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar
Re: Do we need a When function? Itai Seggev <itais@wolfram.com> - 2014-03-10 08:37 +0000 Re: Do we need a When function? Szabolcs Horvát <szhorvat@gmail.com> - 2014-03-11 07:17 +0000 Re: Do we need a When function? David Bailey <dave@removedbailey.co.uk> - 2014-03-13 07:11 +0000
csiph-web