Questions tagged [variable-definitions]

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.

Filter by
Sorted by
Tagged with
0
votes
0answers
48 views

a variable as all values from a list

I have a small Problem with lists and variables. I want mathematica to calculate a formula I derived for 200 values of w between 0 and 5 in ascending order. (maybe I can define w along the lines of) <...
1
vote
1answer
59 views
1
vote
3answers
89 views

Access to variables in surrounding context from within Manipulate

Suppose you want to access a variable defined in a "surrounding" context (scoped via Begin and End) from within ...
2
votes
1answer
55 views

Number of values stored in a function

Suppose I run this: f[0] = 1; f[n_] := f[n] = 2 f[n - 1]; f[1000] Now there will be 1001 values stored in f, and ...
7
votes
1answer
439 views

very simple but unexpected aspect of Function[ ]

I've always thought (2#+1)& and Function[x,2x+1] are interchangeable in common sense. But, ...
1
vote
1answer
35 views

Changing expression using a new variable definition

If I have an expression that looks like the following: $$ \frac{i \sqrt{\frac{\sigma _2}{\sigma _1}} \left(-\sigma _2 q_1(t){}^2+\sigma _1 q_2(t){}^2+\left(\omega _2-\omega _1\right) q_2(t) q_1(t)\...
0
votes
0answers
16 views

Partially activate a derivative

Is there a way to make an inactive derivative do some amount of simplification? For instance, say I have Inactive[D][{{f,g},{u,v}},x] I would like ...
1
vote
2answers
732 views

Can one define 0 Log[0] = 0?

With reference to this post, can one define 0 Log[0] = 0 in Mathemtica?
0
votes
1answer
48 views

Nested usage of NIntegrate

I'm trying to compute an integral of f(x,y) such that an integration over "y" is performed inside the integral over "x". My trial is below: <...
23
votes
4answers
785 views

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

Often new users face problems with lingering definitions that, if unaware, may cause unexpected and frustrating behaviour. There are several answers that illustrate different aspects of the solution, ...
4
votes
1answer
24 views

KroneckerProduct with arbitrary number of Pauli matrices

I want define a function of n variables as follows $$f(x_1, ..., x_n)= KroneckerProduct[\sigma^{x_1}, ..., \sigma^{x_n}]$$ where $\sigma^{x_i}=PauliMatrix[x_i]$. Given a number $n$, I can explicitly ...
3
votes
1answer
58 views

Definition of multiple variables from a list

I have a list of the form {{0, 0, 0, 0, 0, 0, a, b, c, d, e, f}, {0, 0, 0, 0, 0, 1, a, c, b, d, f, e}, {2, 2, 2, 2, 1, 2, b, f, c, a, d, e}, ... } and I would ...
3
votes
0answers
75 views

Module inside With, With inside Module, or Just Module?

In general, if one wants to define local variables and local constants, which of the following is the best programming practice in terms of performance and code "safety"? (Assume ...
1
vote
1answer
104 views

Solve using PolyGamma function

I am trying to solve for the value of y. My code is: x = 0.165; f = -Log[y] - PolyGamma[0.5 + 0.2*(x/y)] + PolyGamma[0.5]; Solve[f == 0, y] Running gave me ...
0
votes
0answers
43 views

How to declare a list of symbols as Real numbers?

I have a list of symbols : gammaVecs =Table[Symbol[StringJoin["gamma", ToString@j, ToString@i]], {j,7}, {i, 40)}]; Which I want to use later in ...
7
votes
2answers
312 views

Is it possible to define conjugate of a function?

Could you help me, please? I have two complex functions of real variable f[x] and fc[x] such that one is a conjugate of another one. I'd like to take a conjugate of an expression that includes both of ...
1
vote
1answer
72 views

How to implement this recursion relation?

I am trying to implement the following relation in Mathematica for any $k$: $$\begin{align} t_{n,n_1, \ldots, n_k} &= \frac{1}{2} \sum_{m=0}^{n-2} \left( t_{m,n-m-2,n_1, \ldots, n_k} - \frac{1}{N} ...
1
vote
1answer
59 views

Define/store individual elements of Table output for back-substitution

At its core, mine is a very simple communication error between me and Mathematica that I haven't been able to crack, so I do apologise if it seems embarassingly inane. Please consider the following ...
0
votes
2answers
23 views

Redefining the output of a function

ftn1 = Sin[s]^2 + 3 t^2 + u^5 3 t^2 + u^5 + sin[s]^2 ftn2[s_, t_, u_] := ftn1 ftn2[2, 1, 2] 3 t^2 + u^5 + Sin[s]^2 Hi ...
3
votes
2answers
128 views

Why does f[set_]:=AppendTo[set,5] not work when applying f to a set?

The following code works ccc = {}; AppendTo[ccc, 5]; ccc and gives the output {5} However, the following code does not work: <...
4
votes
1answer
133 views

Construct 1 variable function from given 2 variable function

Given a function f[x , y]. The definition of f is not self-describing. (f was defined with objects other than f.) Then I want to construct a function g[x] such that f[x,y] and g[x][y] are the same, ...
5
votes
1answer
6k views

`Quit` vs `ClearAll[“Global`*”]`

Quit erases all the definitions (by quitting the kernel), but so does ClearAll["Global`*"] (or other contexts). What's the ...
2
votes
0answers
56 views

Certain plotting functions are setting variables unwantedly

I came across the following problem. Certain plotting functions set variables to values, which I do not want. For example, if I run SliceVectorPlot3D with an ...
2
votes
1answer
63 views

Display assumptions of a variable

If I restrict a variable x to x>0 via $Assumptions=x>0, is there a way I can get ...
0
votes
1answer
68 views

Passing symbols, variables and defining functions within functions

Moderators state: “You will be brief and straight to the point”, and so do I. All codes test whether a polynomial is even or odd, using different approaches. This works: ...
3
votes
2answers
1k views

Can you unpack a list of values into variables in a With expression?

For instance, if I am trying to draw a graph with 5 vertices labeled a through e, and I want to draw edges connecting a and b, a and c, a and d, a and e, b and c, b and d, and c and e, I would run the ...
2
votes
2answers
123 views

Function incorrectly renaming variables in Rule Pattern

Consider the simple pure Function containing a Rule with the named pattern q. ...
1
vote
0answers
34 views

evaluating some of the same variables inside a function while some of them are declared beforehand

This is a style question I suppose, not sure. Ok so I have this: a := RandomInteger[{1, 10}]; f[a_] := a - a f[a] ...
44
votes
3answers
70k views

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

I'm trying to expedite some quantum mechanical calculations (expectation values etc.) by running them through Mathematica. When I say, for example, ...
0
votes
0answers
51 views

Proper way to construct a function that takes too long to plot and integrate

Consider the functions phi[t_] and psi[t_] defined in the code below. These are used as basis functions for interpolation ...
1
vote
2answers
107 views

'Do' loop acting on a global variable

I need a code that will allow me to alter a matrix (a global variable) iteratively using a function update (which depends on ...
18
votes
1answer
756 views

Is it possible to Clear all variables (but not functions)?

I have written a Mathematica script in which I define functions and variables. Here is a vastly simplified example: ...
14
votes
3answers
3k views

Character to replace _ in symbol names?

In C, _ comes in handy when naming variables and functions, as a replacement for space. For example: ...
1
vote
0answers
31 views

Variables with super/sub scripts

So I know how to create variable names with subscripts/superscripts at the end (using ctrl+_ etc), however, how would I use them in the middle of a variable name?
0
votes
0answers
77 views

Issue with several replacements in a function under certain conditions

I am trying to define a function (reduce[x]) which does several substitutions, depending on the values of certain parameters. Concretely, consider an expression x, which contains an arbitrary number ...
0
votes
1answer
48 views

Getting error messages form Set while trying to implement the bisection method with a For-loop [duplicate]

My objective is to build a function using modules that runs the bisection method. I have a working bisection method but when I try to implement it as a module I keep getting this error and I can't ...
1
vote
1answer
97 views

Basic Question: how should I read this instruction?

I'm very new at using Mathematica and it's not my Job. It's just something I like. ...
1
vote
1answer
39 views

Multiple functions compact form

I have a sort of silly question... I need to define two functions in the following way ...
2
votes
4answers
215 views

How can we make the input of a function multiple lists?

I want to create a function that takes inputs that are multiple lists of ordered quadruples. For example, when we run the code our input line should read something like inputs[{2,3,4,5},{6,7,8,9},...} ...
0
votes
2answers
80 views

Save a Derivative

I have defined the derivative of a function Derivative[1][f][x_] := Sin[x] How can I save this definition in a file? Derivatives are not included in ...
0
votes
1answer
47 views

Compact notations for multiple variables

Why does this work Manipulate[ListPlot[{a[1],a[2],a[3]}], {a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}] But this doesn't? ...
6
votes
3answers
183 views

KeyExistsQ[] but for functions?

Is there a good way to check if, say, f[5] has been defined if f is a function, similar to ...
0
votes
1answer
48 views

Defined function keeps the variable

I'm a beginner at Mathematica, and I have a problem finding the roots of a function for a specific variable (using Jens' findAllRoots function). Due to the nature of my problem, I need to compute ...
6
votes
3answers
438 views

Create variables in a for loop

I am very new to mathematica, but it would be much easier for me to use For loops in order to create variables. My train of thought was simple, but apparantly does not work. Let's say I defined these ...
14
votes
4answers
821 views

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?

Yes I know there is no built-in native function called SymbolQ (but JavaScript does). However, could one be simulated to work for most cases? I often rely on ...
2
votes
1answer
41 views

How to Append the value of a variable and not the variable itself

So lets say I have a = Some complicated code that changes list = {} AppendTo[list, a] Obviously it would give me ...
3
votes
1answer
28 views

Referencing data in sets of sets

Given a data set how to assign definitions to the subsets. For instance given the data data = {{0, 1, 1}, {0, 1, 1}, {1, 1, 0}, {0, 1, 1}, {1, 1, 0}} if {0, 1, 1} ...
7
votes
1answer
95 views

Is there something wrong with DistributeDefinitions?

I have the following code, which fails to distribute the DownValues for the symbol f. Could anybody please explain what is wrong. ...
0
votes
1answer
84 views

Subscripts use to make expression readable [duplicate]

I am trying to write a function in mathematica which depends on different parameters. For example $$f(x_1, x_2, x_3, x_4, x_5)= 2x_1+\sin(x_2)+x_3*x_4-x_5^{-1}$$ (My actual function has 12 such ...
2
votes
1answer
258 views

Functions without arguments [duplicate]

In the course of a project recently I had to use a particular function u[x, t] a number of times. Because in mathematics I would typically write this function as $...

1
2 3 4 5
9