New answers tagged

6

One must somehow assert the domain $\mathbb{R}$ because the default domain is $\mathbb{C}$. The symbol RealAbs is one such way: RealAbs'[1] 1 Also note that just because there are no explicit complex numbers doesn't mean the derivative exists over $\mathbb{C}$. Here's the derivative at z == 1 over both domains: dq = DifferenceQuotient[Abs[z], {z, h}] /. ...


5

ClearAll[f] f[i__Interval] := Min @ IntervalIntersection[i] /. Infinity -> {} f[l__Line] := Module[{int = Interval /@ Sort[{l}[[All, 1, All, 1]]]}, f @@ int] Examples: f[Interval[{5, 10}], Interval[{6, 11}]] 6 f[Line[{{5, 1}, {10, 1}}], Line[{{6, 2}, {11, 2}}]] 6 f[Interval[{5, 100}], Interval[{7, 9}]] 7 f[Interval[{5, 10}], Interval[{11, 15}]] ...


3

Mathematica uses copy-on-write. No copies are made until necessary. Passing an argument to a function does not cause a copy to be made.


3

Array[Fibonacci, 450, 1, CountsBy[OddQ] @* List] <|True -> 300, False -> 150|> Also Array[Fibonacci /* OddQ, 450, 1, List /* Counts] <|True -> 300, False -> 150|> and 450 // Range /* Fibonacci /* CountsBy[OddQ] <|True -> 300, False -> 150|>


7

Similar to mgamer's nice answer: Mod[Array[Fibonacci, 450], 2]//Counts <|1 -> 300, 0 -> 150|> Edit To 'borrow' the neat suggestion made by rcollyer in a comment to rgamer's answer: Array[Fibonacci, 450] // CountsBy[OddQ] <|True -> 300, False -> 150|> Edit 2 lhf points out in a comment that "$F_n$ is even iff $n$ is a multiple of 3" Table[...


7

Let's find the conditions under which a Fibonacci number is odd or even. Happily, Reduce has us covered: isOdd = Reduce[Mod[Fibonacci[n], 2] == 1, n, Integers] (* Element[C[1], Integers] && (n == 1 + 3 C[1] || n == 2 + 3 C[1]) *) isEven = Reduce[Mod[Fibonacci[n], 2] == 0, n, Integers] (* Element[C[1], Integers] && (k == 0 && n == 3*...


2

This is an extended comment. Clear["Global`*"] Since you know that you are dealing with the GammaDistribution, you should make use of the built-in functions rather than doing multiple integrals. dist = GammaDistribution[a, 1/l]; To be a valid distribution requires $Assumptions = DistributionParameterAssumptions[dist] (* a > 0 && 1/l > 0 *)...


9

First the Fibonacci numbers out = Table[Fibonacci[n], {n, 450}]; then counting.... Mod[#, 2] & /@ out // Counts (<|1 -> 300, 0 -> 150|>)


7

Need to throw an Abort[] when it times out to catch it. res = CheckAbort[TimeConstrained[Pause[2], 1, Abort[]], 0] (*0*) or res = CheckAbort[TimeConstrained[Pause[2], 1, Abort[]],"Oh no, timed out"] (* Oh no, timed out *)


1

If you plot it this way: Plot[{Re[f[x]], Im[f[x]]}, {x, 0, 5}, PlotStyle -> {Red, Blue}] with the folloowing effect: you will see that the function has everywhere the real part (red), and in most part has also the imaginary one (blue), though there are several intervals where the imaginary part is equal to zero. No contradiction. Have fun!


0

You can also use a placeholder wrapper symbol, like so: Module[{x, expr, f}, expr = 2 x; Attributes[f] = {HoldAll}; f[x, Evaluate[expr]] /. f -> Function] Mathematica provides 1001 ways to skin this particular cat, but this one has been useful to me on some occasions.


0

As noticed by J.M. in a comment, it is usually not necessary to replace an option since " options are parsed left to right, and leftmost ones have higher priority" More details in this older question. Filtering rules But let's pretend here I really really want to replace a rule in the options list. A possible solution in two steps would be: Using ...


3

The general case: I can mention some patterns with different interfaces. Just a small example. f1 is just a list of functions. f2 is a vector valued function. f3 is like f2, but encapsulates the indexing as function parameter. f1 = Table[With[{nn = n}, Function[x, x^nn]], {n, 1, 5}] f1[[2]][x] f2 = Function[{x}, Table[x^n, {n, 1, 5}] // Evaluate] f2[x][[2]]...


4

You have a broad question - a few different ones, in fact. To answer the first one, you can use TrueQ which tests whether an expression is equal to True or not: g[f_]:=TrueQ[PolynomialMod[f, 2] == 0] g[x+y] (* False *) g[2x + 4y] (* True *)


0

The normal way is simply FD1[(d - 1)/t, \[Eta]] - FD1[(d - 1)/t, \[Eta] - vd]/.AsymptoticSolve[eqn, {\[Eta]s, 2^(-(t/d)) nd^(t/d)}, {vd, 0, 2}]


1

I'd say you can't be very sure unless you have an idea of the number of votes cast, or a more exact number than 0.29. The easiest way for me to check is: sol = Solve[x/(x + y) == .29, y] // Flatten (*{y->2.44828 x}*) Then Table[{x, Round[y /. sol], x/(x + Round[y /. sol])}, {x, 1, 10}] // N 1. 2. 0.333333 2. 5. 0.285714 3. 7. 0.3 4. 10. 0....


11

It comes down to being consistent with the convention/idiom/pattern of WL functions typically having as a first argument an object to be operated on with typically the second argument doing the operating. Operator forms, in contrast, most naturally take as a first argument the thing doing the operating which is therefore most commonly the original function's ...


4

Clear["Global`*"] f = (-(x1 - x11)^4 - 6 (x1 - x11)^2 (x2 - x22)^2 + 3 (x2 - x22)^4)/(4 Pi ((x1 - x11)^2 + (x2 - x22)^2)^3); An indefinite integral, i.e., antiderivative is not unique ad1 = FullSimplify@Integrate[f, x1, x2, x11, x22] (* (1/(16 \[Pi]))(3 (x1 - x11)^2 - 8 (x1 - x11) (x2 - x22) ArcTan[(x1 - x11)/( x2 - x22)] + (-3 (x1 - x11)^2 +...


3

Since 11.3 a range of Asymptotic based functions are available. See: https://blog.wolfram.com/2018/07/26/big-o-and-friends-tales-of-the-big-the-small-and-every-scale-in-between/ Your MinBigOh[2x+3, x^2] could be implemented as AsymptoticLessEqual[2 x + 3, x^2, x -> Infinity]


0

Ok, as far as I see you need to interpolate. For your simple example: h[y_?NumericQ] := NIntegrate[y*Sin[Sin[x]], {x, 1, 2}] h1 = Interpolation[Table[{y, h[y]}, {y, 1, 10, .01}], Method -> "Spline", InterpolationOrder -> 2] h[2]==h1[2] (*True*) h1 is your function


3

Please try this code. Assuming it works for you feel free to ask for clarification as needed. f = a*Cos[x] + b*Sin[x]; vars = Variables @ Level[f, {-1}]; pats = Pattern[#, _] & /@ vars; g[pats] = f; coeff = DeleteCases[vars, x]; ranges = {#, 0, 1} & /@ coeff; With[{body = g[vars]}, Manipulate[PolarPlot[body, {x, 0, 2 Pi}], ##] & @@ ranges ...


4

ClearAll[r, f] sol = NDSolve[{rr'[t] == rr[t] Cos[t + rr[t]], rr[0] == 1}, rr, {t, 0, 20}]; r[t_?NumericQ] := rr[t] /. sol f[u_] := 2 u + 3 u^2 Plot[{r[t], f[r[t]]}, {t, 0, 20}, PlotLegends -> {"r[t]", "f[r[t]]"}]


0

Try nn = Table[ If[z[n] < 0.05*n, n, Nothing], {n, 1,3000}] ; ListPlot[Table[{n, z[n] }, {n, nn}]] Alternatively DiscretePlot and RegionFunction works: DiscretePlot[z[n], {n, 1, 3000},RegionFunction -> Function[{n, z}, z < .05 n ],PlotRange -> {0, 120}, AxesLabel -> {"n", "z[n]"} ] Don't know where the shadows come from… 88


7

Following the comments I am encouraging the use of brackets rather than subscripts or superscripts. Here is an example where a function may take a variable with a subscript or a variable without a subscript. The function will use pattern recognition to sort out how to behave. First we define the function. Note I start with a ClearAll[f] so that previous ...


3

Below I have defined a few different forms which will do exactly as you asked. While I, too, agree with the sentiments iterated by user @Hugh regarding the use of sub- and superscripts, that this is possible with the Wolfram Language must be noted and demonstrated. Regardless, please see the following: ClearAll[f]; f[Subscript[x_, i_]] := Subscript[x, i] i; ...


5

Assuming you trying to compute the sum of the digits for each of the first 30000 multiples of 31 Array[31*# &, 30000] // Map[Total[IntegerDigits[#]] &]


3

Another approach (with Mr.Wizard's suggestion of x_?AtomQ instead of x_Symbol): f[x][y][z] /. x_?AtomQ :> Return[x, ReplaceAll] (* f *) Function: rootHead = # /. x_?AtomQ :> Return[x, ReplaceAll] &; MarcoB's examples: lst = {f1[a][x, y], f2[a, b, c], f3[a, 2][3]}; rootHead /@ lst (* {f1, f2, f3} *)


1

NestWhile rootHead = NestWhile[Head, #, # =!= Symbol &, 1, ∞, -1] &; rootHead /@ {f1[a][x, y], f2[a, b, c], f3[a, 2][3]} {f1, f2, f3}


6

I think Level might be the purest way to handle this. dh = Level[#, {-1}, # &, Heads -> True] &; dh /@ {f1[a][x, y], f2[a, b, c], f3[a, 2][3]} {f1, f2, f3} Other perhaps useful or entertaining methods: lst = {f1[a][x, y], f2[a, b, c], f3[a, 2][3]}; Scan[Return, #, {-1}, Heads -> True] & /@ lst Do @ MapAll[Break, #, Heads -> True] &...


Top 50 recent answers are included