Questions tagged [pattern-matching]
Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.
1,615 questions
3
votes
1
answer
221
views
What is the meaning/utility of __[] AKA BlankSequence[][]?
I understand BlankSequence in other contexts, but not this one __h means a pattern of one or more things each of which have head ...
3
votes
1
answer
156
views
What, if any, is the list analogue of KeyValuePattern?
I am trying to understand the function of KeyValuePattern. According to the documentation,
KeyValuePattern[{patt1, ...}] is a ...
4
votes
2
answers
139
views
Matching an expression in Hold
Trying to replace some of the expressions wrapped by Hold as below to get $\frac{k d\theta }{dt}$, ReplaceAll does not replacing ...
1
vote
0
answers
81
views
Replacement of I to -I not working [duplicate]
Let
$f(x) = A \exp(i k x) + B \exp (-i k x)$.
If we replace every $i$ by $-i$, we are supposed to get
$g(x) = A \exp(-i k x) + B \exp (i k x)$.
However, the Mathematica code does not work as expected.
...
3
votes
1
answer
124
views
Why is Position with level spec {1} evaluating on {0}?
The title says it all. These two examples both have StringEndsQ emit an error message because it's being evaluated on the head of the input list.
...
1
vote
3
answers
211
views
Using Switch to return a value depending on the sign of a real number
I would like to use Switch to test if a real number myNum is negative, zero, or positive and to return "N", "Z&...
2
votes
0
answers
92
views
How to define the derivative of variables inside an expression so chain rule can be correctly applied to the full expression
I don't use Mathematica as much and only use it for some specific tasks from time to time (mostly simplifying expressions and calculating integrals and derivatives).
Lets say I have an large ...
1
vote
1
answer
149
views
How do we find an asymptotic approximation of the function $\mathbf{P}(r)=\left(t_1+\prod\limits_{k=1}^{r}(t_2+k^{s})\right)^n$?
Suppose we have the following function, where $s\in\mathbb{R}$ and $t_1,t_2,n\in\mathbb{N}\cup\{0\}$ are constants:
$$\mathbf{P}(r)=\left(t_1+\prod_{k=1}^{r}(t_2+k^{s})\right)^n$$
Question: What is ...
2
votes
3
answers
154
views
Finding full-depth positions ("absolute positions") of elements within sublists satisfying a criterion
I have exampleList, a list of lists of lists:
...
8
votes
1
answer
472
views
What's happening when we execute a_b=1?
OK, of course I know that, as mentioned in e.g. here, symbol names in Mathematica cannot contain underscore _ because it's a reserved character, but, what's ...
3
votes
2
answers
214
views
Usage of replacement
With a naive thought, it seems that rule as below can match in expr. However, it does not match. Could I know what mistake I ...
9
votes
2
answers
978
views
Why f[x___] := {x==="To be", x=!="not to be"} is both True for f[]?
The behavior of pattern matching in functions surprised me at the
unexpected behavior of this example
...
8
votes
1
answer
544
views
Why does MatchQ[ 3 + x, _ + _ ] return False?
Consider
MatchQ[ 3 + x, _ + _ ]
It returns False. Why?
After some research, I concluded that it's due to the ...
2
votes
1
answer
134
views
Difference in pattern matching before & after Expand
The goal is to substitute m2/m1 in expr with k. Code1 does not match the pattern, but if it is pre-processed with Expand like in code2, it matches the pattern. Could I know why there is this ...
2
votes
0
answers
92
views
Unexpected result with Position using pattern
I use Mathematica 12 and have the following code
t1 = {1, 2, 3.1, 4, 5, 6, 7.1}
Position[t1, x_ /; Not[IntegerQ[x]]]
which gives me
...