Hot answers tagged

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

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

Internal`InheritedBlock What does Internal`InheritedBlock do? Daniel's MathGroup post Strange behaviour of Internal`InheritedBlock How safe is the use of Block and Internal`InheritedBlock Internal`Localizedblock What is the purpose of Internal`LocalizedBlock? Internal`Bag Internal`Bag 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: 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 ...


21

dat = {0.71, 0.685, 0.16, 0.82, 0.73, 0.44, 0.89, 0.02, 0.47, 0.65}; Module[{t = 0}, Split[dat, (t += #) <= 1 || (t = 0) &] ] {{0.71, 0.685}, {0.16, 0.82, 0.73}, {0.44, 0.89}, {0.02, 0.47, 0.65}} Credit to Simon Woods for getting me to think about using Or in applications like this. Performance I decided to make an attempt at a higher ...


19

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


18

Internal`PartitionRagged This one has a usage statement! Internal`PartitionRagged[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 *) Internal`PartitionRagged[Range[14], {3, 5, 2, 3}] Internal`PartitionRagged[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}


18

You seem to be re-evaluating the eigenvalues at every point. Just use this definition: Clear[Eval,kx, ky, kz]; Eval[kx_, ky_, kz_] = FullSimplify[ Eigenvalues[H[kx, ky, kz] + Subscript[H, 1][kx, ky, kz]]]; Then the plots will be faster. This will symbolically evaluate the eigenvalues once, and the variables kx, ky, kz get substituted into the ...


17

Compile`InnerDo 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 Compile`InnerDo! f1 = Compile[{{x}}, Module[{a}, a = x; Compile`InnerDo[a++, {i, 10^8}]] ...


17

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


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

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


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_] := Internal`InheritedBlock[{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

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


13

Usually, when one defines a function that's not too complex (usually a one-liner) it is customary (here we mean Mathematica custom) to define it directly without any scoping constructs (Module, With or Block). For example: myFunction[x_]:= 2 Sin[x] + Exp[-x^2] But as the function definition gets more complex, instead of polluting the Global context with ...


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


12

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


11

One approach is to employ a helper function that unwraps singleton lists: {delist[v_]} ^:= v With this, the GroupBy expression is fairly succinct: dataset // GroupBy[{#type&, #subtype& -> delist}] (* <| "a" -> <| "I" -> <|"type" -> "a", "subtype" -> "I", "value" -> 1|> , "II" -> <|"type" -> ...


11

Complement seems to work without stripping Association in both cases. Complement[b, a] (* <|"b" -> 5|> *) Complement[Sequence @@ KeyUnion[{b, a}]] (* <|"b" -> 5, "c" -> Missing["KeyAbsent", "c"]|> *) What version are you using?


11

It looks like this has not only been corrected in Mathematica 10.4, but has been made about 7 times faster than 10.1. $Version lis = Riffle[RandomReal[100, 10^6], -1]; First @ AbsoluteTiming @ HistogramList[lis, {0, 6, 0.02}] "10.4.0 for Microsoft Windows (64-bit) (February 26, 2016)" 0.12289


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


10

I would define your recursion as follows: f[0] = x + Abs[x - 100] - Abs[x + 100]; f[n_] := Abs[f[n - 1]] - 1 You can then plot the value of f[100]; you will notice that it is bounded between $-300\le x\le 300$, but it grows linearly outside those boundaries: Plot[f[100], {x, 250, 320}] Plot[f[100], {x, -320, -250}] The plot suggests that the $f(100) ...



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