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

learn more… | top users | synonyms (1)

2
votes
1answer
44 views

Confusion over blank sequence in pattern matching

My overall goal was to produce a list of heads used in a graphics output. For an example I used: ...
3
votes
3answers
129 views

Find position avoiding For loop

I have a list: {a,b,c,d,e,f} And a function sizeFunc[element_] which returns the size ...
2
votes
1answer
42 views

Dropping rational coefficients

I have a list of expressions (they all are exact numeric quantities, not containing any variables), some of them have integer or rationals coefficients, or complex coefficients with rational or ...
2
votes
2answers
63 views

Trouble simplifying using Replace

I have the following expression: w.a.b.r+w.c.d.r I want to get: w.(a.b+c.d).r So far I used ReplaceRepeated: ...
2
votes
3answers
228 views

Help with PatternSequence

I have a list of strings of the form list = {str1, str2, strx, str3, .., str10} each string contains both letters and numbers, except for ...
8
votes
2answers
132 views

An equivalent to Condition that I can use anonymously when string pattern matching

When I name a pattern I'm working with, that name stands for the same particular expression wherever it shows up. That's a basic precept of how the Wolfram Language's pattern-matching works. But if I ...
-2
votes
0answers
41 views

Find list element matching pattern

I have the following list: {A , B.C , B.C.S[d].S.F} I need to find the index of all elements containing S[d].S, i.e. an ...
0
votes
1answer
59 views

Match a variable name pattern [duplicate]

I have a list of variables: {RBI,RCD,ASD,FGH} I want to select only those whose name starts with R. Using the pattern Rx_ ...
3
votes
3answers
191 views

How to identify all rules that match pattern type? [duplicate]

I would like to be extend the below solution to be able to find get all rules from the list step[2] that match the generic pattern k_Digit_p. Where k and p are not ...
1
vote
1answer
46 views

How to use EvenQ[] in patterns? [duplicate]

This works: (w[1] + w[2] + w[3]) /. w[s_] -> If[s == 2, w[s], 0] (* w[2] *) This doesn't: ...
6
votes
1answer
94 views

How to Negate a Pattern Test and Transform a List of Strings to a JSON array

First I would like to find out how to negate a pattern and based on this I would like to solve this little problem on how to transform a list of strings to a JSON array. This is how I started working ...
0
votes
0answers
41 views

Return true if expression contains subexpression [closed]

I am trying to do the following (written in pseudo-code): if (expr contains a.b) return true otherwise return false For example, ...
-1
votes
2answers
66 views

Delete Element within a List based on the Order [closed]

I want to do the following, deleting the element of a list that do not match an increasing order as example: {1,2,3,4,6,5,7,8}->{1,2,3,4,6,7,8} I've thought of using DeleteCases but I'm not able to ...
2
votes
2answers
330 views

Opposite of Alternatives

I was looking at this question (here) and I tried to find any thing gives the opposite of Alternatives. In other words, some pattern matching function that gives ...
19
votes
3answers
249 views

Is there a GraphicsPrimitiveQ (or a complete list of Heads of graphics primitives)?

For various reasons related to workflow associated with creating figures for journals, I am creating functions that will accept graphics primitives and a set of options and spit out a ...
2
votes
2answers
151 views

How to make a function that yields a number under power operator by using pattern matching?

Can I ask you a question related to function and pattern? I would like to make the function below work which doesn't yield return values I expect. How can it be done? ...
4
votes
2answers
160 views

Is there any way to use head of Real and Integer altogether?

Is there any way to use the head of Integer and Real together like below? The below is not right, I know that, it is just for ...
2
votes
1answer
104 views

Why is there a difference when formulating patterns for Select and FirstPosition?

Select[{1, 2, 4, 7, 6, 2}, # > 2 &] Gives {4, 7, 6}. OK! So one should think that the following would work: ...
3
votes
1answer
49 views

Remove cases where region specified incorrrectly

I am using MeshPrimitives to get a list of 3D components of a Mesh Region. Within that list, I normally get some incorrectly specified regions, such as ...
7
votes
1answer
117 views

How to debug this failing pattern-matching expression?

NB: this question is primarily about troubleshooting and debugging, and only secondarily about pattern-matching. IOW, I'm far less interested in getting an explanation for the situation described ...
9
votes
0answers
79 views

Strange result of DeleteCases in an association

The function DeleteCases can be used for associations, also with level specification. Here is an example where some values are associations: ...
0
votes
0answers
13 views

Comparing association using MatchQ [duplicate]

For me below query results into False.I expect it to return True. ...
6
votes
3answers
330 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
80 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
65 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
66 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
739 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
53 views

Why has FreeQ a quadratic complexity with high depth?

I am a little puzzled by the following test: ...
2
votes
2answers
155 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
95 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
36 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
84 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
62 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
50 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
67 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
76 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
166 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
78 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
46 views

Pattern matching & simple replacement

I have a question about an expression that I want to modify. Here is the expression: ...
4
votes
1answer
56 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
74 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
53 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
73 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
87 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: ...