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

learn more… | top users | synonyms (1)

2
votes
1answer
55 views

Removing characters from non-strings

I have a large list of lists that contains mixed data, some real, some strings. The lists are of various lengths and depths. Some of the real data values have an asterisk appended to them, e.g.: ...
3
votes
5answers
432 views

Determine if all the zeros in a list occur at the end

I have a collection of (short) lists of nonnegative integers. For each, I would like a predicate that decides if all of the zeros in the list appear at the end. For example, ...
4
votes
1answer
81 views

Compare tree structures in mathematica

Since Mathematica expression is basically a tree structure, I would like to perform tree structure comparison with certain restrictions, for instance that the identity of nodes on the same level be ...
8
votes
2answers
89 views

Pattern matching with Complex. Feature or a bug?

I have run into some strange behavior while doing some pattern matching. First, this works as expected: Exp[2 I u x] /. Exp[Complex[0, a_] u x] :> a (* 2 *) ...
-1
votes
3answers
56 views

How to create a general expression for an integer derivative?

I need MMa to return a general expression for the jth derivative wrt v of v^n. This didn't work: ...
0
votes
1answer
43 views

How to use pattern matching for integral identities?

I have an unknown pdf f[x], and I need to express integrals involving f[x] as expansions of the moments of f[x]. For example: ...
4
votes
1answer
155 views

How can I construct a recursive pattern guard?

I am modelling binary trees in Wolfram Language by writing them as three-element lists, like so: binTree = {nodeVal, leftChild, rightChild}. It does not yet matter ...
5
votes
5answers
110 views

Isolating cross terms

I have a set of expressions that contains terms like $\frac{1}{(x + a + b)(c+d)}$. I would like to simplify the denominator so that products of $a,b,c,d$ are dropped, but $x c$ and $x d$ are kept. ...
0
votes
2answers
37 views

FilterRules with pattern for name [duplicate]

I have a list of rules and I want to filter it using a pattern for the initial string representing the name of the rule. So that in the example below it filters linearBool even if it is called ...
2
votes
1answer
101 views

Odd behavior matching powers of pi

I have a long list of generated values from which I want to remove "weird" ones like Sin[Sin[1]] and Pi^2/4. However, trying to ...
3
votes
1answer
62 views

Output function control and manipulation for efficient computation

I have taken note of a similar post related to modifying function parameters. I tried to adapt Kuba's solution to this post but I can't get it to work. I will illustrate my problem using a similar ...
3
votes
1answer
51 views

Pattern matching multiple symbols names and powers

Seemingly a relatively simple problem, I can't seem to find a solution... I want to build a pattern that will match any symbol whose name starts with V, and is elevated at any power. Something on the ...
1
vote
1answer
42 views

How to modify the output result of an evaluation

For ease of notation, I have defined a function, $f$, that defines the permutation of Bosonic particles such that it has the following properties (the form of $f$ is irrelavent for this question): ...
7
votes
3answers
198 views

Why these ways of defining a function with variable number of arguments fail?

First: I know about the correct way to define a function with variable number of arguments: Clear[f] f[s__] := Plus[s] f[1, 2, 3, 4] (* 10 *) BUT I ...
0
votes
2answers
52 views

Keep exponents separately

I have the following expressions $g^{ab+cd+...}$ or in the full form Power[g,Plus[Times[a,b],Times[c,d],...]] How to convert this expression into ...
4
votes
3answers
64 views

Extracting subscripts and superscripts

Suppose I have several types of expressions (in some random order) of the form Power[Subscript[g, a], μ] which is $g_a{}^\mu$ ...
9
votes
1answer
314 views

How to visualize pattern matching process?

I want a function that does the same job like ReplaceRepeated, and can make the matching process visible. I searched the Internet but didn't find any existing code ...
3
votes
3answers
159 views

How to search for patterns to find the positions of prime numbers

I have a summation which yields a prime number at each location there is a 2, and I do not know how to search for the 2's. ...
7
votes
5answers
333 views

Calculating weird numbers

A weird number is a number such that the sum of the proper divisors (divisors including 1 but not itself) of the number is greater than the number, but no subset of these divisors sums to to ...
4
votes
3answers
173 views

How can I improve NumberString?

I would like to test strings to see if they represent numbers. This should include number representations with precision marks and scientific notation. NumberString ...
10
votes
2answers
491 views

Is there a way to identify a symbolic fraction?

Is there a way to use Head to detect a symbolic fraction? In particular I find, Head[a/b] Head[1/5] Times Rational ...
4
votes
4answers
134 views

Cases with Alternatives failed

Consider this list of rules rules = {a -> 1, b -> 2, c -> 3} I'd like to fish some of the rules out of such a list using ...
4
votes
4answers
123 views

Counting subsequences (i.e., patterns) within a list?

I would like to count the number of subsequences in list. Say I have data given by: data = {1, 1, 1, 2, 1, 2, 2, 2, 1, 2, 1}; I would like to count the number ...
3
votes
4answers
103 views
2
votes
3answers
92 views

Testing for a vector of reals [closed]

Suppose I define: RealVector[expr_]:=VectorQ[expr,NumberQ[#]&&Head[#]===Real)]; Then: ...
1
vote
1answer
66 views

How to use a list of rules as a data set

I have a file of models that I have saved that I want to be able to call in another program to be solved with NDSolve. Can I use ...
0
votes
1answer
51 views

Replacement of terms/Pattern matching involving products of derivatives of a function

In delving into Ramanujan summation, I'm trying to get a hold of the relations of the form $$\sum_{n=0}^\infty f(n)=\dfrac{h\frac{d}{dx}}{{\mathrm{e}^{h\frac{d}{dx}}}-1}\int_{0}^\infty ...
10
votes
0answers
88 views

Unexpected pattern matching behaviour: PatternSequence vs. Optional

Consider the following pattern-matching expressions: ...
5
votes
5answers
184 views
0
votes
0answers
50 views

“Meta-matching” patterns [duplicate]

The title refers to the general problem of writing patterns to match pattern-containing expressions (hence "meta-match"). Here's an example. In the following rules ...
2
votes
1answer
48 views

What's the pattern that will match a head anywhere in an expression

I'm looking for the pattern patt[head] that will match any expression expr in which head occurs as a head anywhere within expr. ...
3
votes
2answers
125 views

Replace expression, but only “outside” of a function [duplicate]

I have a horrid expression, which is basically a sum of exponents with coefficients. Here's a taste: What I want to do is to replace ks with another expression, ...
2
votes
2answers
96 views

Problem using Position applied on matrix with Infinity elements

Am I doing something wrong here? ...
10
votes
1answer
207 views

Orderless pattern matching

The MMA docs state that "In matching patterns with Orderless functions, all possible orders of arguments are tried". Why then does the following not work? ...
0
votes
2answers
35 views

Matching exponents using Cases[]

I have a question regarding pattern matching. Can anyone explain the results i get ? Cases[Table[x^n, {n, 1, 10}], a_^1] Which gives the following result. {x, ...
1
vote
2answers
168 views

Pattern Matching in Lists

I have a UN voting dataset – taken from a CSV file – where each row is in following format: ...
1
vote
2answers
105 views

Pattern Match and Replace Expression involving Pure Function

Let's say I have an expression that in held form contains a pointer to a function. expr = Hold[2*{"FUNCTION1"} + 5]; ...
2
votes
1answer
37 views

Default value behaves differently when used in Function and when used with Cases

Consider this example: Cases[{4, x, 5 a , 6 x}, x_.*a_ :> x] (*{1, 1, 5, 6}*) It is clear that the _. took 1 as a ...
7
votes
1answer
128 views

LetterCharacter for Cyrillic symbols

Why does the code StringMatchQ["ы", LetterCharacter] return False? Probably because: ...
3
votes
2answers
83 views

Power[__] Pattern works like Level

I came across this case: ...
2
votes
0answers
54 views

pattern matching and applying a function, not working as expected [duplicate]

Consider the following code G[x, y] /. a_[b__] -> Total[a @@@ Permutations[List[b]]] I expect it to give G[x, y]+G[y,x] ...
0
votes
1answer
71 views

How to ensure efficient order of operations for rules, slots, substitutions?

I need to calculate several functionals Q[[t]] of a pdf f[x]. ...
2
votes
1answer
115 views

How to use slots and patterns to reduce repetitive calculation?

This is kind of complicated... How can we make MMa calculate values of a function before doing multiple pattern or slot substitutions? Answers to similar questions didn't cover the case where the ...
10
votes
2answers
145 views

Unexpected result from StringReplace

The following works as expected. StringReplace["xxxyxz", "x" ~~ _ -> "ww"] "wwwwww" And so does this. ...
2
votes
0answers
99 views

Is this a bug in NCAlgebra library?

In course of my work with NCAlgebra package for Mathematica, http://math.ucsd.edu/~ncalg/ , I encountered unexpected behavior of replacements. I am using ...
1
vote
1answer
45 views

Pattern matching functions of variables

I am trying work with a general operator z[] and later plug in candidate forms for z[]. But pattern matching turns the inside z[] into a constant and can no longer perform operations on it: ...
2
votes
3answers
221 views

Mathematica won't plot the a function that is solved from NDSolve

I have used NDSolve to solve for a function (an angle of a triple pendulum), and now I wish to plot the derivative of that function (the angular velocity). ...
1
vote
1answer
121 views

Finding the index of (x,y) pairs in a list

I have a large list of $(x,y)$ pairs, and I want to find the index of a particular pair in order to extract some data from another array. By way of an example, ...
5
votes
3answers
401 views

Camel hump string split

Given System names of 2 or more characters: systemNames = Names["System`*"] // Select[StringLength[#] > 1 &] ; Is ...