New answers tagged

1

Since ybeltukov already showed the usual recursive way to generate the associated Stirling numbers, let me just show that one can use the bivariate generating function from Comtet directly in SeriesCoefficient[]: With[{r = 2}, Table[n! SeriesCoefficient[Exp[u Exp[t] GammaRegularized[r, 0, t]], {t, 0, n}, {u, 0, k}], {k, ...


1

You may also use FromCoefficientRules and Indexed. ClearAll[poly]; poly[coeff_Symbol, vars_?VectorQ, order_Integer?Positive] := FromCoefficientRules[ Flatten@MapIndexed[#2 - 1 -> Indexed[coeff, #2] &, ConstantArray[0, ConstantArray[order + 1, Length@vars]], {Length@vars}], vars] poly builds a list of CoefficientRules using Indexed of the ...


5

For the sake of demonstration, let us suppose f[r_Real, p_Real, t_Integer] := r Sin[p t] With[{n = 10}, triples = Transpose[ {RandomReal[{0, 1}, n], RandomReal[{0, 2 Pi}, n], RandomInteger[{1, 10}, n]}]]; Note that this last definition can generate a list of triples of any size by changing the value given to n. Now, N. J. ...


4

The issue was due to a simple, but unobvious syntax mistake. The intended behavior is given by FoldList[cpdz[#1, 2, #2, 0.01, 2, 720] &, 1569.3 , #] & /@ SetofLists


4

One possibility: y[x_, n_Integer] := n Pi + {-1, 1} ArcSin[Sin[x]] Plot[y[x, 3], {x, -2 Pi, 2 Pi}] Here's another approach, in case you want the two "branches" to have different color: With[{n = 3}, Plot[Evaluate[n Pi + {-1, 1} ArcSin[Sin[x]]], {x, -2 Pi, 2 Pi}] ] If you use Evaluate you get the two colors, as shown; if, instead, you leave the ...


0

Your If contains C > 0, so when you try a3[2, list1, list2, list3], you are trying to compare a List with an Integer. That would not be evaluated. Try this: a3[m_, C_, St_, Stt_] := If[AllTrue[Flatten[C], Positive], (m*C)/St*Stt, 0]


1

I thought I'd offer a definition that works more like Mathematica's D function: HirotaD[a_, b_, x_, n_] := Module[{fa, fb, d, s}, ( fa = a /. (x -> (x + s)); fb = b /. (x -> (x - s)); d = D[fa fb, {s, n}]; d /. s -> 0 // Simplify )] HirotaD[a_, b_, x_] := HirotaD[a, b, x, 1] Which you can call like HirotaD[a[x], b[x], x] (* ...


3

The following definition takes as arguments two pure functions, a and b, their argument x and the parameter n. HirotaD[a_, b_, x_, n_] := Module[{}, sol = D[a[x + y]*b[x - y], {y, n}] /. y -> 0 // TraditionalForm; Print[("\!\(\*SubscriptBox[\(D\), \(x\)]\)")^n, "=", sol]]; It works on general functions, not yet defined. HirotaD[a, b, x, 1] (*...


2

Instead of using ((Subscript[a, Row[#]])&/@idx) or ((ToExpression["a"<>ToString[Row[#]]])&/@idx) or Array[coeff, Length@#] or a[i, j, k] it is possible to simply use Unique["a"] to get nice simple unique coefficient names.


2

Whenever the current computation depends upon the result of the previous step, think FoldList. FoldList is a fast and efficient function to use for those cases. Let's look at this for three steps where the inputs are symbolic (tip: don't use upper case symbols, you may end up clashing with system symbols). vt[ea_, v0_, cm_, b_] := ea + (v0 - cm)*b ea = {...


3

None of the answers thus far used one of my favorite Mathematica functions. Thus, With[{vars = {x, y, z}, deg = 3}, Sum[With[{fs = FrobeniusSolve[ConstantArray[1, Length[vars]], k]}, Inner[#2^#1 &, fs, vars, Times].(C @@@ fs)], {k, 0, deg}]] C[0, 0, 0] + z C[0, 0, 1] + z^2 C[0, 0, 2] + z^3 C[0, 0, 3] + y C[0, 1, 0] + y z C[0, 1,...


6

for a one liner poly[{x_,y_,z_},n_,a_]:= Sum[a[i, j, k] x^i y^j z^k, {i, 0, n}, {j, 0, n - i}, {k, 0, n - i - j}]


1

RecurrenceTable[{vt[n + 1] == EA + (vt[n] - Cm)*B, vt[1] == 3500}, vt, {n, 1, 10}]


4

another way poly[vars_List, a_, order_] := Module[{n = Length@vars, idx, z}, idx = Cases[Tuples[Range[0, order], n], x_ /; Plus @@ x <= order]; z = Times @@@ (vars^# & /@ idx); z.((Subscript[a, Row[#]]) & /@ idx) ] poly[{x, y, z}, a, 3] (*a is used for coefficient*) poly[{x, y, z}, a, 2] poly[{x, y}, a, 2] poly[{x}, a, 4] ...


8

polynomial[vars_List, n_Integer, coeff_] := #.Array[coeff, Length@#] &@ DeleteDuplicates[Times @@@ Tuples[Prepend[vars, 1], n]] Clear[a] polynomial[{x, y, z}, 3, a] (* a[1] + x a[2] + y a[3] + z a[4] + x^2 a[5] + x y a[6] + x z a[7] + y^2 a[8] + y z a[9] + z^2 a[10] + x^3 a[11] + x^2 y a[12] + x^2 z a[13] + x y^2 a[14] + x y z a[15] + x z^2 ...


0

I know of no method to achieve the specific syntax and evaluation that you want that I would recommend in practice. However for the sake of discussion a way to avoid the unwanted evaluation of the argument is to only define UpValues on f, which only evaluates f[x] when it has some surrounding expression. To get evaluation of a bare input/output we need ...


2

I don't think up-values are the way to go. I think you should write a new function, say diag, that has a special behavior for f, but works like Diagonal for any other args. Something like f[x_] := x.Transpose[x] f[x_, Diagonal -> True] := Diagonal[x] SetAttributes[diag, HoldAllComplete] diag[f[x_]] := f[x, Diagonal -> True] diag[args___] := Diagonal[...


5

Global` symbols are blue unless they have a value but if you use any other context that is on $ContextPath the symbol color will be black just because it was created. So use System` by mentioning option name when it is parsed for the first time: Options[TwoNumberSameQ] = {System`ShowDifference -> True} or define your function in Begin/EndPackage ...


3

Do you really need to return x from your function (even as part of a larger expression)? There is simply no good way to do this if x has a global value, as x = 1; x -> 2 immediately evaluates to 1 -> 2. You could return the solution value only (val instead of x -> val): SolveIt[a_, b_] := Module[{x, soln}, soln = Solve[a x + b == 0, {x}]; ...


3

As explained in Mathematica help, "Module creates a symbol with name xxx\$nnn to represent a local variable with name xxx. The number nnn is the current value of $ModuleNumber." This variable is not renamed after the module has completed. If instead the variable x refers to a global variable which already has a value (e.g. x=3), the x in Solve[eqn,x] will ...


3

I think the following does what you are asking for. ψcurl = Curl[{r, θ, ϕ} ψ[θ, ϕ, r], {r, θ, ϕ}, "Spherical"]; TEnorm[θ_, ϕ_, r_] = Abs[Norm[ψcurl]]; In this case, you do not want to delay evaluation of the assignment (use = rather than :=) making the Evaluate redundant. Putting [r, θ, ϕ] after ψcurl in the second line is wrong, as they don't appear in ...


4

The behavior you are encountering is the time taken by Mathematica evaluate all the Symbols in the System context, including definitions (and Options) that are only loaded on first use. (For one of my own encounters with this delayed loading please see Why do I have to evaluate this twice?) In a fresh Kernel observe that GraphPlot has no Options: Quit[] (...


4

To understand grouping and precedence, use HoldForm and PrecedenceForm. I'll insert a screenshot to make the output clearer: It is useful to know that // has even lower precedence than & and can save you some parentheses. You probably meant: ({#[[1]], #[[3]]} &) /@ ({#[[1]], #[[2]], #[[3]]} &) /@ {{"A", "B", "C"}, {1, 2, 3}} It is useful ...


2

Perhaps this example helps: 3 # & /@ Sin[#] & /@ {x, y, z} (*{Sin[3 x], Sin[3 y], Sin[3 z]}*) vs. 3 # & /@ (Sin[#] & /@ {x, y, z}) (*{3 Sin[x], 3 Sin[y], 3 Sin[z]}*)


1

If you really want to program Mathematica as if it is C, you could write total[u_List] := Module[{i, sum = 0}, For[i = 1, i <= Length[u], i++, sum += u[[i]]]; sum] but as suggested in the comments, there are very much better ways to do this. I would suggest understanding them all. Total[List1] or Plus @@ List1 or Fold[Plus, List1]


4

You can use my OptionsValidation framework to add options validation to your functions. We start by loading the package: Import["https://raw.githubusercontent.com/jkuczm/MathematicaOptionsValidation/master/NoInstall.m"] Now "register" tests you want to perform on option values. You do it by defining CheckOption for your function. ClearAll[func] Options[...


11

Simple greedy matching can be done as follows: construct a string pattern with longest words first and find words: StringCases["tableapplechairtablecupboard", Alternatives @@ SortBy[DictionaryLookup[], Minus@*StringLength]] (* {"table", "apple", "chair", "table", "cupboard"} *) Intuitive assumption is that one might use Longest to find longest match ...


2

You could use Clip: al[ Clip[x, {3, Infinity}] ]


3

I stopped debugging after the first line. The problem is you need p1 : {x1_, x2_} and you have p1_ : {x1_, x2_} where {x1_, x2_} is Optional for missing entry. Which makes xi_ not associated with an input at all, they will appear only when the input is missing. Unless you fix your code you can only use pi references. But the very outer statement is If ...


2

Maybe I misunderstood this problem? My solution is much more simpler(and readable, cause I'm simply too stupid to understand @jkuczm's code. I'll appreciate that if you may kindly add some explanation?) than @jkuczm's solution, but they generate the same result........ Code first: p[e_] := If[AtomQ@e, If[NumericQ@e, e, e[##]], p /@ e] f[e_] := Evaluate[p[e]...



Top 50 recent answers are included