Skip to main content
added a second example
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

There is no need to explicitly refer to MatchQ. Mathematica's pattern language is up to expressing what you want as an argument pattern.

f[lst : {{Repeated[Except[_Complex, _?NumberQ], {2}]} ..}] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]}&) /@ lst
f @ {{0.1, 0.2}, {0.3, 0.4}, {1/2, 1}, {2, 2.}}
{{0.3, -0.1}, {0.7, -0.1}, {3/2, -(1/2)}, {4., 0.}}

The function does not evaluate for lists containing pairs having a complex component.

f @ {{1, 1}, {1, I}}
f[{{1, 1}, {1, I}}]

There is no need to explicitly refer to MatchQ. Mathematica's pattern language is up to expressing what you want as an argument pattern.

f[lst : {{Repeated[Except[_Complex, _?NumberQ], {2}]} ..}] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]}&) /@ lst
f @ {{0.1, 0.2}, {0.3, 0.4}, {1/2, 1}, {2, 2.}}
{{0.3, -0.1}, {0.7, -0.1}, {3/2, -(1/2)}, {4., 0.}}

There is no need to explicitly refer to MatchQ. Mathematica's pattern language is up to expressing what you want as an argument pattern.

f[lst : {{Repeated[Except[_Complex, _?NumberQ], {2}]} ..}] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]}&) /@ lst
f @ {{0.1, 0.2}, {0.3, 0.4}, {1/2, 1}, {2, 2.}}
{{0.3, -0.1}, {0.7, -0.1}, {3/2, -(1/2)}, {4., 0.}}

The function does not evaluate for lists containing pairs having a complex component.

f @ {{1, 1}, {1, I}}
f[{{1, 1}, {1, I}}]
deleted 1 character in body
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

There is no need to explicitly refer to MatchQ. Mathematica's pattern language is up to expressing what you want as an argument pattern.

f[lst : {{Repeated[Except[_Complex, _?NumberQ], {2}]} ..}] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]}&) /@ lst;lst
f@f @ {{0.1, 0.2}, {0.3, 0.4}, {1/2, 1}, {2, 2.}}
{{0.3, -0.1}, {0.7, -0.1}, {3/2, -(1/2)}, {4., 0.}}

There is no need to explicitly refer to MatchQ. Mathematica's pattern language is up to expressing what you want as an argument pattern.

f[lst : {{Repeated[Except[_Complex, _?NumberQ], {2}]} ..}] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]}&) /@ lst;
f@{{0.1, 0.2}, {0.3, 0.4}, {1/2, 1}, {2, 2.}}
{{0.3, -0.1}, {0.7, -0.1}, {3/2, -(1/2)}, {4., 0.}}

There is no need to explicitly refer to MatchQ. Mathematica's pattern language is up to expressing what you want as an argument pattern.

f[lst : {{Repeated[Except[_Complex, _?NumberQ], {2}]} ..}] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]}&) /@ lst
f @ {{0.1, 0.2}, {0.3, 0.4}, {1/2, 1}, {2, 2.}}
{{0.3, -0.1}, {0.7, -0.1}, {3/2, -(1/2)}, {4., 0.}}
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

There is no need to explicitly refer to MatchQ. Mathematica's pattern language is up to expressing what you want as an argument pattern.

f[lst : {{Repeated[Except[_Complex, _?NumberQ], {2}]} ..}] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]}&) /@ lst;
f@{{0.1, 0.2}, {0.3, 0.4}, {1/2, 1}, {2, 2.}}
{{0.3, -0.1}, {0.7, -0.1}, {3/2, -(1/2)}, {4., 0.}}