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

learn more… | top users | synonyms (1)

2
votes
1answer
40 views

Pattern matching & simple replacement

I have a question about an expression that I want to modify. Here is the expression: ...
3
votes
1answer
39 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
71 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
37 views

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

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
67 views

Applying ReplaceAll to I (imaginary unit) [closed]

Why does I/.I->-I return -I but Exp[-I]/.I->-I yields ...
1
vote
2answers
87 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
73 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
80 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
83 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 ...
0
votes
0answers
35 views

Dealing Repeated pattern in StringReplace

A problem about StringReplace When I'm running into this problem possible [bug] ...
0
votes
4answers
94 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
102 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
705 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
84 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
182 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
113 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
169 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
190 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
79 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
248 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
45 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
106 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
74 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
41 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
92 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
275 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
32 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
68 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
437 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 ...
1
vote
0answers
31 views
1
vote
1answer
32 views

How do I match all the subscripted terms in Times?

Suppose I have an expression: $W = A_{i,p,q...}\,\, a_ia_ja_ka_l...$, where $A_{i,p,q...}$ is an expression that does not depend on $a$, the indexes $i,j,k,l,p,q...$ are integers and all the integers ...
2
votes
1answer
54 views

How to make Simplify/Expand work in patterns?

I am trying to calculate deriv of a complicated function and store the values for reuse. But the FullSimplify in my definition doesn't seem work all the time: ...
2
votes
2answers
57 views

Extracting numerical coefficients from an expression

I have very large expression and I want to extract and sum up all the numerical coefficients which multiply the summands. For example, expr = x + 2*y + 3*z*w ...
3
votes
3answers
113 views

Change matrix elements at specific locations?

Let's say I have an array a={{1,2,3}, {4,5,6}, {7,8,9}} I want to have all elements along the diagonal multiplied by 2. ...
2
votes
2answers
138 views

Can a pattern examine all partitions of a sequence?

Say I have a list: {a, b, c, d}. I'd like to check that the list has some partition, say {{a, b, c}, {d}} such that all the elements of the partition satisfy a ...
0
votes
1answer
56 views

Replace elements that match pattern

I am using pattern matching to identify elements in a list to be replaced. How can I supply a list of patterns to match and replace all elements in the list that match the pattern? The problem I have ...
2
votes
1answer
74 views

Removing characters from non-strings

I have a large list of lists that contains mixed data, some real, some strings. The lists are of various lengths and depths. Some of the real data values have an asterisk appended to them, e.g.: ...
6
votes
6answers
511 views

Determine if all the zeros in a list occur at the end

I have a collection of (short) lists of nonnegative integers. For each, I would like a predicate that decides if all of the zeros in the list appear at the end. For example, ...
4
votes
1answer
93 views

Compare tree structures in mathematica

Since Mathematica expression is basically a tree structure, I would like to perform tree structure comparison with certain restrictions, for instance that the identity of nodes on the same level be ...
9
votes
3answers
117 views

Pattern matching with Complex. Feature or a bug?

fixed in 10.1 (windows) I have run into some strange behavior while doing some pattern matching. First, this works as expected: ...
0
votes
3answers
70 views

How to create a general expression for an integer derivative?

I need MMa to return a general expression for the jth derivative wrt v of v^n. This didn't work: ...
0
votes
1answer
56 views

How to use pattern matching for integral identities?

I have an unknown pdf f[x], and I need to express integrals involving f[x] as expansions of the moments of f[x]. For example: ...
5
votes
1answer
175 views

How can I construct a recursive pattern guard?

I am modelling binary trees in Wolfram Language by writing them as three-element lists, like so: binTree = {nodeVal, leftChild, rightChild}. It does not yet matter ...
5
votes
5answers
126 views

Isolating cross terms

I have a set of expressions that contains terms like $\frac{1}{(x + a + b)(c+d)}$. I would like to simplify the denominator so that products of $a,b,c,d$ are dropped, but $x c$ and $x d$ are kept. ...
0
votes
2answers
44 views

FilterRules with pattern for name [duplicate]

I have a list of rules and I want to filter it using a pattern for the initial string representing the name of the rule. So that in the example below it filters linearBool even if it is called ...
2
votes
1answer
110 views

Odd behavior matching powers of pi

I have a long list of generated values from which I want to remove "weird" ones like Sin[Sin[1]] and Pi^2/4. However, trying to ...
4
votes
2answers
99 views

Output function control and manipulation for efficient computation [closed]

I have taken note of a similar post related to modifying function parameters. I tried to adapt Kuba's solution to this post but I can't get it to work. I will illustrate my problem using a similar ...
3
votes
1answer
106 views

Pattern matching multiple symbols names and powers

Seemingly a relatively simple problem, I can't seem to find a solution... I want to build a pattern that will match any symbol whose name starts with V, and is elevated at any power. Something on the ...
1
vote
1answer
48 views

How to modify the output result of an evaluation

For ease of notation, I have defined a function, $f$, that defines the permutation of Bosonic particles such that it has the following properties (the form of $f$ is irrelavent for this question): ...