Skip to main content

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.

Filter by
Sorted by
Tagged with
4 votes
1 answer
167 views

Why is {_Integer..} matching {_Rule..}?

Why is this getting stuck in a loop: ...
user13892's user avatar
  • 9,651
1 vote
2 answers
87 views

How to pass arguments of outer function into inner function implicitly?

Consider the following setup: ...
user13892's user avatar
  • 9,651
0 votes
1 answer
128 views

How can I effectively input more than two list arguments into a function?

I am trying to input more than two list arguments into a function. For example, I have written the following code: ...
Rakesh's user avatar
  • 179
1 vote
2 answers
51 views

In a function definition, how should I declare an XMLObject["Document"] argument?

Here we have an XMLObject["Document"]: ...
Felix Kasza's user avatar
5 votes
3 answers
138 views

Implementing symmetry assumptions in FullSimplify

I want to symmetrise a long expression, M, that involves a function of 4 arguments, f[u1,u2,d1,d2], and its products (for ...
jms547's user avatar
  • 399
2 votes
1 answer
101 views

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 ...
Roberta's user avatar
  • 33
5 votes
1 answer
75 views

Assumptions about indexed variables

I am interested in the most efficient way to impose assumptions on indexed variables. In the following stylized problem, the following code ...
oyy's user avatar
  • 187
0 votes
2 answers
60 views

Pattern for Matching for Numeric 3 Vector [closed]

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 ...
StealthyLlama's user avatar
2 votes
2 answers
110 views

Is it possible to combine a default value in a function definition with type-checking, in case the optional parameter is actually given in a call?

1.) In a function definition, e.g. ...
Adalbert Hanßen's user avatar
1 vote
3 answers
72 views

How can I write a function definition which gets another function as parameter whether the parameters of the function match a pattern?

One can write functions which depend on the type of actual parameter before they are actually called. E.g.: ...
Adalbert Hanßen's user avatar
0 votes
1 answer
59 views

How can I distinguish between different structures of actual function parameters?

Overloading functions for different types of parameters is very powerful. I can easily write different versions of a function depending on the function parameters being Integer, Real, Complex, Sring ...
Adalbert Hanßen's user avatar
9 votes
1 answer
475 views

How do I correctly apply patterns to function arguments using Pattern (:), PatternTest (?), and Condition (/;)

Given: Clear[func]; listOfLists = {{a, b}, {c, d, e}}; listOfRules = {a -> b, c -> d}; I can discern between listOfLists and listOfRules using MatchQ: ...
tjm167us's user avatar
  • 993
6 votes
1 answer
306 views

A problem on the pattern (:) of Heads

I defined Clear[f, g] f[(h : Exp | Log)[x_]] := x; g[(h : Log | Exp)[x_]] := x; The code gives a weird result: ...
Curious Cat's user avatar
2 votes
2 answers
160 views

'Update' and how to use variables to define patterns in functions' arguments...?

This seems like an obvious issue that I have nevertheless never encountered before. In a package, I want to use a variable to define a pattern that is used in the definition of some functions. However ...
berniethejet's user avatar
  • 1,417
6 votes
3 answers
249 views

Part specification using list elements

How to use a list to specify part of another nested list mat? We don't want to write ...
xiaohuamao's user avatar
  • 4,738
4 votes
1 answer
83 views

How to specify the type/Head of the optional named arguments to a function?

I am trying to improve my own dsolver API. Currently it takes optional arguments. Is it possible to specify that an optional argument should say be String only? Or List Only? Or Integer only? and so ...
Nasser's user avatar
  • 149k
3 votes
1 answer
83 views

Is it possible to name a pattern within a repeating pattern during function construction?

Question I would like to know if it is possible to have a named pattern within a repeated pattern during function desctiption. For example: f[(a_)..]:= Why I am ...
nathan bartholomew's user avatar
1 vote
1 answer
165 views

How to make a pattern that matches only when one of the argument's Head is a Plus?

I am trying to set up rules as follows: ClearAll[CircleTimes]; If the arguments are the same the output is zero: ...
user13892's user avatar
  • 9,651
7 votes
4 answers
738 views

Map a two argument function to every element on list

I'm new to use the amazing map function in Mathematica, and I've found it really elegant to program in simple lines of code. However, I'd like to perform more elegant stuff. I've found this question, ...
Joshua Salazar's user avatar
1 vote
1 answer
103 views

How to define a mixed pattern consisting of string and regular patterns for function definition (using SetDelayed)?

I'm kinda stuck here, and I could really use some help: I want to define a function, let's call it quarterValue, that accepts a ...
joka's user avatar
  • 342
1 vote
1 answer
55 views

Why this pattern can't be matched? [closed]

My code is {x,y,z}/.PatternSequence[x,y]->t.I think the answer is {t,z},while the output is still ...
helloworld's user avatar
3 votes
1 answer
232 views

Pattern matching with two variables - derivatives on 4-vectors

I am interested in pattern matching with two variables and not just one, and more specifically implementing derivatives on 4-vectors. In the case of interest the 4-vectors have two indices; one is in ...
user avatar
0 votes
0 answers
75 views

How to specify specific (custom) function arguments

Suppose that I want to have a function that is evaluated only when a user-specific, predefined flag is given out of a list of possible flags. For example ...
hal's user avatar
  • 793
2 votes
0 answers
370 views

How to test for arguments for a pattern that is of a positive Integer OR Infinity so that both filter through to the definition on RHS?

How can I filter an argument of a function so that definition works on positive Integers (y >= 0) or Infinity? It will ...
Jules Manson's user avatar
  • 2,467
2 votes
0 answers
84 views

How to specify complicated patterns in function arguments?

Unfortunately, I have not found in documentation how to create complicated patterns in arguments of a function Consider a toy example. Say, I would like to have a simple function ...
Konstantin's user avatar
0 votes
0 answers
49 views

Detecting whether a function is initially defined with the form R[x,y] or R[x_,y_], etc

I have a very long code that may have errors in definition of functions and I want to trace these errors. Is there any attribute or function to detect whether a function is initially defined in the ...
Alex97's user avatar
  • 420
0 votes
1 answer
111 views

Matching a Dynamic Argument Pattern

times is a list of times i.e. matching {{hour_, min_, sec_Real}..}. I want to call ...
RobertNathaniel's user avatar
2 votes
1 answer
80 views

Defining function with arguments in a specific pattern similar to Graphics[]

I'm trying to define a function that is of this form: ...
Timevortex's user avatar
1 vote
1 answer
80 views

Denoting the whole in Position/Extract/ReplacePart

Position returns lists of numbers consumed by Extract, ReplacePart and others as "...
Perry The Cynic's user avatar
7 votes
1 answer
229 views

How to set deeper patterns in arguments within function definitions. For example if an argument must be a list of 1 or more strings

Suppose I want to define a function whose argument must be a List of only Strings and List must have at least one member. I know how to require that the Head must be a List but how does one qualify ...
Jules Manson's user avatar
  • 2,467
4 votes
0 answers
114 views

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 ...
rocky's user avatar
  • 245
4 votes
0 answers
89 views

Preventing $-renaming of pattern names in lexically-scoped constructs

When you use a lexically-scoped construct like With or Function to perform replacements in an expression, Mathematica might ...
thorimur's user avatar
  • 9,030
8 votes
0 answers
74 views

Why does Longest not work on OptionsPattern?

When having a function with optional arguments and options, I frequently find myself writing something like this: ...
Lukas Lang's user avatar
  • 34.4k
8 votes
2 answers
613 views

How to check the number of function arguments given by the user

Suppose I have defined the following function inside a package that can take 2 arguments. f[x_,y_]:={x,y} So, if the user gives ...
Epsilon's user avatar
  • 1,122
0 votes
1 answer
255 views

Restrict the domain of a function (and plot) it) when it depends on the value of a parameter [closed]

I know similar questions have been asked, but I am very new at Mathematica and other questions/answers are quite complex and do not suit the code I want to write. I want to restrict the domain of the ...
Banalaude's user avatar
  • 107
2 votes
1 answer
26 views

Refer to argument in condition for other argument

The condition for b here doesn't recognize a. How can I fix this? ...
H.v.M.'s user avatar
  • 1,093
0 votes
0 answers
319 views

How to convert Symbol to Number?

Question I have a variable x which is associated with the number 1 : x= 1 In the program, I faced with the data type problem ...
ten's user avatar
  • 113
1 vote
2 answers
98 views

Unusual performance of the Cases function [closed]

Recently I was surprised by unusual performance of Cases function. The code ...
Konstantin's user avatar
2 votes
0 answers
104 views

How does Mathematica choose which function definition to use?

I was wondering what the process of matching function arguments arguments against different function definitions actually looks like for Mathematica. (Just to be clear, I'm not simply looking for an ...
thorimur's user avatar
  • 9,030
7 votes
0 answers
170 views

Exceeding recursion limit with no obvious recursion: Optional pattern-matching causing nonlocal behavior

So, I've come across something strange, and wanted to get a deeper understanding of what was going on. Consider the following code: ...
thorimur's user avatar
  • 9,030
2 votes
1 answer
196 views

Function of function with arbitrary number of arguments

Given the function (simplified example) g[En_, k1_] := En^2 *k1 I have to plug it inside another expression, where En is ...
Albercoc's user avatar
  • 1,092
0 votes
1 answer
98 views

Using conditions to construct a function [closed]

This is the hint my professor provided. We have 6 separate cases to define the function pow[x_ , r_]. The first case is where ...
Michelle's user avatar
4 votes
1 answer
99 views

Force results to be displayed in a way often seen in traditional math [closed]

I want to get my output in the form that I would write by hand. I have tried with TraditionalForm, but it did not work. I've looked at several answers in this SE ...
juan muñoz's user avatar
6 votes
0 answers
119 views

Difference between the infinite rewriting behavior of evaluation and ReplaceRepeated

In the documentation tutorial/Evaluation it's said that: Every time the expression changes, the Wolfram Language effectively starts the evaluation sequence over ...
asd1dsa's user avatar
  • 573
0 votes
1 answer
83 views

passing splice instructions

Consider some passing splice instructions using start,stop,step pattern ...
lineage's user avatar
  • 1,164
1 vote
3 answers
79 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 ...
მამუკა ჯიბლაძე's user avatar
0 votes
0 answers
22 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 ...
მამუკა ჯიბლაძე's user avatar
0 votes
0 answers
56 views

Head of complex sum

We have: 1 + 2 I + 3 + 4 I // Head Complex and 1 + 2 I + 3/5 + 4/5 I // Head Complex But we get: ...
murray's user avatar
  • 12k
1 vote
0 answers
65 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 ...
Weather Report's user avatar
5 votes
1 answer
88 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: ...
Aster's user avatar
  • 3,856