Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

Condition has three forms in which it can be used in defining a function:

  1. f[x_Integer /; EvenQ[x] && Positive[x]] := x+1,
  2. f[x_Integer] /; EvenQ[x] && Positive[x] := x+1, and
  3. f[x_Integer] := x+1 /; EvenQ[x] && Positive[x]

which are interpreted slightly differently by Mathematica. For instance, the first form results in the Pattern, x_Integer, being interpreted as

Condition[Pattern[x,Blank[Integer]], And[EvenQ[x],Positive[x]]] 

or, in short hand

Condition[ x_Integer, EvenQ[x] && Positive[x]] 

The second form looks like,

Condition[f[x_Integer], EvenQ[x] && Positive[x]] := x + 1 

The third form is interpreted as,

f[x_Integer] := Condition[ x + 1, EvenQ[x] && Positive[x]] 

They all give identical results. The timings reveal a slightly different tale:

f0[x_Integer?(And[EvenQ[#], Positive[#]] &)] := x + 1 f1[x_Integer /; EvenQ[x] && Positive[x]] := x + 1 f2[x_Integer] /; EvenQ[x] && Positive[x] := x + 1 f3[x_Integer] := x + 1 /; EvenQ[x] && Positive[x] rnds = RandomInteger[{-10, 10}, 1000000]; Timing[f0 /@ rnds;] Timing[f1 /@ rnds;] Timing[f2 /@ rnds;] Timing[f3 /@ rnds;] (* {1.75124, Null} {1.41897, Null} {1.4107, Null} {2.17659, Null} *) 

The timings for f1 and f2 do exhibit some volatility, and occasionally exceed the timing for f0, but, for the most part, are consistently faster than PatternTest. The timing for f3 is always slower, much slower.

Edit: However, some caution needs to be applied here. With a single condition, such as EvenQ, the results for PatternTest are on par with the results for the first two forms of Condition. Again, the third form is slower, and usually over twice as slow.

Edit - 2: The increase in speed I was seeing when using Function was illusoryillusory. The results for simple conditions still holds, though. The more complex conditions using functions is as slow as f3.

(* Note the parantheses around Function *) f4[x_Integer?(Function[{x}, EvenQ[x]&&Positive[x]])] := x + 1 fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {2.37899, Null} {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. An advantage that is not easily overcome.

Condition has three forms in which it can be used in defining a function:

  1. f[x_Integer /; EvenQ[x] && Positive[x]] := x+1,
  2. f[x_Integer] /; EvenQ[x] && Positive[x] := x+1, and
  3. f[x_Integer] := x+1 /; EvenQ[x] && Positive[x]

which are interpreted slightly differently by Mathematica. For instance, the first form results in the Pattern, x_Integer, being interpreted as

Condition[Pattern[x,Blank[Integer]], And[EvenQ[x],Positive[x]]] 

or, in short hand

Condition[ x_Integer, EvenQ[x] && Positive[x]] 

The second form looks like,

Condition[f[x_Integer], EvenQ[x] && Positive[x]] := x + 1 

The third form is interpreted as,

f[x_Integer] := Condition[ x + 1, EvenQ[x] && Positive[x]] 

They all give identical results. The timings reveal a slightly different tale:

f0[x_Integer?(And[EvenQ[#], Positive[#]] &)] := x + 1 f1[x_Integer /; EvenQ[x] && Positive[x]] := x + 1 f2[x_Integer] /; EvenQ[x] && Positive[x] := x + 1 f3[x_Integer] := x + 1 /; EvenQ[x] && Positive[x] rnds = RandomInteger[{-10, 10}, 1000000]; Timing[f0 /@ rnds;] Timing[f1 /@ rnds;] Timing[f2 /@ rnds;] Timing[f3 /@ rnds;] (* {1.75124, Null} {1.41897, Null} {1.4107, Null} {2.17659, Null} *) 

The timings for f1 and f2 do exhibit some volatility, and occasionally exceed the timing for f0, but, for the most part, are consistently faster than PatternTest. The timing for f3 is always slower, much slower.

Edit: However, some caution needs to be applied here. With a single condition, such as EvenQ, the results for PatternTest are on par with the results for the first two forms of Condition. Again, the third form is slower, and usually over twice as slow.

Edit - 2: The increase in speed I was seeing when using Function was illusory. The results for simple conditions still holds, though. The more complex conditions using functions is as slow as f3.

(* Note the parantheses around Function *) f4[x_Integer?(Function[{x}, EvenQ[x]&&Positive[x]])] := x + 1 fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {2.37899, Null} {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. An advantage that is not easily overcome.

Condition has three forms in which it can be used in defining a function:

  1. f[x_Integer /; EvenQ[x] && Positive[x]] := x+1,
  2. f[x_Integer] /; EvenQ[x] && Positive[x] := x+1, and
  3. f[x_Integer] := x+1 /; EvenQ[x] && Positive[x]

which are interpreted slightly differently by Mathematica. For instance, the first form results in the Pattern, x_Integer, being interpreted as

Condition[Pattern[x,Blank[Integer]], And[EvenQ[x],Positive[x]]] 

or, in short hand

Condition[ x_Integer, EvenQ[x] && Positive[x]] 

The second form looks like,

Condition[f[x_Integer], EvenQ[x] && Positive[x]] := x + 1 

The third form is interpreted as,

f[x_Integer] := Condition[ x + 1, EvenQ[x] && Positive[x]] 

They all give identical results. The timings reveal a slightly different tale:

f0[x_Integer?(And[EvenQ[#], Positive[#]] &)] := x + 1 f1[x_Integer /; EvenQ[x] && Positive[x]] := x + 1 f2[x_Integer] /; EvenQ[x] && Positive[x] := x + 1 f3[x_Integer] := x + 1 /; EvenQ[x] && Positive[x] rnds = RandomInteger[{-10, 10}, 1000000]; Timing[f0 /@ rnds;] Timing[f1 /@ rnds;] Timing[f2 /@ rnds;] Timing[f3 /@ rnds;] (* {1.75124, Null} {1.41897, Null} {1.4107, Null} {2.17659, Null} *) 

The timings for f1 and f2 do exhibit some volatility, and occasionally exceed the timing for f0, but, for the most part, are consistently faster than PatternTest. The timing for f3 is always slower, much slower.

Edit: However, some caution needs to be applied here. With a single condition, such as EvenQ, the results for PatternTest are on par with the results for the first two forms of Condition. Again, the third form is slower, and usually over twice as slow.

Edit - 2: The increase in speed I was seeing when using Function was illusory. The results for simple conditions still holds, though. The more complex conditions using functions is as slow as f3.

(* Note the parantheses around Function *) f4[x_Integer?(Function[{x}, EvenQ[x]&&Positive[x]])] := x + 1 fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {2.37899, Null} {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. An advantage that is not easily overcome.

added 77 characters in body
Source Link
rcollyer
  • 34.3k
  • 7
  • 95
  • 198

Condition has three forms in which it can be used in defining a function:

  1. f[x_Integer /; EvenQ[x] && Positive[x]] := x+1,
  2. f[x_Integer] /; EvenQ[x] && Positive[x] := x+1, and
  3. f[x_Integer] := x+1 /; EvenQ[x] && Positive[x]

which are interpreted slightly differently by Mathematica. For instance, the first form results in the Pattern, x_Integer, being interpreted as

Condition[Pattern[x,Blank[Integer]], And[EvenQ[x],Positive[x]]] 

or, in short hand

Condition[ x_Integer, EvenQ[x] && Positive[x]] 

The second form looks like,

Condition[f[x_Integer], EvenQ[x] && Positive[x]] := x + 1 

The third form is interpreted as,

f[x_Integer] := Condition[ x + 1, EvenQ[x] && Positive[x]] 

They all give identical results. The timings reveal a slightly different tale:

f0[x_Integer?(And[EvenQ[#], Positive[#]] &)] := x + 1 f1[x_Integer /; EvenQ[x] && Positive[x]] := x + 1 f2[x_Integer] /; EvenQ[x] && Positive[x] := x + 1 f3[x_Integer] := x + 1 /; EvenQ[x] && Positive[x] rnds = RandomInteger[{-10, 10}, 1000000]; Timing[f0 /@ rnds;] Timing[f1 /@ rnds;] Timing[f2 /@ rnds;] Timing[f3 /@ rnds;] (* {1.75124, Null} {1.41897, Null} {1.4107, Null} {2.17659, Null} *) 

The timings for f1 and f2 do exhibit some volatility, and occasionally exceed the timing for f0, but, for the most part, are consistently faster than PatternTest. The timing for f3 is always slower, much slower.

Edit: However, some caution needs to be applied here. With a single condition, such as EvenQ, the results for PatternTest are on par with the results for the first two forms of Condition. Again, the third form is slower, and usually over twice as slow.

Edit - 2: The increase in speed I was seeing when using Function was illusory. The results for simple conditions still holds, though. The more complex conditions using functions is as slow as f3.

(* Note the parantheses around Function *) f4[x_Integer?(Function[{x}, EvenQ[x]&&Positive[x]])] := x + 1 fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {2.37899, Null} {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. An advantage that is not easily overcome.

Condition has three forms in which it can be used in defining a function:

  1. f[x_Integer /; EvenQ[x] && Positive[x]] := x+1,
  2. f[x_Integer] /; EvenQ[x] && Positive[x] := x+1, and
  3. f[x_Integer] := x+1 /; EvenQ[x] && Positive[x]

which are interpreted slightly differently by Mathematica. For instance, the first form results in the Pattern, x_Integer, being interpreted as

Condition[Pattern[x,Blank[Integer]], And[EvenQ[x],Positive[x]]] 

or, in short hand

Condition[ x_Integer, EvenQ[x] && Positive[x]] 

The second form looks like,

Condition[f[x_Integer], EvenQ[x] && Positive[x]] := x + 1 

The third form is interpreted as,

f[x_Integer] := Condition[ x + 1, EvenQ[x] && Positive[x]] 

They all give identical results. The timings reveal a slightly different tale:

f0[x_Integer?(And[EvenQ[#], Positive[#]] &)] := x + 1 f1[x_Integer /; EvenQ[x] && Positive[x]] := x + 1 f2[x_Integer] /; EvenQ[x] && Positive[x] := x + 1 f3[x_Integer] := x + 1 /; EvenQ[x] && Positive[x] rnds = RandomInteger[{-10, 10}, 1000000]; Timing[f0 /@ rnds;] Timing[f1 /@ rnds;] Timing[f2 /@ rnds;] Timing[f3 /@ rnds;] (* {1.75124, Null} {1.41897, Null} {1.4107, Null} {2.17659, Null} *) 

The timings for f1 and f2 do exhibit some volatility, and occasionally exceed the timing for f0, but, for the most part, are consistently faster than PatternTest. The timing for f3 is always slower, much slower.

Edit: However, some caution needs to be applied here. With a single condition, such as EvenQ, the results for PatternTest are on par with the results for the first two forms of Condition. Again, the third form is slower, and usually over twice as slow.

Edit - 2: The increase in speed I was seeing was illusory. The more complex conditions using functions is as slow as f3.

(* Note the parantheses around Function *) f4[x_Integer?(Function[{x}, EvenQ[x]&&Positive[x]])] := x + 1 fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {2.37899, Null} {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. An advantage that is not easily overcome.

Condition has three forms in which it can be used in defining a function:

  1. f[x_Integer /; EvenQ[x] && Positive[x]] := x+1,
  2. f[x_Integer] /; EvenQ[x] && Positive[x] := x+1, and
  3. f[x_Integer] := x+1 /; EvenQ[x] && Positive[x]

which are interpreted slightly differently by Mathematica. For instance, the first form results in the Pattern, x_Integer, being interpreted as

Condition[Pattern[x,Blank[Integer]], And[EvenQ[x],Positive[x]]] 

or, in short hand

Condition[ x_Integer, EvenQ[x] && Positive[x]] 

The second form looks like,

Condition[f[x_Integer], EvenQ[x] && Positive[x]] := x + 1 

The third form is interpreted as,

f[x_Integer] := Condition[ x + 1, EvenQ[x] && Positive[x]] 

They all give identical results. The timings reveal a slightly different tale:

f0[x_Integer?(And[EvenQ[#], Positive[#]] &)] := x + 1 f1[x_Integer /; EvenQ[x] && Positive[x]] := x + 1 f2[x_Integer] /; EvenQ[x] && Positive[x] := x + 1 f3[x_Integer] := x + 1 /; EvenQ[x] && Positive[x] rnds = RandomInteger[{-10, 10}, 1000000]; Timing[f0 /@ rnds;] Timing[f1 /@ rnds;] Timing[f2 /@ rnds;] Timing[f3 /@ rnds;] (* {1.75124, Null} {1.41897, Null} {1.4107, Null} {2.17659, Null} *) 

The timings for f1 and f2 do exhibit some volatility, and occasionally exceed the timing for f0, but, for the most part, are consistently faster than PatternTest. The timing for f3 is always slower, much slower.

Edit: However, some caution needs to be applied here. With a single condition, such as EvenQ, the results for PatternTest are on par with the results for the first two forms of Condition. Again, the third form is slower, and usually over twice as slow.

Edit - 2: The increase in speed I was seeing when using Function was illusory. The results for simple conditions still holds, though. The more complex conditions using functions is as slow as f3.

(* Note the parantheses around Function *) f4[x_Integer?(Function[{x}, EvenQ[x]&&Positive[x]])] := x + 1 fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {2.37899, Null} {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. An advantage that is not easily overcome.

added 1 characters in body
Source Link
rcollyer
  • 34.3k
  • 7
  • 95
  • 198

Also, thereEdit - 2: The increase in speed I was seeing was illusory. The more complex conditions using functions is a form ofas slow as PatternTestf3 that is much faster than condition:.

(* Note the parantheses around Function *) f4[x_Integer?(Function[{x}, EvenQ[x]&&Positive[x]]]EvenQ[x]&&Positive[x]])] := x + 1 Timing[f4 /@ rnds;] (* {0.263022, Null} *) 

which beats out all the others, including using a separate function, like so

fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {2.37899, Null} {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. Although,An advantage that is not easily overcome with PatternTest in f4.

Also, there is a form of PatternTest that is much faster than condition:

f4[x_Integer?Function[{x}, EvenQ[x]&&Positive[x]]] := x + 1 Timing[f4 /@ rnds;] (* {0.263022, Null} *) 

which beats out all the others, including using a separate function, like so

fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. Although, that is easily overcome with PatternTest in f4.

Edit - 2: The increase in speed I was seeing was illusory. The more complex conditions using functions is as slow as f3.

(* Note the parantheses around Function *) f4[x_Integer?(Function[{x}, EvenQ[x]&&Positive[x]])] := x + 1 fcn1[x_] := EvenQ[x] && Positive[x] f5[x_Integer?fcn1] := x + 1 fcn2 = EvenQ[#] && Positive[#] & f5[x_Integer?fcn2] := x + 1 (* Timings, respecively {2.37899, Null} {1.92366, Null} {1.86119, Null} *) 

Condition does have the advantage of clarity as you can use the variable names within it directly. An advantage that is not easily overcome.

added 1 characters in body
Source Link
rcollyer
  • 34.3k
  • 7
  • 95
  • 198
Loading
Changed EqualQ by EvenQ in last example as it should be.
Source Link
Loading
added addt'l info
Source Link
rcollyer
  • 34.3k
  • 7
  • 95
  • 198
Loading
Source Link
rcollyer
  • 34.3k
  • 7
  • 95
  • 198
Loading