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

learn more… | top users | synonyms (1)

8
votes
3answers
204 views

Select and Blank

I try to filter sublists of a list which match a pattern. test = {{"String1", "a"}, {"String2", "b"}, {"String3", "a"}, {"String4", "a"}}; The result should ...
8
votes
4answers
280 views

Check in a series if there exists adjacent values with less than a certain number of missing values

I'm working with a large number of series of different experiments, where there exist a lot of experiments where there are no entries. I now want to draw a sample where there should exist at least ...
8
votes
3answers
502 views

How can I convert x^2 to x*x?

When I try the following code: a b^2 c /. b c -> e Mathematica gives me: a b^2 c but what I want is: ...
8
votes
1answer
184 views

How to tell when a job is too big for the pattern matcher?

Pattern matching is great but it has its limitations. Consider sorting a list of numbers from smallest to largest: ...
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 ...
8
votes
1answer
147 views

CSS Selectors for Symbolic XML

Symbolic XML is a convenient way of managing XML in Mathematica, at least for casual use or small tasks. Here and elsewhere you can find examples of XML handling in Mathematica, including XSLT/XPath ...
8
votes
2answers
206 views

Using Except with strings

Suppose that I have the following sample list, called list: list = {"bBb", "aAa", "aaa"}; Now suppose I want to select the ...
8
votes
2answers
228 views

Replacing product expressions - named vs. unnamed pattern?

Suppose I have an expression of the form a b c d + x y z The FullForm of this is ...
8
votes
1answer
227 views

Is it possible to specify a context-sensitive, “depth-agnostic” rewrite rule?

I'm using the term "depth-agnostic" in this post to describe structural patterns featuring a "target" sub-pattern that can occur "at any depth" relative to one or more "context" sub-patterns. As a ...
8
votes
1answer
280 views

Does Mathematica use first order or second order order unification?

When Mathematica is pattern-matching expressions, does it use first order or second order unification. Just to clarify the difference between first order and second order unification: In Second order ...
8
votes
1answer
208 views

MatchQ and patterns for similar lists

I can't seem to find the right set of patterns to independently match two different lists. Simplified versions of the lists are as follows: ...
8
votes
2answers
744 views

Mathematica rule substitution memory

First I present the specific programming question, and then I provide further background details. Given an expression in Mathematica, say f=3x+7y, there are two ...
8
votes
1answer
118 views

How to get a list of all expressions in a notebook matching a certain pattern?

Suppose I have a Mathematica notebook saved to a certain location on disk. Given a pattern I need to get the list of all expressions matching this pattern that appear in input, output or print cells ...
8
votes
1answer
223 views

Transformation rule on a list of non-negative integers

I am trying to create a transformation rule that takes a list of non-negative integer values of any length, finds a non-zero entry in the list, adds 1 to all preceding numbers, subtracts 1 from the ...
8
votes
1answer
317 views

Why does Mathematica choose the second function definition?

While working on a solution to this question I've come across a case where I simply don't understand Mathematica's behaviour. I've got the following definitions: ...
8
votes
2answers
164 views

Held keys in associations

Jacob Akkerboom commented: Note that PositionIndex does work correctly with held expressions, whereas this is a bit painful to implement using ...
8
votes
2answers
402 views

How to Enable Syntax Coloring of Pattern Match Variable Only (i.e. Without Coloring any Associated Pattern)?

Syntax coloring for the following function definition foo[x_List] := ... will color green (and italicize by default) the argument name ...
8
votes
1answer
283 views

Mma10 mystery: Failure of pure function substitution with Interpretation?

Bug introduced in 10.0.0 and fixed in 10.0.1 I had code to display with subscripts from Displaying index as subscript on output: e.g. C[i] -> C_i with Notation[...] or Interpretation[..]? which ...
7
votes
5answers
387 views

Find patterns in a list

again, I have a list like this: ...
7
votes
5answers
430 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 ...
7
votes
5answers
396 views

Splitting strings at lower case characters and numbers

I'm trying to split the strings of chemicals into their elements and numbers. See this example "Fe3O4" will be split into ...
7
votes
3answers
230 views

Pattern matching Q for rectangular data matrices

Given a List of rectangular nx3 arrays of data, each of the form {...,{x,y,z},...}, (the ellipses are not Mathematica pattern syntax, just for illustration). Some members of this list may be - by ...
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 ...
7
votes
2answers
253 views

Using MatchQ (or other means) to parse an expression using sums of COS or SIN correctly

I am writing a function that will take an expression as input. I want to make sure the expression is valid first before processing it. Looking for a correct to use ...
7
votes
4answers
686 views

How to collect terms with positive powers in polynomial

I am trying to collect all terms with non-negative powers of $x$ in polynomials like $\frac{1}{x^2}\left(a x^2+x^{\pi }+x+z\right)^2$ First expand the polynomial ...
7
votes
3answers
218 views

Why these ways of defining a function with variable number of arguments fail?

First: I know about the correct way to define a function with variable number of arguments: Clear[f] f[s__] := Plus[s] f[1, 2, 3, 4] (* 10 *) BUT I ...
7
votes
2answers
391 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 ...
7
votes
3answers
162 views

Checking through an expression

I am writing a code that manipulates expressions that contains functions with an arbitrary number of arguments. An example expression is ...
7
votes
1answer
260 views

How does Mathematica wrap code for front-end needs? Why does this wrapping affect the evaluation?

Mathematica wraps any expression with some boxing code before showing the evaluation result in the front-end. Unfortunately, the wrapping algorithm and its position in the Mathematica end-to-end flow ...
7
votes
1answer
141 views

LetterCharacter for Cyrillic symbols

Why does the code StringMatchQ["ы", LetterCharacter] return False? Probably because: ...
7
votes
1answer
223 views

Built in way to de-list singletons?

Is there a built in method to de-list singleton expressions, like this rule-based solution: expr_ :> If[Length[expr] == 1, First@expr, expr] In data ...
7
votes
1answer
794 views

Extracting coefficients from a partial differential equation

Frequently, I come across the following problem: How to rewrite a complicated partial differential equation in a more clear way? I would like to create some order by collecting terms that are equal. ...
7
votes
0answers
184 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 ...
6
votes
5answers
421 views

The correct pattern for Cases

I have a problem that, admittedly, I have already solved using Select instead, but it is irking me that I cannot seem to construct the right pattern to solve it ...
6
votes
6answers
512 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, ...
6
votes
4answers
302 views

Is there a more Mathematica-y way to label these plots?

I am studying an infinite square well in the context of quantum mechanics. ...
6
votes
2answers
226 views

Is it possible to use PatternTest and Optional value on one Pattern simultaneously? [duplicate]

Possible Duplicate: How to Combine Pattern Constraints and Default Values for Function Arguments First a simple example: define a function "add" with two arguments, and its second argument ...
6
votes
4answers
304 views

How to match a sequence of complex patterns?

I have list with the form {{a1,b1}->{x1,y1},{a2,b2}->{x1,y2},...} where the {ai,bi}'s can be ...
6
votes
2answers
248 views

Unexpected behavior of rule matching a pattern

I am a beginner exploring the world of Mathematica. I expected the following code T[6, 5, 4, 1, 2, 3] /. {T[a___, 1, b___] -> Length[List[b]]} should return ...
6
votes
2answers
299 views

Fastest way to test if a numerical array has complex elements

I am looking for the fastest way to test whether an n-dimensional numerical (NumericQ) array (in the ArrayQ sense) is complex or ...
6
votes
3answers
386 views

Replacement rule only matches part of expression

I have the result of a calculation (which is too long to mention here) and it gives ...
6
votes
5answers
202 views
6
votes
3answers
209 views

Problem with WordBoundary in string patterns

I am trying to extract from a string all words that end with "-man", but the problem is that WordBoundary does not seem to work as it returns whole string ignoring ...
6
votes
4answers
592 views

Function argument to default under certain condition

Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met? For example, if a function is defined as: ...
6
votes
3answers
204 views

Using PatternTest and/or Condition to detect equal list lengths as well as list element formats

On searching Mathematica.SE for information about overloading functions, I came across several threads about PatternTest and ...
6
votes
3answers
183 views

Isolating string characters

I have the following string: ...
6
votes
2answers
301 views

Rule for orderless function Times

I have problems extending a replacement rule. Schematically, this is the situation: I have an expression and a rule, and they work perfectly together. ...
6
votes
5answers
232 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 ...
6
votes
3answers
135 views

Pattern matching: Times[a_] vs Times[a__]

I don't understand why MatchQ[ HoldComplete[Times[3, 2, 2]], HoldComplete[Times[a_]] ] is returning True. I thought that ...
6
votes
2answers
227 views

Apply Trig Identity to Transform Sum of Sinusoids into Phase-Shifted Cosine Function

I'm trying to solve for the output of a simple RC low-pass filter, with Cos[w t] input. The convolution of the input with the RC transfer function gives me the right output, but I'd like to be able to ...