Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
0answers
12 views

Operating functions with two arguments? [duplicate]

Let us first consider function with single argument. f1[x_] := x^2 The value of function at any value of $x$, for example, $x=3$ can be evaluated with any of the ...
3
votes
2answers
69 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-...
4
votes
1answer
100 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 ...
2
votes
1answer
36 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 ...
5
votes
1answer
78 views
1
vote
2answers
65 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 ...
6
votes
1answer
98 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 ...
3
votes
1answer
72 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. ...
5
votes
5answers
332 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 ...
0
votes
1answer
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 ...
6
votes
2answers
423 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 ...
2
votes
1answer
44 views

Creating a function that differentiates rational functions

I want to create my own differentiation function Derivada. So, I already set some properties like: ...
9
votes
1answer
194 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 ...
0
votes
0answers
48 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 : ...
4
votes
2answers
412 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. ...
0
votes
0answers
30 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 ...
2
votes
1answer
113 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 ...
5
votes
2answers
2k 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. ...
1
vote
1answer
90 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 ...
5
votes
0answers
159 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 ...
2
votes
0answers
42 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 ...
1
vote
1answer
66 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 ...
0
votes
1answer
51 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): ...
2
votes
1answer
70 views

Testing if list elements are numerical

I want to define a function with an argument, which is a list: m = 10; a = Table[k,{k,1,m}]; TestFun[listArg_] := Sum[listArg[[k]],{k,1,m}] Now if I run the ...
1
vote
1answer
57 views

Extra empty lists as function arguments

Consider the following piece of code: ...
3
votes
1answer
81 views

A pattern involving Times

If I define (this is 10.0.0.0) w[x : Times[xs__]] := {x, xs} w[x : h[xs__]] := {x, xs} Definition[w] gives me ...
0
votes
1answer
95 views

Function with number of arguments as parameter

I would like to write a function that generates the expression $\sum_{i_1,\ldots,i_L} f(i_1,\ldots,i_L) \left(s_{i_1}\ldots s_{i_L}\right)$ where $i_j\in \left\{0,1,2,3\right\}$ and $s_{i_j}$ are ...
4
votes
4answers
145 views

How to tell the dimension of an argument function

Let's say I'm trying to write my own gradient operator function. If I know the function is going to be $\mathbb{R}^2\rightarrow\mathbb{R}$, I could write ...
1
vote
2answers
55 views

Definine a function which accepts either functions or constants as arguments

I wish to define a function, which accepts either functions or constants as arguments. I tried g[t_, y_] := Module[{}, t[y]] which simply evaluates some function ...
2
votes
1answer
144 views

Argument pattern behavior for built-in symbol

I'm having troubles with this: test1[forAll[t,x_]]:=x test2[ForAll[t,x_]]:=x While syntactically exact, test1 works and ...
1
vote
0answers
77 views

Using Two Arguments on the Same Function

I'm having some trouble wrapping my head around how I would go about using two arguments in one function. In the first section of the code below, I have scraped some "tweets" on the Seeking Alpha ...
3
votes
1answer
71 views

Dynamic number and position of arguments in a function

I'm trying to write a function (A) which calls another function (B) (in my case KroneckerProduct) where the number and the position of the arguments of function (B) ...
8
votes
2answers
1k views

A hack for aggressive type-checking?

I tend to like programming in a style where I can be very explicit about the types of expressions. While Mathematica isn't and shouldn't be a strongly-typed language like Haskell, I think it would ...
3
votes
1answer
139 views

Replacement rules with Lists

first time user and not quite familiar with how to enter my code here properly, but this isn't lengthy.. I have a previously defined function that computes the total time it takes to ride a bike with ...
5
votes
2answers
120 views

How to hold evaluation of a value that is passed to a function

Say, I want to take the first part of an operation in a function: In: func[operexpr_] := operexpr[[1]]; func[a < b] Out: a But every once in a while a ...
10
votes
2answers
393 views

Smart Pattern Test

I defined two functions f and g which accept some argument which must be of some type, so I defined befor ...
12
votes
4answers
595 views

Optional argument that can be completely omitted?

I am writing a simple function return similar words. Clear[similarWords] similarWords[string_]:=Nearest[WordList[],string] I want to add another argument ...
14
votes
1answer
305 views

How to define homogeneous functions that respect Euler's theorem of homogeneous functions?

A homogeneous function of degree $k$ is defined as a function that observes the following specification rule: $f(a x_1, a x_2, ..., a x_n) = a^k f(x_1, x_2, ..., x_n)$ (see Wikipedia entry, ...
2
votes
0answers
68 views

Automating generation of argument error messages

You can run into messages like DistributionFitTest["kk", "TestConclusion"] ...
2
votes
1answer
60 views

Finding an Argument Pattern for Function Defintion

I'm defining what's called a fractional integral in Mathematica, which acts on a list of functions that depend on $x$: ...
3
votes
1answer
82 views

How to speed up accidental large output?

From time to time I accidentally let large expressions like RandomReal[1., {10^5, 10, 10}] reach the front end. The computation of the result is quite fast (...
9
votes
1answer
207 views

Why is this syntax for patterns with Optional values not supported?

I want to express Optional[PatternTest[Pattern[a,Blank[]],t],1] concisely. a : _?t : 1 is not quite this but works: ...
4
votes
1answer
90 views

Possible bug with `KeyValuePattern` and `Optional`

Bug introduced in 10.4, resolved in 11.0.0 I expect to get 0 on every line of output of this: ...
5
votes
4answers
216 views

How to scope `Pattern` labels in rules/set?

Module[{x}, f@x_ = x; p@x_ := x; {x, x_, x_ -> x, x_ :> x} ] ?f ?p gives ...
1
vote
1answer
75 views

Part function: can we use pattern for part functions? [duplicate]

Can we use some patterns for the arguments of Part functions? ...
5
votes
2answers
166 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 11.0 [CASE:3631078] In Mathematica 8 and 9, defining this rule: ...
12
votes
3answers
634 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
52 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
87 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 ...
5
votes
1answer
277 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: ...