Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.

learn more… | top users | synonyms (1)

6
votes
3answers
302 views

String matching balanced pairs of braces

I am trying to extract from a string in Mathematica all instances of \newcommand{ABC}{DEF} Where ABC and ...
4
votes
0answers
76 views

Is there an elegant way of delaying evaluation of `A==B` when calling a function that uses this pattern

Suppose I wanted to define a number of functions that accepts expressions of the form: ...
1
vote
2answers
63 views

What is the role of the pattern object in this implementation?

In the documentation I found a nice idiom which allows me to do model fitting with interpolating functions. I think this kind of thing is amazing and a credit to the utility of an environment like ...
3
votes
3answers
63 views

How to exclusively match in the numerator?

Here is an expression (one of many) that involves two complicated terms: \begin{multline}\text{expression} = \frac{\log \left(\frac{c}{d}\right) \left(2 a d F(a,b,c)+a^2 \left(a^2-2 a (b+c)+b^2+4 b ...
16
votes
4answers
728 views

How to define a recursive pattern?

I want to translate this recursive syntactic definition into a Mathematica pattern1: $$ \mathtt{x}: \begin{cases} \text{Null}\\ \{\textit{integer}, \mathtt{x}\} \end{cases} $$ In other ...
1
vote
0answers
52 views

Why has FreeQ a quadratic complexity with high depth?

I am a little puzzled by the following test: ...
2
votes
2answers
152 views

Two ways to speed up slow Integrate[]s? (Can't do either)

I run into problems like this all the time on the complicated integrands I pass to Integrate. The following code works but takes a veeery looong time: ...
9
votes
0answers
87 views

Confused by the opts : OptionsPattern[ ] pattern

I'm trying to make sense of the syntax in the clause opts:OptionsPattern[], as found in this standard "rule-based" (pseudo-)function definition pattern: ...
1
vote
0answers
35 views

What does __Rule do in Mathematica? [closed]

What does __Rule do in mathematica? I understand the __ is used for any repeated expression, but why is the Rule appended at the end? For example: Import nested JSON file Thanks!
0
votes
2answers
65 views

Match groups in regular expressions

I'm trying to understand Mathematica's pattern matching in strings, but I have some doubts. 99% of the time I need to a) check if a match occurs and b) retrieve the matched patterns. I come from ...
1
vote
0answers
78 views

How to Pattern Match complicated patterns? [closed]

How can I get the output expressed in terms of p1 and p2 instead of exponentials? ...
1
vote
1answer
59 views

How to write a pattern that matches all functions

What is the correct way to match a function in an expression in the way that Cases[Sin[x] Cos[x], _Sin | _Cos, {0, Infinity}] matches ...
4
votes
1answer
48 views

Ordering in noncommutative multiply

I have an expression like s ** p1 ** p1 ** p2 - s ** p1 ** p2 ** p1 Which should be zero because the commutator between p1 ...
3
votes
3answers
63 views

How to apply ReplaceAll to denominator?

Here is the simple task. In reality, it's far more complicated, but it comes down to this. I want to take the expression $\frac{y}{x^2}$ and replace $x^2$ with $k$. This doesn't work: ...
1
vote
1answer
74 views

Pattern matching list of elements with different heads

I have a function, which I want to accept only a list of kind of elements belonging to a group. The task comes down to finding an elegant pattern suitable for easy to read function prototype. As an ...
1
vote
1answer
50 views

Emulating Case Pattern replacement

As most regular users, I have developed utility functions complementing the Wolfram language for frequent tasks. In particular, I have variations on Cases (see ...
4
votes
1answer
160 views

Sow only if different from zero

I am trying to collect elements with Reap and Sow, where I am interested only in non-zero elements, that is I am trying something like that ...
4
votes
1answer
75 views
0
votes
0answers
38 views

Where can I find mathematica documentation about @@@? [duplicate]

For example, to build a rule from 2 lists {a,b,c},{1,2,3},I use : Rule @@@ Transpose[{{a, b, c}, {1, 2, 3}}] : {a -> 1, b -> 2, c -> 3} OK, but how @@@ works ? (Black magic for me) I am using ...
2
votes
1answer
44 views

Pattern matching & simple replacement

I have a question about an expression that I want to modify. Here is the expression: ...
3
votes
1answer
50 views

Using patterns to modify TeXForm output

When I use TeXForm on a matrix I often want to replace some the output with some number of verticle and horizontal lines. Is there a way to get control over this, possibly using patterns? As an ...
2
votes
2answers
73 views

Evaluation speed of apparently equivalent functions

Consider the following expressions: f[x_Integer] := x, f[x_] := x /; x ∈ Integers, f[x_?IntegerQ] := x All of the expressions above (and probably others which ...
0
votes
1answer
50 views

Generate all 3-permutations of the set {x, y, z, w, u, t} excluding the ones where 'u' is after 't' [closed]

I have to generate all 3-permutations of a the set $(x, y, z, w, u, t)$ without the ones where the element $u$ is after element $t$. So far I have generated all the the 3-permutations like such ...
0
votes
1answer
70 views

Applying ReplaceAll to I (imaginary unit) [closed]

Why does I/.I->-I return -I but Exp[-I]/.I->-I yields ...
1
vote
2answers
93 views

Get the sum with cases

I have the following list: list = {{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}, {a, b, c}, {e, f}, {g}, {}, {Sin[a], Cos[b]}}; I should use ...
-3
votes
2answers
84 views

Write a function, write a pattern [closed]

I have the following list: list = {{1, 2, 4}, 6, 7, 8, {8, 2}, {3, 4}, {4, 5}, {1, 2, 3, 1}}; (a) I should write a predicate-function in combination with an ...
0
votes
1answer
47 views

Find/Write a pattern [closed]

I should find a pattern for the following expression: expr = {a^2, a^b, x^4, (x - 1)^2}; And that should be the result: ...
9
votes
1answer
83 views

Surprising behavior when OptionsPattern is part of a larger pattern

Say we have a pattern defined as: pattern = {foo[OptionsPattern[]] ...}; We can then use MatchQ to see whether an expression ...
1
vote
2answers
92 views

Removing solutions which are particular cases of others

I have an algorithm which gives me a list of solutions (in matrix form, if it is of any importance) of an equation. Some of these solutions are particular cases of other. For example, in this list ...
1
vote
1answer
78 views

Dealing Repeated pattern in StringReplace

A problem about StringReplace When I'm running into this problem possible [bug] ...
0
votes
4answers
103 views

How to trim the zeros from the right of a list? [duplicate]

I need to remove the zeros from the right of an integer list up to the first non-zero element. Such as {0,1,0,0,2,0,0}->{0,1,0,0,2}. Padding zeros can be simply ...
4
votes
0answers
109 views

Why the flat f[x] is treated as ghostHead_[f[x]]?

While reading this answer I feel confused. If we do not care any specific detail of the original question, then ybeltukov's answer essentially makes use of the following fact ...
14
votes
6answers
733 views

Finding the period of an array of integers

A simple array of integers is given. The problem is to detect if a pattern is repeatedly occurring in the array, and find the length of that pattern. For example, for ...
0
votes
1answer
85 views

Can we say that “all functions in Mathematica are some kind of pattern-matching-and-replacement procedures”? [duplicate]

Currently I am studying Mathematica programming, and when I study pattern matching and replacement, I find it is quite similar to functions. So can we say "all functions in Mathematica are some kind ...
7
votes
0answers
234 views

Empty Blank regression in v10.1

In v10.1.0.0 on Linux, when Blank[] is used in Alternatives with pattern that can match a sequence, whole pattern unexpectedly ...
4
votes
4answers
127 views

Express block matrix in terms of matrix basis

I'm trying to construct a function that returns the multiplication table of a matrix Lie algebra. Assuming a basis of matrices, my definitions: ...
4
votes
2answers
171 views

Assumptions with patterns

I want that Mathematica assumes every symbol like Subscript[s,i] to be evaluate as a real and positive number. I have tried ...
4
votes
4answers
197 views

How to highlight the elements in a list that match a pattern?

For example, {{1,2,3},{1,2,3,1,2,3,4,5}}with pattern {1,_,3}is transformed to ...
1
vote
2answers
82 views

Is there a way to search for Arithmetic Progressions in a list?

If you have a list of integers is there a way of searching that list to see if there is a trend of Ax+B in the list? Not using all of the numbers, just seeing what trends like that are embedded in a ...
8
votes
2answers
256 views

Matching pair of numbers {a,-a}, where a is numeric

Imagine you have a list of pairs of numbers, such as {{2,-2}, {1,5}, {1,-3},{-3,3}} The task is to reduce the given list to a list of pairs of type ...
3
votes
0answers
47 views

Error when combining Except, “*” and an additional character

Anyone knows why StringMatchQ["x", Except[{"1", "2"}]] StringMatchQ["x", Except[{"*"}]] StringMatchQ["x", {"1", "*"}] (* without Except *) all work as ...
2
votes
3answers
115 views

DeleteDuplicates in a list of 2-tuples under condition

Having a list of 2-tuples, I would like to delete such 2-tuples that have 0 at second position, but only those, whose first element is not unique in the list. E.g. ...
1
vote
2answers
78 views

How do I use ReplaceAll to replace strings of a specified form?

Why is "Xabcde" /. "X" ~~ e__ -> e "Xabcde" and not "abcde"
1
vote
1answer
43 views

Matching x,y,z,2

I have a list of integers; I would like to replace anything of the form $x,y,z,2$ in such a list with $x+1, y+1, 0, 0$. The problem is that the $2$ may appear at the front, or in the first three ...
4
votes
3answers
99 views

Better output for NonlinearModelFit “ParameterTable”

The Standard table fo parameters of NonlinearModelFit is obtained from NonlinearModelFit[{{0, 1}, {1, 2}, {3, 3}}, a + b*x, {a, b}, x] %["ParameterTable"]] I ...
9
votes
2answers
284 views

Why can I not replace a Dataset using a rule?

Simple replacement of a Dataset object does not seem to follow the expected behavior... Dataset[{}] /. _Dataset->1 Returns the empty Dataset not 1 as I would ...
0
votes
1answer
36 views

Selecting from a list [duplicate]

OK, I know this must be a simple problem, but right now I'm just stuck and need some advice with this :). I have the following list: ...
0
votes
3answers
70 views

ReplaceRepeated made a single replacement where I expected multiple replacements to be made

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y} but it replaces only the first element giving ...
7
votes
4answers
439 views

Strange answer from StringReplace

I have a list of strings and I would check if there is an empty string element, that's "", because I need to replace it with a "0". I try the following ...