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.

learn more… | top users | synonyms

10
votes
2answers
241 views

Checking the argument of user-defined function with side-effect method

Today, I answered a question of mine that asked two month ago. Please see here Now I would like to add the argument checking in this function. Then I used a method that Mr.Wizard answered ...
19
votes
3answers
266 views

Is there a GraphicsPrimitiveQ (or a complete list of Heads of graphics primitives)?

For various reasons related to workflow associated with creating figures for journals, I am creating functions that will accept graphics primitives and a set of options and spit out a ...
2
votes
1answer
42 views

What workarounds are there to TagSetDelayed::tagpos?

My program creates an object based on an Association, with a custom head: g = Gate[<| "key1" -> val1 |>] I have ...
8
votes
5answers
353 views

How to check the style and number of arguments like the built-in functions?

Given that I have a function sqrt[x] that I must check the style and number of argument x. I can do like this: ...
9
votes
0answers
99 views

Confused by the opts : OptionsPattern[ ] pattern

I'm trying to make sense of the syntax in the clause opts:OptionsPattern[], as found in this standard "rule-based" (pseudo-)function definition pattern: ...
1
vote
1answer
47 views

Why this isn't a valid argument? [duplicate]

I am having truble understanding why F1 produces an error, since both time and velocity are the same kind of object: ...
2
votes
1answer
64 views

Meaning of Pattern at derivatives of arbitrary functions

I want to derivate some general functions like D[F[x[r_, p_], y[r_, p_]], r] or this simpler one D[F[G[x_]], x] . When I ...
0
votes
2answers
66 views

Construct a function whose definition depends on the values of its arguments

I have to evaluate the components of a 6x6 matrix $S$. This matrix depends upon the semi-axes of an ellipsoid $a1$, $a2$ and $a3$. For various specific cases (sphere, prolate spheroid, oblate ...
1
vote
1answer
52 views

Emulating Case Pattern replacement

As most regular users, I have developed utility functions complementing the Wolfram language for frequent tasks. In particular, I have variations on Cases (see ...
2
votes
2answers
76 views

Evaluation speed of apparently equivalent functions

Consider the following expressions: f[x_Integer] := x, f[x_] := x /; x ∈ Integers, f[x_?IntegerQ] := x All of the expressions above (and probably others which ...
3
votes
0answers
65 views

How to match the arguments of the function in any order

The issue is the following, with a dumb function with (4) arguments of (4) different heads (for each head is always no grater than one arguments): ...
2
votes
1answer
65 views

Generic templating system

I'm trying to implement a templating system in Mathematica, a bit like the List<List<T>> you would find in C#. Here's how it looks so far: ...
2
votes
2answers
143 views

Using list elements as function calls ?

Is it possible to take elements of a matrix or list and use them as arguments of a function? I have a matrix of values, something like this; ...
2
votes
2answers
150 views

Can a pattern examine all partitions of a sequence?

Say I have a list: {a, b, c, d}. I'd like to check that the list has some partition, say {{a, b, c}, {d}} such that all the elements of the partition satisfy a ...
3
votes
1answer
129 views

How can I overload a function with multiple bracket-slots so f[a][b] and f[a] can coexist?

Maybe this is not even possible: I want to create a function f that can have two input brackets like: f[a_][b_:1]:= a*b and ...
2
votes
1answer
83 views

Patterns for destructuring “Function” function arguments

I want to define a function for finding a Jacobian matrix of pure vector function of vector argument. I try ...
3
votes
0answers
65 views

Tab Completion Command Line

How do I effectively use tab completion with Mathematica program on the command line? programmable completion intro for Bash shell Are there any available tools or packages for tab completion of ...
5
votes
3answers
217 views

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 ...
1
vote
3answers
299 views

How to extract arguments of functions in an expression? [duplicate]

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]
3
votes
1answer
85 views

Function of arguments given in non-fixed order [duplicate]

normally we define functions as f[x_, y_, z_] := ArcSin[x^2*y/z] however when one has many arguments (as I do) I find difficult to remember what is the meaning ...
3
votes
1answer
78 views

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: ...
11
votes
3answers
245 views

Find all heads a function is defined on

So say I've got a function g defined as follows: g[x_Integer] := x + 1 g[s_String] := s <> "!!!" How would I write a ...
1
vote
2answers
125 views

An argument pattern that my function can treat as Times[constant, variable]

I want to define a function of two variables, func[f_, g_], such that when it sees a general constant multiplying one of the variables, say ...
8
votes
2answers
148 views

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 ...
0
votes
0answers
42 views

How to correctly use PatternTest (?) [duplicate]

This works fine: {3, -5, 2, 7, -6, 3} /. x_ /; x < 0 :> 0 (*{3, 0, 2, 7, 0, 3}*) and this works fine: ...
12
votes
1answer
205 views

Why doesn't repeated PatternSequence work with explicit pattern names?

Why would this work: Clear[f] f[a : PatternSequence[b_, c_]] := {a}; f[1, 2] (* {1, 2} *) and this also works: ...
3
votes
1answer
133 views

Why does pattern test not work in this case to restrict the function parameters?

I was looking at this Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across ...
15
votes
2answers
475 views

Quick way to use conditioned patterns when defining multi-argument function?

I have a simple function that is supposed to only accept numeric values (i.e. complex/real numbers and constant symbols e.g. Pi, E). $$f(a,b,c)=a+b+c$$ Edit: I should have chosen a less simple ...
4
votes
2answers
209 views

Alternatives ordering affects pattern matching in Cases?

Given expr = f[x, g[y], z] In the following query, the pattern h_[__, c_] appears as last slot in ...
4
votes
2answers
99 views

Function pattern to support listed and non-listed arguments [duplicate]

I often want to write functions that take as an argument either a) a rule or b) a list of rules. As an example, when using Replace with just one rule, it does not ...
1
vote
0answers
46 views

Placement of pattern matching rules in functions [duplicate]

Is there any difference at all between f[x_Integer /; x >= 0] := x + 1; f[x_Integer] := x - 1; f[x_] := Null; and ...
2
votes
1answer
143 views

Why evaluation doesn't fail if arguments are invalid? [duplicate]

Sorry for a noob question, but this is something that I wonder about each time I work with Mathematica. Coming from standard programming languages (Python and Java), I expect my functions to fail ...
5
votes
3answers
485 views

Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
3
votes
2answers
161 views

What is the correct way to pattern match nested lists?

I have the following function: f[lst_List /; MatchQ[lst, SOME PATTERN HERE]] := ({#[[1]] + #[[2]], #[[1]] - #[[2]]} &) /@ lst; I want to match lists that ...
0
votes
1answer
242 views

Minimize: How to work with specific numeric types

This is a question about the use of symbols in Minimize and how to persuade Mathematica to treat them in a specific way (as integers or rationals) that I have not ...
4
votes
2answers
374 views

How to test whether an expression is a valid variable?

I would like to test whether an argument is a valid variable for functions like Solve and DSolve. For instance several ...
4
votes
0answers
198 views

Syntax error for optional pattern, but only in debugger

This expression (explained here by Leonid) evaluates fine unless Evaluation->Debugger is enabled: h[x : (_Integer) : 1] := x Syntax::sntxf: "x:" cannot be ...
7
votes
2answers
246 views

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: ...
0
votes
1answer
113 views

Function generating a rule with a Patterns: e.g. toRule[ lhs, rhs] into lhs[x_] -> rhs [duplicate]

I want to create a rule as a function of z from a left and right hand side. My (non-working) code and usage is: ...
6
votes
3answers
209 views

Using PatternTest and/or Condition to detect equal list lengths as well as list element formats

On searching Mathematica.SE for information about overloading functions, I came across several threads about PatternTest and ...
13
votes
3answers
1k views

How can I type-check the arguments of a Mathematica function?

(For educational purposes) I defined the following functions: Translation (in $\mathbb{R}^2$): trM[vec_] := {{1, 0, vec[[1]]}, {0, 1, vec[[2]]}, {0, 0, 1}} ...