Tagged Questions

Questions about Mathematica's rule-based replacement functionality, including functions such as Rule (->), Replace, ReplaceAll (/.) and ReplaceRepeated (//.).

learn more… | top users | synonyms (4)

2
votes
1answer
32 views

Remove redundant parameters from equation

I have random expressions of the form (b1 x + b2 x)/(b3 + b4/b5 x) + Sin[b6 x] where $x$ is a variable and {b1, ..., b6} are parameters to be fitted. This ...
1
vote
2answers
43 views

Randomly changing matrix entries

1.Write a block of code that generates a 4x4 matrix with randomized real number entries each between 0 & 1, then selects 3 matrix entries at random, and finally replaces these 3 entries with a ...
1
vote
0answers
41 views

Understanding the order of applying Rule to expression [on hold]

I am a little confused about: In what order is a Rule tried to apply to an expression? In a depth-first order or otherwise? For example, ...
1
vote
0answers
93 views

Replace part in a matrix that matches specific pattern

I have the following problem. Given a matrix L which looks as follows, Matrix L is a link, route matrix. Further, I got the following vector (d), which indicates the demand per vertex combination. ...
1
vote
2answers
56 views

Replace list of symbols in an expression

I have an expression: exp = a+b+c and a list containing all the symbols of my expression: syms = {a,b,c} and I would like ...
5
votes
2answers
114 views

Apply Trig Identity to Transform Sum of Sinusoids into Phase-Shifted Cosine Function

I'm trying to solve for the output of a simple RC low-pass filter, with Cos[w t] input. The convolution of the input with the RC transfer function gives me the right output, but I'd like to be able to ...
2
votes
2answers
34 views

replace an interval of values [duplicate]

I want to replace values in an interval [a,b] by 0 in a list of values. Example: {{.5,.2,.49,1,1},{.49,.5,.495,1}} I want to get: ...
3
votes
4answers
104 views

How to Remove Undirected Edges that are the Same [duplicate]

Given: theData = {1 <-> 2, 2 <-> 3, 2 <-> 1} I would like to remove any duplicates where I define a duplicate to be: ...
2
votes
3answers
69 views

How to get an element of a list , which is computed out of substituted variables?

What is the best way to get the element of a list, which is computed out of substituded variables? For example one wants to get the first element of the first row of ...
-2
votes
0answers
48 views

How to achieve this transformation using rules

Here it is: ...
0
votes
1answer
32 views

Subsitute symbolic function

I have a have two functions, two recurrance function $x(k+1) = A x(k) + B \tilde{u}(k)$ and $\tilde{u}(k) = (I - \Gamma)\tilde{u}(k-1) + \Gamma u(k)$ Which I have defined as the ...
2
votes
2answers
62 views

Conditional replace of a column

Im trying to replace the value in the Nth column of a list if a condition is true, returning the modified list. I've tried modifying the answer from 61219 and been through most of related questions ...
5
votes
1answer
140 views

Conditional string replacement

I have a list of text and need to replace an element based on the text that appears in the element before it. Example of my list: ...
4
votes
1answer
92 views

Simplifying a b = 1 in non-commutative products?

Yesterday, I try to define some rules for non-communicative multiplication, here. I find some quite helpful rules, but things still do not work properly; the distributive rule can't be applied (e.g.: ...
0
votes
1answer
50 views

Why Infinity edges with WeightedAdjacencyGraph [closed]

I can't understand why WeightedAdjacencyGraph wants Infinity weight when there are no edges between two nodes. In particular ...
0
votes
0answers
34 views

Replacing variable names in a notebook

I am working on a code that simply imports and graphs a function. If I preset the directory I would have something that looks like this: ...
3
votes
6answers
166 views

Change last column of a matrix

Given a (rectangular) matrix of integers, i.e. RandomInteger[4, {10, 3}] I want to apply the following rules only to the last column ...
0
votes
0answers
41 views

How to do replacements in Compile'd code? [duplicate]

I want to do something in the spirit of this code: ...
9
votes
3answers
298 views

How to replace the first occurance in an expression instead of replacing all?

Is there a function that replaces the first occurance of the expression instead of replacing all? For example, if I have HoldForm[x + 2 + 4 + x] /. x -> 4, is ...
12
votes
7answers
850 views
5
votes
3answers
116 views

Assign output of solve to variables [duplicate]

I have this input: Solve[x^2 + 3 x + 2 == 0, x] which gives this output: {{x -> -2}, {x -> -1}} I want the first x ...
1
vote
1answer
87 views

Function returned an unexpected list

I was trying out a question in Wellin's Programming with Mathematica that says the following. Question: Given a set of points in the plane (or 3-space), find the maximum distance between any pair of ...
3
votes
1answer
47 views

Select a Placeholder in a Notebook?

How might I select the 1st Placeholder in a Notebook? SelectionMove Has no option. ...
2
votes
1answer
65 views

Add to the last digit a number

I have data from a Multimeter that has a precision of 0.6%rdg+4dgt. So i want to take the last digit of, say, 12.007 and add 4 to the last digit like ...
0
votes
1answer
48 views

Speeding up ReplaceRepeated while truncating to desired order

I need to program in an algorithm that recursively makes algebraic replacements which leads to an utterly complicated algebraic function of $x$, but whose final result is only needed at fixed order in ...
3
votes
3answers
159 views

Transformation of Derivatives under change of coordinates [duplicate]

I have an expression depending on the two variables z and t, and the derivative of a known function $f(t,z)$. Something like: $$myexp \ =\ t\ z\ \partial_z\ f(t,z)$$ Now I want to make a change of ...
0
votes
0answers
73 views

Using one complicated equation to reduce another

(Relative noob here so don't assume much!) I need to use a vector equation (with $N-1$ entries) in the general form $y_j(x_{i,j},x_{i,j+1},x_{i,j-1})=0, i=1...3, j=1...N-1; x_{i,0}=x_{i,N}=0$, ...
0
votes
0answers
26 views

How to prevent function from substituting the part of the parameter name if it has a subscript? [duplicate]

I have the following problem, when defining and using a function, that's supposed to have an outside parameter xmin: A[x_] := Exp[-x/Subscript[x, min]] A[5] ...
5
votes
1answer
183 views

How can I make a “self-referential” replacement operation self-contained?

Fairly often I find use for replacement rules that call themselves on the right-hand side of the rule, e.g.: ...
1
vote
3answers
133 views

How to ReplaceAll independent of depth?

I have a big list which in principle can be described as this more compact list : ...
0
votes
3answers
65 views

Unexpected behavior of rule-replacing with string concatenation [closed]

Here's the code. Clear[x,y,z]; {x,y,z} /. x_ :> ("^_^" <> ToString[x]) I get the result ^_^{x, y, z}, which is not ...
1
vote
1answer
76 views

How can I write a function which takes another function of a variable and the variable itself as arguments and gives a list of critical points?

This is an exercise from Power Programming with Mathematica by Wagner. I need a function which takes a function and a variable as arguments, like this: critpts[f_,x_]:= and then on the rhs I've got ...
13
votes
4answers
979 views

How to change element color in Periodic Table?

The command ColorData["Atoms", "Panel"] generates a periodic table. As I don't want to reinvent the wheel, I asked myself how to change the color of each cell ...
3
votes
4answers
187 views

Replace all symbols in a matrix with their numbered forms

I have a matrix made mostly from "x" with a few 0's thrown in: ...
3
votes
3answers
79 views

Replacing a sum of expressions

I have an equation which Mathematica solves but gives a huge output. However I notice in this output that the same expression occurs many times. Namely: $E=864 a^6 - 432 a^4 b^2 + 54 a^2 b^4 - b^6 + ...
1
vote
1answer
82 views

Solving equations with QuantityVariable

I'm trying to understand how to easily solve equations that use QuantityVariable. Why does this fail? ...
5
votes
3answers
239 views

When I use Rule, why doesn't this work?

k.k.k //. {k.k -> k} works perfectly, so the output is just k. But, when I use ...
4
votes
3answers
78 views

Question about Replacements and String Manipulations

I'm trying to get better at Mathematica and in the book I'm using (by Nancy Blachman) is the following question (which apparently was taken from another book: The Art and Science of C) which reads: ...
0
votes
0answers
67 views

Why is my Dynamic expression so much slower than the same in Manipulate?

I'm trying to do some optimization based on interactive parameters, then compute and present stuff based on the optimized results. In trying to do that, I came across some strange behavior I can't ...
1
vote
2answers
52 views

How do I format expressions so that replacement rules correctly apply to exponents? [duplicate]

I have a series of rules that I would like to apply to a series of expressions with variables, but cannot get Mathematica to correctly recognize that these rules should apply when my variables have ...
7
votes
3answers
149 views

Substituting values in 3j symbols

I believe these three expressions should give the same answer of -1/Sqrt[3], but the first gives +1/Sqrt[3] whereas the second ...
3
votes
2answers
202 views

Using ReplaceAll to replace a head

Why would this work to replace the head b with newHead: ...
7
votes
2answers
151 views

Probablity weighted ReplaceAll

I'm looking for an elegant way of doing a probability weighted replacement rule, something like this ...
5
votes
1answer
147 views

Is there a faster way to Map an Association?

Consider mapping an existing Association in a manner such as this: ...
1
vote
1answer
36 views

Automatic substitution of fractions

I have several equations with many variables, and here and there I would like to do a variable substitution. For example, in the equation: a (b + 42 c / d) I ...
26
votes
2answers
1k views

Best practice of passing a large number of parameters to functions

I have a number of functions that all take a large number of parameters. I am wondering what is the best practice of passing these parameters to those functions. I could, of course, simply specify ...
1
vote
4answers
114 views

Replacing more than one element from a Sublist

The following expression allows me to extract the value of x and y: ...
0
votes
1answer
37 views

Replace all instances of Integrate by NIntegrate and add a Method option

I have an analytic expression which involves several integrals Integrate[f[x], {x, lb, ub}] where the functions f and bounds ...
3
votes
3answers
153 views

Multivariate Polynomial Manipulation

I have a large homogenous multivariate polynomial in, say, 5 variables $a,b,c,d,e$. As an example take the polynomial $$a^4+2abcd+a^2 b^2+e^4+cde^2.$$ Now I would like to replace $k$-th power of any ...
2
votes
1answer
84 views

How to program a clickPanel for chemical elements (like a periodic table)?

The command ColorData["Atoms", "Panel"] indicates RGBColors of "Atoms" by clicking a panel. As I don't want to reinvent the wheel, I asked myself: How to ...