Tagged Questions

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

learn more… | top users | synonyms (1)

2
votes
1answer
46 views

transformation patterns don't work with constants

I want to do the reverse of PowerExpand and convert 2*Log[x+y] to Log[(x+y)^2]. The rule: t_ Log[x_] -> Log[x^t] works for ...
5
votes
1answer
192 views

Why does this pattern for combining two lists require triple underscores (___) to work?

I understand that __ is a list of one or more elements, and that ___ is 0 or more elements, but when I try this rule with 2 ...
1
vote
2answers
110 views

Named patterns as function argument descriptors

I have a function timeToMinutes that takes a 24-hour time as a string argument (in digital clock format, e.g., "15:47") and converts it to the number of minutes ...
5
votes
2answers
277 views

Keep Ordering of Bra Ket When Printing

I would like to keep the ordering of the bra ket-print but right now this doesn't happen. Any Idea? I tried playing with HoldForm but then i could evaluate the expression inside properly ...
5
votes
0answers
189 views

Convert logical relational expression to / from disjunctive and conjunctive forms?

I would like to convert logical relational expressions in disjunctive form, e.g., $$(x \lt -1) \lor (-1 \lt x \lt +1) \lor (x \gt +1)$$ into conjunctive form, e.g., $$(x \ne -1) \land (x \ne +1)$$ ...
13
votes
3answers
283 views

Question about using a pattern in ReplacePart

I noticed that "ReplacePart" can use pattern. For example: ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {i_, i_} -> x] {{x, 0, 0}, {0, x, 0}, {0, 0, x}} So ...
11
votes
2answers
415 views

Soft-Match String Comparison

I have a list of names in which I would like to check for duplicates. However, within the list, the duplicates may not show up as exact duplicates of each other - for instance, ...
3
votes
1answer
151 views

Extending D for four-derivatives

I'm currently trying to consistently define rules for extending D[] to four-derivatives. As 'backend' I'm using the package TRACER (http://library.wolfram.com/infocenter/MathSource/2987/), which can ...
1
vote
2answers
242 views

Deep Pattern matching with repeating arguments

I have a function that accepts a single argument that is described by the following grammar: ...
2
votes
1answer
180 views

Using transformation rules to manipulate expressions

I'm trying out Mathematica as a replacement for LaTeX for typesetting math. It works very well, but I would like to take advantage of Mathematica's computer algebra capabilities as well. Specifically, ...
3
votes
1answer
143 views

Downvalues vs. Scoping for Functions

Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but ...
4
votes
2answers
159 views

How to achieve Set+Part like behaviour in custom Set function?

So I've been toiling away on my DataFrame package. I've been trying to get Set to work with it, but it seems it either can't be done (ie via ...
6
votes
2answers
257 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 ...
8
votes
3answers
318 views

Patternmatching sets

How to match set-patterns against sets? A set (in the mathematical sense) is a list of elements without repetition and order of elements does not matter. For example, we have a pattern set ...
3
votes
2answers
137 views

Get DownValues that do not involve patterns?

Say I have a function with a few definitions that also implements memoization: f[x_] := f[x] = x^2 f[x_, y_] := f[x, y] = x^2 + y^2 I then apply the function to ...
8
votes
4answers
421 views

Best way to modify values in a list of rules?

Recently I had to solve a problem similar to this: Let's say I have a list of list of rules ...
6
votes
4answers
485 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: ...
9
votes
5answers
311 views

How can I remove matching sublists from my list?

I want to use DeleteCases (or any appropriate function) to remove elements of a list, which in turn are lists of fixed size. The rule I wish to apply is that any ...
16
votes
3answers
577 views

Position of a pattern-matched part of an expression

What you have A pattern, with certain part of it labeled As an example — Label: wally ...
3
votes
1answer
113 views

Detect highest order of derivative in expression?

Is it possible to detect the highest order (or generally all orders) of derivatives in an expression? Consider eqn = D[a[x,t],t] == D[a[x,t],x,x] How would I go ...
4
votes
2answers
235 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
104 views

Pattern match any member of a Context

Let's say I have four functions: x, y, X`x, and X`y. ...
5
votes
2answers
178 views

Why rule does not work if Flat attribute?

The following works: ...
2
votes
1answer
202 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
187 views

How to systematically classify Mathematica expressions?

How to systematically classify Mathematica expressions? I can think of using Head[], Depth[], ...
9
votes
3answers
708 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
218 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 ...
20
votes
1answer
689 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 ...
3
votes
2answers
112 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
825 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
404 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
270 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
344 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 ...
8
votes
1answer
205 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 ...
3
votes
1answer
126 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
174 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
198 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
172 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
245 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
123 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
193 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 ...
17
votes
2answers
365 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 ...
5
votes
1answer
296 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
194 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
140 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
662 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
208 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: ...