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

5
votes
2answers
92 views

How do I construct a Pattern with Alternatives and PatternSequence without getting Pattern::patvar messages?

Bug introduced 8.0 or earlier and persisting through 10.4.1 [CASE:3631078] In Mathematica 8 and 9, defining this rule: ...
15
votes
2answers
260 views

How to check the validity of an option value

I have a user-defined function, func, which takes the following form: func[arg1, arg2, options] Here is the actual code ...
1
vote
0answers
43 views

Is there any point to using a variable as a function parameter?

Normally one only sees patterns as function parameters: fun1[x_]:=x^2 Is there any point to using a variable in a function definition: ...
1
vote
1answer
62 views

User-defined density plotting function with a functional argument: extracting variables from the function and plotting in a circular region

I would like to customize the function DensityPlot and use it in a package as a function called MyDensityPlot: The functions ...
4
votes
1answer
55 views

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

Pass List / Vector argument through NDSolve to a function

I am trying to pass vector or list as argument to the function from NDsolve.And I am getting these error. Also I am pretty new to stack overflow. Apology if this a possible duplicate. Here is my code. ...
0
votes
2answers
76 views

Function with translation property

I want to define a function Υ[b, x] of two arguments that has some special properties when there is a b in the second position: ...
4
votes
0answers
55 views

How to get f[x] to match with f[c_., x] when OneIdentity doesn't match

After setting: SetAttributes[f, OneIdentity] Default[f] = 1; We have the following successful matches: ...
4
votes
1answer
137 views

Are these two functions equivalent?

I checked several cases and saw that two functions are equivalent in these samples. However, are the two equivalent in all cases? Is there a way to check that? Explicit function: ...
4
votes
2answers
154 views

What kind of pattern should be used

I was trying to make a simple question but made it trivial. So the original setting is as follows: expr = {{a}, b, {{c}, {e}}, d, {}} I need that all lists are ...
4
votes
2answers
101 views

Derivative before Numerical Integration

I need to take the derivative of a function that have a numerical integration on it. The variable in which the integration will be taken is not the same as the one in which the derivative will be ...
2
votes
2answers
46 views

Deciding whether the return value of a function is defined based on its input [duplicate]

I have been met with the following problem: I have defined the following: myFunc[e_Integer]; That means, if I call: ...
1
vote
1answer
39 views

Please explain this pattern/Message related issue and help with finding workaround

I attempted to clean up my code and also add messages to functions if they are called with wrong arguments. This introduced some error which I tracked down to the following: ...
11
votes
1answer
156 views

ValueQ returns false positive for one argument type only

I'm seeing some perplexing behavior from ValueQ in 10.3. Consider: ...
0
votes
1answer
41 views

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

I have defined a function that returns different output according to the values of the axes of an ellipsoidal. If none of the Which statement cases is satisfied then the function evaluates the ...
-3
votes
1answer
92 views

Untangling delayed definitions and substitutions [closed]

I am attempting to find the values of B1 and B0 that will minimize a metric. In the code below I define the metric and then attempt to calculate is by substituting in the current values of B1 and B0. ...
16
votes
3answers
287 views

Is it possible to change the order that symbol definitions are checked? (DownValues vs UpValues)

Suppose I have the function f with the following definition assigned to it: ...
0
votes
1answer
70 views

a function that evaluates a Section

I have defined a function that returns different output according to the values of the axes of an ellipsoidal. The function is given below: ...
10
votes
1answer
288 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 ...
22
votes
4answers
336 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
44 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 ...
10
votes
4answers
428 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
150 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
49 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
123 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
82 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
60 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
81 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
69 views

How to match the arguments of the function in any order [duplicate]

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
71 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
149 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
163 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 ...
4
votes
1answer
222 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
106 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 ...
5
votes
3answers
315 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
808 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
0answers
87 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
82 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: ...
12
votes
3answers
262 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
3answers
143 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
171 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
47 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
238 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
177 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 ...
17
votes
2answers
582 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
227 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
108 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
48 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
191 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 ...
7
votes
3answers
560 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 ...