Tag Info

Hot answers tagged

15

In addition to the error messages quoted in the question the line returns: GeneralUtilities`Benchmarking`PackagePrivate`plot[ IndexBy[{{{16, 9.37132*10^-6}, . . . IndexBy was removed from 10.1.0: Note that IndexBy will be removed in a future version of Mathematica. It was something that was considered for 10.0.0 but didn't make the cut. – Stefan R ...


14

Introduction This post is long overdue as I have been repeatedly asked to explain code of mine containing these things. As I see increased use of this construct by others perhaps it is past due also. SparseArray objects can behave as functions accepting certain arguments to return internal data or efficiently return data in certain forms. These are known ...


8

This is more of a comment than an answer but it's too long for a comment box and I hope to extend it as I learn more. My first thought was that the negative value might be preventing some optimization so I looked for a counterexample and found something surprising: Table[ With[{a = RandomInteger[{1, 100000}, 1000000 + x]}, First @ Timing @ Do[Tally[a], ...


7

My guess is that Tally preallocates a number of bins equal to 10% of the length of the list. If the need exceeds that, then it probably has to reallocate the bins, apparently in a time-consuming manner. Table[ l1 = l2 = RandomInteger[{1, max}, 10 max]; l2[[-1]] = -1; {First@Timing@Tally[l1], First@Timing@Tally[l2]}, {max, 2^Range[12, 21]}] // ...


7

evenFQ[f_] := Simplify[f[t] - f[-t]] === 0 oddFQ[f_] := Simplify[f[t] + f[-t]] === 0 Examples: ef[x_] := x^2 of[x_] := x^3 evenFQ/@ {ef, of} {True, False} oddFQ/@ {ef, of} {False, True} evenFQ /@ {# &, Im, Sin, Tan, Sinh, Erf} {False, False, False, False, False, False} oddFQ /@ {# &, Im, Sin, Tan, Sinh, Erf} { True, ...


6

It was actually a lot easier than I thought f = Function @@ {vars, expr}


6

If you have a list of variables stored in vars in the desired order and expr is the function's formula, then f = Function @@ {vars, expr} will define a function for you. If you're content with the variables being in their sorted order and the expression expr is polynomial-ish (test Variables[expr] first), then f = Function @@ {Variables[expr], expr} ...


6

This is a comment rather than an answer, but it grew too long for the comment box, and I wanted to show graphics, so I'll leave it here in hopes that someone better versed in the inner workings of MMA might explain this behavior. I am using your definition of the wrapper function pp. First of all, I get a slightly different version of the "wrong" function: ...


6

In Mathematica you can use $\it{\text{expression // TeXForm}}$ to get the TeX code for any mathematical expression you have written down (also works nicely for arrays and matrices). For your problem Sum[ 1 + 2 Floor[Sqrt[r^2 - 3 x^2]], {x, -Floor[r/Sqrt[3]], Floor[r/Sqrt[3]]}] + Sum[2 Floor[ Sqrt[r^2 - 3 x^2] + 1/2], {x, -Floor[(r/Sqrt[3]) + ...


5

How about Table[{a, x /. Solve[a*x - 10 == 0, x][[1, 1]]} // N, {a, 1, 10}] ? You can then export to .csv using Export["ans.csv", %].


5

Three options that spring to mind: You can run Mathematica code without opening a notebook by treating it as a Mathematica script. You can tell Mathematica to evaluate certain cells every time the notebook opens by designating those cells as initialization cells. Under the evaluation tab in the menu there are different options for queueing cells for ...


5

As MarcoB posted before me this has to do with evaluation differences. Simply setting HoldFirst on pp produces the original plot: SetAttributes[pp, HoldFirst] pp[ψ_, options___] := ParametricPlot[{r, ψ}, {r, -1, 1}, options, PlotRange -> {Automatic, 1.05 {-1, 1}}] pp[ZernikeR[100, 0, r]] You will want this attribute anyway as without it the ...


5

Rather than imposing x>0 one can also do FullSimplify[ ForAll[x, myOddFunction[x] == myOddFunction[-x]]] which yields False.


4

tsw[n_?NumericQ] := Piecewise[{{1, n <= 2}, {-1, 2 <= n <= 4}}] Plot[tsw[n], {n, 0, 10}, Exclusions -> None]


4

Yet another approach; However you do not have to worry about truncating the signal, just scale it. Plot[WaveletPsi[HaarWavelet[], x/4], {x, 0, 10}, Exclusions -> None, PlotRange -> {{0, 10}, {-2, 2}}, AxesOrigin -> {0, -1.5}]


4

Using the option Evaluated->True or wrapping the first argument of Plot with Evaluated gives a 100x speed-up: ls1[t_] := Quantity[10, "Nanometers"]/t Plot[ls1[Quantity[t, "Seconds"]]/Quantity[1, "SpeedOfLight"], {t, 0, 10}] // Timing ls2[t_] := Quantity[10, "Nanometers"]/t Plot[ls2[Quantity[t, "Seconds"]]/Quantity[1, "SpeedOfLight"], {t, 0, 10}, ...


4

So it seems that Nest is limited to 32-bit integers for the 3rd argument. The way around this is to apply Nest to the result of the first ~2^32 Nests. Supplying his own function using Fold, @martin suggests f[x_, r_] := Nest[# + Log[#] &, x, r]; g[r_, times_] := Fold[f[#1, #2] &, f[2., r], ConstantArray[r, times - 1]]; {f[2., 3*10^3], g[10^3, 3]} ...


4

Here's a way to define a function that does your computation: With[{WN = WhiteNoiseProcess[NormalDistribution[0, 10]]}, aV[m_] := Module[{data, points, yBinLst, meanLst}, data = RandomFunction[WN, {1, 10000}]; points = data["Values"]; yBinLst = Partition[points, m]; meanLst = Mean /@ yBinLst;; Total[Differences[meanLst]^2]/(2 ...


4

Here's a simple fix/workaround. It appears that all you need to do to make your code work as intended is to make the following change to your $dateFormat assignment: $dateFormat = {"Year", "", "Month", "", "Day", "", "Hour"} This generates an identical string to your test example, but it appears that the empty strings are necessary to correctly parse the ...


4

You have defined the elements of group and could approach as follows: r[a_] = RotationMatrix[a Degree]; rot = {r90, r180, r270} = r /@ Range[90, 270, 90] ref = {rh, rv, d1, d2} = ReflectionMatrix[#] & /@ {{1, 0}, {0, 1}, {1, 1}, {-1, 1}}; tab = {IdentityMatrix[2]}~Join~rot~Join~ref; rules = {IdentityMatrix[2] -> "\!\(\*SubscriptBox[\(R\), ...


4

f1 = Which[# < 10, ## &[#, 0], # > 10, ## &[0, #]] &; f2 = ## & @@ Which[# < 10, {#, 0}, # > 10, {0, #}] &; f3 = ## & @@ {Append, Prepend}[[1 + Boole[# > 10]]][{#}, 0] &; f4 = ## & @@ {Identity, Reverse}[[1 + Boole[# > 10]]][{#, 0}] &; f5 = Which[# < 10, {#, 0}, # > 10, {0, #}] /. List -> ...


4

You need Simplify with an assumption: myOddFunction[x_] := x^3; Simplify[ Equal[myOddFunction[x], myOddFunction[-x]], x > 0 ] False Refine also works in this case, again with the appropriate assumption: Refine[Equal[myOddFunction[x], myOddFunction[-x]], x > 0] False


3

Just sample your function over a 2D window with a Table. f[x_,y_]:= 1-2*Sinc[2(x^2 + y^2)] step = .2; kern = Table[f[x,y], {x, -3, 3, step}, {y, -3, 3, step}]; The dimensions of the kernel can be returned with Dimensions[kern]. Experiment with values of step and window sizes. Now just do an ImageConvolve[img,kern] and optionally use ImageAdjust to ...


3

Given that you just want to relabel the square each time, it's probably easiest just to view this as a set of permutations. Imagine numbering the corners of the square clockwise from the upper left. When we do an operation on the square, we could then write down a list of the corner that's in each position. For example, {1,2,3,4} would correspond to the ...


3

I believe this question should eventually be closed as its elements have been addressed before. However since you are new I hope to give a better welcome than merely "go read this" etc. To that end: If you can solve your equation Symbolically you can use Solve once, then populate values of a as desired. The output format of Solve is a List of Lists of ...


3

You can also use Boole to truncate SquareWave: Plot[SquareWave[x/4] Boole[0 <= x <= 4], {x, 0, 10}, Exclusions -> None, BaseStyle -> Thick, Frame -> True, Axes -> False, PlotRangePadding -> {0, 1}] Using Piecewise[{{SquareWave[x/4], 0 <= x <= 4}}] instead of SquareWave[x/4] Boole[0 <= x <= 4] gives the same result.


3

Cases[list, x : {_, _} :> Plus @@ x] (* {3, 9, 6, e + f, Cos[b] + Sin[a]} *) Cases[list, x : {_Integer, _Integer} :> Plus @@ x] (* {3, 9, 6} *) Cases[list, x : {_, __} :> Plus @@ x] (* {3, 8, 9, 6, a + b + c, e + f, Cos[b] + Sin[a]}*) Cases[list, x : {___} :> Plus @@ x] (* {3, 2, 8, 9, 6, a + b + c, e + f, g, 0, Cos[b] + Sin[a]} *) Or ...


3

Edit: see the bottom of this post for the best solution. The documentation for CoefficientList says: The dimensions of the array returned by CoefficientList are determined by the values of the Exponent[poly, vari]. ser1 = Series[ArcTan[x]/(1 - x^2), {x, 0, 10}]; ser2 = Series[ArcTan[x]/(1 - x), {x, 0, 10}]; Exponent[ser1, x] Exponent[ser2, x] 9 ...


3

Here are a couple of approaches: using ParametricPlot Quiet@ParametricPlot[{Graham[10^-9, t], t}, {t, 0, 1}, AspectRatio -> Full] Let p be your plot and just extract points: ListPlot[Reverse /@ First[Cases[p, Line[x__] :> x, -1]], Joined -> True]


2

You just need fail as the last expression in the module, so that it gets returned: f[spend_] := Module[{fail = 0, returns, x}, For[k = 1, k <= n, k++, returns = RandomVariate[LogNormalDistribution[mu, sigma], {years}]; x = portfolio; For[i = 1, i <= years, i++, x = returns[[i]]*x - spend]; fail = fail + If[x <= 0, 1, 0];]; fail] ...



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