Predicates and Boolean operations

Một phần của tài liệu IT training an introduction to programming with mathematica (3rd ed ) wellin, kamin gaylord 2005 01 31 (Trang 51 - 56)

Predicates

When working with data sets, you are often presented with the problem of extracting those data points that meet certain criteria. Similarly, when you write programs, oftentimes what to do next at any particular point in your program will depend upon some test or condition being met. Every programming language has constructs for testing data or conditions.

Some of the most useful such constructs are called predicates. Apredicate is a function that returns a value of true or false depending upon whether its argument passes a test. For example, the predicate PrimeQ tests for the primality of its argument.

In[1]:= PrimeQ 144

Out[1]= False

Other predicates are available for testing numbers to see whether they are even, odd, integral, and so on.

In[2]:= OddQ[21]

Out[2]= True

In[3]:= EvenQ[21]

Out[3]= False

In[4]:= IntegerQ[5/9]

Out[4]= False

TheNumericQ predicate tests whether its argument is a numeric quantity. Essen- tially, NumericQ[x] givesTrue wheneverN[x] evaluates to an explicit number.

In[5]:= NumericQ

Out[5]= True

In[6]:= NumericQ

Out[6]= False

This is distinct from a related function,NumberQ, which evaluates toTrue whenever its argument is an explicit number (that is, has head one of Integer, Rational, Real, Complex).

In[7]:= NumberQ 3.2

Out[7]= True

In[8]:= NumberQ

Out[8]= False

Many other predicates are available for testing if an expression is an atom, a list, a matrix, a polynomial, and much more.

In[9]:= AtomQ "string"

Out[9]= True

In[10]:= ListQ a, b, c

Out[10]= True

In[11]:= MatrixQ

1 0 0 0 1 0 0 0 1

Out[11]= True

In[12]:= PolynomialQ 1 x

1 x2

1 x3

, x

Out[12]= False

In[13]:= IntervalMemberQ Interval 3, 4 ,

Out[13]= True

Relational and logical operators

Another type of predicate that is commonly used in programming are relational operators.

These are used to compare two or more expressions and return a value ofTrue orFalse.

The relational operators in Mathematica are Equal ( ), Unequal ( ), Greater (>), Less (<),GreaterEqual( ), and LessEqual ( ). They can be used to compare num- bers or arbitrary expressions.

In[14]:= 7 5

Out[14]= False

In[15]:= Equal 3, 7 4, 6 2

Out[15]= True

In[16]:= x2 1 x4 1

x2 1

Simplify

Out[16]= True

Note that the relational operators have lower precedence than arithmetic operators.

The second example above is interpreted as 3 (7-4) and not as (3 7)-4. Table 2.1 lists the relational operators and their various input forms.

StandardForm Functional form Meaning

x y Equal x,y equal

x y Unequal x,y unequal

x y Greater x,y greater than

x y Less x,y less than

x y GreaterEqual x,y greater than or equal x y LessEqual x,y less than or equal

Table 2.1: Relational operators

The logical operators (sometimes known as Boolean operators) determine the truth of an expression based on Boolean arithmetic. For example, the conjunction of two true statements is always true.

In[17]:= 4 5 && 8 1

Out[17]= True

The Boolean operation “and” is represented inMathematica byAnd, with shorthand notation && or . Here is a table that gives all the possible values for theAnd operator.

(The function TruthTable is developed in Chapter 10.)

In[18]:= TruthTable A B, A, B

Out[18]//DisplayForm=

A B A B

T T T

T F F

F T F

F F F

The logical “or” operator, represented byOr and with shorthand notation|| (or ), is true when either of its arguments is true.

In[19]:= 4 3 3

6 2

Out[19]= True

In[20]:= 0 0.0001 22

7

Out[20]= False

Note the difference between this Boolean “or” and the common notion of “or.” A phrase such as, “It is cold or it is hot,” uses the word “or” in anexclusive sense; that is, it excludes the possibility that it isboth cold and hot. The logicalOr is inclusive in the sense that ifA and B are both true, thenA||B is also true.

In[21]:= True True

Out[21]= True

Mathematica also contains an operator for the exclusive or,Xor.

In[22]:= Xor True, True

Out[22]= False

In[23]:= Xor True, False

Out[23]= True

Table 2.2 shows the logical operators and their input forms.

StandardForm Functional form Meaning

x Not x not

x y Unequal x,y unequal

x&&y And x,y and

x y Or x,y or

x y && x&&y Xor x,y exclusive or

Table 2.2: Logical operators

Introduced in Version 4 ofMathematica are thebitwise logical operators. These func- tions operate on integers as binary bits. For example, BitOr[x,y] gives the integer whose binary representation has 1s wherever the binary representation of x ory has 1s. Here is the bitwise OR of 21 and 19, given in binary form.

In[24]:= BaseForm BitOr 2^^10101, 2^^10011 , 2

Out[24]//BaseForm=

101112

Similarly,BitXor[x,y] gives the integer with 1s at positions where either x ory have 1s, but not both.

In[25]:= BaseForm BitXor 2^^10101, 2^^10011 , 2

Out[25]//BaseForm=

1102

Functional form Meaning

BitAnd x,y bitwise AND ofxandy BitOr x,y bitwise OR ofxandy BitNot x bitwise NOT ofx BitXor x,y bitwise XOR ofxandy

Table 2.3: Bitwise operators

In Chapter 4 we will look at an application of bitwise operators to an example involving error-correcting codes: the computation of Hamming distance.

Exercises

1. Create a predicate function that returns a value of True if its argument is between 1 and 1.

2. Write a predicate function NaturalQ[n] that returns a value of True ifn is a natural number and False otherwise; that is,NaturalQ[n] is True ifn is among 0, 1, 2, 3, ….

3. Create a predicate function SubsetQ[lis1,lis2] that returns a value of True iflis1 is a subset of lis2. Remember: the empty set {}, is a subset of every set.

Một phần của tài liệu IT training an introduction to programming with mathematica (3rd ed ) wellin, kamin gaylord 2005 01 31 (Trang 51 - 56)

Tải bản đầy đủ (PDF)

(556 trang)