21

What happens to the list after we assign an element to Sequence[] The answers given so far already contain most of the pieces needed to explain this behavior, but I decided to still add this answer. This topic has been discussed many times in the past, although not on this site. Here is one such discussion, where I also contributed an answer. I will take ...


18

The documentation for Minus states that -x is converted to Times[-1,x] on input. So -Sequence[a,b] == Times[-1,Sequence[a,b]] == Times[-1,a,b] by this definition. Similarly the documentation for Divide states that x/y is converted to x y^-1 on input. and therefore x / Sequence[a,b] == x Sequence[a,b]^-1. Sequence[a,b]^c == Power[a, Power[b,c]]. When ...


17

x/## & // FullForm Function[Times[x,Power[SlotSequence[1],-1]]] and Power[a,b,c...] == Power[a, Power[b, c...]] so now it should be clear. This syntax is mentioned in the last bullet point in details of Power documentation.


12

Here is one idea: Clear[sf, mySequence] sf[x_] := If[x > 0, mySequence[8, 9], 0] mySequence /: (h : Except[If])[x___, mySequence[y___], z___] := h[x, y, z] f[1, 2, sf[1], 4] (* ==> f[1, 2, 8, 9, 4] *) So I defined a sf function that returns the sequence as the result of an If statement. This is just an example, illustrating the general ...


12

You can use SplitBy[Range@Length@m1, m1[[#]] &]


12

You may make a helper function with attribute SequenceHold. ClearAll[sequenceQ]; Attributes[sequenceQ] = {SequenceHold}; sequenceQ[_Sequence] = True; sequenceQ[_] = False; This can be used to test for a sequence directly or with mapping and threading functions as can the built in *Q functions. Note that I use Set here instead of SetDelayed as it works ...


11

First, you can try to apply the FunctionExpand command to the DifferenceRoot object. If it is able to find a closed form of the sequence, then the Limit might be able to find an exact symbolic limit. To find a numerical approximation, you can use the SequenceLimit command. In general, it does not guarantee to give the correct result, but if your sequence ...


11

The previous answers correspond to the recursive model of the form $$[x_n,y_n]=[f(x_{n-1},y_{n-1}),g(x_{n-1},y_{n-1})]$$. However, the state-space model of the question is of the form \begin{align} [x_n,y_n]&=[f(x_{n-1},y_{n-1}),g(x_{n-1},y_{n-1},x_n)]\\ &=[f(x_{n-1},y_{n-1}),g(x_{n-1},y_{n-1},f(x_{n-1},y_{n-1}))] \end{align} The latter equation is ...


11

data = {1, 1, 1, 2, 1, 2, 2, 2, 1, 2, 1}; sub = {{1, 1}, {1, 2}, {2, 1}, {2, 2}}; Count[Partition[data, 2, 1], Alternatives @@ sub] 10 Or each separately: Count[Partition[data, 2, 1], #] & /@ sub {2, 3, 3, 2} More general, for different length subsequences: Length[ReplaceList[data, {___, ##, ___} :> {}]] & @@@ sub {2, 3, 3, 2} Length[...


11

FindSequenceFunction[{8/35, 5/21, 8/33, 35/143, 16/65, 21/85, 80/323, 33/133, 40/161}, n] // FullSimplify (* ((1 + n) (3 + n))/((3 + 2 n) (5 + 2 n)) *)


10

f[x_] := Module[ {s = IntegerDigits@x, s1}, s1 = s /. {y__ ..} :> {y}; {s1, Length@s/Length@s1}] f@212212212212 (* {{2, 1, 2}, 4}*)


10

If you examine the documentation page of FindSequenceFunction you'll see it has an option called FunctionSpace. Its doc page makes clear that the type of function that would match this sequence is not available. Possible values are "Polynomial", "RationalFunction", "HypergeometricTerm", "ConstantRecursive", "HolonomicSequence" as continuous function spaces ...


9

Solve[Table[Sum[f[i], {i, 1, n}] == n^3 + 4 n, {n, 15}], Array[f, 15]] (* {{f[1] -> 5, f[2] -> 11, f[3] -> 23, f[4] -> 41, f[5] -> 65, f[6] -> 95, f[7] -> 131, f[8] -> 173, f[9] -> 221, f[10] -> 275, f[11] -> 335, f[12] -> 401, f[13] -> 473, f[14] -> 551, f[15] -> 635}} *)


9

There is a complicated trade-off between the speed and compact form in this case, so I have decided to post the version with Range, which I consider simple enough (comprehensible for new users) and second fast among the conterparts (at least, on my machine). It is heavily based on @Mr.Wizard solution farsightedly provided by ChrisDegnen, so I do not claim ...


9

In versions 10 and later, you can get the result using a single function SequencePosition: repSeqPos = SequencePosition[#, {Repeated[a_, {2, Infinity}]}, Overlaps->False]&; repSeqPos @ listA {{1, 10}, {11, 13}, {14, 16}, {18, 48}, {51, 88}, {89, 99}, {100, 249}, {251, 253}, {255, 258}, {259, 260}, {268, 272}, {282, 283}, {285, 286}, {...


8

This bug has been fixed in version 10.2. $Version (* "10.2.0 for Linux x86 (64-bit) (July 6, 2015)" *) matchLists[patt_] := MatchQ[#, patt] & /@ {{}, {1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}} matchLists[{_ | PatternSequence[]}] matchLists[{x_ | PatternSequence[]}] (* {True, True, False, False, False} *) (* {True, True, False, False, False} *) ...


8

Perhaps: s = Split@m1; Internal`PartitionRagged[Range[Length@m1], Length /@ s]


8

By inspection of the first few terms we can infer that the correct generalization should be: fun[m_, n_] := Sum[(-1)^(q + m) Binomial[m - 1, q - 1] q^n, {q, 1, m}] which properly reproduces the results: Table[fun[m, n], {m, 2, 5}] // MatrixForm


8

Here's an explanation of why you get {4}. As mentioned above, your problem is the use of Rule instead of RuleDelayed, which means the RHS of the rule evaluates before the rule is used. So, let's see what the RHS of the rule evaluates to: -{a} //FullForm List[Times[-1,a]] So, when evaluated, the rule becomes: L[a___] -> {Times[-1, a]} Now, it should ...


8

The answer is simple: First, you really need to look up the documentation of ##. It means "take all arguments". But more importantly, you can use the property that a > b stays unevaluated when neither true or false can be computed. So check this out: If[#1 > #2, ##] &[a, b] (* If[a > b, a, b] *) Do you see how ## converts this construct into a ...


7

Calculate the List of results you wish to return and use Apply to replace the head: listFn[a_, b___] := If[a > 0, {b}, {0}]; seqFn[args___] := Sequence @@ listFn[args]; f[1, seqFn[2, 3, 4, 5], 6] f[1, seqFn[-2, 3, 4, 5], 6] (*--> f[1, 3, 4, 5, 6] *) (*--> f[1, 0, 6] *) Here listFn represents the calculation of the results and does not need to be ...


7

Nice problem! Maybe something like this: repeatingdigits[n_Integer] := Module[{digits = IntegerDigits[n], res = False, div}, div = Divisors[Length[digits]]; Do[With[{z = Partition[digits, d]}, If[Equal @@ z, res = {z[[1]], Length[z]}; Break[]]], {d, Reverse[Most[div]]}]; res] repeatingdigits /@ {1, 11, 212212, 2122126} (* {False, {{1}, 2}, ...


7

An alternative to belisarius's answer f[n_] := IntegerDigits@n /. l : {x__ ..} :> {{x}, Length@l/Length@{x}} /. {_, 1} -> False f[123123] (* {{1, 2, 3}, 2} *) f[1] (* False *)


7

If your data is in list form (conversion from string will swamp advantage), this should be quite a bit faster (5-50+X than existing answers, timings on the loungbook, so I'd expect 10+X faster for all on W/S): tOnes = Module[{p = Append[Pick[Range@Length@#, #, 1], 0], sa}, If[p === {0}, {}, sa = SparseArray[Subtract[Rest@p, Most@p], Automatic, 1]["...


7

It would be also nice to preserve scoping of x by Plot so: xrange = Sequence[0, 2 \[Pi]]; x = 1; Plot[Sin[x], {x, ##}] &[xrange] or, based on linked topic: {xrange} /. {r__} :> Plot[Sin[x], {x, r}]


7

(FindSequenceFunction@ Differences[Prepend[n^3 + 4 n /. n -> Range@10, 0]])@15 (* 635 *)


7

How to define such functions If you want to define a function that has optional arguments and also takes (zero or more) options, a good way to do it is to make patterns specific and ensure that no unexpected matches will happen. I like to do something like this: f[x : (_?NumericQ) : 0.1, opt : OptionsPattern[]] := ... We want to make sure that: opt will ...


7

This tests if a symbol represents a Sequence: With[{a = mySeq}, HoldComplete[a][[1, 0]]] === Sequence or: (mySeq -> 0)[[1, 0]] === Sequence


7

Internal`InheritedBlock[{FreeQ}, SetAttributes[FreeQ, SequenceHold]; FreeQ[mySeq, 1] ] False


7

This is not a complete answer. It is a long comment about what I found when I looked at the inspectable source code of this function using various spelunking tools (e.g.. Spelunking package, GeneralUtilities`PrinteDefinitions, etc.). Method parsing looks unusual First, I wonder if parsing for the Method option is buggy. First it does this internally: $...


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