Questions on assigning and unassigning definitions to names that represent them, including issues raised by context and localization. Use tag [assignment] for issues relating to Set versus SetDelayed.

learn more… | top users | synonyms

11
votes
0answers
123 views

Is garbage collection broken in version 10.0.0?

In version 10.0.0 under Windows I am observing the following behavior: Do[Module[{foo}, foo[x_] := 1; foo[0]], {10}] Names["foo$*"] ...
11
votes
3answers
907 views

Elegant manipulation of the variables list

I have a list of global variables (some of them are indexed variables), for example: varsH = Hold[U0[1], U0[2], B0, V0[1], V0[2]] Or (if it is easier to handle) ...
2
votes
2answers
75 views

Local variables whose definitions depend on each other [duplicate]

Consider the following (pseudo-)function definition: f[l_] := Let[{ a = Length[l], b = First[l], c = b/a, }, c*10] So for ...
1
vote
3answers
70 views

Store matched variables

Edit: I am matching and replacing a particular expression. At the same time... I want to store matched variables for later use. I got it to work with messed up code involving ...
4
votes
4answers
344 views

How can I prevent Superscript from being interpreted as Power?

I have very strong desire to use superscript as the index of the variable. However, it looks like that the Mathematica automatically recognize the superscript as the power and I got message that my ...
0
votes
1answer
113 views

Maximization of a 3-variable function

So I define a 3-variable function $h$ and I want to find the maximum of $h$ in a region $\mathcal{R}$: ...
1
vote
1answer
57 views

Solving equations with QuantityVariable

I'm trying to understand how to easily solve equations that use QuantityVariable. Why does this fail? ...
0
votes
1answer
28 views
6
votes
2answers
97 views

How to name a variable with the value of another variable?

A simple but very important (at least for me) question. In some languages for defining a variable, for example, we can do: i=10 "d"&i=30 So I have defined the variable d10, and... d10=30 ...
6
votes
4answers
457 views

Why do I see the error “only assignments to symbols are allowed” when using a Module and not otherwise?

When I type Remove["Global`*"] foo[] := Module[{Subscript[a, 1] = "x"}, 0]; foo[] I get the expected error "only assignments to symbols are allowed". I ...
0
votes
1answer
31 views

Reducing Output length by introducing new variables [duplicate]

I have outputs like "expression/(1+3*c^2) +anotherexpression/(1+3*c^2)" and so on. Is there any way to set 1/(1+3*c^2)=A where A is a new variable so that the output reads like ...
1
vote
0answers
44 views

specifying a list (vector) of arbitrary length

When trying to do symbolic calculations in mathematica involving a space of dimension n, which is arbitrary but fixed, I'd often like to work with vectors that have ...
4
votes
1answer
70 views

How do I constrain variables to a user-defined domain in ArgMax?

I am trying to ArgMax a function over x, but x is only defined between ...
18
votes
2answers
1k views

Best practice of passing a large number of parameters to functions

I have a number of functions which all take a large number of parameters. I am wondering which is the best practice of passing these parameters to those functions. I could of course simply specify the ...
0
votes
1answer
56 views

Is there a command returning information about a particular object? [closed]

Suppose I have a variable var1, a function MyFunction, and a graphic SomePlot. I remember ...
4
votes
1answer
107 views

Multiple down-values with exactly the same lefthand side

I was working on an answer to this question, when I was sidetracked by what I bring up here. I wanted a function, which when given a string representing a binary number, would convert the string to a ...
0
votes
0answers
41 views

Coupled ODEs with boundary conditions - DSolve Error

I'm relatively new to mathematica and I can't find the error here. I suspect it has something to do with the fact that vx, vy, vz aren't blue on my mathematica - as in, they already have some assigned ...
1
vote
2answers
105 views

Define the square of a function [closed]

Is there any good way to define the square of a function in M9? Usually one can write e.g. f[z_] = 3*Sin[z] etc etc. But is it possible to define ...
1
vote
2answers
85 views

Using AddTo in a function

I want to create a function that will take a variable $x$ and reassign the value $x+2$ to $x$. So f[x_]:= x += 2 However: ...
0
votes
3answers
89 views

Identifying the symbol which has a specific value

I'm writing a program that will solve the value of 1 variable in a 4-variable equality equation, but the variable to be solved will depend on the user's input to the program. This is what I've come ...
1
vote
2answers
68 views

Calling variables with use of numbers in their names

I have got few huge matrices named by ascending numbers, e.g. x1, x2, x3,... and I need to do the same operation with all of them - to multiply their components, ...
2
votes
2answers
122 views

Using a variable for two different purposes

I want to name a bunch of tables after the OEIS entries which describe them. Is there any way to use these names for both the table name and the link at the same time? The second line uses the ...
2
votes
3answers
108 views

Extract memoized results in the form of a rule

Lets say I define a function f with memoization. Is there some way to extract the memoized values? For example, ...
2
votes
1answer
92 views

Define function that counts recursive fibonacci

If I write count=0; fib[0]:=(count=count+1; 0); fib[1]:=(count=count+1; 1); fib[n_] := (count = count+1; fib[n-2] + fib[n-1]); Then I can ...
0
votes
1answer
26 views

Indexed variable with alternatives

I have a["tree"] := "oak" a["flower"] := "rose" a["dog" | "cat"] := "animals" I want that the last definition of a behaves ...
4
votes
1answer
126 views

When to use indexed variables

Still learning the fundamentals of the language I would like to ask you what advantages there might be in writing something like: a[1] = 2; a[2] = 4; a[3] = "x"; ...
0
votes
1answer
61 views

How I can define a new function as a function argument?

I need to pass a function with t as independent variable as a function parameter and want to create a new function of the same through this provided the argument of another function. Here is my code. ...
0
votes
0answers
35 views

Can the value of a variable be made to persist between front end sessions? [duplicate]

For example, say I create a notebook with a cell a = 5 + 2 and then I close Mathematica, reopen the notebook, enter a new cell ...
3
votes
1answer
82 views

How to localize symbols on the right-hand side of Rule when using ReplaceAll?

I am frustrated by the following: {1, 2, 3.0} /. a_Integer -> a + 1 The a on the left side of the arrow is localized and ...
2
votes
1answer
44 views

Quoting definition explicitly

I'm trying to quote a definition using DownValues, OwnValues, etc. Everything seems to be fine, except for temporary symbols' ...
0
votes
2answers
44 views

how to define roots of an equation as a function? [closed]

I need to solve an equation depending on 3variables in only one variables and define the roots as a function of the remaining variables? I used some thing like that: ...
7
votes
0answers
67 views

Maximum number of literal values for SetDelayed?

Under what circumstances should I expect Mathematica to turn a delayed assignment into an immediate assignment? I have put together a minimal example (available here) which demonstrates this behavior. ...
0
votes
0answers
21 views

Get value for a variable from user inside a module [duplicate]

I am writing a module. When I run the module, I want it to ask for a variable, like I can in other programing languages such as C++ or Python. For example, in C++: ...
12
votes
7answers
870 views

How to Set parts of indexed lists?

I would like to assign a list to an indexed variable and then change it using Part and Set like this: ...
2
votes
3answers
225 views

Treat strings as variables?

I have a list of strings, {"x1", "x2", "x3", "x4"} And a list of linear equations: ...
14
votes
4answers
1k views

How to implement the Observer design pattern?

As a programmer with a history with procedural languages, the Observer design pattern inmediately springs to my mind when attacking certain class of problems, mostly UI/notifications related. A ...
2
votes
1answer
93 views

How to define a matrix with all elements 0? [closed]

A matrix can be defined as M = Array[m, {3, 3}] which gives ...
0
votes
0answers
74 views

Is there a better way of creating/assigning variables programmatically than using strings and ToExpression?

For quite a while now I've been wondering how to assign variables programmatically, and I have finally figured out one way of doing this. For example in a game where you have a variable number of ...
0
votes
0answers
30 views

How to change $n$ variables with $n$ Buttons automatically [duplicate]

I was trying to make a little game, and I came accross this problem. The goal is simple. There are going to be $n$ number of buttons, and each one will change a value of one variable. For example, ...
12
votes
4answers
1k views

how to differentiate formally?

I have been wrapping my head around this for a while now and I have not found a solution so far. I want to work with an arbitrary number of variables in mathematica and use some built in functions. ...
1
vote
1answer
39 views

How to set the values into some variables from different lists [closed]

I have a list of Values and a list of variables. I want to set the Values into Variables. Assume the following code: ...
0
votes
2answers
126 views

setting real parameters in Hamiltonian matrix

The following Matrix represents my Hamiltonian. m = {{ B/2 - d - j/2 + a, (Sqrt[2]*j)/2, M}, {(Sqrt[2]*j)/2, B/2 + a, 0}, {M, 0, (-3*B)/2 - d + j/2 + a} How can ...
0
votes
0answers
49 views

How to hide global variables in package?

in the package I'm writing I have a list of global variables which should not be visible from a notebook. Here's an extract of the package: ...
0
votes
3answers
134 views

Clean-up Code / Reduce variables

I'm writing a playing card simulator of sorts, and everything works fine, but some Modules use a disgusting amount of variables. For Example: ...
0
votes
0answers
32 views

Block, With, Module, can anyone help me understand the distinction? [duplicate]

I've been working heavily with Block[] for months in my research so I've read the documentation for the seemingly similar commands With[], Block[], and Module[]. Personally, I've had by far the ...
1
vote
0answers
56 views

Telling mathematica that something is an integer [duplicate]

Is it possible for me to tell mathematica something is an integer? like if I typed Sin[n Pi]+Cos[n Pi] it would tell me it equals 1 for even n and -1 for odd n? ...
7
votes
1answer
230 views

Defining my own invisible operator

There are some invisible operators in Mathematica (\[ImplicitPlus],\[InvisibleTimes],...
9
votes
2answers
227 views

How to take derivative of parameterized coordinate?

Suppose I have a vector in $\mathbb{R}^n$ but $n$ is not known in advance. I want to be able to write functions which operate on the components of that vector, and then I'd like to be able to take ...
0
votes
0answers
87 views

Local variable names - Is there an easy way?

I apologize first, because this has been asked, like in Treat all variables as local in a module [duplicate] Variable scope similar to other languages But I don't get how to use them. Some codes ...