Skip to main content
Added more robust form of pattern
Source Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263

I frequently write functions that take one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This pattern is used like so:

f[x_f[x : validNum] := x^2 

###Update

As Guesswhoitis points out the above is not fool-proof. A more robust version is

validNum = Except[z_ /; Head[N[z]] === Complex, _?NumericQ]; f /@ {1, 1/2, .5, Pi, 1 + I/2, 1. + .5 I, Sqrt[-1], (-1)^(2/3), E + I Pi} 
{1, 1/4, 0.25, Pi^2, f[1 + I/2], f[1. + 0.5*I], f[I], f[(-1)^(2/3)], f[E + I*Pi]} 

I frequently write functions that take one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This pattern is used like so:

f[x_: validNum] := x^2 

I frequently write functions that take one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This pattern is used like so:

f[x : validNum] := x^2 

###Update

As Guesswhoitis points out the above is not fool-proof. A more robust version is

validNum = Except[z_ /; Head[N[z]] === Complex, _?NumericQ]; f /@ {1, 1/2, .5, Pi, 1 + I/2, 1. + .5 I, Sqrt[-1], (-1)^(2/3), E + I Pi} 
{1, 1/4, 0.25, Pi^2, f[1 + I/2], f[1. + 0.5*I], f[I], f[(-1)^(2/3)], f[E + I*Pi]} 
added 1 character in body
Source Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263

I frequently write functions that take one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This pattenpattern is used like so:

f[x_: validNum] := x^2 

I frequently write functions that take one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This patten is used like so:

f[x_: validNum] := x^2 

I frequently write functions that take one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This pattern is used like so:

f[x_: validNum] := x^2 
added 10 characters in body
Source Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263

I frequently write functions that take argument(s)one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This patten is used like so:

f[x_: validNum] := x^2 

I frequently write functions that take argument(s) which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This patten is used like so:

f[x_: validNum] := x^2 

I frequently write functions that take one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ]; 

This patten is used like so:

f[x_: validNum] := x^2 
Source Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263
Loading