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

learn more… | top users | synonyms (1)

8
votes
1answer
127 views

What kind of pattern should I use with StringCases? [duplicate]

Consider the string str = "ABCaDEFbcABCdefDEF" I'd like to take out all substrings surrounded by the substrings "ABC" and <...
8
votes
3answers
314 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 ...
8
votes
1answer
256 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
2answers
807 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
304 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
216 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
120 views

Why is a longer pattern matched rather than a short pattern?

I was reading the Mathematica V5 Book and am a bit perplexed on the behavior of a particular pattern matching example where it seems to be choosing a longer pattern rather than a shorter pattern. The ...
8
votes
2answers
507 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 <...
8
votes
1answer
254 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
327 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
1answer
277 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 ...
8
votes
1answer
90 views

Matching HoldPattern expressions

My original problem is to delete all rules starts with a[1] from SubValues[a] For example, ...
8
votes
1answer
162 views

Why does pattern test work only with pure functions?

Why is it that pattern test works only with pure functions? Cases[Range[0, 70], _?(Divisible[#, 7] &)] Cases[Range[0, 70], x_?(Divisible[x, 7])] I ...
8
votes
2answers
162 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 ...
8
votes
2answers
442 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
310 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
748 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
605 views

Find patterns in a list

again, I have a list like this: ...
7
votes
3answers
248 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
474 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
261 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 ...
7
votes
3answers
134 views

How can I know if a pattern corresponds to a string pattern?

What would be the best way to detect if a pattern corresponds to a string pattern ? An invalid string pattern p would lead to an error in ...
7
votes
5answers
327 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 ...
7
votes
4answers
103 views

Problem with handling a factor of $1$ in pattern matching

I am trying to implement the creation and annihilation operators $\hat{a}$ and $\hat{a}^{\dagger}$ of quantum mechanics with the following code, but alas, there is a problem with the pattern matching ...
7
votes
2answers
333 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
963 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
560 views

Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
7
votes
2answers
293 views

Why doesn't this pattern work?

I want the in-built function Count to count the elements in a list which are greater than a value. Example: ...
7
votes
2answers
154 views

How to define patterns to filter lists of replacement rules?

I have a list of replacement rules like list˘of˘replacement˘rules = {a -> 1, b -> 2, c -> 3} and I want to filter all the elements where ‘b’ is replaced ...
7
votes
1answer
81 views

How to match Association[]?

The following input: Cases[{"key" -> Association[]}, HoldPattern["key" -> Association[]]] Returns {}. Why does it not ...
7
votes
2answers
275 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 ...
7
votes
2answers
150 views

Good coding for a tricky expansion with convolutions, limits, substitutions

I am attempting to generate a power series in $v$ as $v\rightarrow0$ for $$g(x,v)\equiv \ln\int_{-\infty}^{\infty}e^{-y\left(z\right)}N\left(x-z,v\right)\,\mathrm{d}z $$ where $y(x)\equiv\sum_{i=1}...
7
votes
3answers
169 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
168 views

LetterCharacter for Cyrillic symbols

Why does the code StringMatchQ["ы", LetterCharacter] return False? Probably because: ...
7
votes
1answer
258 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 analysis,...
7
votes
1answer
275 views

How to set up new types for pattern matching strings?

Consider the following toy example: I have a set of language sounds, which I partition into two exclusive subsets, consonants and vowels. I want to set up string patterns for e.g. ...
7
votes
1answer
931 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
1answer
88 views

Why can't I replace I with HoldPattern@I?

Just try this sample: I /. HoldPattern@I -> a (* I *) Why does it fail to replace I with ...
7
votes
1answer
87 views

How does a construct like `i : func[arg_] := i = an expression using arg` work?

Occasionally I run into a Pattern that looks like i:func[arg_]:=i= an expression using arg I have two questions on this: Can ...
7
votes
1answer
146 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 ...
7
votes
1answer
140 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 ...
6
votes
5answers
491 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
522 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
3answers
556 views

Camel hump string split

Given System names of 2 or more characters: systemNames = Names["System`*"] // Select[StringLength[#] > 1 &] ; Is ...
6
votes
4answers
336 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
4answers
371 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
5answers
772 views

How can I name patterns in a Switch expression?

Suppose I have an expression and want to match it against successive patterns and return an expression corresponding to the first pattern that matched. In that case, I can use ...
6
votes
2answers
388 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
230 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
3answers
479 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 ...