Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.
4
votes
2answers
158 views
Pattern matching repeated structures with named subexpression components
I am trying to write a function with a pattern that tests its arguments for a particular structure. But in that pattern I'd like to name a subpattern to make it easier to exract and operate on.
The ...
3
votes
2answers
80 views
5
votes
2answers
126 views
2
votes
1answer
97 views
Convert boolean test function to pattern?
What is a "reverse" to MatchQ? MatchQ converts pattern into boolean function. So how to convert back?
Suppose I want to use ...
-1
votes
3answers
158 views
How to systematically classify Mathematica expressions?
How to systematically classify Mathematica expressions? I can think of using Head[], Depth[], ...
7
votes
3answers
311 views
Pattern matching a pattern with patterns
Confusing title, I know. But the question is, if we have two patterns which have the same general structure but different names used in the patterns and different names:
...
6
votes
2answers
131 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 ...
18
votes
1answer
223 views
replacement rules from a pattern and a matching expression
(This seems to be a very basic necessity in a language having pattern-matching capabilities such as Mathematica, yet I struggled for many hours to find a common solution to this problem.)
Suppose ...
2
votes
1answer
93 views
3
votes
2answers
79 views
Pattern matching not going over a list when using default values
if I execute the following command in Mathematica:
{a,a^2,a^3}/.a^(n_:1)->b^(2 n)
Mathematica returns:
{b^2,b^4,b^6}
...
1
vote
3answers
223 views
How do I combine the data from two tables according to a rule of my own devising?
I have two matrices a = m x 5 and b =m x 5 (m is large, say 1,000) which are already sorted as below:
How to generate a new matrix c = m x 5 which consists:
Column 1: exact replica of Column 1 ...
-1
votes
2answers
145 views
Correct way to compare arrays and do conditional evaluations
I would like to compare two arrays a=:
and b=
and get:
using:
(b/.(b_?Positive->a+b))//MatrixForm
but this doesn't seem to work? Can ...
1
vote
2answers
212 views
How to generate patterns by a list of integers
I have a list of integers {3,4,2,2}.
This list will give patterns {x_,_,y_,y_,_,_,z_,z_,w_,w_,x_} and ...
13
votes
3answers
300 views
Make mathematica treat $e_i^2$ as numeric
With NumericQ[symbol] = True, I can declare that a symbol is numeric. I want the expressions matching: $$e_{\text{i$\_$}?\text{IntegerQ}}^2$$ to be treated as ...
7
votes
1answer
113 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 ...
0
votes
1answer
77 views
Why does “Pattern(1,0)[w,_]” show up in the resulting expression of a differentiation? [closed]
I would like to differentiate the function s, and be able to evaluate it for different w afterwards. I tried the following:
...
3
votes
1answer
111 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
97 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 ...
1
vote
1answer
131 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.
...
4
votes
2answers
151 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:
...
3
votes
1answer
168 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
99 views
Applying several patterns to `Split`
Please consider the following:
Split[data, patterns &]
patterns=Not[#=="a"]&&Not[#=="b"]&&...&&Not[#=="blabla"];
How can I define ...
8
votes
2answers
117 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
...
12
votes
2answers
196 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
219 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 ...
4
votes
1answer
158 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
0answers
98 views
How to collect terms of a given type (with subscript and power) in an expression?
Suppose that I have an expression:
...
1
vote
1answer
53 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
162 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.
...
1
vote
0answers
55 views
Evaluation in mapping constructs [closed]
I'm trying to use ReplacePart inside of Map, where the address of the element being replaced is the domain of the map, like this:
...
8
votes
3answers
478 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
162 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 ...
4
votes
4answers
192 views
How to pick out the element in a list of lists which satisfies a particular condition?
I have got a list:
...
18
votes
4answers
432 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
209 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 ...
8
votes
2answers
256 views
26
votes
6answers
946 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
168 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
193 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 ...
7
votes
2answers
250 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 ...
10
votes
5answers
710 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
256 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
125 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
297 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
242 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 (...
5
votes
2answers
166 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 ...
6
votes
3answers
284 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? ...
4
votes
3answers
178 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
149 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 ...
5
votes
3answers
204 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 ...

