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

learn more… | top users | synonyms (1)

4
votes
1answer
179 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 ...
3
votes
3answers
67 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 ...
2
votes
2answers
159 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: ...
1
vote
0answers
53 views
7
votes
5answers
544 views

Calculating weird numbers

A weird number is a number such that the sum of the proper divisors (divisors including 1 but not itself) of the number is greater than the number, but no subset of these divisors sums to to ...
9
votes
0answers
98 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
38 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
67 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 ...
5
votes
3answers
501 views

Camel hump string split

Given System names of 2 or more characters: systemNames = Names["System`*"] // Select[StringLength[#] > 1 &] ; Is ...
1
vote
0answers
88 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
67 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
52 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
71 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
77 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 ...
4
votes
5answers
299 views

Pattern matching in nested lists

I'm sure this has been asked before, but I can't find the right combination of search keywords to find it. Apologies. I want to do a ReplaceAll on a list of lists, ...
1
vote
1answer
52 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 ...
1
vote
1answer
83 views

Dealing Repeated pattern in StringReplace

A problem about StringReplace When I'm running into this problem possible [bug] ...
0
votes
1answer
118 views

Replacement of terms/Pattern matching involving products of derivatives of a function

In delving into Ramanujan summation, I'm trying to get a hold of the relations of the form $$\sum_{n=0}^\infty f(n)=\dfrac{h\frac{d}{dx}}{{\mathrm{e}^{h\frac{d}{dx}}}-1}\int_{0}^\infty ...
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
48 views

Pattern matching & simple replacement

I have a question about an expression that I want to modify. Here is the expression: ...
4
votes
1answer
59 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
1answer
62 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
76 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
57 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 ...
12
votes
1answer
255 views

Orderless pattern matching

The MMA docs state that "In matching patterns with Orderless functions, all possible orders of arguments are tried". Why then does the following not work? ...
6
votes
5answers
247 views

Extracting sequences from a list?

Suppose I have a list of arbitrary length like {1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5} and I am trying to extract the sequences ...
55
votes
4answers
3k views

Using a PatternTest versus a Condition for pattern matching

My last question to the site resulted in several answers that involve using pattern matching in Mathematica, a feature I wasn't very familiar with at the time. I am currently reading Mathematica ...
16
votes
1answer
307 views

Why is ReplaceAll behaving like this?

I'm learning to use the ReplaceAll function and I found the behavior of which is quite confusing. For Sqrt[f[x, y]] /. f[___] -> u Mathematica returns ...
0
votes
1answer
78 views
1
vote
2answers
94 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 ...
2
votes
1answer
225 views

Match a sequence of integers

I would to match a pattern p1 = 0,0,1,0,0 to the elements in listA. The elements are a boolean sequence that may look like this ...
6
votes
5answers
238 views

Counting subsequences (i.e., patterns) within a list?

I would like to count the number of subsequences in list. Say I have data given by: data = {1, 1, 1, 2, 1, 2, 2, 2, 1, 2, 1}; I would like to count the number ...
10
votes
9answers
1k views

How can a power of 0 or 1 be replaced?

Consider this given example for ReplaceAll 1 + x^2 + x^4 /. x^p_ -> f[p] This returns ...
-3
votes
2answers
91 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: ...
1
vote
2answers
93 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 ...
9
votes
1answer
84 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 ...
4
votes
4answers
130 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: ...
42
votes
2answers
856 views

How is pattern specificity decided?

Mathematica has a notion of pattern specificity, which is a partial ordering on patterns. The rules (e.g. DownValues, ...
4
votes
2answers
112 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 ...
4
votes
0answers
111 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 ...
0
votes
4answers
111 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 ...
15
votes
6answers
767 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 ...
38
votes
6answers
2k views

Find zero crossing in a list

I'm looking for a function that finds the index of the zero-crossing points of a list. Before I go making my own subroutine to do this, I was wondering if anyone knows of any built-in Mathematica ...
0
votes
1answer
88 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 ...
8
votes
2answers
421 views

Adapting “Find and Replace” functionality to work with Patterns

I was thinking if there is something like Find and Replace (Crtl+F) functionality adapted to work with patterns. Probably not, but it seems possible to create this with some effort and skills in ...
4
votes
2answers
182 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 ...
1
vote
2answers
87 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 ...