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

learn more… | top users | synonyms (1)

8
votes
2answers
356 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 ...
3
votes
1answer
123 views

How to make a pattern to change x^(2/y) to (x^2)^(1/y) for any x and y?

This might have been asked before. Please let me know if it is. I was looking at an interesting Mathematica question in students forum and trying to solve it, and a chance to learn more about ...
1
vote
2answers
154 views

A function that only evaluates on lists of pairs [duplicate]

Possible Duplicate: Why doesn’t PatternTest work with Composition? I'd like my function to only evaluate when the argument is a list of pairs. It seems like ...
4
votes
2answers
167 views

How to use Optional as an omissible parameter in pattern-match?

Below is a function intended to test if an expression is a polynomial in which all the coefficients and exponents are integers: ...
1
vote
1answer
186 views

Functions that operate on symbolic matrices?

I'd like to write functions that operate on symbolic matrices, and do nothing when fed anything else. ...
3
votes
1answer
231 views

How do you pattern match a DataDistribution

I have a function, f[dist_, samp_]:=somework[dist, samp] that I want to return Null or zero if passed a null distribution. I ...
2
votes
3answers
119 views

Applying several patterns to `Split`

Please consider the following: Split[data, patterns &] patterns=Not[#=="a"]&&Not[#=="b"]&&...&&Not[#=="blabla"]; How can I define ...
7
votes
1answer
243 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
2answers
176 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 ...
4
votes
1answer
269 views

Pattern matching in subscripted variables

I am trying to use subscripted variables as symbols and provide assumptions elegantly for them, e.g. in order to simplify logs, I'd like to have ...
2
votes
1answer
129 views

replacement rule to pull independent expression outside of Integrate

I'm trying to write a fairly general replacement rule that will pull expressions independent of the variable of integration outside of Integrate, since Simplify and ...
0
votes
2answers
192 views

Pattern constraint for two or more variables

Is it possible to have a pattern constraint for a function with two or more variables where the pattern is a relation between the vars being accepted e.g. ...
8
votes
3answers
495 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: ...
9
votes
4answers
513 views

Using Position (which requires a pattern) in the same manner as Select (which requires a criterion)

I am using Mathematica7. I have a list of lists (creatively named list in the following sample code) containing data. I would like to find the positions of the ...
10
votes
4answers
295 views

How to match expressions with a repeating pattern

The expression I want to match obey a simple pattern that repeats it self a number of times. f[a]@f[b]@f[c]@...@f[X] How would you match all expressions of this ...
6
votes
1answer
363 views

Count sublists which match pattern

I have a list which looks like this: data={{12,0},{0,0},{20,1},{0,0}, {0,-1}}. Now I would like to count the number of sublists which: have both slots zero the ...
7
votes
2answers
209 views

Why does the name of a pattern affect the result of a transformation rule?

I was playing a little dirty, trying to get a template for defining a Curl[] operator. I wrote the following rule, which I knew was going to spit out a few ...
16
votes
1answer
313 views

Is pure pattern matching without PatternTest and Condition Turing-complete?

Note that this question is not a duplicate of that one. It is specifically about the pattern-matching engine, and explicitly excluding those patterns which call back into the full evaluation (i.e. ...
16
votes
2answers
805 views

How do I perform string matching and replacements?

What are, and how do I use Mathematica's string matching and replacement tools?
7
votes
2answers
620 views

Determine whether some expression contains a given symbol

Given a symbol t and an expression expr, how can I determine whether or not the symbol t ...
7
votes
3answers
482 views

Specifying string patterns in DeleteCases

It seems that DeleteCases is not compatible with string patterns, at least directly (see, for example, this question). Is this true? If so, why is this the case? ...
10
votes
4answers
381 views

How to use pattern matching to assign values to Subscript[f,x_]?

I want to define two subscripted functions Subscript[f,1] and Subscript[f,2]. To keep the assignments local, I would like to ...
1
vote
2answers
159 views

Using patterns in ReplaceAll to extract elements from tuples at arbitrary depths

Please consider the following: I need to extract from MyList all last parts of each 3-tuple. ...
11
votes
4answers
432 views

Assessing argument type in set delayed function definitions

I'm wondering how to properly assess the type of arguments passed to functions defined with :=. I want my functions to be the most efficient, unambiguous and clear ...
12
votes
4answers
294 views

The gap between MapAt and ReplacePart for 2D data tables with headers

Consider a relational table derived from survey data, where each column ("001-01" ...) represents a responder and each row (...
4
votes
1answer
298 views

Path queries for tree-structured data

Can anyone suggest documentation or tutorials for developing path queries and indices for (XML-like) tree-structured data? Suppose data is organized hierarchically in key->value pairs, eg: ...
5
votes
2answers
195 views

Delete contexts from a string representation of a symbol

I want to delete the string representing the context of a symbol. For info I need such a functionality in my ShowIt function defined here I'd like to know how I ...
5
votes
3answers
428 views

position of sequence of elements in list [duplicate]

Possible Duplicate: Finding a subsequence in a list Question The position of {3, 5} is the list {1, 3, 4, 3, 5, 5, 1} is 3. How can such a position ...
7
votes
1answer
192 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 ...
8
votes
3answers
385 views

Find subexpression to minimize leafcount after replacment with temporary variable

Given an expression expr, is there an automated way to find a subexpression subexpr such that replacing ...
27
votes
0answers
515 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, ...
7
votes
2answers
307 views

Building a tree

Given a list of word characters, such as this one, I'd like to build a tree, similar to this makeTree function, but with the tree in a different format. So, for an input such as ...
13
votes
2answers
360 views

How can I separate a separable function

I have a separable function $f[x,y]$, and I would like to find two functions $g[x]$ and $h[y]$ with $f[x,y]=g[x] h[y]$ where $g[x]$ doesn't depend on $y$ and $h[y]$ doesn't depend on $x$. Ideally, ...
15
votes
2answers
401 views

Successful match in Replace but not in Cases?

I have a little example that exhibits a successful pattern match in ReplaceAll that Cases misses and I wonder if the assembled ...
4
votes
4answers
476 views

Creating lists from a string, separating words from numbers

Say I have a string that contains numbers and words, such as this one: string = "there are 1234 words and numbers 5678 in here $999"; How would I separate the ...
9
votes
1answer
298 views

Selectively clear DownValues

How can I clear a subset of a symbol's DownValues ? For example, suppose I have created some DownValues for $f$ like this: ...
39
votes
1answer
1k views

How to generally match, unify and merge patterns?

This question was split from this one. While that question is now about how to match two particular patterns (mostly using Verbatim or ...
3
votes
1answer
164 views

Pattern matching a multivariate derivative

I can extract the parameters of a single variable derivative with the rule D[f[x], x] /. Derivative[order_][fcn_][variable_] -> {order, fcn, variable} ...
9
votes
1answer
267 views

Mixing patterns with string patterns

I have imported an XML document into Mathematica. It is now represented as Symbolic XML expression in the variable xml: ...
5
votes
2answers
209 views

Pattern matching deletion of list items [duplicate]

Possible Duplicate: How can I remove B -> A from a list if A -> B is in the list? I'm working toward building a graph and use some code to construct my rules which results in a form ...
11
votes
2answers
295 views
2
votes
2answers
281 views

Pattern test for variables during function definition

I am having a problem with pattern testing on variables during function definition ...
11
votes
4answers
427 views

Calling Table with custom iterator

I often find myself in situations where I, for example, need to build a table for some expression, but want to set the number of points rather then the step size, so the code ends up looking like this ...
16
votes
4answers
421 views

Splitting a list

Please consider the following list: data={1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3}; Now I would like to split the list into 0-sequences and Not-0-sequences as following: ...
5
votes
5answers
390 views

How can I use nested conditionals inside my functions?

I'm a big fan of using conditionals inside my functions to deal with cases, that is, to pattern match in stages and possibly leave a function unevaluated. ...
16
votes
1answer
434 views

NotebookFind and String Pattern Expressions

Is there a way that NotebookFind can be used to match string pattern expressions rather than just strings? The documentation for ...
4
votes
1answer
147 views

Searching Notebooks for String Patterns using Workbench v2

The Workbench webpage describing new features in version 2 lists the ability to "Search notebook documents with the Mathematica pattern search utility" under the heading Mathematica Front End ...
7
votes
3answers
217 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 ...