33 votes

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

You can also use Refine with Element : ...
  • 7,225
28 votes

Preventing Superscript from being interpreted as Power when using Ctrl+^ shortcut?

Superscript is not interpreted as Power: Presumably you are referring to what happens when you enter a power in superscript ...
  • 265k
28 votes

Best practice of passing a large number of parameters to functions

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 ...
  • 265k
27 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: ...
  • 38.1k
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.4k
24 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.5k
22 votes
Accepted

Orderless pattern matching

I think the documentation needs to be more clear on this; the order of definitions is important: ...
  • 265k
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: ...
  • 265k
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 ...
  • 228k
21 votes

Best practice of passing a large number of parameters to functions

In V10, another option is to use Association. ...
  • 25.6k
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.5k
16 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. ...
  • 133k
16 votes
Accepted

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

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

Assign output of solve to variables

{x1, x2} = x /. Solve[x^2 + 3 x + 2 == 0, x] {-2, -1}
  • 27.2k
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 ...
  • 222k
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 ...
  • 27.1k
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 ...
  • 133k
14 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 ...
  • 228k
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 ...
  • 133k
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

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

Find all heads a function is defined on

If your definitions are exactly like you show, every time, you can use belisarius's method, slightly refined: ...
  • 265k
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. ...
  • 21.7k
11 votes

Cannot get determinant of matrix

Mathematica does not distinguish between row and column vectors (...
  • 126k
11 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 ...
  • 228k
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 ...
  • 29.6k

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