# Tag Info

23

I had a go with HiddenMarkovProcess[], based on the assumption that the data is normally distributed around two different means (it looks like it!). This approach should be fine for cases where the number of "states" is small, e.g. 2 in this case. Otherwise you're looking at Infinite Hidden Markov Models, or see the bottom of this answer. To remove some ...

15

ListPlot@{l1, msf = MeanShiftFilter[l1, IntegerPart[Length@l1/10], MedianDeviation@l1, MaxIterations -> 10]} And here are the detected means (assuming there are three): fc = FindClusters[msf]; Mean /@ fc ( *{3.77282, 220.788, 387.444} *)

14

There seems to be a bug regarding this, in version 10.1.0 under Windows. For a first evaluation in a fresh kernel I get: expr = Product[Unique["a"], {i, 1, 25}]; rep = {x_ f[y_] /; FreeQ[x, y] -> 0}; expr /. rep // Short // AbsoluteTiming {9.64113*10^-6, a15 a16 a17 a18 a19 a20 << 14 >> a35 a36 a37 a38 a39} But the second time I ...

9

Another approach is to use compound median filtering which returns a blocky function. Then threshold the jumps between blocks. No assumptions about the number or size of blocks is made. Function to plot the input series as discrete jumps. BlockPlot[s_] := Partition[ Flatten[{s[[1]], Table[{{s[[i, 1]], s[[i - 1, 2]]}, s[[i]]}, {i, 2, ...

8

I think an acceptable solution is to Thread over Alternatives: Basic solution: SetAttributes[f, Flat]; f[a, b, c] /. Thread[f[a, f[b, c] | other], Alternatives] -> post post Though, it won't be very helpful in more complex situations: f[a | b, f[b, c | h]]. General solution (experimental) tupplesOver[ f[a | g, f[b, c | h] | other], ...

8

The two examples in this question relate to two different aspects of pattern matching. I will start with the simpler to understand and intentional aspect, which is the second example. g[2] /. g[ 1 + (1|other) ] -> post (* g[2] *) In the above, the pattern doesn't match, and it can never match. g[2] has one argument. Since Plus is OneIdentity, 2 ...

7

Okay, this has been a bit of a headache. The short answer is that it is all working as expected. Now for too much detail to put into a comment. To begin, the original question was a bit of a blind, though certainly not by intent. When one considers what has to happen in a match with an Orderless function such as Times, it seems quite plausible that this ...

6

(expr /. List :> Composition[f, List])[[1]] (*{f[{a}], b, f[{f[{c}], f[{e}]}], d, f[{}]}*)

5

Instead of using BlankSequence, use PatternSequence: expr /. Times[rest___, subExpr : PatternSequence[___a, ___d]] :> myfunc[Times[subExpr]] myfunc[a[x, r] a[y, 1] d[w, m]] The orderlessness of the pattern sequence is ensured by the Orderless attribute of Times.

5

Look at the FullForm of the expression FullForm[a.b.c.d.a.b] Dot[a,b,c,d,a,b] and the replacement pattern FullForm[x_ . y_ . x_] Dot[Pattern[x,Blank[]], Pattern[y,Blank[], Pattern[x,Blank[]]] Clearly both the expression and the pattern are enclosed by Dot[...]. So what we need is for: Dot[a,b,c,d,a,b] and Dot[x_, y_, x_] to match. The only ...

4

The results we see are due to a subtle interaction between the Flat attribute of Dot and the outermost-in, left-to-right scanning strategy employed by the pattern matcher. The expression a . b . c . d . a . b /. x_ . y_ . x_ :> p[x,y] could legitimately return three different solutions depending upon how we decide to group the . (Dot) operators, ...

4

As an example, let us consider the following expression: expr= Log[ 3 Sin[x] + 2 Exp[Pi+ 4 a b + 1/7]]; This is not a polynomial, so the function Variables cannot be used. On level -1 we have the atoms: Cases[expr,_, {-1}] (* {2,E,1/7,4,a,b,\[Pi],3,x} *) Observe that 1/7 is an atom! We restrict ourselves to symbols: Cases[expr,_Symbol, {-1}] (* ...

4

Personally, I find this behavior somewhat surprising -- in particular, I would intuitively expect the following patterns to be completely equivalent: somePattern[..., a|b, ...] somePattern[..., a, ...] | somePattern[..., b, ...] While that may seem a natural expectation I do not believe the documentation ever states that they are. Nowhere can I ...

3

After discussing with Kuba, I conjecture the following: Patterns involving alternatives are not evaluated further when attempting to match each alternative That is, somePattern[..., a|b, ...] originally evaluates as if a|b is a black box. Then, during pattern matching, the pattern does not evaluate any further when a|b is replaced by a and b in turn. To ...

3

Cases handles an expression with head Rule specially, providing replacement functionality, therefore you need to keep the literal head of your pattern from being Rule. Examples: Cases[rules, _[b, _]] Cases[rules, x : (b -> _)] Cases[rules, HoldPattern[b -> _]] Cases[rules, Verbatim[Rule][b, _]] All evaluate to {b -> 2}. None of the patterns ...

3

In V10.0+ you can stick with functions: CountsBy[{1, 1, 2, 3}, (# > 1.5) &][True]

3

Some of these could be implemented differently, of course, but I've gone the way of making all of them pure functions (in the Mathematica sense). Every single one takes a Sequence of arguments as the inputs, but some of them accept function names as inputs first, and the projection function accepts an integer for which argument is chosen (I have chosen to ...

3

With WolframLanguageData your list of graphics primitives will stay up to date. ListOfGraphicsPrimitives[] = Symbol /@ WolframLanguageData[ EntityClass["WolframLanguageSymbol", {"FunctionalityArea","GraphicsPrimitiveFunctions"}], "Name"] {AASTriangle, AffineHalfSpace, AffineSpace, Annulus, Arrow, ASATriangle, Ball, BezierCurve, BSplineCurve, ...

3

Instead of pattern-matching the expressions you want to keep, you could also remove the ones you do not want to keep: myfunc[Replace[expr,x_ /; Not@MemberQ[{a, d}, Head@x] -> Sequence[], {1}]] This approach should be faster than using pattern matching using multiple blanks. It also works with other expression heads than Times.

2

There are a number of ways this could be done, e.g. list /. {_Missing, _} :> Sequence[] or missing any position: list/. {___, _Missing, ___} :> Sequence[] or Cases[list, Except[{___, _Missing, ___}]] DeleteCases[list, {___, _Missing, ___}] Select[list,FreeQ[Missing][#]==True&] Pick[list, FreeQ[Missing] /@ list] True /. GroupBy[list, ...

2

Ok. Here comes the Inactivate, Mathematica 10's powerful feature. Inactivate could not solve injecting expression into Hold. But you mentioned you actually want to use this to inject Do iterator in Compile. This can be done directly by Inactivate without Hold stuff. Use this Activate[Inactivate[Compile[{}, Do[code, iterators]]] /. iterators -> ...

2

Daniel Lichtblau has supplied the answer in a comment: use PolynomialReduce, rather than PolynomialQuotientRemainder. As such: PolynomialReduce[expr, c*(p^2) + c, {c, p}] (* -> {{1}, 1 + 2 c + c^2 + 2 p + p^4} *) which is the desired result.

2

As discussed in comments, we can get the result directly. StringCases["tthis is a book fine", Repeated /@ {"is", "book", "t"}] {"tt","is","is","book"}

2

expr /. x : {_} | {} :> f[x] /. x : {__f} :> f[x]

1

You can use WolframLanguageData with Alternatives to built the Except. numConstFunc = Except[Alternatives @@ Symbol /@ WolframLanguageData[ EntityClass[ "WolframLanguageSymbol", {"FunctionalityArea", "NumericConstantFunctions"}], "Name"], _Symbol]; Then expr= Log[ 3 Sin[x] + 2 Exp[Pi+ 4 a b + 1/7]]; Cases[expr, numConstFunc, {-1}] ...

1

SearchAll[sequence_, search_] := Module[{map}, map = Map[# -> "\!$$\*StyleBox[\"" <> # <> "\",FontColor->" <> ToString@RGBColor[RandomReal[], RandomReal[], RandomReal[]] <> "]$$" & , search]; StringReplace[sequence, map]] SearchAll["CGACATCACCGATGGGGAAGATCGGGCTCGCCACTTCGGGCTCATGA", {"CGA", "CATG"}] // Style[#, ...

1

Are you looking for a shorter function to create a pattern? Or just a way to write the pattern without a function? Your function could be shorter by writing ep[x_, e_] := _?(Abs[x - #] <= e &) lst = 10.4 + RandomReal[{-.02, .02}, 10] Cases[lst, ep[10.4, 10.^-2]] Cases[lst, _?(Abs[10.4 - #] <= .01 &)] (* {10.4099, 10.4196, 10.3874, 10.3976, ...

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