Tag Info

Hot answers tagged

30

Internal`InheritedBlock (IIB) is similar to Block, except that it preserves the original definition of the function being passed to it. The function can then be modified as we wish inside the IIB without affecting the external definition. Let's see how Block works first: f[x_] := x Block[{f}, Print@DownValues[f]; f[x_, y_] := x y; ...


21

test = {5, 6, 9, 3, 2, 6, 7, 8, 1, 1, 4, 7} MaxFilter[test, 1] (* {6, 9, 9, 9, 6, 7, 8, 8, 8, 4, 7, 7} *) You can also use Max /@ Transpose[{Rest[Append[#, 0]], #, Most[Prepend[#, 0]]}] &[yourList] which is competitive with the MM MaxFilter, but will allow you to change the 'slide' (e.g.pad with zeroes, or other arbitrary 'start').


19

There was an update for Array, not done to the end. The method below does not work for earlier versions even though that Array is New in 1 | Last modified in 4 Moreover WRI forgot to update docs for error messages: Array::plen - the first example gives no error in V9. V9 Array[# &, n, {start, stop}] Array[# &, 10, {-1, 1}] {-1, ...


17

Not sure if this will work for you, but... There is a cool blog by Roman Osipov in Russian (use Google Translate to translate): Study of arbitrary functions by methods of mathematical analysis in the system Mathematica I will give 2 functions from there (see the blog for more tricks). The domain of the function DefinitionDomain[expr_, variable_: x] := ...


16

Have a look at this; http://reference.wolfram.com/mathematica/guide/MathLinkCLanguageFunctions.html I haven't used it in C/C++ but it works fine in C# and Java. Basically you create a connection to a Mathematica kernel and then pass it native data types. Works nicely. Here is some sample code in Java that I used when I first did this; import ...


15

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied: Join @@ Cases[expr, x : _Times :> Hold[x], 3] Hold[2/2, 8/4, 1/0] This could be done automatically as follows: makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, ...


15

There are nice trigonometric formulas δ = 0.01; trg[x_] := 1 - 2 ArcCos[(1 - δ) Sin[2 π x]]/π; sqr[x_] := 2 ArcTan[Sin[2 π x]/δ]/π; swt[x_] := (1 + trg[(2 x - 1)/4] sqr[x/2])/2; Plot[{TriangleWave[x], trg[x]}, {x, -2, 2}, PlotRange -> All] Plot[{SquareWave[x], sqr[x]}, {x, -2, 2}, PlotRange -> All, Exclusions -> None] Plot[{SawtoothWave[x], ...


15

Looking at the Trace of one which does work: x = Sin[Pi/5] (* Sqrt[5/8 - Sqrt[5]/8] *) Trace[ArcSin[x], TraceInternal -> True] It appears that Mathematica computes the ArcSin numerically and then recognises the result, 0.628319 as possibly equal to Pi/5. To check it computes Sin[Pi/5], and subtracts it from the original argument to see if it gets ...


14

Accumulate is absolutely the most idiomatic and appropriate answer here. However since Mathematica is very powerful at list manipulation, it might be illustrative to show you couple of other ways of doing the same thing, just so you learn to think outside of mainstream procedural ways. 1. Using FoldList: This is a functional way of doing exactly what you ...


14

Short story $$ \vartheta(x) = \arg \left[(\operatorname{Bi}x+i \operatorname{Ai}x)e^{-\frac{2}{3} i (-x)^{3/2}}\right]+\frac{2}{3} \operatorname{Re}\left[(-x)^{3/2}\right] $$ Update: I see that you want use only real functions, so you can expand this as $$ \vartheta(x) = \begin{cases} \arctan\frac{\cos \left(\frac{2}{3} (-x)^{3/2}\right) ...


13

I know two approaches to this: In[1]:= FullSimplify[SeriesCoefficient[ArcTan[y], {y, x, n}] n!, Element[n, Integers] && n > 0] Out[1]= 1/2 I ((-I - x)^n - (I - x)^n) (1 + x^2)^-n Gamma[n] and In[2]:= FullSimplify[InverseFourierTransform[(-I k)^n FourierTransform[ ArcTan[x], x, k] , k, x], Element[n, Integers] && n > 0] ...


13

The code for the default ComplexityFunction was posted on MathSource a number of years ago by Adam Strzebonski (of Wolfram Research). You will see reference to the original reply from Adam referenced in a MathGroup reply from Andrzej Kozlowski dated 12 Jan 2010 with the subject: "[mg106386] Re : Radicals simplify". I mention all that because I can't get the ...


12

You need partitioning, Partition and parameters: 2 for pairs, 1 for unit overhang/offset, and then averaging each pair, using Map, short-notated /@. Partition[{a, b, c, d}, 2, 1] {{a, b}, {b, c}, {c, d}} These will all make the averages: Mean /@ Partition[N@badSource, 2, 1] MovingAverage[N@badSource, 2] ListConvolve[{{.5}, {.5}}, badSource] ...


12

$Post is handy but it can get confusing when you want to use it for many things at once. I propose using MakeBoxes for this kind of thing as it is specifically intended for specifying formatted (Box) output. Interpretation is used to make the output work correctly as input. The right-hand-side of the definition can be either explicit *Box expressions or ...


12

The question made me wonder about zero-order interpolations. It's hardly clear which is the right way. When I tried to figure out why ListLinePlot would use a different Interpolation, I noticed it didn't seem to use an Interpolation for orders 0 or 1 at all, but did it the simple way which you might use by hand: connect the dots. This was probably done ...


11

You have many possible solution satisfying: Reduce[{f[-6] == -4, f[-5] == -5, f[1] == 3, f[2] == 2}, {a, b, c, d}] b == -10 a && c == -a && d == -2 a && a != 0 To find a few of those: FindInstance[{f[-6] == -4, f[-5] == -5, f[1] == 3, f[2] == 2}, {a, b, c, d}, Reals, 5] {{a -> -235, b -> 2350, c -> 235, d -> ...


11

The "unnecessary" complication is needed for those cases where you specify deeper levels than the first: MapIndexed[f, {{a}, {b}}, {2}] (* {{f[a, {1, 1}]}, {f[b, {2, 1}]}} *) The following code produces what you want: myMapIndexed[f_, l_] := Inner[f, l, Range[Length[l]], List]; myMapIndexed[f, {a, b, c, d}] (* {f[a, 1], f[b, 2], f[c, 3], f[d, 4]} *)


11

Yes, there is. Group your extra arguments in a list, and address them by their positions in the function under Fold. For your particular example: FoldList[#1 (1 + First@#2) - Last@#2 &, 1000, Transpose@{{.01, .02, .03}, {100, 200, 300}}] (* {1000, 910., 728.2, 450.046} *)


11

Using the fourth and fifth arguments of Partition gives you exactly what you want lis = {5, 6, 9, 3, 2, 6, 7, 8, 1, 1, 4, 7} Max @@@ Partition[lis, 3, 1, {2, 2}, {}] Gives: {6, 9, 9, 9, 6, 7, 8, 8, 8, 4, 7, 7} Update As Simon Wood suggested in the comment below (I also know this but on my system the difference isn't that much), Maping Max instead ...


11

Another option is Developer`PartitionMap. In RunnyKine's solution we first partition the list and sweep through it to add Max to every element. With Developer`PartitionMap we can do both at the same time, which is faster. Here's a table for reference. My first table was incorrect and I apologize for that, it was an honest mistake which I am not sure how it ...


11

It gives those errors because you explicitly specified that pfunc only has "test" as an option. OptionValue is finicky and will complain if it sees options that it doesn't recognize. There are a couple of alternatives that I can think of: 1: Use FilterRules everywhere instead of OptionValue ClearAll@pfunc2 pfunc2[x0_, plotopts : OptionsPattern[]] := ...


11

I don't know why no one mentioned this, but all you have to do is to use a special form of OptionsPattern: pfunc[x0_, plotopts : OptionsPattern[{Plot, pfunc}]] := your-code where inside OptionsPattern go all sub-functions you need, in a list. Now everything is fine and dandy. There might be a downside of this, in case when several sub-functions can take ...


10

A little bit more. Still not fully diagnosed, but the problem isn't due to DSolve ... : s1 = DSolve[{x'[t] == f*x[t] (1 - (x[t]/b)) - l x[t]}, x[t], t]; s2 = DSolve[{x'[t] == e*x[t] (1 - (x[t]/b)) - l x[t]}, x[t], t]; And the problem shows up when matching the initial condition: Solve[(x[t] /. s2[[1]] /. t -> 0) == 4/10, C[1]] (* {{C[1] -> ...


10

This is perhaps a place to start: position[expr_, level_: 1] := With[{positionData = SortBy[ #[[1, 1]] -> #[[All, 2]] & /@ GatherBy[Extract[expr, #, Verbatim] -> # & /@ Position[expr, _, level], First], Min[Length /@ #[[2]]] & ] // Dispatch}, Replace[#, positionData] & ] The second argument controls the ...


10

First, you can try to apply the FunctionExpand command to the DifferenceRoot object. If it is able to find a closed form of the sequence, then the Limit might be able to find an exact symbolic limit. To find a numerical approximation, you can use the SequenceLimit command. In general, it does not guarantee to give the correct result, but if your sequence ...


10

Shooting method with Manipulate One pragmatic approach of getting a solution for your boundary value problem is just guessing efficiently (which is what most numerical BVP codes do anyway...). A nice way of doing this in Mathematica after setting up our ordinary differential equation ode=1/\[Eta] D[\[Eta] ...


10

This appears to be a perfectly legitimate use of DownValues. These are often used by experienced users as a hash table. There are some ways you might improve this. First, you could use the value True directly, and it's arguably better to Scan than to Map, but I've used the latter often enough myself as it rarely matters. Scan[(both[#] = True) &, ...


10

I believe I have a solution for you, assuming we've worked out all the discrepancies in the original question. You will need my dynamicPartition function or one of its "core function" equivalents. process[data_List] := Module[{f, s1, s2}, f[_, {1, ___, 1}] = 1; f[_, {___, 0}] = 0; f[x_, _] := x; s1 = Split @ Rest @ FoldList[f, 0, data]; ...


10

If we define f[x] e.g. like this: f[x_] := Abs[x] the following returns interesting points: Reduce[ Limit[(f[x + h] - f[x])/h, h -> 0, Assumptions -> x ∈ Reals, Direction -> -1] != Limit[(f[x + h] - f[x])/h, h -> 0, Assumptions -> x ∈ Reals, Direction -> 1], x] x == 0 Let's try another function defined with Piecewise, ...


10

You are calculating the small difference between numbers which are getting quite large and the default WorkingPrecision of Plot (which is MachinePrecision, usually about 16) is just not high enough. So simply increase the WorkingPrecision of Plot, e.g. Plot[CoshIntegral[x] Sinh[x] - Cosh[x] SinhIntegral[x], {x, 0, 40}, WorkingPrecision -> 40, ...



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