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

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

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]= 3y

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

View Full Document
we can use a pure function instead: In[4]:= Function @ x, 3 * x [email protected] y D Out[4]= 3y 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 * [email protected] y D Out[7]= 3y In[8]:= 3 * & @ y D Out[8]= 3y 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 + 2x 2 + 3x 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 + 2x 2 + 3x 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 + 2x 2 + 3x 3 or In[18]:= Select @ myExpr, PolynomialQ @ , x D & D Out[18]= 1 + x + 2x 2 + 3x 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

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

View Full Document
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]:= Nest @H 1 H 1 + LL &, x, 4 D Out[24]= 1 1 + 1 1 + 1 1 + 1 1 + x In[25]:= Clear @ q D Assign attributes to a pure function e.g. by using Function[x,body,attr] This option is given inside ?Function (follow >> ) In[26]:= ?Function Function @ body D or body & is a pure function. The formal parameters are H or 1 L , 2, etc.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern