Re: Argument typing in Compile [correction]
- To: mathgroup at smc.vnet.net
- Subject: [mg9404] Re: Argument typing in Compile [correction]
- From: "Xah" <xah at best.com>
- Date: Wed, 5 Nov 1997 01:56:24 -0500
- Organization: smtp.best.com
- Sender: owner-wri-mathgroup at wolfram.com
Sorry Sean for my previous sloppy reply. Here's a corrected version with code sample. At 12:34 AM -0800 1997.11.01, Sean Ross wrote: >Does anyone know of a way to define a symbol that will execute one bit >of compiled code for certain types of arguments and another bit of >compiled code for other arguments without resorting to an explicit If >or Case structure? The following is one solution: Clear[f]; f[m_Integer]:=(Compile[{{x,_Integer}},x+1] at m); f[m_Real]:=(Compile[{{x,_Real}},Sin at x]@m); Previously, I (mistakingly) said the following would work: f[x_Integer]=Compile[...]; f[x_Real]=Compile[...]; They don't work because the left side is a pattern that is meant to be a function with an argument (e.g. f[x]), while the right side is only a function. So in essense, we are defining f[x]=f. The fixed version is in essense f[x]:=CompiledF[x]. I said: "Set and SetDelayed are permament versions of Rule and RuleDelayed". Here's some elaboration: When we want to do a substitution to a variable, we use Rule as in expr /. x->y If we don't want immediate evaluation on the right hand side of the rule, we use RuleDelayed as in expr /. x:>y Now suppose we ALWAYS want this subsitution to take place, then we use Set or SetDelayed x=y or x:=y I may be repeating the obvious. The point is that we can think of Set and SetDelayed as permament versions of Rule and RuleDelayed. (note that x may be any pattern, not just a fixed symbol) Xah, xah at best.com http://www.best.com/~xah/Wallpaper_dir/c0_WallPaper.html Mountain View, CA, USA