# Tag Info

30

InternalInheritedBlock (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; ...

22

Taking a limit depends on the path used to approach that limit. Consider the function in the question: f[x_, y_] := Piecewise[{{x y / (x^2 + y^2), x != 0 && y != 0}}, 0]; base = Plot3D[f[x, y], {x, -1, 1}, {y, -1, 1}, MeshStyle->Opacity[0.2], PlotStyle->Opacity[0.5]] (A plot of its graph, saved here as base, appears in subsequent figures.) ...

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').

20

Head can return any head. There is no predefined list. expr = myArbitraryHead[1, 2, 3]; Head[expr] myArbitraryHead A head does not even need to be a Symbol: expr2 = (2 Pi)[x, y, z]; Head[expr2] 2 π Most heads are shown explicitly in the FullForm of the expression: FullForm[{"a" + "b", 1/3}] Head /@ {"a" + "b", 1/3} List[Plus["a", "b"], ...

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

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, ...

15

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], ...

14

Use a dispatch table. It is an optimized element -> value lookup table that can be used to replace an element any time with its value. Now it does matching-and-finding every time, but if your list is not too big, this is pretty fast. dispatch = Dispatch[Thread[elements -> chemistry]]; ratio[elemA_, elemB_, disp_] := (elemA/elemB) /. disp; ratio[elemA_, ...

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

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 ...

13

As you say, this is a straightforward application of Fold, which is also perhaps the cleanest solution you can get. I'm guessing that you're seeking Table based approaches since you didn't want to deal with having to "fold properly". I'll complete the Fold application here so that you can see how it is applied: n = {n1, n2, n3, n4, n5}; d = {d1, d2, d3, d4, ...

13

Simple version using a variant of memoization While part of the answer I was going to give was already posted by Istvan, I will still post mine since the self-precomputing part was not part of Istvan's answer. The following will use the variant of memoization to precompute the dispatch table: ClearAll[elem]; elem[chem_, element_] := With[{dispatchTable = ...

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

Well, the smartass answer would be ContinuedFraction[E - 2, n] So your approximation function (if I understood correctly) would be FromContinuedFraction[ContinuedFraction[E - 2, n]] But the way I would generate this particular sequence manually is something along these lines: ls[n_] := Module[{s}, s = 2 Range[n]; {1, Riffle[s, {{1, ...

12

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] ...

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'll want Composition[] or ComposeList[] for the purpose: ComposeList[{f1, f2, f3}, x] {x, f1[x], f2[f1[x]], f3[f2[f1[x]]]} Composition[f3, f2, f1][x] f3[f2[f1[x]]] Since OP wants to be able to feed a list: (Composition @@ {f3, f2, f1})[x] f3[f2[f1[x]]]

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 DeveloperPartitionMap. 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 ...

10

You can do Hold[f[5]] /. DownValues[f] which would return to you the r.h.s. wrapped in Hold - which you can then strip or do anything else with it. Note that while this is a useful technique, in many cases it is not really needed, so I'd first reconsider the design of your functions, and only use the above if it is really necessary.As a light-weight ...

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 ...

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