class22 - PHYS 5900 Class 22 11am-12:15pm Zi-Wei Lin 3.3...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: PHYS 5900 Class 22 (10/19/2009 11am-12:15pm) Zi-Wei Lin 3.3 Functions Pure Functions Named functions and variables are global. They need to be cleared after they are no longer needed in order to avoid problems. So it is better to minimize the number of named functions and variables. To minimize the number of variables, we prefer replacements (transformation rules) over assignments. To minimize the number of named functions, we prefer pure functions. They are anonymous functions (i.e. without names). In[1]:= ? Function Function @ body D or body & is a pure function. The formal parameters are H or 1 L , 2, etc. Function @ x , body D is a pure function with a single formal parameter x . Function @8 x 1 , x 2 , … < , body D is a pure function with a list of formal parameters. Forms of a pure function include: Function[x, body] Function[x, body][arg] Function[{x1, x2, ...}, body] Function[{x1, x2, ...}, body][arg] Instead of defining a named function as: In[2]:= f @ x_ D : = 3 * x In[3]:= f @ y D Out[3]= 3 y we can use a pure function instead: In[4]:= Function @ x, 3 * x D@ y D Out[4]= 3 y In[5]:= Clear @ f D Short forms of a pure function include: Function[body] or body& Function[body][arg] or body&[arg] where # or (#1, #2 ...) refers to the formal parameter x or (x1, x2 ...) , and arg will replace # . In[6]:= ? # represents the first argument supplied to a pure function. n represents the n th argument. In[7]:= Function @ 3 * D@ y D Out[7]= 3 y In[8]:= 3 * & @ y D Out[8]= 3 y Instead of used a named function: In[9]:= test @ x_ D : = x > 4; In[10]:= Select @8 1, a, x ^ 2, 3, 5., 1 + x, 7 < , test D Out[10]= 8 5., 7 < We can use a pure function: In[11]:= Select @8 1, a, x ^ 2, 3, 5., 1 + x, 7 < , > 4 & D Out[11]= 8 5., 7 < In[12]:= Clear @ test D Example: Earlier we used a test function to constrain a pattern: In[13]:= myExpr = 1 + x + 2 * x ^ 2 + 3 * x ^ 3 + Sin @ x D ; In[14]:= test @ expr_ D : = PolynomialQ @ expr, x D 2 class22.nb In[15]:= Select @ myExpr, test D Out[15]= 1 + x + 2 x 2 + 3 x 3 Now we can use a pure function instead (without defining a test function): In[16]:= Select @ myExpr, Function @ v, PolynomialQ @ v, x DDD Out[16]= 1 + x + 2 x 2 + 3 x 3 Note: in the above we ask for a polynomial in x , so we have to use another variable name ( v ) for the formal parameter in the pure function. or use the short form of a pure function: either In[17]:= Select @ myExpr, Function @ PolynomialQ @ , x DDD Out[17]= 1 + x + 2 x 2 + 3 x 3 or In[18]:= Select @ myExpr, PolynomialQ @ , x D & D Out[18]= 1 + x + 2 x 2 + 3 x 3 In[19]:= Clear @ test, myExpr D Example: Instead of defining a named function as: In[20]:= q @ x_ D : = 1 H 1 + x L In[21]:= Nest @ q, x, 4 D Out[21]= 1 1 + 1 1 + 1 1 + 1 1 + x we can use a pure function instead: In[22]:= Nest @ Function @ v, 1 H 1 + v LD , x, 4 D Out[22]= 1 1 + 1 1 + 1 1 + 1 1 + x class22.nb 3 or use the short form: either In[23]:= Nest @ Function @ 1 H 1 + LD , x, 4 D Out[23]= 1 1 + 1 1 + 1 1 + 1 1 + x or In[24]:=...
View Full Document

This note was uploaded on 04/25/2010 for the course PHYS 5900 taught by Professor Lin during the Fall '09 term at East Carolina University .

Page1 / 11

class22 - PHYS 5900 Class 22 11am-12:15pm Zi-Wei Lin 3.3...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online