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

learn more… | top users | synonyms (1)

4
votes
1answer
38 views

help with string patterns and RemoveCellTags

I am building a table of contents using AuthorTools. I used it to create cell tags so the TOC can make hyperlinks, but then I decided I wanted to change them. But I am having trouble removing the ...
2
votes
0answers
59 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 ...
12
votes
2answers
288 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: ...
11
votes
4answers
191 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
76 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 ...
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 ...
3
votes
1answer
118 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
579 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 ...
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 ...
8
votes
2answers
120 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 ...
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 ...
4
votes
2answers
151 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 ...
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
58 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 ...
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
0answers
54 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
150 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: ...
7
votes
4answers
100 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 ...
11
votes
2answers
191 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): ...
4
votes
2answers
84 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
71 views

Replacement producing unexpected result

If I use a replacement rule like ...
3
votes
1answer
87 views

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

For the string ((!@#)), the shortest match for the pattern "("~~__~~")" should be "(!@#)", ...
8
votes
1answer
140 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
125 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
88 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
55 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 ...
13
votes
2answers
304 views

Named patterns in Except

Bug introduced in 8 or earlier and fixed in 10.4.0 Bug isn't present in version 5.2 I don't think I quite understand how Except works. I want to define $f(a,b) ...
19
votes
1answer
258 views

What is the difference between defining a function and specifying the type of argument, versus applying a test to that argument?

Say I want to create a function that evaluates differently based on what type of argument is given. I've found two ways of doing this, ...
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): ...
5
votes
2answers
163 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: ...
2
votes
1answer
34 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 ...
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
60 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
65 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
172 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 ...
3
votes
1answer
84 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 ...
1
vote
3answers
247 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
162 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 ...
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
101 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 ...
1
vote
0answers
70 views

Unexpected result when trying to match against the arguments of Times

Consider the following question: ...
3
votes
1answer
125 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 ...
2
votes
2answers
89 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 ...
1
vote
1answer
67 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 ...
1
vote
1answer
66 views

How to use PadeApproximant with a defined Derivative?

I need to generate a Pade approximant expansion in v of the following: $$g(x,v)\equiv ...
6
votes
1answer
101 views

Find subsequence A in sequence B A007908 - OEIS

I have sequence A as A007908 - OEIS and pattern B which is a sub-sequence of that sequence A. I need to find index of first occurrence of the given pattern. For example pattern ..1010... I've looked ...
1
vote
1answer
80 views

Can I map a function over a sequence of arguments without making the arguments into a list first?

I would like to set evenFunction[f_][a_, b_, c_, ...] = f[Abs[a], Abs[b], Abs[c], ...] I have come up with two ways to do this so far. Use pure functions ...
3
votes
2answers
188 views

Consecutive identical integer members of a list

Can you help me to find and count sub-sequences of consecutive, identical integer members of a long list (at least 1000 members)? By sub-sequences I mean runs like ...