# Tag Info

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

27

Superscript is not interpreted as Power: Presumably you are referring to what happens when you enter a power in superscript notation using the key combination Ctrl+6. Mathematica is capable of representing both this power notation and a formatted plain Superscript. In my opinion it is a failing that the power notation appears in the Typesetting menu while ...

27

Basic proposal There are a number of options and their attractiveness will depend on the scenario for their use, therefore it is difficult to make any broad recommendations of best practice. I will say that generally it is not recommended to rely on global assignments as in your first example, because this method scales poorly and because it is easy to make ...

27

You can also use Refine with Element : Refine[Sqrt[2] Conjugate[Sqrt[1/L]] Sin[(Pi* Conjugate[n x])/Conjugate[L]], {Element[L, Reals], Element[n, Integers]}] gives and if you add that L>0: Refine[Sqrt[2] Conjugate[Sqrt[1/L]] Sin[(Pi* Conjugate[n x])/Conjugate[L]], {Element[L, Reals], Element[n, Integers], L > 0}] Other simple examples : 1. ...

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

25

If you wrap your definitions in Once then their results will be remembered across sessions: f[0] = Once[Print["a"]; {10, 20, 30}, "Local"] Here the printing and the numbers {10, 20, 30} are used instead of a lengthy calculation that you only want to do once and whose result you want to remember in the next session. On the first execution, the above code ...

23

General The definitions get reordered at definition-time by a part of the pattern matcher, that takes care of automatic rule reordering. It does so, based on relative generality of rules, as far as it is able to determine that. This is not always possible, so when it can't determine which of the two rules is more general, it appends the rules to DownValues (...

22

In general, it is good practice to include i among the local variables. Table does not localize its variable (or, as some say, it only localizes the value but not the variable). It is relatively safe to leave i unlocalized when variables only have numeric values, like in fun. But the same is not true when variables can have symbolic values, e.g.: fun2[x_]...

22

Actually we have direct control over this via a System Option. Set: SetSystemOptions["DefinitionsReordering" -> "None"]; Then: Clear[f]; f[x_] := Sin[x]; f[x_?EvenQ] := x; f[x_?OddQ] := x^2; {f[1], f[2], f[3], f[4], f[3/2], f[Newton]} {Sin[1], Sin[2], Sin[3], Sin[4], Sin[3/2], Sin[Newton]} Restore the default behavior with: ...

22

You asked for a general explanation instead of just focusing on specific application examples, so here it goes ... The concepts of "pass by reference" and "pass by value" that you may know from languages like C do not apply very well to Mathematica. Do not try to think in this framework. The right question is not "how to pass by ...

21

In V10, another option is to use Association. par=<|"mu"->1,"sigma"->1,"lb"->0,"ub"->10|>; f[x_, p_Association:par] := PDF[LogNormalDistribution[p["mu"], p["sigma"]], x] Plot[f[x, ##], {x, #lb, #ub}] &@par Another form for Plot is: Plot[f[x, par], {x, par@"lb", par@"ub"}] And as @Mr.Wizard commented, you can use the default value ...

21

I think the documentation needs to be more clear on this; the order of definitions is important: Remove[plus] Attributes[plus] = {Orderless}; plus[x__Integer, y__Real] := x + y plus[2.5, 3] 5.5 So the Orderless attribute must be active at the time the definition is created. Noteworthy is that definitions made before setting the attribute can cohabit ...

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

Why is this happening The explanation was basically given by ciao in comments. You can also find a lot of information on this in this great answer of Mr.Wizard. I will perhaps try to view it from a somewhat different perspective. To understand what happens, one should go back and consider what happens when we enter and execute some code. The steps are ...

19

You almost have found a simple solution: try x[i] instead of x[[i]] Solve[{x[1] + x[2] == 2, x[1] - x[2] == 1}, {x[1], x[2]}] {{x[1] -> 3/2, x[2] -> 1/2}} List of this variables: Array[x,2] {x[1], x[2]}

18

General considerations To my mind, the only robust way to do this is to build some custom object model in Mathematica, and in particular to restrict the way values can be changed to some well-defined route you can control. Because, as it follows from one of the discussions you linked to, there seems to be no reliable way to intercept arbitrary value changes ...

17

You can give the function one of the Hold Attributes. SetAttributes[fun, HoldFirst] Then as Leonid suggested fun[var_] := SymbolName[Unevaluated@var] Without the hold attribute, this will not work.

17

Use Symbol to convert a string into a symbol... Table[Symbol["$x" <> ToString@i], {i, 5}] {$x1, $x2,$x3, $x4,$x5} One word of caution. I tend to keep programmatically generated variables prepended with a $to avoid any collisions with any other variables I might've defined. Just from experience. 16 According to the documentation of Clear or ClearAll it is possible to provide symbols in form of regular expression (limited), in particular as string with exact symbol name. Clear @@ {"width", "long", "line", "distance"} Let's say there is no possibility to do that, one way would be: Map[Clear, ToExpression[{"width", "long", "line", "distance"}, ... 16 I will make no attempt to defend the fact that Mathematica simulates scoping by means of variable renaming. However, the behaviour that we see is consistent with the principles under which Mathematica does operate. Whenever Mathematica tries to interpret a symbol name, it first checks to see whether a symbol with that name already exists in a package in ... 15 Overview Here's two functions that operate in a manner close to what you want. The first uses a locally defined context to provide the scoping. The second uses Block, and is likely closer to what you wish. Version 1 Here's a single function that does what you want: ClearAll[Scope]; SetAttributes[Scope, HoldAll]; Scope[{globals : (_Symbol | _Set | ... 15 {x1, x2} = x /. Solve[x^2 + 3 x + 2 == 0, x] {-2, -1} 15 J. Fultz' answer indicates that it is not a bug but a feature which needs better documentation. Get a habit of defining your procedures with [], e.g. f[]:=Print[1]. (Which creates DownValues instead of OwnValues). DynamicModule saves its variables' OwnValues in the first argument of that DynamicModule. Which does not support SetDelayed and is sliently ... 15 Mathematica is an expression rewriting language. When it evaluates: q[[RandomInteger[{1, 2}]]] += 1 It first rewrites it as: q[[RandomInteger[{1, 2}]]] = q[[RandomInteger[{1, 2}]]] + 1 The result is that RandomInteger[{1, 2}] gets rewritten twice, possibly as different random integers. 15 You can modify DownValues[f] directly. For example DownValues[f] = DeleteCases[DownValues[f], HoldPattern[_[f[1, ___]]] :> _] 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["...

14

If you just want backwards compatibility, there is no need to create any symbols in the System context. It seems like a bad idea to do such things. A package should create symbols only within its own context to avoid conflicts. Imagine what would happen if two different packages tried to define SystemEcho, each in their own and incompatible way? I ...

14

The issue is that to SetDelayed1 (:=), Function is just a normal head. For your third example, this means the following: You are defining a downvalue for F that says "every time you see F[something] replace it with the expression Function[x, 2 x + 1][x^2], where all x are to be replaced by something". Notice that at no point in that process is the ...

13

This seems to be a very easy way to bite yourself in the foot (non-flexible programmers this is not for you). I also have the habit of doing those blocked set-based definitions. It's probably time to change the habit now. It seems to me that SaveDefinition extracts the definitions of the symbols required by the Manipulate, as you entered them. If you used :=...

13

Utilizing ValueFunction as mentioned here, we could do something like this: SetAttributes[Observe, HoldFirst]; Observe[s_Symbol, fn_] := With[{VF := ExperimentalValueFunction[s]}, VF = If[ValueQ @ VF, ReplacePart[VF, {2, 2, 2} -> Append[VF[[2, 2, 2]], fn]], Function[Null, Table[f[##], {f, {fn}}], HoldAll] ]; ] We will need to include ...

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