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
0 votes
0 answers
47 views

How to implement this in Mathematica?y=argmax{max{f(x,y),g(x,y),h(x,y)}}

There are three bivariate functions with respect to x and y. Slice x into 100 pieces from 0 to 1. Starting from each slice, calculate the function with respect to y. Find the maximum value of each ...
5 votes
1 answer
48 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 ...
  • 187
0 votes
2 answers
46 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 ...
2 votes
2 answers
49 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. ...
1 vote
3 answers
68 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.: ...
0 votes
1 answer
53 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 ...
9 votes
1 answer
201 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: ...
  • 973
6 votes
1 answer
298 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: ...
2 votes
2 answers
143 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,375
3 votes
2 answers
108 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,564
4 votes
1 answer
61 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 ...
  • 127k
3 votes
1 answer
56 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
139 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,667
5 votes
2 answers
390 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
52 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 ...
  • 162
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
221 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 ...
0 votes
0 answers
53 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
208 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 ...
  • 2,337
2 votes
0 answers
66 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
106 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
79 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
67 views

Denoting the whole in Position/Extract/ReplacePart

Position returns lists of numbers consumed by Extract, ReplacePart and others as "...
7 votes
1 answer
212 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 ...
  • 2,337
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 ...
  • 245
4 votes
0 answers
68 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,695
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: ...
  • 30.2k
8 votes
2 answers
373 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
126 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
172 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
94 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,695
7 votes
0 answers
138 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,695
2 votes
1 answer
134 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 ...
  • 630
0 votes
1 answer
88 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
97 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
117 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 ...
  • 573
0 votes
1 answer
81 views

passing splice instructions

Consider some passing splice instructions using start,stop,step pattern ...
  • 998
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 ...
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
53 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.6k
1 vote
0 answers
63 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
84 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,766
1 vote
2 answers
54 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: ...
  • 2,337
2 votes
1 answer
97 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,667
3 votes
1 answer
506 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 ...
  • 641
3 votes
2 answers
77 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: ...