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

learn more… | top users | synonyms (1)

2
votes
0answers
60 views

Pattern Matching and overloading NonCommutativeMultiply [duplicate]

My goal right now is to implement some rules like linearity etc for noncommuting operators (arbitrary matrices etc). This is partly an exercise for me to understand MMA better so giving me a package ...
11
votes
4answers
199 views

Evaluation of Alternatives

I recently stumbled upon a few examples that suggest patterns containing Alternatives do not evaluate for individual alternatives. For example: ...
3
votes
2answers
78 views

Except[] with levelspec

I am trying to determine all the variables used in list. For This I use: DeleteDuplicates[Cases[l1,_Symbol,-1]] This is great except that ...
12
votes
2answers
302 views

why MatchQ is fast and Replace slow?

Consider a large expression, say a polynomial with 25 terms expr=Product[Unique["a"],{i,1,25}]; to which you apply the following replacement rule: ...
1
vote
0answers
91 views

Need help interpreting a small block of code [closed]

I have recently "inherited" some Mathematica scripts which I need to interpret to write in some other languages (Maple and C++). I have never used Mathematica before so I have been learning on the job....
3
votes
1answer
125 views

The basic primitive recursive functions

How can the basic primitive recursive functions be expressed in the Wolfram Language? Apparently here's an example for primitive recursion: ...
20
votes
3answers
608 views

Detecting components in timeseries

I'm looking at this sequence of values: I'd like to detect the points where the center of the time-series shifts (around x=1000 and x=2000). Many of the transforms and smoothing methods I have ...
22
votes
4answers
342 views

Is there a GraphicsPrimitiveQ (or a complete list of Heads of graphics primitives)?

For various reasons related to workflow associated with creating figures for journals, I am creating functions that will accept graphics primitives and a set of options and spit out a ...
8
votes
2answers
121 views

Why is a longer pattern matched rather than a short pattern?

I was reading the Mathematica V5 Book and am a bit perplexed on the behavior of a particular pattern matching example where it seems to be choosing a longer pattern rather than a shorter pattern. The ...
4
votes
2answers
57 views

Is it possible to match a sequence with multiple heads without using Condition

Suppose I have: expr = Times[2, a[x,r], a[y,1], c[z,0,0], d[w,m]]; If I want to match a sequence of expressions with the same head ...
3
votes
1answer
64 views

How to collect elements in one list with the constraints provided in a second list

I wish to collect the items in the first list provided the constraints in the second list. For example in the lists shown below the first list shows a bunch of strings and the second list shows the ...
15
votes
3answers
432 views

Highlighting pattern strings in a large text

I would like to apply Style to strings that are matched, for example, with one or more specified patterns or regular expressions, a string or a list of both, ...
4
votes
2answers
154 views

What kind of pattern should be used

I was trying to make a simple question but made it trivial. So the original setting is as follows: expr = {{a}, b, {{c}, {e}}, d, {}} I need that all lists are ...
3
votes
1answer
80 views

How to prevent fractions in polynomial quotients?

(This was a hard question to give a succinct title to, so feel free to edit it.) When I divide polynomials, I would like Mathematica to NOT create negative powers of variables. For example: ...
1
vote
1answer
50 views

Pattern that matches numbers in the $\epsilon$-neighborhood of a given number?

I dfined the following function to create a pattern that matches the $\epsilon$-neighborhood of a given number. ep[x_, e_] := _?(x - e <= # <= x + e &) ...
2
votes
2answers
63 views

Remove an element of a list [duplicate]

I want to check if any element of a list contains Missing["NotAvailable"] If so I want to delete the entire list. If I am correct I need to have some ...
56
votes
10answers
2k views

Injecting a sequence of expressions into a held expression

Consider the following toy example: Hold[{1, 2, x}] /. x -> Sequence[3, 4] It will give Hold[{1, 2, Sequence[3, 4]}] ...
1
vote
0answers
55 views

Pattern placeholder for zero or more matching items in list [closed]

I'm working through the exercises in Stephen Wolfram's book "An Elementary Introduction to the Wolfram Language." In Chapter 32 on "Patterns", Exercise 32.1 is "Find lists beginning with 1 and ending ...
5
votes
2answers
155 views

How to name a repeated pattern sequence inside Plus?

Here is a sample expression that contains a bunch of f and some other stuff: ...
12
votes
2answers
195 views

Optional throws error when called from a package. Workaround?

Bug introduced in 8.0 or earlier and persisting through 10.3 or later. Case number: 305932. In the front-end notebook (Mathematica 9.0 and 10.2): ...
5
votes
2answers
172 views

Why this upvalue doesn't escape from Sum?

Probably a hard question, but I decide to cry out loud :). This is actually another problem I encountered when answering this question. Consider the following transform rule stored as an upvalue: <...
7
votes
4answers
105 views

Problem with handling a factor of $1$ in pattern matching

I am trying to implement the creation and annihilation operators $\hat{a}$ and $\hat{a}^{\dagger}$ of quantum mechanics with the following code, but alas, there is a problem with the pattern matching ...
4
votes
2answers
85 views

How can I extract something from an expression and assign it without changing the expression?

I am researching how to extract a certain value from an expression by using a pattern mechanism without actually changing the original expression. The paragraph above could be very ambiguous, so here ...
2
votes
1answer
72 views
3
votes
1answer
90 views

How to use patterns to return non-overlapping sequences of consecutive elements from an array

Let me first describe the data structure I'm looking at before describing my question. I have an array of nx3 elements with a data-generating process such that each row has the format: {previous row'...
3
votes
1answer
91 views

How to use `Shortest` for pattern matching? [duplicate]

For the string ((!@#)), the shortest match for the pattern "("~~__~~")" should be "(!@#)", ...
8
votes
1answer
142 views

How to take all sub-strings in a string that are combinations of specified characters only

"tgtaaaaacaagaaaaacatggaatgtataatactataaataaaaaaagtaggaagaagaaccaaaaca\ aagggcgttttaaagagagacgaat" based on the above string (a DNA sequence) I wish to extract ...
2
votes
1answer
135 views

Pattern condition involving multiple arguments to a function

I can define conditions on individual arguments to a function: f[a_List/;condition(a),b_List/;condition(b)]:=(...) I am wondering if there is a way of declaring ...
3
votes
1answer
90 views

How to inject segments of code into functions defined by SetDelayed [duplicate]

I need to define a set of functions that are written in terms of common but lengthy expressions. To effect this, I want to inject abbreviations for such expressions into the RHS of ...
3
votes
1answer
56 views

MatchQ’ing Except[…] and Except[…]

A PostScriptForm1 for Mathematica must recurse over the likes of Plus[…]. Output should be as follows: 9+n: either ...
1
vote
1answer
43 views

Pattern matching for product of two terms fails if one factor is 1

I have a long set of replacement rules that I utilize to solve some longish integrals over trigonometric functions. Just look at this snippet (from this answer): ...
2
votes
1answer
35 views

Evaluation of BlankNullSequence inside StringReplace

I would like to replace the occurrence of "a(x)" and "b(x)" in a list with "a(x-na)" and <...
19
votes
2answers
361 views

What does f_[whoCalled]^:=f really mean?

I came across the following post by John Doty in this Google Groups discussion: On Friday, January 11, 2013 8:23:16 PM UTC-7, amzoti wrote: It is clear the Mathematica uses Lisp as one ...
6
votes
3answers
355 views

Match a sequence of integers

I would to match a pattern p1 = 0,0,1,0,0 to the elements in listA. The elements are a boolean sequence that may look like this ...
7
votes
3answers
134 views

How can I know if a pattern corresponds to a string pattern?

What would be the best way to detect if a pattern corresponds to a string pattern ? An invalid string pattern p would lead to an error in ...
1
vote
1answer
39 views

Please explain this pattern/Message related issue and help with finding workaround

I attempted to clean up my code and also add messages to functions if they are called with wrong arguments. This introduced some error which I tracked down to the following: ...
1
vote
1answer
62 views

Count and NMinimize don't seem to work together

Initially I write down a 2x2 unitary (Uni) , make a tensor product 4 times (LocalUni) and apply it to an initial numerical matrix Rho, to obtain RhoLU, that depends on 12 parameters. This is my code: ...
2
votes
0answers
66 views

Good coding for integral substituions

This question is aimed at improving both my coding and my understanding of MMa's capabilities. A lot of my code is still hacks and workarounds for things I don't know how to do properly. I want to ...
4
votes
1answer
181 views

Using Mathematica's Position Function

I have a list a = {-1, 2/3, -2 - 4 I, -2 + 7 I, 2 I} and when I use the Position[a, I] it returns ...
1
vote
3answers
253 views

Command like “find” in Matlab [closed]

Is there a command working exactly like Matlab's find? I know that Pick might work, but I just want to give a logic condition ...
10
votes
1answer
172 views

How to write this pattern in Pick function

Suppose I have a list called mask composed of 1,2,3,...n. n is different in different situation. Let me takes n=3 for demonstration ...
10
votes
1answer
243 views

Question about Rules and Pattern (The operator patt/;test)

Maybe this question is so trivial but it has confused me. I'm studying the What the @#%^&*?! do all those funny signs mean? and in the Rules and patterns under ...
5
votes
2answers
78 views

Why does this pattern fail to match?

I'd like to replace fractions with Missing[] as the denominator with Missing[]. This pattern in ...
3
votes
3answers
54 views

How to use pattern matching to apply a function to specific elements of a matrix

I have a 3x3 matrix testweightedgraph = {{{}, {p, q, pq, pq}, {}}, {{p, q, pq, pq}, {q}, {}}, {{}, {}, {}}}; whose elements are (possibly empty) lists. I would ...
1
vote
1answer
105 views

How to write or extend operators like $*\!\!*$

I'm stuck on trying to define simplification rules for NonCommutativeMultiply, much like ExpandNCM found in Documentation Center, under Applications. For example, I'd like ...
3
votes
1answer
126 views

Using Select with Alternatives

I want to select the elements of a list like: ...
2
votes
1answer
172 views

Given an expression using kets, such as '|2> + 4|3>+…,' find the largest ket index in a ket?

I have some expressions containing Kets, like |1> + Sqrt[6] |3> where the kets are created using [esc]ket[esc]. How can I find the largest number inside a ...
1
vote
1answer
68 views

How to bound a recursive Derivative definition?

I need to convert derivatives of g[x,v] wrt v into derivatives wrt x using this relation: $$\frac{dg[x,v]}{dv}=\frac{1}{2}\frac{d^{2}g}{dx^{2}}+\frac{1}{2}\left(\frac{dg}{dx}\right)^{2}$$ This code ...
2
votes
2answers
93 views

Strange failure of Series and Derivative

I just spend three hours and posted two Questions trying to figure something out, and it turned out all the confusion was caused by this mysterious quirk. I want to expand g[x,v] in v at v=0, using ...