Re: Re: Re: Replace list element
- To: mathgroup at smc.vnet.net
- Subject: [mg106767] Re: [mg106692] Re: [mg106652] Re: [mg106630] Replace list element
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Sat, 23 Jan 2010 07:31:08 -0500 (EST)
- References: <201001191013.FAA29062@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
_Integer is a pattern, not a data-type declaration. It matches integers without naming them. Clear[type] type[_Integer] = "Integer"; type[Complex[_, _]] = "Complex"; type[Rational[_, _]] = "Rational"; type[_Symbol?NumericQ] = "Numeric Symbol"; type[_Symbol] = "Symbol"; type[_Real] = "Real"; type[_String] = "String"; type[_List] = "List"; type[_?AtomQ] = "Other Atom"; type[_] = "Other non-Atom"; type /@ {2, I, 1/2, Pi, pi, 3.4, "string", {}, Pi + 3} {"Integer", "Complex", "Rational", "Numeric Symbol", "Symbol", \ "Real", "String", "List", "Other non-Atom"} As you can see, I couldn't think of an example for "Other Atom". Use n_Integer, if you need to DO something with the argument, more than merely recognizing it as an integer: Clear[g] g[n_Integer] := PrimeQ@n g[_] := (Print["not an Integer"]; False) g /@ {Pi, 2, 7, 4} not an Integer {False, True, True, False} Bobby On Thu, 21 Jan 2010 03:53:08 -0600, Canopus56 <canopus56 at yahoo.com> wrote: > Thanks, Leonard. That is an interesting performance difference. > >> In[18]:= lst1 /. n_Integer :> fncHourFix[n] > > Another newbie type question. Several responders recommended: > >> In[18]:= lst1 /. n_Integer :> fncHourFix[n] > > Q1. What is the "_Integer" part of "n_Integer"? > > Is that just declaring a variable name "n_Integer" or is that a way to > declare a datatype "Integer". > > If that syntax is a way to declare a datatype, where is that referenced > in Mathematica documentation? I really scoured the Documentation trying > to find how to force a datatype, e.g. - VBA's equivalent to CStr, CDbl, > CInt, etc. > > Thanks - Kurt > > ________________________________ > From: Leonid Shifrin <lshifr at gmail.com> > To: Canopus56 <canopus56 at yahoo.com>; mathgroup at smc.vnet.net > Sent: Wed, January 20, 2010 6:25:42 AM > Subject: [mg106692] Re: [mg106652] Re: [mg106630] Replace list element > based on a condition how to > > Hi Kurt, > > just in case you have really large datasets to work on, the rule-based > solution is not the most efficient. Here is another one, which worked on > my tests about 40 times faster: > > Clear[fncHourFixAlt]; > fncHourFixAlt[ > hours_List] := ((hours - 6)*# + (hours - 6 + 24)*(1 - #)) &@ > UnitStep[hours - 6]; > > > In[17]:= lst1 = {18, 19, 20, 21, 22, 23, 0, 1, 2, 3} > > Out[17]= {18, 19, 20, 21, 22, 23, 0, 1, 2, 3} > > In[18]:= lst1 /. n_Integer :> fncHourFix[n] > > Out[18]= {12, 13, 14, 15, 16, 17, 18, 19, 20, 21} > > In[19]:= fncHourFixAlt@lst1 > > Out[19]= {12, 13, 14, 15, 16, 17, 18, 19, 20, 21} > > In[20]:= largelst = RandomInteger[{0, 23}, 500000]; > > In[21]:= (res1 = largelst /. n_Integer :> fncHourFix[n]); // Timing > > Out[21]= {1.25, Null} > > In[25]:= (res2 = fncHourFixAlt[largelst]); // Timing > > Out[25]= {0.031, Null} > > In[23]:= res1 === res2 > > Out[23]= True > > Regards, > Leonid > > > > > On Wed, Jan 20, 2010 at 2:48 PM, Canopus56 <canopus56 at yahoo.com> wrote: > > Thanks to all for the many replies in this thread. All the replies has > me on the right track. - Kurt >> >> > > > > -- DrMajorBob at yahoo.com
- References:
- Replace list element based on a condition how to
- From: Canopus56 <canopus56@yahoo.com>
- Replace list element based on a condition how to