# Tag Info

58

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; Print@...

57

These three functions are similar (speaking commonly), and in some applications any of them could be used, yet they have very different special applications. Rudimentarily: Map wraps (sub)expressions in a given Head, and returns the modified input Apply replaces Heads in (sub)expressions, and returns the modified input Scan "visits" (sub)expressions, ...

57

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

52

The following simulates Mathematica's behaviour after using it for more than 24 hrs. MathLinkCallFrontEnd[FrontEndUndocumentedCrashFrontEndPacket[]] Works as advertised! :D

50

Mathematica does it internally by using BoxFormArrangeSummaryBox, which is quite straightforward to figure out. Example ClearAll[MyObject]; MyObject /: MakeBoxes[obj : MyObject[asc_? myObjectAscQ], form : (StandardForm | TraditionalForm)] := Module[{above, below}, above = { (* example grid *) {BoxFormSummaryItem[{"Name: ", asc["...

46

Compose and Composition There is, but it is deprecated (in favor of Composition): Compose: MapThread[Compose, {{a, b, c}, {1, 2, 3}}] (* {a, b, c} *) I still use Compose myself, but I would not take the responsibility to recommend this as a common practice. You can also use Composition[#1][#2] &, although this is hardly better than your ...

46

First, let's enumerate some of the functions: Fit[] is the simplest of fitting functions. It has been introduced in v5, and hasn't been updated since v6 (as of v11). It finds a least-squares fit as a linear combination of functions. As such it can only be used for simple functions. Fit[] can fit $c\sin{x}$ but not $\sin{c x}$, where c is an unknown ...

45

Sequential With From Daniel Lichtblau's comment there is a new undocumented syntax for With introduced sometime after version 10.1 that allows: With[{a = 0}, {a = a + 1}, {a = a + 1}, a] 2 Delayed With, Block, and Module These scoping constructs support the use of := in definitions which allows one to handle unevaluated expressions. With[{x := 2 + 2}, ...

45

Animated Mathematica Functions contains cool animated illustrations of the way a number of built-in functions work. Among them are Thread Inner: Outer See also: cormullion's video

44

Let you have a function and an initial point f[x_] := Cos[x] x0 = 0.2; Then you can calculate a sequence seq = NestList[f, x0, 10] (* {0.2, 0.980067, 0.556967, 0.848862, 0.660838, 0.789478, \ 0.704216, 0.76212, 0.723374, 0.749577, 0.731977} *) and vizualize it with a so-called Cobweb plot p = Join @@ ({{#, #}, {##}} & @@@ Partition[seq, 2, 1]); ...

42

First let me note that I didn't write PositionIndex, so I can't speak to its internals without doing a bit of digging (which at the moment I do not have time to do). I agree performance could be improved in the case where there are many collisions. Let's quantify how bad the situation is, especially since complexity was mentioned! We'll use the ...

37

InternalPartitionRagged This one has a usage statement! InternalPartitionRagged[Range, {3, 5, 2, 4}] {{1, 2, 3}, {4, 5, 6, 7, 8}, {9, 10}, {11, 12, 13, 14}} Note that Length[list] must equal n1 + ... + nk. (* changed the last 4 to 3 *) InternalPartitionRagged[Range, {3, 5, 2, 3}] InternalPartitionRagged[Range, {3, 5, 2, 3}] InternalS1,...

36

InternalInheritedBlock What does InternalInheritedBlock do? Daniel's MathGroup post Strange behaviour of InternalInheritedBlock How safe is the use of Block and InternalInheritedBlock InternalLocalizedblock What is the purpose of InternalLocalizedBlock? InternalBag InternalBag inside Compile Daniel Lichtblau's answer On the use of of ...

36

System The default value for the overhang parameter k (3rd argument) in ListCorrelate is None. Internal Predicates InternalLinearQ[expr, var] yields True if expr is a polynonial of exactly order one in var, and yields False otherwise. InternalRationalFunctionQ[expr,var] returns True if expr is a rational function of the symbol var, and returns False ...

34

The behavior described here is the same from 10.0.0 up to at least 10.3. Summary We can look at the code of DeleteDuplicatesBy and it turns out it uses GroupBy. The test cases proposed by Mr.Wizard are all handled by some part of the code of DeleteDuplicatesBy. Other parts of this code also seem to have some issues. Most of the members of the *By family of ...

32

Short answer The local variables of the form varname\$... are used by the system, and it is unwise to use symbols with such names as local variables. With, like many other lexical scoping constructs, performs excessive renamings, often even in cases where it isn't strictly necessary. This probably has to do with efficiency - full analysis may be more costly. ...

31

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], swt[x]...

31

The term pure function used in Mathematica is not being used in the same sense as the cited Wikipedia article. In Mathematica it refers to an anonymous function. In the Wikipedia article it is a term extracted by analogy from the increasingly popular term "purely functional" which refers (mainly) to deterministic programming free of side-effects. The ...

31

Performance issues with large numeric arrays very often happen because of unpacking of PackedArrays. Numeric operations on Packed arrays are very well optimized, while unpacking and operations on huge unpacked lists are usually slow. If you turn on packing warnings (On["Packing"]) before your tests, you will see that applying Plus to packed array leads to ...

31

data = WolframLanguageData[{"Select", "FileNames", "StringContainsQ", "Or", "StringJoin", "ToString"}, {"Name", "DateIntroduced", "DateLastModified", "VersionIntroduced", "VersionLastModified"}]; TableForm[data, TableHeadings -> {None, {"Name", "DateIntroduced", "DateLastModified", "VersionIntroduced", "VersionLastModified"}}] So ...

30

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

30

The following is based on the fact that the determinant of a matrix is equal to zero when two rows are the same. Thus, if you plug any of the points in, you get a true statement. SeedRandom; pts = RandomReal[{-1, 1}, {5, 2}]; row[{x_, y_}] := {1, x, y, x*y, x^2, y^2}; eq = Det[Prepend[row /@ pts, row[{x, y}]]] == 0 (* Out: 0.0426805-0.0293168x-0....

30

Edit: This solution no longer works due to changes in the Entity framework. The "UnderDevelopment" EntityClass no longer exists. It's not part of EntityClassList["WolframLanguageSymbol"] or WolframLanguageData["Classes"] anymore. The symbols that are market [[EXPERIMENTAL]] in the documentation are in their own entity class of the "WolframLanguageSymbol" ...

29

I see no mention of the new-in-10 PositionIndex in the other answers, which takes a list (or association) of values and returns a 'reverse lookup' that maps from values in the list to the positions where they occur: In:= index = PositionIndex[{a, b, c, a, c, a}] Out= <|a -> {1, 4, 6}, b -> {2}, c -> {3, 5}|> It doesn't take a level ...

29

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, -(7/9), -(5/9)...

29

I think of Outer just like nikie showed. Inner is a generalization of matrix multiplication. I like the picture from the Wikipedia page. To calculate an entry of matrix multiplication, you first pair list entries (a11,b12) and (a12,b22). You "times/multiply" those pairs (a11*b12) and (a12*b22), and then you "plus/add" all the results (a11*b12)+(a12*b22). ...

29

Here is the example from the documentation adapted for the OP's data: data = MapIndexed[ Flatten[{#2, #1}] &, {2, 5, 9, 15, 22, 33, 50, 70, 100, 145, 200, 280, 375, 495, 635, 800, 1000, 1300, 1600, 2000, 2450, 3050, 3750, 4600, 5650, 6950}]; f = Interpolation@data (* InterpolatingFunction[{{1, 26}}, <>] *) pwf = Piecewise[ Map[{...

29

A variation on Szabolcs's suggestion: overTake = PadRight[##, "Periodic"] &; overTake[{1, 2}, 5] {1, 2, 1, 2, 1}

28

f = {1, 5, 9, 14}; v = {-1, 1, 3, 4, 6, 9, 10, 13, 14, 15}; BinLists[v, {Join[{-Infinity}, f, {Infinity}]}] {{-1}, {1, 3, 4}, {6}, {9, 10, 13}, {14, 15}}

28

Good News Everyone! Two-parameter syntax for Fold and FoldList has been (silently) implemented! Taliesin Beynon informs me that this was implemented in 2011, so check your older versions as well. As Naitree notes this is now documented in 10.0.2: Fold[f, a] FoldList[f, a] f[f[f[1, 2], 3], 4] {1, f[1, 2], f[f[1, 2], 3], f[f[f[1, 2], 3], 4]} And the held ...

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