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

learn more… | top users | synonyms (1)

17
votes
2answers
368 views

Pattern that matches colors

Suppose I'm writing a function that takes a color as a parameter; for example: drawShape[color_] := Graphics[{Style[Disk[], color]}]; But if the caller inputs an ...
7
votes
1answer
253 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 ...
6
votes
1answer
300 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 ...
0
votes
1answer
198 views

How to collect terms of a given type (with subscript and power) in an expression?

Suppose that I have an expression: ...
2
votes
1answer
142 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 ...
14
votes
5answers
667 views

Getting number of binary digits combinations without “forbidden” patterns

I need to get the number of all combinations of binary digits in an 8-digit binary number, but not including those that follow some "forbidden" patterns like these: ...
0
votes
2answers
211 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
498 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: ...
7
votes
1answer
219 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 ...
20
votes
4answers
569 views

How can a big table be treated as a database?

I often work with big tables that I want to treat kind of like a database. Here's an example table. ...
10
votes
4answers
316 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 ...
10
votes
3answers
490 views
35
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 ...
6
votes
1answer
421 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
211 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 ...
9
votes
2answers
809 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 ...
14
votes
6answers
2k views

Counting negative values in list

I would like to count the negative values of a list. My approach was Count[data, -_] which doesn't work. How can I tell Mathematica to count all numbers with ...
10
votes
4answers
440 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
168 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
496 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
336 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 (...
15
votes
3answers
461 views

Grabbing a number of characters around a string pattern

If I have some string data: text = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 400] "When in the Course of human events, it becomes ...
5
votes
2answers
198 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 ...
8
votes
3answers
571 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? ...
5
votes
3answers
494 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
208 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 ...
9
votes
3answers
488 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 ...
35
votes
2answers
661 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, ...
13
votes
2answers
392 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, ...
10
votes
4answers
409 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 ...
9
votes
1answer
315 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: ...
4
votes
4answers
637 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 ...
14
votes
4answers
554 views

Returning an unevaluated expression with values substituted in

Let's say I have a function formula[x_List] := (x[[1]] - x[[2]]) + (x[[3]] - x[[4]]); and I want to pass it a variable and get back the unevaluated formula with ...
3
votes
1answer
189 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} ...
5
votes
3answers
326 views

Highlighting text with StringReplacePart but also using Style, Subscript

For a Natural Language Processing analysis task it's desirable to highlight a text string programmatically according to XML tags that reference specific words or phrases. The XML tags specify the ...
10
votes
1answer
341 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: ...
9
votes
4answers
619 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 ...
5
votes
2answers
221 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 ...
6
votes
2answers
291 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. ...
11
votes
2answers
353 views

Should I use \\\“ or \” to match " in regex?

Both of the following return True: ...
2
votes
2answers
326 views

Pattern test for variables during function definition

I am having a problem with pattern testing on variables during function definition ...
6
votes
2answers
248 views

Formatting text through pattern matching

I am trying to format some text based on some patterns. ...
18
votes
2answers
1k views

How do I perform string matching and replacements?

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

The difference between 0. and 0

I have a function for which 0 is a special case: f[A___, 0, B___] := 0 But since I am doing numerics, sometimes in the course ...
12
votes
4answers
522 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 ...
5
votes
5answers
479 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. ...
17
votes
4answers
452 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: ...
7
votes
3answers
224 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 ...
3
votes
1answer
146 views

Set promotes Rule to RuleDelayed?

In mentally building a model of how Mathematica works, I naively expected Set to create a Rule in a symbol's ...