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: ...
Roman's user avatar
  • 46.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, ...
rcollyer's user avatar
  • 33.9k
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 ...
Leonid Shifrin's user avatar
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: ...
Mr.Wizard's user avatar
  • 270k
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 ...
Szabolcs's user avatar
  • 233k
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 ...
Leonid Shifrin's user avatar
17 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 <...
Moonsun Pervez's user avatar
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. ...
Kuba's user avatar
  • 136k
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 ...
Szabolcs's user avatar
  • 233k
16 votes
Accepted

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

You can modify DownValues[f] directly. For example ...
Simon Woods's user avatar
  • 84.7k
16 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 ...
Michael E2's user avatar
  • 234k
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: ...
John Doty's user avatar
  • 13.7k
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 ...
Lukas Lang's user avatar
  • 33.5k
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 ...
Kuba's user avatar
  • 136k
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 ...
Kuba's user avatar
  • 136k
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 ...
Szabolcs's user avatar
  • 233k
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 ?
John Doty's user avatar
  • 13.7k
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. ...
march's user avatar
  • 22.9k
11 votes

Cannot get determinant of matrix

Mathematica does not distinguish between row and column vectors (...
Bob Hanlon's user avatar
  • 152k
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 ...
Daniel Huber's user avatar
  • 47.4k
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: <...
LLlAMnYP's user avatar
  • 11.4k
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 (...
Albert Retey's user avatar
  • 23.5k
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 ...
Carl Woll's user avatar
  • 130k
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 ...
Szabolcs's user avatar
  • 233k
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? ...
Kuba's user avatar
  • 136k
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 ...
Edmund's user avatar
  • 41.7k
10 votes

Different strategies to get a clean Kernel. Quit, Exit, ClearAll, Remove, CleanSlate?

PREVENTION This is a Community Wiki answer, experienced users are welcome to add to it and correct mistakes. Best practice is to avoid the need to clean the kernel at all, by avoiding the creation ...
10 votes
Accepted

KeyExistsQ[] but for functions?

Here's a simple method for checking if any of the down values of a function matches f[5] literally. It's a good use case for ...
Sjoerd Smit's user avatar
  • 21.9k
9 votes

How to differentiate formally?

Starting in M11.1, this works: sum[n_] = Sum[i x[i],{i,1,n}]; D[sum[n],x[2]] //InputForm Piecewise[{{2, n >= 2}}, 0]
Carl Woll's user avatar
  • 130k

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