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
2 votes
2 answers
130 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 ...
  • 1,335
3 votes
2 answers
86 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 ...
  • 4,370
4 votes
1 answer
59 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 ...
  • 116k
3 votes
1 answer
52 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 ...
1 vote
1 answer
128 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: ...
  • 8,417
5 votes
2 answers
329 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, ...
0 votes
1 answer
48 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 ...
  • 122
1 vote
1 answer
52 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 ...
3 votes
1 answer
215 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 ...
  • 4,163
0 votes
0 answers
46 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 ...
  • 763
1 vote
0 answers
104 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 ...
  • 1,977
2 votes
0 answers
57 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 ...
0 votes
0 answers
48 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 ...
  • 350
0 votes
1 answer
103 views

Matching a Dynamic Argument Pattern

times is a list of times i.e. matching {{hour_, min_, sec_Real}..}. I want to call ...
2 votes
1 answer
75 views

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

I'm trying to define a function that is of this form: ...
1 vote
1 answer
65 views

Denoting the whole in Position/Extract/ReplacePart

Position returns lists of numbers consumed by Extract, ReplacePart and others as "...
7 votes
1 answer
207 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 ...
  • 1,977
4 votes
0 answers
112 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 ...
  • 223
4 votes
0 answers
62 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 ...
  • 8,458
7 votes
0 answers
69 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: ...
8 votes
2 answers
315 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 ...
  • 1,092
0 votes
1 answer
91 views

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

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 ...
  • 107
2 votes
1 answer
24 views

Refer to argument in condition for other argument

The condition for b here doesn't recognize a. How can I fix this? ...
  • 1,073
0 votes
0 answers
115 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 ...
  • 113
1 vote
2 answers
94 views

Unusual performance of the Cases function [closed]

Recently I was surprised by unusual performance of Cases function. The code ...
2 votes
0 answers
83 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 ...
  • 8,458
7 votes
0 answers
129 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: ...
  • 8,458
2 votes
1 answer
112 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 ...
  • 350
0 votes
1 answer
84 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 ...
4 votes
1 answer
95 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 ...
6 votes
0 answers
116 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 ...
  • 563
0 votes
1 answer
79 views

passing splice instructions

Consider some passing splice instructions using start,stop,step pattern ...
  • 912
1 vote
3 answers
78 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 ...
0 votes
0 answers
19 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 ...
0 votes
0 answers
52 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: ...
  • 11.5k
1 vote
0 answers
62 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 ...
5 votes
1 answer
83 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: ...
  • 3,706
1 vote
2 answers
52 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: ...
  • 1,977
2 votes
1 answer
86 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 ...
  • 483
1 vote
0 answers
35 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: ...
  • 8,417
3 votes
1 answer
431 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 ...
  • 621
3 votes
2 answers
73 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: ...
1 vote
1 answer
41 views

FilterRules works in DateListPlot but not in Plot

I just experience the following problem (this is a MWE just to illustrate). First DateListPlot ...
  • 5,495
2 votes
1 answer
106 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}$. ...
2 votes
1 answer
77 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 ...
0 votes
1 answer
89 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 ...
22 votes
7 answers
865 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 ...
  • 687
6 votes
2 answers
220 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 ...
  • 687
0 votes
0 answers
65 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 ...
1 vote
1 answer
408 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 ...
  • 1,535