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

learn more… | top users | synonyms (1)

2
votes
1answer
133 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 ...
13
votes
2answers
313 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
261 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
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: <...
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 <...
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
180 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
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'...
1
vote
3answers
251 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
171 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
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 ...
1
vote
0answers
71 views

Unexpected result when trying to match against the arguments of Times

Consider the following question: ...
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 ...
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 ...
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 ...
1
vote
1answer
67 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 \ln\int_{-\infty}^{\infty}e^{-y\left(z\right)}N\left(x-z,v\right)\,\mathrm{d}...
6
votes
1answer
104 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
81 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
192 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 ...
9
votes
3answers
213 views

How to make a ReplaceAll Rule for adjacent items in a list?

I would like to make only one pass of a list of 2D items and replace adjacent items that have matching $x$ or $y$ coordinates with a series of linearly interpolated points between the two points. My ...
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, ...
3
votes
2answers
95 views

How can I filter a permutation list depending of a pattern? [closed]

I've created a permutation list Grid[Permutations[{a, e, q, r, t, u}], Frame -> All] and only want to see rows where the 5th element is a "t". I tried to do ...
4
votes
2answers
273 views

How do Reap and Sow work in this context

I have a list of points in 3D and I need to count how many times each point is repeated. For a similar problem with a list of characters many different solutions have been proposed in this post: ...
1
vote
1answer
36 views

ReplaceAll - Position of list arguments not displayed properly [closed]

This code A[1, 2, 4, 5] /. A[a___] -> Position[{a}, 4] return {} Why this code not return 3 (a position of number 4) ? I ...
-1
votes
2answers
70 views

What would be a pattern matching only numbers that are not real?

I wanted to delete all complex numbers out of a list, and I tried the following, but it didn't work. Suggestions? Im[x] != 0
2
votes
0answers
59 views

How do I let Fourier Transform know that it is a linear operator? [duplicate]

I have the following issue with FourierTransform, Fourier transform doesn't seem to know it is a linear operator. ...
1
vote
2answers
40 views

“Nonatomic expression” error when identifying certain positions in a list [duplicate]

Please can anyone help ? I have a list of sets, and I am trying to find the positions in that list of those sets which are subsets of a given set. (Here I am ignoring the distinction between lists and ...
2
votes
1answer
59 views

Braces in pattern with Cases function

Here's my problem: I've got a JSON-formatted response and want to match some variables over the response. I'm doing it using Cases: ...
7
votes
2answers
154 views

Good coding for a tricky expansion with convolutions, limits, substitutions

I am attempting to generate a power series in $v$ as $v\rightarrow0$ for $$g(x,v)\equiv \ln\int_{-\infty}^{\infty}e^{-y\left(z\right)}N\left(x-z,v\right)\,\mathrm{d}z $$ where $y(x)\equiv\sum_{i=1}...
16
votes
3answers
287 views

Is it possible to change the order that symbol definitions are checked? (DownValues vs UpValues)

Suppose I have the function f with the following definition assigned to it: ...
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 ...
6
votes
3answers
145 views

Filtering lists by partial matches

Suppose I have the following data: ...
19
votes
2answers
326 views

Pattern does not match with Orderless head

Why does the following pattern matching not succeed? ...
6
votes
2answers
99 views

Pattern matching an expression involving Times

I'm trying to write a Mathematica function that will format expressions according to my preferences, for example displaying (5 c x)/Sqrt[2] as ...
8
votes
1answer
164 views

Why does pattern test work only with pure functions?

Why is it that pattern test works only with pure functions? Cases[Range[0, 70], _?(Divisible[#, 7] &)] Cases[Range[0, 70], x_?(Divisible[x, 7])] I ...
5
votes
1answer
78 views

Transformation rules to distinguish common variable with Subscript and OverBar $x,x^2,\bar{x},\bar{x}^2, x_p,x_p^2, \bar{x}_p, \bar{x}^2_p $

I am looking for help in writing transformation rules in which I wish to distinguish between eight variables $x,x^2,\bar{x},\bar{x}^2, x_p,x_p^2, \bar{x}_p,\bar{x}^2_p $. For each of these eight ...
1
vote
3answers
162 views

Making a replacement on the numerator and denominator of a rational number

It's isn't a big deal, but when I run the following I don't get the output I expect. After discussing this in the comments it appears it has to do with being an atomic expression. ...
0
votes
1answer
55 views

Avoiding misconversion of data with ToExpression function [closed]

I've got a list of data with the following structure which can be easily converted To expresion by some manipulation such as ...
3
votes
1answer
126 views

Specifying string patterns in a plain text

I need to extract three data, like that: (1.05, 1.37,A) from an imported list of plain texts: "... Syntax of parallax is : 'value quality [error] bibcode' value : parallax value quality : flag of ...
14
votes
2answers
693 views

How to detect loops in a vector field?

Given a vector field and I'd like to detect if there are streamlines in closed patterns. For example, if the input is a vector field like this: ...
1
vote
2answers
65 views

Substituting a list of values into a function [closed]

g[p_] := Mod[19^(-1 + p), p^2] This is my function, and the set of values I have is the first 999 prime numbers. I made a table listing the first 999 prime ...
9
votes
2answers
239 views

Pattern and Orderless function

I'm a Mathematica beginner. I am exploring the use of patterns and (only for fun) am trying to implement a permutations function. My (dirty) code is: ...