New answers tagged pattern-matching
2
You have already seen that there are a number of ways to skin this cat but I'd like to add some comments of my own.
Other approaches
When possible I prefer to avoid these situations in the first place, instead using something like:
{{1, 2}, {3, 4}, {5, 6}} /. {x_, y_} :> Thread@{x, {y, y^2, y^3}} // Thread
{{{1, 2}, {3, 4}, {5, 6}}, {{1, 4}, {3, ...
2
I'll just aggregrate here the various responses from the comments, plus my own humble thoughts and understanding (which may lack precision or contain mistakes, so feel free to improve or correct):
Be careful about variable naming. (This is assuming that, generally speaking, you have good reason to have global variables spilling out in your program.)
Use ...
4
First, I'd like to point out that your "JoinH" function is already implemented by Join:
a = {{1, 2}, {3, 4}};
Join[a, a + 5, 2]
{{1, 2, 6, 7}, {3, 4, 8, 9}}
Second, you don't need Flat or whatever if you write the function to natively handle multiple arguments:
jh[m__] := Transpose[Join @@ Transpose /@ {m}]
jh[a, jh[a + 5, a + 11]]
jh[a, a + 5, a + ...
6
Here's another way to proceed, using Derivative[], and sidestepping the use of a dummy variable:
LogDerivative[f_] := Derivative[1][Composition[Log, f]]
Test:
LogDerivative[Sin][x]
Cot[x]
LogDerivative[Gamma][x]
PolyGamma[0, x]
LogDerivative[#^3 &][x]
3/x
6
Your operator must depend on both function and variable - in analogy to D function:
logD[f_, x_] := D[f, x]/f
or an alternative definition:
logD[f_, x_] := D[Log[f], x]
Of course your variables of differentiation and in the function must agree. Test it:
logD[f[x], x]
Derivative[1][f][x]/f[x]
logD[Sin[x], x]
Cot[x]
f = x^2; logD[f, x]
...
4
Actually this is more like a comment to Michael E.'s answer than an own answer, but it became too long for a comment. I think it is worth mentioning that $IterationLimit (and also $RecursionLimit and probably some others as well) is somewhat special and thus needs special treatment: For a "normal" variable it would be quite simple to achieve what Michael ...
4
What this does is set a default value for iter, meant for use if cfRemainders is called with only one argument. The default value for iter in this case is $IterationLimit, and the Hold[] enclosing it means cfRemainders will use $IterationLimit symbol for the new rule. If there was no enclosing Hold[], $IterationLimit would have been replaced with the Integer ...
6
The original complete definition is
cfRemainders[x_, iter_: Hold[$IterationLimit]] :=
NestWhileList[FractionalPart[1/#] &, FractionalPart[x], # != 0 &, 1, ReleaseHold[iter]]
The iter_ : Hold[$IterationLimit] makes iter an Optional argument with the default value Hold[$IterationLimit] if the argument is omitted.
Secondly, by using Hold, ...
14
It's easy to search if you break it down:
Regex Meaning Mathematica command
-------------------------------------------------
\w word character WordCharacter
{2,3} repeat 2 to 3 times Repeated[..., {2, 3}]
Combine it and use as:
StringMatchQ[{"a", "ab", "abc", "abcd"}, Repeated[WordCharacter, {2, 3}]]
(* {False, True, True, ...
4
Here's a plain pattern approach, I'm not quite sure how robust it is:
ReplaceList[expr, {___, "Open", x : Except["Close"] ..., "Close", ___} :> {x}]
Also take a look at Longest and Shortest, which may come in handy.
1
I feel this is way too complicated, but anyway:
l = {1, 2, 3, "Open", 3, 4, 5, 2, "Close", 9, 3, 4, "Open", 0,
"Close", "Close", 3, 5};
Reap[l //. {a___,
PatternSequence["Open", mid : _?NumericQ .., "Close"],
b___} :> {a, Sow[{mid}]; mid, b}][[2, 1]]
{{3, 4, 5, 2}, {0}}
This example might have some limited instructional value ...
1
My humble attempt:
list = {1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0,
"Close", 3, 5};
SplitBy[
Select[list,
(open = # != "Close" && (# == "Open" || open)) &], # == "Open" &]
//. "Open" | {} -> Sequence[]
5
Try this one
ReplaceList[expr, {__, PatternSequence["Open",v__ /; Count[{v}, _String] == 0,
_String], __} -> {v}]
4
As belisarius observed, the result in version 9 contains additional constants that enumerate a range of equivalent solutions for x. This causes s to no longer contain the expressions you're looking for with Select.
One way to eliminate the additional constants is to specify what you'e looking for in the Solve expression directly, as for example here:
s = ...
4
If you do this:
s = Solve[(3 - Cos[4*x])*(Sin[x] - Cos[x]) == 2, x, Reals];
x /. FullSimplify[s, Element[C[1], Integers]]
Both versions will give you the same result:
{1/2 (Pi - 8 Pi C[1]), Pi (-1 + 4 C[1]), Pi + 4 Pi C[1], -(1/2) Pi (3 + 8 C[1])}
0
How about Hold[{1, 2, x}] /. {a : ___, x, b : ___} :> {a, 3, 4, b}
Top 50 recent answers are included




