How Do You Count Opposite Sign Pairs in Mathematica?

  • Mathematica
  • Thread starter ascky
  • Start date
  • Tags
    Mathematica
In summary, to count the number of neighbor pairs of opposite sign in a list of 1's and -1's, you can use the ReplaceList function with a pattern of {pre___,1,-1,___}|{pre___,-1,1,___} and then wrap a Length function around the output to get the total number of sign flips. This is not an intuitive approach and other methods like MatchQ, Position, and Count may not work.
  • #1
ascky
51
0
So I want to be able to count the number of neighbour pairs of opposite sign in a list of 1's and -1's (eg. 1 for {1,1,1,-1,-1} and 3 for {1,-1,1,-1,-1}), but I can't work out the pattern matching syntax in Mathematica even after reading the documentation. I was thinking to use the Count function. Anybody able to help, please?
 
Physics news on Phys.org
  • #2
Like so many things in Mathematica, I don't know how you would ever stumble onto this on your own.

This will tell you the positions where the sign flip happens.

In[1]:= ReplaceList[{1,1,1,-1,-1}, {pre___, 1,-1,___}|{pre___,-1,1,___} :>Length[{pre}]+1]

Out[1]= {3}

In[2]:= ReplaceList[{1,-1,1,-1,-1}, {pre___,1,-1,___}|{pre___,-1,1,___} :>Length[{pre}]+1]

Out[2]= {1,3,2}

This trick courtesy http://mathematica.stackexchange.com/questions/941/finding-a-subsequence-in-a-list among other places

And if you wrap a Length[] around those it will tell you how many sign flips.

In[3]:= Length[ReplaceList[{1,-1,1,-1,-1}, {pre___,1,-1,___}|{pre___,-1,1,___} :>Length[{pre}]+1]]

Out[3]= 3

When would you think that the way to find how many of something you have is to begin by destroying your data?
MatchQ doesn't work, Position doesn't work, Count doesn't work, not as far as I've ever been able to find.
 
Last edited:
  • #3
How unintuitive! Thanks for that Bill, I wouldn't have figured it out on my own.
 

1. What is pattern matching in Mathematica?

Pattern matching in Mathematica is a powerful tool for finding and manipulating data in expressions. It allows you to search for specific patterns or structures within an expression and then perform operations on those patterns.

2. How does Mathematica handle pattern matching?

Mathematica uses a sophisticated algorithm to match patterns in expressions. It first breaks down the expression into smaller units and then compares each unit to the pattern being searched for. It also takes into account the order of the elements in the expression and any assumptions or constraints given by the user.

3. What are some common patterns used in Mathematica?

Some common patterns used in Mathematica include wildcards, which match any expression, and named patterns, which allow you to assign names to specific parts of an expression. You can also use patterns such as Blank, BlankSequence, and BlankNullSequence to match specific types of expressions.

4. Can I use pattern matching in Mathematica to replace parts of an expression?

Yes, pattern matching in Mathematica allows you to not only find patterns in an expression but also replace those patterns with new expressions. This can be useful for simplifying or transforming data in a more efficient manner.

5. How can I improve my understanding of pattern matching in Mathematica?

To improve your understanding of pattern matching in Mathematica, it is recommended to practice using different patterns and expressions. You can also refer to the Mathematica documentation for more information and examples, or join online communities or forums to discuss and learn from other users' experiences.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
933
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
898
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top