# Tag Info

## Hot answers tagged functions

26

The symbols that are market [[EXPERIMENTAL]] in the documentation are in their own entity class of the "WolframLanguageSymbol" entity type, which is named "UnderDevelopment". EntityClass["WolframLanguageSymbol", "UnderDevelopment"] Here is a list of all the 25 symbols currently (version 10.3) in EntityList[EntityClass["WolframLanguageSymbol", ...

24

Mathematica does it internally by using BoxFormArrangeSummaryBox, which is quite straightforward to figure out: MakeBoxes[obj_MyObject, fmt_] ^:= Module[{o = List @@ obj, shown, hidden, icon = Graphics[{Blue, Circle[]}, ImageSize -> 70]}, shown = {{ BoxFormMakeSummaryItem[{"Name: ", "Name" /. o /. "Name" -> Missing[]}, fmt], ...

24

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

21

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

21

In addition to the error messages quoted in the question the line returns: GeneralUtilitiesBenchmarkingPackagePrivateplot[ 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 ...

18

It seems that there is a significant overhead every time a color scheme is switched. Once a scheme is loaded each use is fast, but changing color schemes apparently unloads and reloads the mechanism. The result is that the speed of application is directly related to the frequency of switching. With sorted values there is only one switch and application is ...

18

InternalPartitionRagged This one has a usage statement! InternalPartitionRagged[Range[14], {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[14], {3, 5, 2, 3}] InternalPartitionRagged[Range[14], {3, 5, 2, 3}] ...

18

fibSequences[n_?EvenQ] := Nest[Accumulate[Join[{1, 0}, #]] &, {}, n/2] fibSequences[n_?OddQ] := Most@Nest[Accumulate[Join[{1, 0}, #]] &, {}, (n + 1)/2] fibSequences[10] {1, 1, 2, 3, 5, 8, 13, 21, 34, 55} fibSequences[9] {1, 1, 2, 3, 5, 8, 13, 21, 34}

17

CompileInnerDo This is the one that initially struck me as interesting since I use compiled functions quite a lot. From the documentation of Do: Unless an explicit Return is used, the value returned by Do is Null. But that doesn't seem to be the case for CompileInnerDo! f1 = Compile[{{x}}, Module[{a}, a = x; CompileInnerDo[a++, {i, 10^8}]] ...

17

What's happening This is not simple by any means. You have encountered another instance of a general situation with lexical scope leaks / emulation / over-protection by symbol renaming. The case at hand is pretty similar to the one discussed here, so you can read the detailed explanation of this behavior in my answer there. Roughly speaking, outer lexical ...

16

The answers of the original questions by Szabolcs: What does Rescale do when infinities are present? What's the justification for this behaviour? Where is it documented? were guessed correctly with the comment: If I may be allowed to speculate, these were picked because they do the job advertised and are "conveniently" algebraic. They certainly ...

15

how to reproduce the default hashing behaviour when explicitly choosing a method Hash[1, "Expression"] (* 6568131406215528669 *) Hash[1, "Expression"] === Hash[1] (* True *) re-create the default hashing behaviour in different versions? Not possible as far as I know. The one-argument Hash implementation may change between ...

15

General When you define a type based on a head, like f[x_List, y_List]:=... the test happens entirely in the pattern-matcher, not involving the main evaluator. I call such patterns "syntactic". Pattern tests on such patterns are usually faster or much faster. The reason is that all the matching happens entirely in the pattern-matcher, and the latter only ...

14

There are subtle differences between #& and Identity. If you pass more than one argument, Identity will complain and remain unevaluated, #& will just return the first argument. Identity[x, y] (* Identity::argx: Identity called with 2 arguments; 1 argument is expected. >> *) (* Identity[x, y] *) #&[x, y] (* x *) Also Identity is ...

14

Suggested solution If I understood the question right, then the simplest solution here would probably be to define a helper function like the following: vv[n_] := InternalInheritedBlock[{v}, v /@ Range[n]]; Then, you get vel = vv[m] and every run of vv would result in different set of values, while the values in the set will all come from the same ...

14

This is a good example of why one should never blindly trust the numerical results of systems like Mathematica, without thinking about numerical methods that these systems use. Mathematica won't ever make numerical analysis courses obsolete. Most interpolation methods use piecewise polynomials, and assume slowly varying smooth functions. Your data has ...

13

It turns out ListSurfacePlot3D does a terribly poor job of approximating the surface in the OP, otherwise one will just apply DiscretizeGraphics to the output obtained from ListSurfacePlot3D and be done with it. But since that's not applicable here, we present an approach that uses alpha shapes to approximate the shape of the given point set by tuning a ...

13

Agree with other answers, this is a bad idea (why, precisely do you want to do this?), but in the spirit of encouraging unmaintainable write-once read-never code, here's my entry into the freak show: $NewSymbol = If[StringMatchQ[#, "f" ~~ NumberString], ToExpression[# <> "[x_]=x+" <> StringDrop[#, 1]]] &; Remove["f*"]; ... 13 You are looking for VertexOutComponent. VertexOutComponent[g, 4] gives you the successors of 4. Use Subgraph to get an actual graph out of those. With HighlightGraph, you can also use a subgraph, it will highlight both vertices and edges: HighlightGraph[g, Subgraph[g, VertexOutComponent[g, 4]]]. For visualizing the graph, use GraphLayout -> ... 12 Your basic requirement is met with: safeExport[file_String, args___] := If[ ! FileExistsQ[file] || ChoiceDialog["File already exists. Overwrite?"], Export[file, args],$Failed ] What you describe as "attributes" (e.g. PlotRange -> All) are known as Options or named optional arguments. (See Attributes for a description of what that ...

12

Properties for SparseArray and InterpolatingFunction objects SparseArray objects can accept a range of Properties (or Methods) that allow the efficient extraction of certain information, most commonly "AdjacencyLists" or "NonzeroPositions" as a frequently faster alternative to Position. I started this answer to detail them but as it grew I came to believe ...

12

fF[x__Integer] := FromDigits[Join @@ IntegerDigits @ {x}] fF[1, 2] (* 12 *) fF[2, 4, 65] (* 2465 *)

12

There are many closely related topics but I've failed to find a duplicate. MapThread[Thread @* f, {First @ list1, list2}] MapThread[f, {list2, list3}] {{f[a, 1], f[a, 2]}, {f[b, 3], f[b, 4]}} {f[{1, 2}, {x, y}], f[{3, 4}, {z, w}]}

11

As was pointed out above, this is a good summary of Mathematica's constrained optimization methods. Read through this if you want to know a lot more. A quick answer is below: The answer to your question is strongly dependent on the function you want to maximize. Convex functions can be maximized quite easily, with the error controlled by the PrecisionGoal ...

11

Yes, they are considered pseudo-listable. Often they are implemented with something similar to f[x_, a_List] := f[x, #]& /@ a

11

No, this is not possible since there is no forward compatibility for MX files. DumpSave will refuse to continue reading the file once it sees it was written by a newer version as demonstrated in the question. Having said that, there happen to be no changes in the format itself between 10.2 and 10.3, so in principle (after some surgery as shown below to ...

10

Most functions in Mathematica are actually global rules, with Function being an (very important) exception. Argument patterns in functions defined by rules are typically used as a flexible typing mechanism. You can go from completely untyped definitions (where you use patterns like _, __, etc.), to something in the middle (like e.g. {__List}), to completely ...

10

You may already have discovered that something like g[#]& doesn't work - this is because Function has the HoldAll Attribute, so its argument (g[#] in this case) doesn't get evaluated. The solution is to force g[#] to evaluate. Rasher showed what one way to do that, by using Evaluate, whose specific purpose is to force evaluation of arguments that would ...

10

Let's start by looking at the code for SequenceCases: << GeneralUtilities PrintDefinitions[SequenceCases] We can see in this code that three different conditions determine whether the expression will be evaluated by sequenceCasesSublist or sequenceCasesPattern. Let's evaluate the two tests that matter on {a_?PrimeQ, b_, c_?PrimeQ} and {a_?PrimeQ, ...

10

Just to be clear, I think this is a terrible idea but nevertheless, a question has been posed for which there is a simple answer: ClearAll@fn SetAttributes[fn, HoldAll] fn[h_[x_]] /; StringMatchQ[SymbolName@h, "f" ~~ DigitCharacter ..] := First@StringCases[SymbolName@h, "f" ~~ d : DigitCharacter .. :> x + ToExpression@d] ...

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