# class24 - PHYS 5900 Class 24(Fri Zi-Wei Lin Upvalues and...

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

PHYS 5900 Class 24 (10/23/2009Fri) Zi-Wei Lin Upvalues and Downvalues The usual function definitions f[arg_] = rhs f[arg_] := rhs are downvalues for f because they are associated with the symbol (head) f: In[1]:= f @ 0 D = 1; f @ x_, y_ D : = x^2 + y^3; f @ g @ x_ D , h @ y_ DD : = p @ x, y D Upvalues for f are associated with the other head functions and include Long Forms Short forms f/: g[. .., f, . ..] = rhs g[f] ^= rhs f/: g[. .., f, . ..] := rhs g[f] ^:= rhs f/: g[. .., f[arg_], . ..] = rhs g[f[arg_]] ^= rhs f/: g[. .., f[arg_], . ..] := rhs g[f[arg_]] ^:= rhs In[4]:= f ± : Re @ f D = 0; f ± : Log @ f @ x_ DD : = q @ x D ; f ± : g @ f @ x_ D , h @ y_ DD : = w @ x + y D or in short forms : In[7]:= Re @ f D ^ = 0; Log @ f @ x_ DD ^: = q @ x D ; g @ f @ x_ D , h @ y_ DD ^: = w @ x + y D Before we go to the next example: In[10]:= Clear @ f D

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

View Full Document
In[11]:= Level @ a + f @ x, y^n D , 8 - 1 <D Out[11]= 8 a, x, y, n < In the above, Level @ expr , 8 - 1 <D gives a list of all œ atomic l objects in expr . In[12]:= Im @8 3, 4 * I, 1 + 2 * I <D Out[12]= 8 0, 4, 2 < In[13]:= ? MatchQ MatchQ @ expr , form D returns True if the pattern form matches expr , and returns False otherwise. ± MatchQ[Im /@ Level[{x, y}, {-1}], {0. .}] requires that there are no imaginary atoms in x and y ( 0.. represents 1 or more repeated 0s). In[14]:= FreeQ @8 x, y < , z_ ^n_ ± ; H ! IntegerQ @ n D @ z DLD Out[14]= True The above ensures that x and y do not contain any subexpression of the form z n where n is not an integer and z is negative Example : redefine Abs[ ] so that it gives the absolute value of u c or c + d * i or u*v where c and d are real variables, u and v can be imaginary. In[15]:= ? Abs Abs @ z D gives the absolute value of the real or complex number z . ± In[16]:= Abs @ 3 + 2 * I D Out[16]= 13 In[17]:= Abs @ 3 + 2 * a * I D Out[17]= Abs @ 3 + 2 ± a D In[18]:= FullForm @ % D Out[18]//FullForm= Abs @ Plus @ 3, Times @ Complex @ 0, 2 D , a DDD Now we redefine Abs[ ] : In[19]:= Unprotect @ Abs D ; 2 class24.nb
In[20]:= Abs @ x_ ± ; H Im @ x D == @ x DLD : = x; Abs @ x_ ± ; H Im @ x D == @ x DLD : = - x; Abs @ u_ * v_ D : = Abs @ u D * Abs @ v D ; Abs @ u_ ^x_ ± ; Im @ x D == 0 D : = Abs @ u D ^x; Abs AI x_ + y_. * Complex @ 0, w_ DM ± ; I MatchQ @ Im ± ± Level @8 x, y < , 8 - 1 <D , 8 0 .. <D FreeQ @8 x, y < , z_ ^n_ ± ; H ! IntegerQ @ n D @ z DLDM E : = Sqrt @ x^2 + H w * y L ^2 D In[24]:= Protect @ Abs D ; For example, we can define a to be positive real variable and b to be negative real variable using upvalue definitions : In[25]:= ClearAll @ a, b D In[26]:= Im @ a D ^ = 0; Positive @ a D ^ = True; In[28]:= Im @ b D ^ = 0; Negative @ b D ^ = True; In[30]:= Abs @ b D Out[30]= - b In[31]:= Abs @ a * H - 5 * I LD Out[31]= 5 a In[32]:= Abs @ 3^ H 3 ± 2 L ± a^ H 3 ± 2 L + a^2 ± b^3 * I D Out[32]= 27 a 3 + a 4 b 6 In[33]:= Abs @H a^3 - b^2 ± a^4 * I L * H u + a L ^2 ± v^3 ± H 2

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

View Full Document
This is the end of the preview. Sign up to access the rest of the 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 / 13

class24 - PHYS 5900 Class 24(Fri Zi-Wei Lin Upvalues and...

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

View Full Document
Ask a homework question - tutors are online