Questions concerning argument patterns used as formal arguments in function definitions (e.g., := expressions) to restrict the kinds of values that can be passed as actual arguments. Also questions about how such patterns can be used to achieve function overloading.
Questions tagged [argument-patterns]
171 questions
15
votes
2 answers
How do I define an argument pattern for a Head of NOT a given value?
I want to define a function f with an argument that matches only when an argument Head is NOT of a given value.
I know I can define a pattern that matches the Head for an argument:
f[a_Integer] := 2 a
Now, how do I define a version that matches…
payne
- 413
- 2
- 7
9
votes
2 answers
Which function definition is used to evaluate an expression that matches the lefthand side of more than one definition?
In a case like this:
f[a_] := a;
f[{a_, b_}] := a;
I'm wondering whether I should expect f[{2, 3}] to return {2,3} or 2, because:
MatchQ[{2, 3}, a_] == MatchQ[{2, 3}, {a_, b_}]
Is the behaviour in this situation defined, or should I avoid this at…
Coolwater
- 20,257
- 3
- 35
- 64
6
votes
1 answer
Multiple pattern test in the definition of a function
I have a very simple question, and I'm sure there is a way to do this but I cannot find it:
if I want to create a function that's defined only for matrices, I can do it like this:
f[x_?MatrixQ]:=Det[x];
but what if I want to define a function which…
bnado
- 413
- 2
- 8
5
votes
3 answers
How to separate factors by dependent variables (arguments)?
How can I separate factors in an expression by their dependent variables? For example:
expr = b * c[2] * a[x] * 2 * x^2 * g[x,y] * z[r] * D[q[x], {x, 4}];
I need function that will separate the factors independent and dependent on a specified…
Jerry Guern
- 4,602
- 18
- 47
4
votes
1 answer
Building up functions from data
Have a tricky Mathematica problem. I am trying to build up functions from data to automate my processes with a minimum of hardcoding.
So let's say I have some data:
myList = {"e1", "e2", "e3", "e4"};
I want these strings to become symbols that are…
GeorgeD
- 41
- 2
4
votes
0 answers
Lint checker for overlapping or conflicting function signature patterns
In building a system like Mathics (or anything having the number of builtin functions of Mathematica) there are lots of function definitions. I would like to sanity check or lint check the pattern in the function signatures to look for obvious…
rocky
- 245
- 2
- 6
3
votes
3 answers
How to extract arguments of functions in an expression?
How can I extract the argument of a function in an expression?
expr=b[c];
I need some f[expr] that will return c.
d[f[expr]] -> d[c]
Jerry Guern
- 4,602
- 18
- 47
3
votes
1 answer
What does this syntax involving Entity mean?
I came across a different style of input while defining functions. Though I tried playing with this form, I have not understood how to interpret this input.
Country[country:Entity["Country", _]]:=
How different is it from the below-given input that…
exp ikx
- 768
- 3
- 13
2
votes
1 answer
Generic templating system
I'm trying to implement a templating system in Mathematica, a bit like the List
- > you would find in C#.
Here's how it looks so far:
ClearAll[ListOf];
ListOf /: ListOf[ListOf[f_Symbol]] := Function[l, ListQ[l] &&…
Literal
- 383
- 1
- 10
2
votes
1 answer
Passing argument to a function
I want to apply a function to a list. The Code for that is:
Function[x,x^2]/@list
>1,4,9
But when I want to made a function of that function, passing the argument for x^2 doesnt work:
xsquared[formula_,list_List] := Function[x,formula]/@list
…
Roberta
- 33
- 4
2
votes
1 answer
Refer to argument in condition for other argument
The condition for b here doesn't recognize a. How can I fix this?
f[a_Integer, b_Integer /; Divisible[a, b]] := a/b
f[6,2]
(* f[6,2] *)
a = 4;
f[6,2]
(* 3 *)
H.v.M.
- 1,093
- 5
- 12
2
votes
0 answers
Is there something like a function pattern test?
Is there something like a function pattern test?
function[x_?FunctionQ]:=Module[{},]
In order that Mathematica can tell if the argument is a function or a parameter?
Mirko Aveta
- 2,192
- 11
- 26
1
vote
0 answers
How conflicting patterns are resolved?
I often find myself doing something like this (probably poor example, sorry)
f[x_] := 0
f[x_Plus] := 1
In the back of my mind generally f[x] is zero but in an exceptional case when the argument is a sum f[x] gives 1. This is exactly what…
Weather Report
- 535
- 2
- 15
1
vote
1 answer
How to require that a function argument be a pure function or a function-style replacement rule?
I'm trying to write a function whose first argument must be another function, defined either as a pure function or a function-style replacement rule. I don't know how to specify a condition on the argument to achieve that. I know I can use…
ibeatty
- 2,533
- 11
- 22
0
votes
2 answers
Pattern for Matching for Numeric 3 Vector
I'm trying to write a basic molecular dynamics sim in Mathematica and would like to define a function calculateForces that takes two arguments. The first is a 3 vector for the coordinates of a particle and I would like to have a pattern that only…
StealthyLlama
- 3
- 2