Tag Info

New answers tagged

6

As with many things in Mathematica there are a great many ways to perform such a simple operation. Which one you choose can depend on what you are comfortable with and what performance level you require. I shall list several that come to mind. Some options have already been provided in other answer; I shall include them here for completeness. A small ...


3

If it is known that all the elements are Real, the solution becomes pretty straightforward: l1/.{x_Real}:>{x,0} bigList/.{x_Real}:>{x,0} More general solution is to Replace at level {-2}: Replace[l1, {x_} :> {x, 0}, {-2}] The same can be achieved by Joining the array with itself multiplied by zero: Join[l1, 0*l1, 3] Or more generally ...


5

The first aim can be accomplished tersely: {#,0}&@@@l1 The 'bigList': {#, 0} & @@@ # & /@ bigList As well as replacement rules. More complex nesting would require more complex approaches.


6

Acting on the simple list you gave l1 = {{-1.34266}, {-0.278541}, {1.37156}} is simple: newlist= l1 /. {x_} :> {x, 0} Now, let's work on the actual list. I'll call it bigList bigList = { {{-1.342}, {-0.28}, {1.372}}, {{-1.34266}, {-0.278541},{1.37156}}, {{-1.34459}, {-0.274215}, {1.37026}}, {{-1.34769}, {-0.267169}, {1.36807}}, {{-1.35177}, ...


3

Here is an old fashioned approach, using stacks. It is quite general in the sense that it will handle tree structures with more than two children per node. It is also fairly slow; it has the correct (linear) complexity but maybe an order of magnitude more pushing/popping than the length. SetAttributes[push, HoldRest]; SetAttributes[pop, HoldAll]; ...


9

Implementations Here is an "idiomatic" one: ClearAll[mapRec, reverse]; mapRec[f_, ll_] := Block[{$IterationLimit = Infinity}, reverse@mapRec[{}, f, ll]]; mapRec[accum_, _, {}] := accum; mapRec[accum_, f_, {head_, tail_}] := mapRec[{f[head], accum}, f, tail]; reverse[ll_] := reverse[{}, ll]; reverse[accum_, {}] := accum; reverse[accum_, {head_, tail_}] := ...


5

So far, the best I've been able to do uses Reap and Sow to construct an ordinary list while I traverse the linked list with a While loop. At the end, I reconstitute it using Fold in the normal way. It's pretty easy to capture the head of the list. It will fail (throw $Failed) if the list isn't terminated properly, or if it contains a head other than the head ...


0

Here is a stepwise approach: sol = First@DSolve[f'[x] == -k (x - \[Mu]) f[x], f[x], x]; yields: {f[x] -> E^(k (-(x^2/2) + x \[Mu])) C[1]} In this case to get to the normal PDF: c1 = First@ Solve[Integrate[f[x] /. sol, {x, -Infinity, Infinity}, GenerateConditions -> False] == 1, C[1]] yields: {C[1] -> (E^(-((k \[Mu]^2)/2)) ...


1

Try this ... I changed your code a bit: eqn1 = f'[x] == -k (x - \[Mu]) f[x] eqn2 = 1/f \[DifferentialD]f == -k (x - \[Mu]) \[DifferentialD]x \[Integral]1/ f[x] \[DifferentialD]f[ x] == \[Integral]-k (x - \[Mu]) \[DifferentialD]x Exp[Log[f[x]]] == Exp[k (-(x^2/2) + x \[Mu])] (* you'll see it's the same as *) DSolve[eqn1, f[x], x] Gives for me: ...


5

You can pass a function (or for that matter, anything!) as an argument to another function and you'll need to modify your code accordingly. See this example: ChebyCoeff[func_, m0_] := Module[{m = m0}, f[t] = func[2 Pi t]; Tn[t] = ChebyshevT[j, 2*t - 1]; wt[t] = 1/Sqrt[t - t^2]; p = Table[Chop[NIntegrate[f[t]*Tn[t]*wt[t], {t, 0, ...


3

As described in the other answers, Composition or Compose is what you need. Since you also wanted them to be applied in reverse order, the new RightComposition that is in the Wolfram Language is the appropriate function (of course, you can always Reverse the list before feeding to Composition). functionList = {f1, f2, f3}; (RightComposition @@ ...



Top 50 recent answers are included