Prev: First nonzero in list
Next: change $UserBaseDirectory ?
From: telefunkenvf14 on 11 Jul 2010 06:19 On Jul 8, 7:34 pm, Peter Breitfeld <ph...(a)t-online.de> wrote: > telefunkenvf14 wrote: > > Group: > > > 1. Can someone provide a clearer way of implementing the following > > example? (Goal: I want a pattern that accepts only a list of real > > numbers and what I have seems hacky.) > > > MatchQ[{1, 2, 2.2},x_?(VectorQ[#,(NumericQ[#]&&Im[#]==0)&]&)] > > > 2. Could someone show me how to create a second pattern which accepts > > only a list (vector) of symbols? I feel like I'm spinning my wheels a > > bit on this one... > > > 3. Does anyone else think it would be nice to compile a list of > > favorite/useful patterns, along with a very brief explanation of the > > intent? (And yes, I know there are a bunch of examples in the > > documentation(!), but I'm thinking more in terms of assembling a list > > of recipes.) I guess I'm just craving more guided examples. > > > -RG > > The answer to question 1 could be: > realpatt={_Real..} > > Question 2 could be done by: > vectorpatt=v:{_Symbol..}/;VectorQ[v] > > There is a plethora of combinations of patterns, but in my opinion some > of the fundamental ones are included in the examples above. Mostly it's > possible to achieve the same goal in different ways. > > -- > _________________________________________________________________ > Peter Breitfeld, Bad Saulgau, Germany --http://www.pBreitfeld.de This is actually part of why I broke down and asked. Guidance on 'best practices', in regard to different types of pattern-based tasks, seems hard to come by. Maybe I just need to sign up for M201, or whatever the course number is... In general, I guess I'm most interested in restricting input patterns for functions---but I'm not really sure if it's worth the effort. How much does Mathematica really benefit by narrowing types of inputs? (besides the benefit of overloading symbols) For example, does it help above and beyond simply declaring, say, SetAttributes[MyFunction]={NumericFunction}? -RG
From: Nasser M. Abbasi on 12 Jul 2010 01:03
On 7/11/2010 3:19 AM, telefunkenvf14 wrote: > > How much does Mathematica really benefit by narrowing types of inputs? > (besides the benefit of overloading symbols) For example, does it help > above and beyond simply declaring, say, > SetAttributes[MyFunction]={NumericFunction}? > > -RG > It is not Mathematica which benefits, it is your program which will. If you know that a function you are writing will only accept input which must be numeric, then better to define the function as such. This will make your program more reliable, because if somewhere else you made a mistake calling the function with the wrong argument, i.e. something other than a numeric argument, then you'll catch that right away, at the call time, not some time later down the road. If in addition, you know something about the range of values the input must be within, it is better to also add this as well. Something like foo[x_?(NumberQ[#]&&Positive[#]&)]:=..... much better than just writing foo[x_]:=..... Since Mathematica does not have data types, the above seems to me to be one way around this that I learned. In languages with type definition, one can define a type to be numeric and positive, and then declare variables of that type, then the compiler will check that caller argument matches the type of called parameter. Learning how to use correct type definition to match the problem at hand, can lead to programs with much less errors. --Nasser |