class17 - PHYS 5900 Class 17 (10/02/2009 Fri) Zi-Wei Lin...

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

View Full Document Right Arrow Icon
PHYS 5900 Class 17 (10/02/2009 Fri) Zi-Wei Lin Obtaining Parts of Expressions We can use First, Last, Extract, Part, Take, Rest, Most, Drop, Select as for Lists (they are just a particular kind of expressions) In[1]:= myExpr = 1 + x + 2 * x^2 + Sin @ x D + 3 * x^3 Out[1]= 1 + x + 2 x 2 + 3 x 3 + Sin @ x D In[2]:= FullForm @ myExpr D Out[2]//FullForm= Plus @ 1, x, Times @ 2, Power @ x, 2 DD , Times @ 3, Power @ x, 3 DD , Sin @ x DD In[3]:= Last @ myExpr D Out[3]= Sin @ x D In[4]:= Take @ myExpr, 2 D Out[4]= 1 + x In[5]:= Drop @ myExpr, 8 2, 4 <D Out[5]= 1 + Sin @ x D Different forms of Part: Part[expr, n] == expr[[n]] for 1 element in list Part[expr, {n1, n2, . ..}] == expr[[ {n1, n2, . ..} ]] for multiple elements in list Part[expr, i, j, . ..] == expr[[ i, j, . .. ]] == expr[[i]][[j]]. .. for 1 element in a multidimensional list In[6]:= Part @ myExpr, 4 D Out[6]= 3 x 3 In[7]:= Part @ myExpr, 4, 2, 1 D Out[7]= x In[8]:= Part @ myExpr, 8 1, 3, 4 <D Out[8]= 1 + 2 x 2 + 3 x 3 In[9]:= Extract @ myExpr, 8 4 <D Out[9]= 3 x 3 In[10]:= Extract @ myExpr, 8 4, 2, 1 <D Out[10]= x
Background image of page 1

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

View Full DocumentRight Arrow Icon
In[11]:= Extract @ myExpr, 88 1 < , 8 3 < , 8 4 <<D Out[11]= 9 1, 2 x 2 , 3 x 3 = In[12]:= Extract @ myExpr, 88 4, 2, 1 < , 8 3, 1 <<D Out[12]= 8 x, 2 < ± The above operations are based on the position of elements: In[13]:= myExpr Out[13]= 1 + x + 2 x 2 + 3 x 3 + Sin @ x D In[14]:= Position @ myExpr, x D Out[14]= 88 2 < , 8 3, 2, 1 < , 8 4, 2, 1 < , 8 5, 1 << Position[expr, pattern, levelspec] finds only objects on levels specified by levelspec. Again, levelspec can be n for levels from 1 to n; Infinity for all levels {n} for level n only {n1,n2} from level n1 through n2 In[15]:= Position @ myExpr, x, 2 D Out[15]= 88 2 < , 8 5, 1 << In[16]:= Position @ myExpr, x, 8 3 <D Out[16]= 88 3, 2, 1 < , 8 4, 2, 1 << We can Select elements based on their properties instead of positions. In[17]:= Select @ myExpr, AtomQ D Out[17]= 1 + x In[18]:= Select @ myExpr, AtomQ, 1 D Out[18]= 1 2 class17.nb
Background image of page 2
Criterion can be found in Testing Expressions » and include: IntegerQ, SameQ, ListQ, PrimeQ, EvenQ, OddQ, StringQ, ValueQ, TrueQ, . .. Negative, Positive,NonNegative Only the name of a function or predicate is used as criterion (without any arguments). A predicate is a function for testing an element and return True or False. Construct criterion using functions that test the properties of expressions : In[19]:= ? PolynomialQ PolynomialQ @ expr , var D yields True if expr is a polynomial in var , and yields False otherwise. PolynomialQ @ expr , 8 var 1 , <D tests whether expr is a polynomial in the var i . ± In[20]:= test1 @ expr_ D : = PolynomialQ @ expr, x D In[21]:= myExpr Out[21]= 1 + x + 2 x 2 + 3 x 3 + Sin @ x D In[22]:= Select @ myExpr, test1 D Out[22]= 1 + x + 2 x 2 + 3 x 3 The following uses PolynomialQ directly but is wrong: In[23]:= Select @ myExpr, PolynomialQ D Out[23]= 1 + x + 2 x 2 + 3 x 3 + Sin @ x D In[24]:= test2 @ expr_ D : = H expr ± Sin @ x DL In[25]:= Select @ myExpr, test2 D Out[25]=
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
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 / 12

class17 - PHYS 5900 Class 17 (10/02/2009 Fri) Zi-Wei Lin...

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

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