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

learn more… | top users | synonyms (1)

3
votes
0answers
25 views

Held keys in associations

Jacob Akkerboom commented: Note that PositionIndex does work correctly with held expressions, whereas this is a bit painful to implement using ...
8
votes
1answer
268 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 ...
13
votes
1answer
88 views

Report: IgnoringInactive: a bug?

Bug introduced in 10.0.0 and fixed in 10.0.1 Just to report. IgnoringInactiveshould work with Cases. In agreement with the ...
0
votes
0answers
109 views

Conceptual understanding of patterns [closed]

Anyone who uses Mathematica's patterns cannot be left unimpressed by its power. To gain an understanding on how it might be working internally, I thought about the process and came up in fact with ...
1
vote
1answer
82 views

Function returned an unexpected list

I was trying out a question in Wellin's Programming with Mathematica that says the following. Question: Given a set of points in the plane (or 3-space), find the maximum distance between any pair of ...
11
votes
4answers
463 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 ...
1
vote
0answers
36 views

How to pattern match an association? [duplicate]

I have the following: Clear[a, b]; a = {{1, 2, 3}, {4, 2, 5}, {6, 7, 8}}; b = GroupBy[a, #[[2]] &]; b whose output is: ...
0
votes
2answers
75 views

Pick bug: level 0 matching

A previous discussion of this issue (Is there a bug in Pick?) accepted that matching at level 0 is not a bug in Pick. Was that correct? I am not yet persuaded. ...
2
votes
0answers
25 views

How to syntax check that all instance of symbol must be contained in Subscript[]

I have a function f[expr] that is only allowed to take an expression expr that is a polynomial in ...
9
votes
4answers
222 views

Recognizing special cases of a defined function for permuted arguments

I have a number of ugly ways to implement the following pattern recognition task, but I'm looking for something elegant to keep my notebook tidy. I need to define a function of six variables ...
7
votes
5answers
334 views

Splitting strings at lower case characters and numbers

I'm trying to split the strings of chemicals into their elements and numbers. See this example "Fe3O4" will be split into ...
6
votes
5answers
372 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 ...
0
votes
0answers
43 views

On the order of rules specification [duplicate]

I wanted to understand how Mathematica parses function definitions in rules-based programming, so I started with the simple code: ...
6
votes
5answers
287 views

Find patterns in a list

again, I have a list like this: ...
10
votes
4answers
288 views

What is the correct way to use specific rule for specific pattern match when parsing an expression?

update: Thanks for the answers below. But I noticed they do not handle the following special cases: lis=1 and lis=z , ...
0
votes
0answers
56 views

Making a list of terms ordered by product order

I have some expressions f,g,h that are elements of a symmetric tensor algebra. The expressions are generated by a basis ...
1
vote
3answers
72 views

Store matched variables

Edit: I am matching and replacing a particular expression. At the same time... I want to store matched variables for later use. I got it to work with messed up code involving ...
13
votes
1answer
327 views

Quick way to use conditioned patterns when defining multi-argument function?

I have a simple function that is supposed to only accept numeric values (i.e. complex/real numbers and constant symbols e.g. Pi, E). $$f(a,b,c)=a+b+c$$ Edit: I should have chosen a less simple ...
0
votes
3answers
61 views

Unexpected behavior of rule-replacing with string concatenation [closed]

Here's the code. Clear[x,y,z]; {x,y,z} /. x_ :> ("^_^" <> ToString[x]) I get the result ^_^{x, y, z}, which is not ...
2
votes
1answer
89 views

Extracting rational coefficients from an irrational sum [closed]

I have a function that returns results that always can be represented as $p+q\,\pi^2+r\ln2+s\ln^22+t\sqrt2$ where $p,q,r,s,t\in\mathbb Q$. But the actual Mathematica expression returned not always in ...
4
votes
1answer
111 views

What would be a pattern for a general row of an identity matrix?

I know this is a novice question but I would like a pattern for a list containing zero or more zeros followed by exactly one 1, followed by zero or more zeros. Something like: {0___,1,0___} But ...
2
votes
3answers
78 views

Finding the maximum order derivative of an expression

I have an expression that is the the sum and product of undefined derivatives and their derivatives. e.g. fg+ Dt[f,t]g +Dt[g,{t,2}]f+ What I would like to find ...
17
votes
2answers
338 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 ...
6
votes
3answers
187 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 ...
1
vote
2answers
45 views

How do I format expressions so that replacement rules correctly apply to exponents? [duplicate]

I have a series of rules that I would like to apply to a series of expressions with variables, but cannot get Mathematica to correctly recognize that these rules should apply when my variables have ...
1
vote
4answers
163 views

Need an efficient way to do this list manipulation

Lets say i have two lists with me as given below: l1 = {4, 6, 8, 9, 10, 12}; l2 = {2, 3, 4, 5, 6, 7}; Now i have a function S2P(details irrelevant) which takes ...
3
votes
2answers
159 views

Using ReplaceAll to replace a head

Why would this work to replace the head b with newHead: ...
8
votes
1answer
89 views

How to get a list of all expressions in a notebook matching a certain pattern?

Suppose I have a Mathematica notebook saved to a certain location on disk. Given a pattern I need to get the list of all expressions matching this pattern that appear in input, output or print cells ...
0
votes
0answers
39 views

How to correctly use PatternTest (?) [duplicate]

This works fine: {3, -5, 2, 7, -6, 3} /. x_ /; x < 0 :> 0 (*{3, 0, 2, 7, 0, 3}*) and this works fine: ...
0
votes
1answer
35 views

Replace all instances of Integrate by NIntegrate and add a Method option

I have an analytic expression which involves several integrals Integrate[f[x], {x, lb, ub}] where the functions f and bounds ...
3
votes
1answer
150 views

Covariant derivative for symbolic tensors

I want to define a "prefix" (D_i) covariant derivative operator CD[] for symbolic tensors in form of a function, i.e. for ...
14
votes
1answer
227 views

MatchQ-ing Associations (MMA 10)

Consider {x->1} which has FullForm List[Rule[x, 1]] and, therefore, ...
2
votes
1answer
80 views

How to efficiently process pasted content from PDF

I had selected a matrix in a PDF file, and paste it onto my MMA notebook. Below is the exact copy-pasted content: ...
4
votes
4answers
426 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 ...
0
votes
0answers
27 views

Problem with replacing a head with another head using pattern [duplicate]

Why would this work Times[a, b, c, d] /. Times :> List (* {a, b, c, d} *) but this doesn't work (the Times still remains ...
3
votes
2answers
160 views

Why wouldn't ReplaceAll (/.) work with this list of number? [closed]

The answer to this might be simple, but why would ReplaceAll return such a weird result when replacing a number in a list of numbers: ...
3
votes
1answer
57 views

Need help in building a pattern [duplicate]

Suppose I have the following list: list = {w, r, {f, first, t, y}, {a, y}, last, {k, r, first, e}, m, e, last, r, t} I am expecting this ...
10
votes
1answer
141 views

Why doesn't repeated PatternSequence work with explicit pattern names?

Why would this work: Clear[f] f[a : PatternSequence[b_, c_]] := {a}; f[1, 2] (* {1, 2} *) and this also works: ...
6
votes
3answers
170 views

Strange behavior of MemberQ, Position

After importing an array of latitudes and longitudes and trying to find the position of some latitudes I was encountered with an unexpected problem since certain values could not be found for some ...
-1
votes
3answers
83 views

How do i import a list of files, choosing the name

I have this list of files, i'm using FileNames[{"*.txt"}, NotebookDirectory[]] to import it, but i just want the ones without inelastic on the name. How can i select just by the name? Thanks ...
4
votes
2answers
135 views

Get part of lousy data

I want to get part of large data. some part of this data is as follows: ...
1
vote
2answers
89 views

Cases do not return all expected result

I have the following line code: Cases[g[h, r, t, b, m], x_[__, y_, __] -> y, {0, -1}] I am expecting the results to be ...
3
votes
1answer
86 views

Why does pattern test not work in this case to restrict the function parameters?

I was looking at this Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across ...
0
votes
1answer
76 views

How can I represent this automorphism of Q(i,a)?

Let $a$ represent some real irrational number. I am trying to perform computations with an automorphism $f:\mathbb{Q}(i,a)\to\mathbb{Q}(i,a)$ which fixes $\mathbb{Q}(i)$ pointwise and maps $a$ to $i ...
4
votes
3answers
418 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 ...
3
votes
2answers
105 views

What is the correct way to pattern match nested lists?

I have the following function: f[lst_List /; MatchQ[lst, SOME PATTERN HERE]] := ({#[[1]] + #[[2]], #[[1]] - #[[2]]} &) /@ lst; I want to match lists that ...
4
votes
2answers
169 views

How to test whether an expression is a valid variable?

I would like to test whether an argument is a valid variable for functions like Solve and DSolve. For instance several ...
1
vote
2answers
56 views

Pattern matching to head with HoldFirst

I want to define a function that only operates on expressions with a head from a given list, such as f[expr:(_And|_Or)] := Print["LHS is ", expr[[1]]]; This of ...