Skip to main content
10 votes
Accepted

Why doesn't b = a; f[a_] := b; f[2] return 2?

Caution The post is long. If you just want to know how to fix the code, please search "fix the code in title in 10 ways" in the page. If you want to read discussions about global/local ...
xzczd's user avatar
  • 66.7k
8 votes
Accepted

Plotting a function containing a $\pm$ sign

Define: ( because I can ) ...
Syed's user avatar
  • 55.3k
5 votes
Accepted

Pattern Matching and Permutations

This is an example of solving such a task. It is divided into separate steps, which I believe are – due to quite descriptive naming conventions in Mathematica – self-explanatory. The only two ...
Domen's user avatar
  • 26.3k
5 votes

Why doesn't b = a; f[a_] := b; f[2] return 2?

I thinks it is pretty obvious why the first code returns a and the second 2. You can see it by comparing ...
azerbajdzan's user avatar
  • 18.6k
4 votes
Accepted

Function with complicated argument constraint with default value

In this case, the first : which is the shorthand of Pattern cannot be omitted (yeah n_ is ...
xzczd's user avatar
  • 66.7k
4 votes

How to do this replacement? Match the variable, but not the function

You can replace \[Lambda][\[Theta]] with \[Lambda] first, and then replace \[Lambda] with <...
Jie Zhu's user avatar
  • 798
4 votes
Accepted

Unexpected result from MemberQ

The FullFormof 1/y[x] is Power[y[x],-1] Let's see what elements ...
rhermans's user avatar
  • 36.7k
4 votes

Why doesn't b = a; f[a_] := b; f[2] return 2?

To understand what is going on you need to look at "DownValues" to see what is actually stored: ...
Daniel Huber's user avatar
  • 52.6k
4 votes

Plotting a function containing a $\pm$ sign

If it won't interfere with the rest of your work, you can define PlusMinus[a_, b_] := {a + b, a - b} and then ...
evanb's user avatar
  • 6,131
4 votes
Accepted

Pattern matching when decode Bencode. Problems with pattern name and nested pattern

I think that actually just trying to parse the input is easier than coming up with patterns that you can use in MatchQ. If the parse fails, then you know that ...
lericr's user avatar
  • 28.9k
4 votes

$n$-letter words with restriction

For something different using PolynomialMod ...
ubpdqn's user avatar
  • 61.5k
4 votes

$n$-letter words with restriction

Instead of generating too many tuples and deleting the false ones, simply only create correct tuples. The constriction "a1 and a5 may not appear in he same word" can be interpreted as: "...
Daniel Huber's user avatar
  • 52.6k
4 votes

$n$-letter words with restriction

Using Select with ContainsAll Select[tuples, Not @* ContainsAll[{a1, a5}]]
eldo's user avatar
  • 74.1k
4 votes
Accepted

$n$-letter words with restriction

Usually in such cases, Mathematica offers a variety of different solutions. OrderlessPatternSequence ...
Domen's user avatar
  • 26.3k
4 votes

Pattern matching for repeated elements

list = {1, 2, 2, 2, 3, 4}; Using PositionIndex ...
eldo's user avatar
  • 74.1k
2 votes

Elegant and fast way to join all string sequences

list = {"a", "b", 1, 2, 3, 4, "a", 2, 2, 2, "edg", "?"}; Using SequenceReplace...
eldo's user avatar
  • 74.1k
2 votes

Extracting sequences delimited by two String markers from a list

list = {1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5}; Using ...
eldo's user avatar
  • 74.1k
2 votes
Accepted

Position outputting lists which seemingly have the wrong dimension for the input array

You have Sqrt[3]/2 entries in struct. The FullForm shows that it has two ...
Syed's user avatar
  • 55.3k
2 votes
Accepted

Using patterns to replace the head of certain expressions

You have hit a common issue when using Times as a pattern. Observe the following: ...
Domen's user avatar
  • 26.3k
2 votes

How do I locate non-numbers

list = {1, 2, 3, 4, 5, 6 p6, 7 p7, 8, 9}; Using SequenceSplit (new in 11.3) ...
eldo's user avatar
  • 74.1k
1 vote
Accepted

Moving all terms with dependents variable to one side of differential equation

This method just searches for terms that lack an instance of the dependent variable name and moves them to one side. It should work for PDEs as well. In condensed form: ...
march's user avatar
  • 23.7k
1 vote

Using patterns to replace the head of certain expressions

How about using an If ? ...
Nasser's user avatar
  • 145k
1 vote

How to make expression-dependent replacements?

For a list of sublists of this type: ...
RGL's user avatar
  • 31
1 vote

How can I remove empty lists { } from a nested list?

list = {{{1}, {2}, {1, 2}, {1, 2, 3}, {}, {}}, {{2}, {3}, {}}} Using SequenceCases ...
eldo's user avatar
  • 74.1k
1 vote

How to copy with the last 1 with pattern matching method in a list?

list = {1, 2, 3, 1, 5, 8, 1, 9, 6}; Following the @eldo's idea and using SubsetReplace: ...
E. Chan-López's user avatar
1 vote

How to copy with the last 1 with pattern matching method in a list?

list = {1, 2, 3, 1, 5, 8, 1, 9, 6}; p = Last @ Position[list, 1] {7} Using MapAt ...
eldo's user avatar
  • 74.1k

Only top scored, non community-wiki answers of a minimum length are eligible