# Questions tagged [argument-patterns]

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.

128 questions
Filter by
Sorted by
Tagged with
32 views

### passing splice instructions

Consider some passing splice instructions using start,stop,step pattern ...
70 views

### My rule does not do anything (understandably)

I want a rule that would transform, for example, And[i,F[j,k],l,G[m,F[j,k],n],p] into And[i,F[j,k],l,G[m,T,n],p], where ...
17 views

### How to memoize with patterns? [duplicate]

Here is an artificial example to explain what I am up to. Define ClearAll[f] f[x_, y_] := f[x, y] = If[x == 0, g[y], g[f[x - 1, y]]] Then ...
44 views

We have: 1 + 2 I + 3 + 4 I // Head Complex and 1 + 2 I + 3/5 + 4/5 I // Head Complex But we get: ...
61 views

### 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 ...
66 views

### How to define a parameter with both Optional and PatternTest

Why this doesn't work? k[x_Integer:2?(#>0&)]:=x^2 k[] (*should be 4*) k[2] (*should be 4*) These following are all working as expected: ...
47 views

### How to define a function that acts invidually on a varible number of arguments

I am trying to generate a function that applies to each argument for any number and type of arguments. The function might work like this: ...
59 views

### Write repeated expressions within a rule more simply

Is it possible to shorten the length of a (delayed) rule (i.e. lhs :> rhs) when there are expressions which repeat several times? Those expressions involve ...
31 views

### Is there a way to get the calling information inside the body of a function defined using down value?

Suppose a function has multiple argument structure like follows: ...
128 views

### How to use patterns to specify variable type as Boolean (True, False) in analogy to x_Integer, x_Real, x_Complex, x_List, etc

Say I define a function f[x_, y_]:= ..., the concrete definition of which is not important now. What is important though is, that I want ...
64 views

### Setting nested optional argument with a default when unpacking from a given Head

I have a head that holds a single argument, and I want to unpack the value inside it while also assigning a symbol to the value itself, for example: ...
31 views

### FilterRules works in DateListPlot but not in Plot

I just experience the following problem (this is a MWE just to illustrate). First DateListPlot ...
93 views

### Prevent an expression from evaluating a certain way (pattern-matching?)

I've been trying to write some simple code to let me use physics vector notation: $\hat{x}$, $\hat{y}$, $\hat{r}$, and $\hat{\theta}$. ...
65 views

### How can I make a function that expects a list of variables behave like a multivariate function? [closed]

I am trying to convert a user defined function that takes two or more variables into a user defined function that takes a list of variables. However, it seems Mathematica treats variables and ...
61 views

### How do I define a function that accepts a subscripted variable?

How do I define a function with subscripted variable? I want to write the following expression $\qquad f[a_9] =1+ a_7 + a_9$ I wrote ...
664 views

### How to replace all elements of a list by a rule “element” -> “element_”

I have a list: {a, b, c, d, e, f, g, h, i, j} The goal is to get: {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} I can replace ...
190 views

### How to divide list according to the pattern?

There is a list of lists: {{0,0,0}, {0,0,1}, {0,1,0}, {0,1,1}, {1,0,0}, {1,0,1}, {1,1,0}, {1,1,1}} I need to regrup it's elements according to a pattern: "if the ...
50 views

### Delayed Rule right-hand-side mis-evaluation

Outline I am trying to create a rule that will give me a minus sign if the number of times A is applied on X is odd where A is some οperator and X a random quantity. Some of the configurations of ...
90 views

### Define Function explicitely with List as argument [duplicate]

Background There are multiple ways to define Functions. I would assume all methods are equivalent, and allow for the same options. However, I fail to find the ...
104 views

107 views

### How can I define the following function for arbitray values of the arguments?

Consider the following function a[m_, l_] = 1/Log[m/l]; with this function I define ...
201 views

### changing number of arguments to a function in secondary evaluation

So I have a function that produces answers involving uninstantiated(?) function names, e.g. Out[1]= f[a,b,c,d]. I then want to evalute this leftover result by, ...
81 views

### 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 ...
55 views

### Respecting excluded index in sum

I'm using a function involving a sum where some indices are excluded: ...
105 views

### Using DeleteCases with a defined function with two arguments as a pattern

I am trying to manipulate some large datasets in Mathematica. I have plotted the data, and I would like to be able to easily remove points from the dataset, and it's fairly easy to identify the x-...
133 views

### A single argument pattern definition applies to multiple-argument patterns?

Consider defining a pattern rule, such as myFun[x_]:=x As far as I understand Mathematica syntax, this rule means ...
41 views

### Define different actions for the same operator depending on the nature of its arguments

I am working with terms of the form A**B1**B2 where A, B1, and ...
86 views

### Why is { } interpreted as an option in my function? [closed]

I don't understand the output from: ...
89 views

### Defining a function that treats input as two unordered lists

I have a function f2 I'd like to code into Mathematica with the property that it takes in two lists, treats those lists as unordered, and maps to a symbolic value. If I were instead using a function ...
121 views

### How to use VBA types with UNDERLINE in it's name in NETLink?

When coding with VBA functions in NETLink, I need the enum reference. It's ok to get the ...
94 views

### 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. ...
453 views

### Appending _?NumericQ automatically to every variable of a function in the definition [duplicate]

I have a list of expressions as follows: as=Table[a[k],{k,1,500}] I would like to append _?NumericQ to every element of this ...
41 views

### Relations between Function Variables in Computations

I have the relation λ=c/f, respectively f=c/λ and a simple function n[λ_]:=λ²+1. I'd like to differentiate the function with respect to f, then plot it as a function of λ again. For this, I can define ...
433 views

### What does _. mean in patterns? [closed]

Here is a quick one, hopefully. I searched through different tutorials and documentation articles but haven't been able to find anything yet. What does _. mean? As ...
52 views

### Creating a function that differentiates rational functions

I want to create my own differentiation function Derivada. So, I already set some properties like: ...
205 views

### Optional pattern behaves differently in version 8, 9, and 10+

We recently ran into an issue when testing Rubi on older versions. It boils down to this simple example which I didn't dare to simplify further as I cannot test it in versions < 10 ...
55 views

### Functions definitions with variable arguments

How do you define a variable amount of functions with a variable amount of arguments? I don't get the reference at all : ...
438 views

### why doesn't this work to screen for Boolean inputs?

I routinely sanitize the inputs to a function using this sort of technique: funcI[x_Integer] := Print[x]; But it doesn't work with Booleans. ...
38 views

### Defining functions with pattern as argument

I want to define a function such that f[18/17 h[y][z]] = 18/17 z for any function h[y][z]. I first tried with ...
146 views

### Can the pattern “_.” be used in head?

The standard usage of the pattern _. seems usually used in the parameter, for example, f_[a_,n_.]. I wonder if such pattern can ...
3k views

### How to restrict the domain of a function

I realize this question has been asked many times, but I am fairly new at Mathematica and the other answers are very complex, and certainly too complicated for this simple code I am trying to write. ...
135 views

### NIntegrate fails because of symbolic evaluation [closed]

The following function can be plotted without any issues, say with ContourPlot, but NIntegrate fails because it tries to ...
228 views

### About the OptionsPattern[] approach of inheriting Options

Szabolcs showed in this post, that there are possibly two approaches to inherit function options. He recommended the Join approach. At first, I agreed. But today I ...
44 views

### Apply same type checking to every argument [duplicate]

Consider the following function which takes 10 arguments. f[Io_, {θ_, ϕ_}][{x0_, y0_, z0_}, d_][h_][L_][ω_] I know that all of these are of same type. I would ...
74 views

### 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 ...
62 views

### How to turn a ruled pattern into a multivariable function [duplicate]

Fix m=2. My code generates a pattern like this one (n=2): ...