Hot answers tagged

34

General usage Here is what I think Using strings and subsequently ToString - ToExpression just to generate variable names is pretty much unacceptable, or at the very least should be the last thing you try. I don't know of a single case where this couldn't be replaced with a better solution Using subscripts is also pretty bad and should be avoided, except ...


33

We can use the Ramer-Douglas-Peucker algorithm to reduce the number of points. This algorithm was originally devised for processing map data. reg = BoundaryDiscretizeGraphics[ Text[Style["S", FontFamily -> "Verdana", FontWeight -> Bold]], _Text] poly = First@reg["BoundaryPolygons"]; pts = SimplifyLine[First[poly], 0.02]; Graphics@{EdgeForm[...


32

This has been discussed on comp.soft-sys.math.mathematica. The gist is that there are lots of Unicode characters you could use, e.g. \[LetterSpace] or \[UnderBracket] (you could consult https://reference.wolfram.com/language/tutorial/LettersAndLetterLikeForms.html for a long list), but I'd strongly urge you not to do that. Once you copy the code out of ...


30

The answer is quite simple. Most people want to multiply numbers without having to use the * symbol, e.g. 3x vs 3*x. So given that this exists in Mathematica, using () for function arguments would introduce ambiguity. Is f(x + y) meant to be f[x + y] or f*(x + y)? This is actually a problem Wolfram|Alpha faces since it allows for all forms of inputs. Other ...


29

So recently I've learned from John Fultz that RawBoxes are kind of verbatim indicator for MakeBoxes which is not well stressed out in documentation. This or I've missed the point but it doesn't matter, here we have handy way to do this: x = 5; ToExpression @ MakeBoxes[RawBoxes["x"] = 123]; x 123


27

Executing Trace on an expression reveals what is actually happening: Trace[Remove@x; x = 1] (*{Remove[Removed[x]];Removed[x]=1,{Remove[Removed[x]],Null},{Removed[x]=1,1},1}*) "the Wolfram Language always reads in a complete input expression, and interprets the names in it, before it executes any part of the expression." (see: https://reference.wolfram.com/...


26

You are looking for $NewSymbol which is run every time a new symbol is created. For example, let say you only want x, y, and z as symbols, then declare them initially In[63]:= {x, y, z} (*Out[1]= {x, y, z}*) Then, set $NewSymbol to issue a message when it is used, e.g. In[2]:= $NewSymbol::undeclared = "`1` was not previously declared."; In[3]:= $NewSymbol ...


24

You can use String "keys" for indexed variables, as I did for A combination of Set::setraw and Set::shape errors. The strings can have spaces or any other characters you want to use: var["Degree of the First Polynomial"] = (* stuff *); You also have a wide range of characters, many of which can be used in Symbol names. Go to menu Palettes > Special ...


20

You can use any built in operator modified with subscripts, superscripts, etc, and retain its precedence, for your own purposes. For example, say you want a general Apply operator like @@ that could work at any level. One could use create the operator @@ with a number subscripted for the level of Apply seems appropriate MakeExpression[RowBox[{fun_, ...


20

I mentioned this in a comment, but I believe this might really be the correct answer to your specific inquiry about underscore. You can escape underscore (Esc+_+Esc or Ecs+ls+Esc), which will give you a \[LetterSpace], which looks like underscore but is slightly lighter. This is just treated like a regular old letter and you can therefore use it in variable ...


19

Max[StringLength@Names["System`*"]] 38 Select[ Names["System`*"], 38 == StringLength[#] &] {"MultivariateHypergeometricDistribution"} As far as I can say there is no limit for lengths of symbol names, besides that of the memory limitation.


19

You can use EntityValue to find out what symbols can be atomic: EntityValue[EntityClass["WolframLanguageSymbol", "Atomic"], "CanonicalName"] {"AggregationLayer", "Association", "Audio", "BasicRecurrentLayer", "BatchNormalizationLayer", "BooleanFunction", "BoundaryMeshRegion", "ByteArray", "CatenateLayer", "ColorProfileData", "Complex", "...


16

No need to use *CharacterCode[] : letters[n_] := CharacterRange["a", "z"][[;; n]] letters[3] (* {"a", "b", "c"} *)


15

The definitions aren't being lost, they're being shadowed, as described in the tutorial on contexts. Mathematica doesn't warn you about this because it only warns when there is shadowing between contexts that are listed in the $ContextPath. Since Begin only changes $Context and not $ContextPath, you don't get a warning when the symbol that causes shadowing ...


14

Edit: method extended for multiple contexts and unlocking mehtod added. Let's protect whatever is a new symbol. In old answer I've manually excluded symbols matching name$digits but that wasn't necessary as according to $NewSymbol details: $NewSymbol is not applied to symbols automatically created by scoping constructs such as Module. BeginPackage["...


13

The * multiplication operator is rendered in InputForm: c = a b; c // InputForm a*b For producing/exporting strings: ExportString[c, "Text"] ToString[c, InputForm] "a*b" "a*b"


13

Nowadays, one can use Alphabet[] along with Take[] or Part[] + Span[]: Take[Alphabet[], 6] {"a", "b", "c", "d", "e", "f"} Take[Alphabet[], {19, UpTo[28]}] {"s", "t", "u", "v", "w", "x", "y", "z"} In addition to this, Alphabet[] is aware of alphabets other than the Latin alphabet: Take[Alphabet["Greek"], 5] {"α", "β", "γ", "δ", "ε"} Take[...


13

Although Chip's answer already suffices to address the question, I would like to quote here a relevant part of the dialog by Theo Gray and Jerry Glynn in their book Exploring Mathematics with Mathematica; as there does not seem to be an easily accessible online version or preview of the book anywhere, I hope the quotation is useful: … Theo: Satisfied?...


13

Extract all Greek letters from the documentation and make replacement rules: nb = Get @ FileNameJoin[{$InstallationDirectory, "Documentation", "English", "System", "Tutorials", "LettersAndLetterLikeForms.nb"}]; letters = Cases[nb, StyleBox[s_String, "TR"] :> s, {-2}]; letters = DeleteCases[letters, "π" | "∈"]; (* reserved Symbols *) names = ...


13

Cloud symbols are stored in cloud objects (under $CloudSymbolBase) and local symbols are stored in local objects (under $LocalSymbolBase), which can be addressed using CloudObject and LocalObject respectively. DeleteFile can be used for both cloud and local objects, for example DeleteFile[CloudObject["MySolution", $CloudSymbolBase]] DeleteFile[...


13

It's a type of otherwise harmless bug that tends to come and go with versions. I would suggest to report it to Wolfram, but also not to worry about it. Notice that the symbols have no associated definitions, which means that they will not interfere with your code. A potential way in which such a situation can arise is the following. Imagine you put Sqr[x_] ...


13

This usually means you've exported a local symbol from the context in which it was localized. Consider: Module[{x}, x] (* x$24939 *) Your results may vary. The Module localizes x to avoid conflicting with any previous definition. x$24939 is just another symbol similar to x, but with $24939 added to its name to insure that there's no confusion with any ...


12

In Mathematica version 10, you can also use Inactive to allow the Symbol to be created before doing the assignment. Here is an example: Clear["x"]; Activate[Inactive[Set][Symbol["x"], 3]] (* ==> 3 *) x (* ==> 3 *)


12

I agree that this is a bug. However, I want to point out that this usage of Except does not seem to be allowed in older versions. In version 9.0: We don't get the expected True answer. An error message is issued. The error message is also triggered by your example. In version 10.0: The error is not triggered by your example in version 10.0.2. (It is ...


12

It appears that PlusMinus is the only wrapper that is not Protected: Charting`PlotParser; (* preload *) Select[Charting`ParserDump`$pAllWrappers, FreeQ[Protected]@*Attributes] {PlusMinus} We can remove it from that list to correct this bug, but perhaps induce others: Charting`PlotParser; (* preload; do not remove! *) With[{ov := OwnValues @ Charting`...


12

I'd probably use x_Symbol in a function argument to control evaluation. Otherwise, one might do the following (thanks to @Leonid for pointing out an oversight). If the argument is to be evaluated before testing: SymbolQ = MatchQ[#, t_Symbol /; AtomQ[t]] & If the argument is not to be evaluated: SymbolQ = Function[s, MatchQ[Unevaluated@s, t_Symbol /; ...


11

Which documented Mathematica function has the longest name I assume then you want all of Mathematica, which includes all standard packages and contexts that come in the installation and not just in the System context. I just run some code I have and added a check to obtain this information. Here is the table. According to this: ...


11

The issue is that Mathematica interprets underscore as Blank and interprets my_variable as a Pattern, when what we'd like is a legitimate symbol name. Head[Unevaluated[my_variable]] (* Pattern *) There are a few Unicode alternatives for underscore: combining low-line combining macron below modifier low-letter macron figure dash fullwidth low-...


11

Core Function Here's a single function that does all this coloring stuff: FESetSymbolColoring[ {syms__}, cont : _String | Automatic : Automatic, contPath : {__String} | Automatic : Automatic, which : "Undefined" | "Removed" | "Defined" | "Cleared" | 1 | 2 | 3 | 4 | {(1 | 2 | 3 | 4 | "Undefined" | "Removed" | "Defined" | ...


11

A quick way is to use TagSet[] and TagSetDelayed[] to teach Power[] how to deal with your special symbol: e /: e^0 = 1; e /: e^1 = e; e /: e^(n_Integer) := e^Mod[n, 2] Thus: Table[e^k, {k, 0, 5}] {1, e, 1, e, 1, e}


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