Tag Info

New answers tagged

0

If anyone was interested in alternative (but not as elegant solution) this is the one I came up with recently (using lists as defined in the above solution). Constant = 5; lists = Table[G[i][[j]], {i, 1, Constant}, {j, 1, 2^i}]; Posi[i_, j_] := Ceiling[j/2] model[1, j_] := lists[[1, j]]; model[i_, j_] := lists[[i, j]] + model[i - 1, Posi[i, j]]; finaltable ...


6

Example data: lists = Table[g[i, j], {i, 1, 3}, {j, 1, 2^i}]; munging: results=Flatten@FoldList[Flatten@MapThread[ Outer[Plus, {#1}, #2] &, {#1, Partition[#2, 2]}] &, lists]; lists results (* {{g[1, 1], g[1, 2]}, {g[2, 1], g[2, 2], g[2, 3], g[2, 4]}, {g[3, 1], g[3, 2], g[3, 3], g[3, 4], g[3, 5], g[3, 6], g[3, 7], g[3, 8]}} {g[1, 1], ...


6

We can apply the method I used for How to match expressions with a repeating pattern: test[Null | {_Integer, _?test}] = True; _test = False; Confirmation: good = { Null, {4, Null}, {3, {4, Null}}, {2, {3, {4, Null}}}, {1, {2, {3, {4, Null}}}} }; bad = {{}, {Null}, {Null, Null}, {3, 4, Null}}; test /@ good test /@ bad {True, True, True, ...


2

No problem in version 10: $Version (* Out[3]= "10.1.0 for Microsoft Windows (64-bit) (March 24, 2015)" *) RSolve[{(k + 2) c[k + 2] q^(k + 1) - c[k] == 0}, c[k], k] $$\left\{\left\{c(k)\to \frac{c_1 2^{-\frac{k}{2}} \left(\frac{1}{q^2}\right)^{\frac{1}{8} k (k+2)} q^{k/2}}{\Gamma \left(\frac{k}{2}+1\right)}+\frac{c_2 (-1)^k 2^{-\frac{k}{2}} ...


2

Although this is not an answer to the question as put, the problem can be solved completely with Mathematica as follows. This is also a hint to try alternative formulations in mathematica if a specific one does not succeed. Assuming, of course, that the problem is in the center of interest. The easiest way to obtain the values of a[n,m] is to start with ...


1

I will not answer the failure of in-built functions but present an alternative approach: f[m_, u_] := With[{r = {{-1, 0}, {0, -1}, {-1, -1}}}, ReplacePart[m, u -> Total[(Extract[m, u + #1] &) /@ r]]]; sa[m_, n_] := Module[{s, p}, s = Normal@SparseArray[{{i_, 1} -> 1, {1, j_} -> 1}, {m, n}, "x"]; p = Position[s, "x"]; Fold[f[#1, #2] ...


6

There is not much to add to the explanation Leonid gave in his comment and above all in his answer about tail-recursion. Probably only one thing that might be additionally interesting in your situation are two collatz functions that look alike but one hits $IterationLimit and the other hits $RecursionLimit. First the iterative one: collatzIt[m_] := ...


6

Another way that seems efficient: pat = Module[{check}, check[_Integer, Null] := Null; check[___] := Throw[False]; Catch[# /. List -> check /. {Null -> True, _ -> False}] ] &; test = { Null, {4, Null}, {3, {4, Null}}, {2, {3, {4, Null}}}, {1, {2, {3, {4, Null}}}}, (*True*) {}, 5, {Null, {5, Null}}, {{5, Null}, 4}, {Null}, ...


21

What you need is something like this: patt = Null | (x_ /; MatchQ[x, {_Integer, patt}] ) The trick is to delay the evaluation for the recursive part, until run-time (match attempt), and Condition is one way to do it. So: MatchQ[#, patt] & /@ {Null, {4, Null}, {3, {4, Null}}, {2, {3, {4, Null}}}, {1, {2, {3, {4, Null}}}}} (* {True, True, True, ...


14

Not sure if this is what you are looking for, but let´s see (wash, rinse, repeat): test = {Null, {4,Null}, {3, {4,Null}}, {2, {3, {4,Null}}}, {1, {2, {3, {4, Null}}}}, {}, {Null}, {Null, Null}, {3,4, Null}}; MatchQ[Null, # //. {_Integer, Null} -> Null] & /@ test (*{True, True, True, True, True, False, False, False, False}*)


2

All done by Morphological Components clusteringb[config_] := Module[{output, cm, cindices, csizes}, output = MorphologicalComponents[Image@Abs@config, CornerNeighbors -> False]; cm = ComponentMeasurements[ output, {"Label", "Mask", "Count"}][[All, 2]]; {cindices, csizes} = Transpose[{{#1, #2["NonzeroPositions"]}, {#1, #3}} & @@@ cm]; ...


5

The kernel crashed because it ran through all the stack space available to it. The Memory Management Tutorial page in the documentation states the following regarding stack space: In the Wolfram System, one of the primary uses of stack space is in handling the calling of one Wolfram Language function by another. All such calls are explicitly recorded in ...


5

I haven't find a way to solve the problem using Mathematica straight away, but we can use it to navigate the formulas and for finding the relevant closed expressions. Please beware that the following is far from a formal proof and only an educated guess (how well "educated" is a matter of discussion, though :) At the end of this post you'll find a ...


5

March - this is not a complete answer, but instead addresses the gathering of indices since that seems to be important. Here's a comparison of some methods, tested only on my cigar-lounge netbook so caveat lector. OP - Virgil's original (position based), Virgil - Virgil's adaptation of my first comment, CMT. 2/3 - my second and third comments (gathered ...


3

If you want a recursive solution, you should first rewrite Fib to return both the last values, so it only needs one recursive call - thus making the number of recursive calls grow linearly instead of exponentially with n (or p, if partially memoized): Clear[recFib] recFib[2] = {1, 1}; recFib[n_] := Module[{last = recFib[n - 1]}, {last[[-1]], ...


0

Here's a simple procedural method: Clear[f, g]; f[1] = 1; f[0] = 1; f[-1] = 0; p = 5; Do[f[n] = f[n - 1] + f[n - 2]; Unset[f[n - 3]]; If[Mod[n, p] == 0, g[n] = f[n]];, {n, 2, 20}] If you now query the values of ?f you can see only the final ones, while ?g stores only every pth entry.


7

Built-in option This sidesteps most of your code, so it might not be what you are looking for, but I believe your goal can be achieved with Mathematica's built-in image processing capability, specifically: MorphologicalComponents! Define a new clustering function clustering1[config_] := Module[{output, csizes, cindices}, output = ...



Top 50 recent answers are included