Tag Info

Hot answers tagged

17

Here is a summary of comments (before @ciao's best answer above), with a change in notation. These functions calculate the number of partitions of n into exactly k distinct parts of size at most m. NumberOfWays000[n_, k_, m_] := Count[Map[Length,Map[DeleteDuplicates, IntegerPartitions[n,{k},Range[m]]]], k] NumberOfWays001[n_, k_, m_] := ...


16

This seems pretty quick, particularly on larger cases / larger k, e.g. 451, 29, 101 finishes in a few seconds on the loungebook. N.B. - I have not tested this exhaustively, just thrown together from ideas... If[Min[#3, #1 - Tr@Range@(#2 - 1)] < 0, 0, SeriesCoefficient[QPochhammer[-x y, x, Min[#3, #1 - Tr@Range@(#2 - 1)]], {x , ...


15

fibSequences[n_?EvenQ] := Nest[Accumulate[Join[{1, 0}, #]] &, {}, n/2] fibSequences[n_?OddQ] := Most@Nest[Accumulate[Join[{1, 0}, #]] &, {}, (n + 1)/2] fibSequences[10] {1, 1, 2, 3, 5, 8, 13, 21, 34, 55} fibSequences[9] {1, 1, 2, 3, 5, 8, 13, 21, 34}


15

Here is a totally different approach based on the fact that successive products forming the generating function are due to multiplication by a binomial $1+t*z^j$. Form a matrix $v$ of zeros with $n+1$ rows and $k+1$ columns. Initialize the top left corner to 1. Iterate $v=v+w$ where $w$ is the matrix $v$ shifted down by $j$ rows and to the right by 1. The ...


8

It is sometimes beneficial to first work with functions (in mathematical sense) as symbols and apply to them some pointwise operations. Then, just at the end, convert resulting expression to pure function (in Mathematica sense) and pass some arguments. This can be automated using something like this: ClearAll[purify] Options[purify] = {"FunctionPattern" ...


7

There are two possibilities you could be aiming for. First, I'll take your question literally and just inject expression into the Module: expression = a + 1; With[{expression = expression}, g[x_] := Module[ {a, b}, a = 1; b = expression; x*b]] expression = 1 (* ==> 1 *) g[xi] (* ==> (1 + a) xi *) As the result after changing ...


6

You can do this by moving method from an option to a parameter and explicitly listing the options in OptionsPattern. With this approach Options is not used. ClearAll[f] f[x_, method : "a", OptionsPattern[{depth -> 5}]] := {x, method, OptionValue[depth]} f[x_, method : "b", OptionsPattern[{totTime -> 10}]] := {x, method, OptionValue[totTime]} ...


6

You told Mathematica to terminate at i == Length[L]-1, not at Length[L]. Additionally, your use of Or is almost certainly not what you intended. I advise reading the Mathematica documentation under tutorial/EverythingIsAnExpression.


6

I rewrote your primeG, ignorant of clever methods. Nevertheless, with [[All,1]] and the fourth argument to Position you can get a 50% speed increase. Also, since you already calculate the primes in aa, there is no need to use Prime in calculating dd. MartinPrimeGaps[nn_]:= With[{aa = Prime@Range@PrimePi@nn}, With[{bb = Differences[aa]}, ...


6

You can achieve this defining an UpValue for g: g/:Power[g,2]:=g[#]^2& Or more generally: g/:Power[g,n_Integer]:=g[#]^n& Using Through now works as wanted: Through[(f + g^2)[x]] (*Out=f[x]+g[x]^2*)


5

Another alternative using Map and a pure function & which are common Mathematica idioms: f1[el_, n_] := (#[[1]] > n || #[[2]] > n) & /@ el /. {True -> "Bad", False -> "Good"} The use of capital letters for variables, functions etc is discouraged as they may clash with built in definitions. Print is also not much used, Mathematica will ...


5

Probably better: f[l_, n_] := Or @@@ Thread /@ Thread[l > n] /. {True -> "Bad", False -> "Good"} l = {{0, 1}, {2, 3}, {4, 5}, {6, 7}}; f[l, 5] (* {"Good", "Good", "Good", "Bad"} *) Looping is strongly discouraged in Mathematica, but if you still want to stick with it: f1[l_, n_] := For[i = 1, i <= Length[l], i++, ...


5

Well... I did use Accumulate! First /@ NestList[{Last@#, Last@*Accumulate@#} &, list, 10] {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55} EDIT Of course I forgot to mention that list = {0, 1}


4

Not that I would recommend this, but anyway: positionPayload = 2. x + 3. t^2; variableList = Variables[positionPayload]; positionPayload = Function[Evaluate@variableList, Evaluate@positionPayload]; positionPayload[q, r] (* 3. q^2 + 2. r *) Edit positionPayload = 2. x[1] + 3. t[3]^2; variableList = Variables[positionPayload]; ul = ...


4

I'm probably missing an important point, but what is wrong with (f[#] + g[#]^2)&[x] f[x]+g[x]^2


3

You might try something like this, which makes pure functions, which means the variables used int the expression to converted only have to be clear at time expToF is called. expToF[exp_, vars : {_Symbol ..}] := With[{body = exp /. Thread[Rule[vars, Slot /@ Range @ Length[vars]]]}, Function[body]] Clear[x,t] f = expToF[2. x + 3. t^2, {x, t}]; f[x,t] ...


3

I strongly advocate the use of a functional idiom such as Map or Thread as shown in the answers by Belisarius and image_doctor. But it is worth noting that you can get something close to your original syntax using Infix notation (see this guide for more information. Aside from the issue with your termination condition, you were trying to use Or as an infix ...


3

You should correct the typo in your code. I'm not even sure how this can happen, when you copied the example from your notebook. Additionally, you shouldn't use $\psi$ for both, a function and a variable. Once this is fixed: energyFunctional[ψ_] := Integrate[ 1/2 (D[ψ[x], x])^2 + 1/2*x^2*(ψ[x])^2, {x, -∞, ∞}]; ψ1 = (1/π)^(1/4) Exp[-(#^2/2)] ...


3

Your code can be much simplified. The following rewrite of your code works. wlines = {427.397, 431.958, 450.235, 557.029, 587.092, 605.613, 645.629, 665.223, 669.923, 681.311, 690.468}; wcal = {4.1989123474370302*^02, -5.3957450948852408*^-02, 6.7152505835315814*^-04, -8.6698204011228679*^-07, 5.5523712684399200*^-10}; g[x_] = ...


3

The solution may be to declare: Attributes[Wigner] = {HoldFirst} The point is to prevent Mathematica from computing the argument before applying the rules for Wigner


3

Many built-in functions use a Method option, and suboptions are given within that option, nested, rather than flat as in your examples. The value of Method is extracted and processed, e.g. with Charting`ConstructMethod and Charting`parseMethod, then used as needed. I suggest that you do something similar. Simplistically: Options[f] = {method -> {"a", ...


2

My one shot at answering this question: Attributes[convert] = {HoldFirst}; convert[def_Symbol?ValueQ] := With[{old = def, pats = Quiet[Sequence @@ Cases[Variables @ def, s_Symbol :> s_]]}, ClearAll[def]; def[pats] := old; ] Test: positionPayload = 2. x + 3. t^2; convert[positionPayload] ?? positionPayload Global`positionPayload ...


2

positionPayload = 2. x + 3. t^2 (* 3. t^2 + 2. x *) variableList = DeleteDuplicates[Variables[positionPayload]] (* {t, x} *) temp = positionPayload; positionPayload =. Evaluate[positionPayload @@ (Pattern[#, Blank[]] & /@ variableList)] := Evaluate@temp Definition@positionPayload (* positionPayload[t_, x_] := 3. t^2 + 2. x *) positionPayload[q, r] (* ...


2

I believe you want expr = u[i + 1] r[i] r[i - 1] expr /. u[n_] :> g[n + 1] f[n - 1] (* f[i] g[2 + i] r[-1 + i] r[i] *)


2

The original version works just fine: Through[(f + Composition[Power[#,2]&, g])[x]] or, for MMA ver. 10 and above, Through[(f + (Power[#,2]&) @* g)[x]] result in (* f[x] + g[x]^2 *) Alternatively, you could do, from the beginning, Through[(f + (g[#]^2 &))[x]] which is perhaps a little easier to parse since it doesn't use Composition. ...


2

If you want to avoid Subscript then x[t_] := Table[ ToExpression["x" <> ToString[i]] , {i, 1, 10} ]


1

Update notice: I added this answer, mutatis mutandis, to Series expansion in terms of Hermite polynomials, which effectively makes the present question a duplicate of the linked one. I decided to leave this one here, because, well, it was already written and would possibly help the OP. Also this question is likely to be closed for reasons other than it is ...


1

If you are okay with using pure functions, you can do the following. expr = u[i + 1] r[i] r[i - 1]; replaceuWith[expr_, h_Function] := expr /. u[x_] :> h[x] Alternatively, replaceuWith[expr_, h_Function] := expr /. u -> h Then, replaceuWith[expr, g[# + 1] f[# - 1] &] (* f[i] g[2 + i] r[-1 + i] r[i] *) replaceuWith[expr, f[# + 1] &] (* ...


1

f1[L_, n_] := For[i = 1, i <= Length[L], i++, If[Or[L[[i, 1]] > n , L[[i, 2]] > n], Print["Bad ", L[[i]]], Print["Good ", L[[i]]]]] You need to note the difference between: For[i = 1, i <= 10, i++; Print[i]] (* <= ;*) For[i = 1, i <= 10, i++, Print[i]] (* <= ,*) For[i = 1, i < 10, i++; Print[i]] (* < ; *) For[i = 1, ...


1

Maybe try If[! TrueQ[SQLConnectionUsableQ[XxSQLConnection]], ...



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