28 votes
Accepted

How can I make assignments persist across sessions?

If you wrap your definitions in Once then their results will be remembered across sessions: ...
  • 41.2k
26 votes

How to make Mathematica variables declarative instead of just-in-time?

You are looking for $NewSymbol which is run every time a new symbol is created. For example, let say you only want x, ...
  • 33.7k
25 votes
Accepted

How can one manually change the rule ordering

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 ...
23 votes
Accepted

Do Table iteration variables need to be localized using Module?

In general, it is good practice to include i among the local variables. Table does not localize its variable (or, as some say, ...
  • 67.9k
22 votes
Accepted

Orderless pattern matching

I think the documentation needs to be more clear on this; the order of definitions is important: ...
  • 267k
22 votes

How can one manually change the rule ordering

Actually we have direct control over this via a System Option. Set: SetSystemOptions["DefinitionsReordering" -> "None"]; Then: ...
  • 267k
22 votes

How to pass variables by reference and by value

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 ...
  • 231k
20 votes
Accepted

Why does the global variable exist here, where it was declared only in Module?

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 ...
17 votes
Accepted

Context of localised (dynamic) symbols

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 ...
  • 67.9k
17 votes
Accepted

Working with DynamicModule: SetDelayed + OwnValues

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. ...
  • 135k
16 votes
Accepted

Attempts at backwards compatibility

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 ...
  • 231k
16 votes
Accepted

How to clear all definitions matching f[1, ...] but not f[2, ...]?

You can modify DownValues[f] directly. For example ...
  • 84.1k
15 votes

How to tell Mathematica that certain variables are real/imaginary, integer-valued, etc

$Assumptions = n > 0 && n ∈ Integers && L ∈ Reals && L > 0 You can type symbol in the form <...
15 votes
Accepted

Confusing behavior when passing a variable vs. inlining a function call

Mathematica is an expression rewriting language. When it evaluates: q[[RandomInteger[{1, 2}]]] += 1 It first rewrites it as: ...
  • 13.6k
15 votes
Accepted

When is a symbol a Symbol? Is there an easy Mathematica way to test if an object is a symbol sort of like a SymbolQ?

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 ...
  • 226k
15 votes
Accepted

very simple but unexpected aspect of Function[ ]

The issue is that to SetDelayed1 (:=), Function is just a normal head. For your third ...
  • 30.2k
14 votes
Accepted

How to make Mathematica variables declarative instead of just-in-time?

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 ...
  • 135k
13 votes

Finding a variable name given its numerical value

a = 1; Select[Names["Global`*"], Symbol[#] == 1 &] {"a"} Notice it may not be robust if you have a habit of writing ...
  • 135k
13 votes
Accepted

When to name a variable with $

Look at the system context to see what the convention is. I can find 257 built-in symbols in 11.3, symbols = Names["System`$*"]; Length @ symbols (* 257 *) In ...
12 votes

How can I make assignments persist across sessions?

Like in all other systems I am familiar with, variable and function definitions exist in memory (RAM) only and do not persist across sessions. If you want a definition to persist, you must save it ...
  • 231k
12 votes

When is a symbol a Symbol? Is there an easy Mathematica way to test if an object is a symbol sort of like a SymbolQ?

How about: SymbolQ[_Symbol] = True SymbolQ[_] = False ?
  • 13.6k
11 votes
Accepted

How can I define variables that follow a naming pattern?

Here's a pretty natural way, I think, that almost matches your variable names. ...
  • 22.4k
11 votes

Cannot get determinant of matrix

Mathematica does not distinguish between row and column vectors (...
  • 140k
11 votes
Accepted

Is it possible to define conjugate of a function?

Conjugate[f[x_]] := fc[x]; will fail because MMA tries to store this rule under Conjugate, which is protected. However, you may ...
  • 37.5k
10 votes

How to Set parts of indexed lists?

This is not a precise answer to the actual question, but the list of answers is from 2012 and IMO does urgently lack a note about Association. Since version 10 (...
  • 23.3k
10 votes

How can one manually change the rule ordering

This is more of an extended comment in response to @LLlAMnYP is there a way to do this without knowing the existing definitions in advance? or maybe use Prepend? You can roll a function like so: <...
  • 11.3k
10 votes

How can one manually change the rule ordering

Updated Updated twice, once to force autoloading, and a second time to eliminate duplicate *Values I will answer the last part of the question, specifically: Or conversely to make sure a definition ...
  • 127k
10 votes
Accepted

`Quit` vs `ClearAll["Global`*"]`

There are significant practical differences. Quit does not "clear" anything, it instead restarts the kernel, i.e. resets it to the default state. There ...
  • 231k
10 votes
Accepted

How do I insert OwnValues inside a held expression without evaluating it?

ClearAll[a, b]; a := 1 + 1 b = Sqrt Is this acceptable? ...
  • 135k
10 votes
Accepted

Can I use a graphic as the name of a variable?

You may use the Notation, Symbolize, and InfixNotation tutorial to define your own notation. This method has the added bonus of not unprotecting built-in symbols. Below is a screenshot of the ...
  • 40.6k

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