Tagged Questions

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

84 views

Better use of pattern matching in replacing elements of a list

I have a list of reals and I'm replacing those which are contained in a given interval with two different values depending on wheter the selected reals are positive or negative. I came up with this ...
64 views

Rule for derivatives

I have a differential equation of the form $$f'(x)=g(x,f(x))$$ (where $g$ is has a known explicit form, e.g. $x f(x)$) and I have an expression which contains several derivatives of $f$. I want to ...
61 views

Use cases to exclude data values over a certain threshold

I am trying to parse data output from a fluorometer. My output is basically signal intensity over time. So I have a list of 2 value lists with the first value time and the second signal e.g., ...
48 views

Replacing Non-Constant Elements from List with Pattern [closed]

I have a list list = {0, 0}, 0, {0, 0}, 0, 1}, {{0, 0}, 0, {0, 0}, 0, 2}, {{7,7}, 20, {7, 7}, 3}, {{8,8}, 30, {8, 9}, 4}} And I want to remove all elements which ...
40 views

Generate rule that replaces if string matches pattern

I have let's say a function f[x], but for x I pass a string. And I want to replace that string with a number, if that string ...
45 views

What is the function of OrderedQ in these expressions?

Could someone please let me why the output of the two expressions change with OrderedQ in place. The first expression is as follows: ...
120 views

Why the pattern match don't work as expected

When I try to figure out this problem,I write such code: ...
56 views

How to Set Priority Between Longest and Shortest [closed]

As is shown in this post's answer, it seems that though you can set priority of Longest (or Shortest) by ...
39 views

Possible bug with KeyValuePattern and Optional

I expect to get 0 on every line of output of this: ...
64 views

Collect partial derivatives and rewrite them with delta/nabla

Ultimately, I wish to get the higher generalized product rule for Laplacian. That is, we have $\Delta(fg)=f\Delta g+2\nabla f\cdot\nabla g+g\Delta f$ but what is $\Delta^n(fg)$? I am tying to guess ...
81 views

Longest and Shortest won't work in ReplaceList?

I'm trying to make a pattern that's easy to preceive by human but hard to write out by Mathematica when I came across this problem. (Check the original problem here) Let's check this simple case: I'...
117 views

How to Find out the Recurring Period by Pattern Matching?

Note: It's not hard to solve this problem AT ALL, so please, try to explain how to use PATTERN MATCHING instead of how to find out the recurring period. We can easily extract real digits from a ...
146 views

How to scope Pattern labels in rules/set?

Module[{x}, f@x_ = x; p@x_ := x; {x, x_, x_ -> x, x_ :> x} ] ?f ?p gives ...
82 views

Using Associations and Pattern matching in numerical functions possibly broken

It looks like the very young feature of pattern matching (/replacing) within Associations (http://mathematica.stackexchange.com/a/119542/6804), with or without ...
83 views

Association matching still not working perfectly?

Up until 10.4 you could not match on internal Association structure at all: http://mathematica.stackexchange.com/a/119542/6804 Now you can, but it seems flawed ...
64 views

Is pattern matching just systematic guessing and checking?

How does the pattern matcher work? how does it match this example? f[3, h[4]] /. f[x_, z_[y_]] :> {x, y, z} My guess is that the pattern matcher collects all ...
598 views

Mysterious behaviour of Pick

As everyone knows, Pick can do amazing things, The most fascinating phenomenon is that it can pick though all sorts of lists: The basic & pattern matching: <...
102 views

How does a construct like i : func[arg_] := i = an expression using arg work? [duplicate]

Occasionally I run into a Pattern that looks like i:func[arg_]:=i= an expression using arg I have two questions on this: Can ...
68 views

How to implement search patterns and take into account language statistics?

Patterns in Mathematica I struggle some with patterns while trying to extract words out of large texts. Let us say that I have a long text (Strindberg) that we can call ...
84 views

How to define a function which takes another function as input in Module

Let $f$ be a function of $n$. I would like to define a function $A$ with module which takes $f[n]$ as input. So for example: ...
213 views

Position and string patterns

I am trying to get Position work with patterns. I have the following code: ...
162 views

MatchQ[x y, (x | y) (x | y)] returns False

MatchQ[x y, (x | y) (x | y)] It returns false. Why? I want to eliminate terms like x^2, ...
95 views

How do I construct a Pattern with Alternatives and PatternSequence without getting Pattern::patvar messages?

Bug introduced 8.0 or earlier and persisting through 10.4.1 [CASE:3631078] In Mathematica 8 and 9, defining this rule: ...
186 views

Delete all strings that are prefixes of another string

I have a long list of strings and I'm trying to find a more efficient way of removing those elements that are prefixes of another: ...
114 views

Nested association values validation. POJsO handling

Sometimes it is convenient to work in your package/app with "objects" like: ...
205 views

Opposite of Alternatives in a Rule

Mathematica provides Alternatives, to match one of several patterns. In a rule, this is used as p1|p2|p3:>replacement. I ...
28 views

Pattern intersection [duplicate]

Given a list of patterns {x, y, z} I want a pattern that matches if and only if each pattern matches. If instead of getting the intersection of patterns I wanted ...
68 views

Why does Repeated not work in this case?

I think I don't understand how to use Repeated. I have a list for example list={a,b,a,b,a,b,a,a,b} I want to detect whether ...
131 views

How can one match a function in a replacement rule

I'm trying to write a delayed replacement rule that is capable of matching both the functions and their parameters, like ...
57 views

Can I use StringExpression or RegularExpression to manipulate Symbolic XML?

Please, consider an XML document containing these fields: ...
129 views

What kind of pattern should I use with StringCases? [duplicate]

Consider the string str = "ABCaDEFbcABCdefDEF" I'd like to take out all substrings surrounded by the substrings "ABC" and <...
55 views

Why is head evaluation inconsistent in pattern replacement?

I noticed the following unexpected result: In[90]:= "foo" /. x_String -> Head[x] Out[90]= Symbol Why is it that Head[x] ...
234 views

Longest common substring for multiple strings?

I'm looking for a way to get the longest common substring for multiple strings such as {home/dir1/dir2/jmoasd.txt,home/dir1/dir2/ivbnoxcihv.txt,home/dir1/dir2/siuhgiuchv.txt} should yield ...
60 views

pattern matching for third degree reciprocal equation

I am trying to create a pattern for third degree reciprocal equation. I try this a_ + b_ x_ + b_ x_^2 + a_ x^3 This matches ...
38 views

Pattern match Shortest BlankNullSequence in SequencePosition

Apologies if this has an answer somewhere but I can't find it. From the examples for SequencePosition we are shown that ...
111 views

Filtering and summing sublists with Cases

I've declared a list: list = {{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}, {a, b, c}, {e, f}, {g}, {}, {Sin[a], Cos[b]}}; now I've made an rule which add 2 pairs ...
62 views

Pattern which will transform a given expression [closed]

Find the Pattern which when applied to this expression expr = {a^2, a^b, x^4, (x - 1)^2}; yields this result: ...
43 views

Rule/Replace/Patterns [closed]

I need help with doing this question. given: Sin[x+y] Cos[x+y+z] Define using Patterns a rule (specifically a list of 2 rules ) , the two adding rules for trigonometric functions sin (a + b )...
123 views

Same input,different result? [duplicate]

Using this code,you can see,same input, but the output are different. So weird! If I copy this code here, it outputs normally. So what's the problem? Ps:I use ...
62 views

How to check if an expression contains a symbol ONLY after a given head?

I have just started programming in Mathematica and still have to get a hang of it (and rule-based programming), so I apologize if this question may seem stupid. I will explain it with a concrete ...
121 views

Using Cases to pick out entries of a SparseArray

I'm trying to use a SparseArray for some large homological algebra calculations. I'd like to find indices of a SparseArray ...
80 views

Replace pattern for exponentials

Ultimately, I want to replace every Exp[(const) w[i,j]] to T[i,j] and Exp[-(const) w[i,j]] to 1/T[i,j] in the following expression ...
82 views

How to “INJECT” with holding Evaluation, specifically into a WhenEvent

I am importing a set of differential and stochastic equations from an external package, and want to use Mathematica to analyse this. I am struggling building the set of equations that I can use with <...
91 views

How to use Except[] inside of Pick[] [duplicate]

Pick[{1, 2}, {0, 1}, Except[0]] returns {1, 2}, the first element of which I did expect to be excluded. What is the thing I ...
90 views

How to match Association[]?

The following input: Cases[{"key" -> Association[]}, HoldPattern["key" -> Association[]]] Returns {}. Why does it not ...
46 views

Replacement rule for summation with Kronecker delta

I want to perform some symbolic computations in index notation, without explicit reference to sums, so that in expressions of the type f[i,j]g[j,k] summation over <...
91 views

Matching HoldPattern expressions

My original problem is to delete all rules starts with a[1] from SubValues[a] For example, ...
13 views

How to match sum of two or more objects? [duplicate]

I am trying to match expressions that are the sum of two or more objects with a specific head. However I can not get this work properly. For example ...