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
1 vote
1 answer
114 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: ...
user avatar
  • 8,075
5 votes
2 answers
299 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, ...
user avatar
0 votes
1 answer
43 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 ...
user avatar
  • 122
1 vote
1 answer
51 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 ...
user avatar
3 votes
1 answer
213 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
  • 4,113
0 votes
0 answers
38 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 ...
user avatar
  • 743
1 vote
0 answers
73 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 ...
user avatar
  • 1,647
2 votes
0 answers
54 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 ...
user avatar
0 votes
0 answers
47 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 ...
user avatar
  • 340
0 votes
1 answer
100 views

Matching a Dynamic Argument Pattern

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

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

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

Denoting the whole in Position/Extract/ReplacePart

Position returns lists of numbers consumed by Extract, ReplacePart and others as "...
user avatar
7 votes
1 answer
203 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 ...
user avatar
  • 1,647
4 votes
0 answers
111 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 ...
user avatar
  • 221
4 votes
0 answers
56 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 ...
user avatar
  • 7,739
7 votes
0 answers
68 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: ...
user avatar
  • 23.2k
8 votes
2 answers
289 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 ...
user avatar
  • 1,018
0 votes
1 answer
65 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 ...
user avatar
  • 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? ...
user avatar
  • 1,055
0 votes
0 answers
92 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 ...
user avatar
  • 113
1 vote
2 answers
93 views

Unusual performance of the Cases function [closed]

Recently I was surprised by unusual performance of Cases function. The code ...
user avatar
2 votes
0 answers
74 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 ...
user avatar
  • 7,739
7 votes
0 answers
117 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: ...
user avatar
  • 7,739
2 votes
1 answer
103 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 ...
user avatar
  • 350
0 votes
1 answer
75 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 ...
user avatar
4 votes
1 answer
94 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 ...
user avatar
6 votes
0 answers
112 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 ...
user avatar
  • 553
0 votes
1 answer
75 views

passing splice instructions

Consider some passing splice instructions using start,stop,step pattern ...
user avatar
  • 902
1 vote
3 answers
77 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 ...
user avatar
0 votes
0 answers
18 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 ...
user avatar
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: ...
user avatar
  • 11.3k
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 ...
user avatar
5 votes
1 answer
81 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: ...
user avatar
  • 3,644
1 vote
2 answers
51 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: ...
user avatar
  • 1,647
2 votes
1 answer
79 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 ...
user avatar
  • 483
1 vote
0 answers
33 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: ...
user avatar
  • 8,075
3 votes
1 answer
376 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 ...
user avatar
  • 621
3 votes
2 answers
72 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: ...
user avatar
1 vote
1 answer
39 views

FilterRules works in DateListPlot but not in Plot

I just experience the following problem (this is a MWE just to illustrate). First DateListPlot ...
user avatar
  • 5,453
2 votes
1 answer
104 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}$. ...
user avatar
2 votes
1 answer
76 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 ...
user avatar
0 votes
1 answer
87 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 ...
user avatar
22 votes
7 answers
824 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 ...
user avatar
  • 687
6 votes
2 answers
216 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 ...
user avatar
  • 687
0 votes
0 answers
62 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 ...
user avatar
1 vote
1 answer
300 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 ...
user avatar
  • 1,535
1 vote
1 answer
164 views

n derivatives on a function of n variables

I want to derive an expression with respect to each variable of a function $f: \mathbb R^n \rightarrow \mathbb R$. More precisely: $\left(\prod_{i=1}^n\frac{\partial}{\partial_{x_i}}\right) g(f(x_1,...
user avatar
0 votes
1 answer
83 views

How to have NonlinearModelFit not evaluate a function too early?

This is a follow up of my previous question How can I define the following function for arbitray values of the arguments? that received no attention so here I will try to be more concise with the ...
user avatar
8 votes
3 answers
299 views

How can I check if a function can be written as $a * \sin (b * x + c) + d$?

I want to write a function that takes another function, say $f(x)$ and a variable $x$ as input and return True it this function can be written in the form ...
user avatar
0 votes
1 answer
110 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 ...
user avatar