{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# class17 - PHYS 5900 Class 17 Fri Zi-Wei Lin Obtaining Parts...

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

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 + 2x 2 + 3x 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]= 3x 3 In[7]:= Part @ myExpr, 4, 2, 1 D Out[7]= x In[8]:= Part @ myExpr, 8 1, 3, 4 <D Out[8]= 1 + 2x 2 + 3x 3 In[9]:= Extract @ myExpr, 8 4 <D Out[9]= 3x 3 In[10]:= Extract @ myExpr, 8 4, 2, 1 <D Out[10]= x

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

View Full Document
In[11]:= Extract @ myExpr, 88 1 < , 8 3 < , 8 4 <<D Out[11]= 9 1, 2x 2 , 3x 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 + 2x 2 + 3x 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
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 + 2x 2 + 3x 3 + Sin @ x D In[22]:= Select @ myExpr, test1 D Out[22]= 1 + x + 2x 2 + 3x 3 The following uses PolynomialQ directly but is wrong: In[23]:= Select @ myExpr, PolynomialQ D Out[23]= 1 + x + 2x 2 + 3x 3 + Sin @ x D In[24]:= test2 @ expr_ D : = H expr Sin @ x DL In[25]:= Select @ myExpr, test2 D Out[25]= Sin @ x D In[26]:= test3 @ expr_ D : = H Length @ expr D >= 1 L In[27]:= Select @ myExpr, test3 D Out[27]= 2x 2 + 3x 3 + Sin @ x D In[28]:= FullForm @ myExpr D Out[28]//FullForm= Plus @ 1, x, Times @ 2, Power @ x, 2 DD , Times @ 3, Power @ x, 3 DD , Sin @ x DD In[29]:= Length @ Sin D Out[29]= 0 In[30]:= Length @8 a <D Out[30]= 1 class17.nb 3

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

View Full Document
In[31]:= Length @ a D Out[31]= 0 In[32]:= Clear @ test1, test2, test3, myExpr D Changing Parts of Expressions we can use Prepend, Append, Insert, Delete, ReplacePart.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

class17 - PHYS 5900 Class 17 Fri Zi-Wei Lin Obtaining Parts...

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

View Full Document
Ask a homework question - tutors are online