Questions about transformation rules in Mathematica--constructs that let you set, or programatically modify, local values for expressions.

learn more… | top users | synonyms

7
votes
6answers
237 views

Combinations of multiple matching patterns

I wonder if there is any nice way to combine DownValues (or any other suitable rule-/pattern-/function -based Mathematica construct) when multiple patterns match an expression. Let me explain what I ...
1
vote
0answers
30 views

How to simplify a ratio [closed]

I have a ration of the form $\frac{p^2}{(p^2+1)^2}$. I can simplify it using $p^2=p^2+1-1$, and to get $\frac{p^2}{(p^2+1)^2}=\frac{1}{p^2+1}-\frac{1}{(p^2+1)^2}$. How can I create the rule to do it ...
6
votes
1answer
104 views

Using two rules for the same variable

I am a newbie in the field so please not angry. I am reading the tutorial below and having a problem. Link: How to Create and Use Rules From the link: If you give two rules for the same ...
4
votes
4answers
175 views

Applying rules to expressions [closed]

I'm trying to write a rule that acts on any number, lists, expression and so on to turn any negative numbers positive. The simple r = n_ -> Abs[n] works ...
14
votes
3answers
214 views

ReplaceAll with Associations instead of Rules

I have just noticed that x /. <|x -> 123|> evaluates to 123 In other words, the association seems to behave as a ...
1
vote
1answer
51 views

How to force a replacement in an order that I want?

This is a follow-up to this question. I have a big sum, with lots of terms of the form ...
3
votes
2answers
52 views

Composition of rules: a->b + b->c = a->c

I am trying to compose two rules to form a new one: {a -> b, b -> d} and {b -> c} should become ...
0
votes
1answer
43 views

Output of Coefficientrules into Jacobian matrix

I have a pair of large polynomials in u, v, a1...a9 and b1...b9, generated by ...
1
vote
1answer
103 views

why doesn't the replacement rule work?

I have the following code: ...
3
votes
2answers
115 views

Swap LHS's (or equivalently RHS's) of two rules in a list

Assume I have a list of rules, each of the form $a_i \rightarrow b_i$. What is the best way, for some $i$ and $j$, to swap $a_i$ with $a_j$ (or equivalently, $b_i$ with $b_j$). Currently, I have ...
9
votes
3answers
210 views

How to make a ReplaceAll Rule for adjacent items in a list?

I would like to make only one pass of a list of 2D items and replace adjacent items that have matching $x$ or $y$ coordinates with a series of linearly interpolated points between the two points. My ...
1
vote
1answer
35 views

ReplaceAll - Position of list arguments not displayed properly [closed]

This code A[1, 2, 4, 5] /. A[a___] -> Position[{a}, 4] return {} Why this code not return 3 (a position of number 4) ? I ...
1
vote
1answer
55 views

How could I check if a differential equation crosses a specific value? [closed]

I'm designing a magnetic field to simulate relativistic speeds in a small cyclotron. I need to see if the ratio -R/B * dB/dR crosses 0.2 in a certain range, and right now I'm trying to do that by ...
5
votes
3answers
177 views

How to change symbols in differential equation with a single rule?

Problem Suppose I have a differential equation such as $\qquad f'(x)+f(x)=0$ and I want to change it into $\qquad y'+y=0$ My Method Using ...
5
votes
3answers
165 views

How to replace an element in a list with an element in another list with matching indices

I have a list like this l = {1 + 2*s, 2 + 3*s, 4 + 1*s}, and I have calculated s = {1, 2, 3}. I need to replace ...
2
votes
1answer
85 views

How avoid error message with Range?

Evaluating Range[1, n] /. n -> 3 produces an error message ...
0
votes
1answer
51 views

Avoiding misconversion of data with ToExpression function [closed]

I've got a list of data with the following structure which can be easily converted To expresion by some manipulation such as ...
3
votes
0answers
61 views

Why is the evaluation of f different using rules and using arguments to it as a function? [closed]

I will provide all the code I used at the end of the question. I was trying to evaluate a function f with the following signature (which I'll give the details at ...
2
votes
3answers
159 views

How does one unpack the contents of a list while using rules to substitute values for each variable?

My intention is having a large collection of variables (symbols to be more specific) in a list and substituting all the variables at once with a value using rule instead of going through each one, one ...
0
votes
1answer
71 views

How to disqualify a rule [closed]

Executing this line Assuming[x < 0, x -> 10] Expected output: False Say x -> 10 is a root of a polynomial ...
3
votes
3answers
255 views

How to identify all rules that match pattern type? [duplicate]

I would like to be extend the below solution to be able to find get all rules from the list step[2] that match the generic pattern k_Digit_p. Where k and p are not ...
5
votes
4answers
201 views

Create Nested List from tabular data

Im trying to create a nested list from tabular data, then onward into a nested association. I can get part way with the help of this answer but it only works for the first level. Repeated use of this ...
1
vote
1answer
58 views

Concatenated rule applications (applying rules to rules correctly)

I am trying to apply a series of individual rules onto an expression sequentially, but also not have to write out the repeated ReplaceAll functions each time I want ...
2
votes
1answer
94 views

What's the difference between functions and replacement?

I used to thought that calling a function is equal to applying a rule, i.e. applying a rule of f[x_]->sth. is equal to calling a function f with definition f[x_]:=sth. However, given ...
1
vote
1answer
108 views

A puzzling result of the `ReplaceAll` [duplicate]

I tried to do something below Log[PDF[NormalDistribution[m, s], x]] /. Log[u_[x__] ] :> Plus @@ Log[List[x]] which gives a desirable result that splits the ...
0
votes
1answer
57 views

Why won't the Rule work? And how to apply it to a combination of variables?

I want to convert SI to Gaussian units given vectors: ...
0
votes
2answers
51 views

How can I match some parts of an expression?

Suppose I have an expression like Abs[x+I y]+Sin[Abs [c+ I (d+e)]], and I want to replace all expressions in the form of ...
5
votes
2answers
223 views

Create a list of rules built from data set fields

I want to convert a dataset to a list of features and a list of target values, for use in Predict and also more generally. I know ...
3
votes
2answers
186 views

How to rewrite the rules in a list when they satisfy a given condition

I think the best way to state my problem is to illustrate it with an example. Assume I have the following toy-list of rules: ...
3
votes
0answers
115 views

Define (conditional) substitution rule for sum/difference of radicals

I want to tell Mathematica to simplify some expressions applying the following substitutions, conditioned on the fact that $a\geq 0,b \geq 0, a^2-b\geq0 $ $$ \sqrt{a+\sqrt{a^2-b}} \pm ...
1
vote
0answers
143 views

A faster way to replace variables than ReplaceAll

I noticed that for large matrix, the rule /. is very slow. In the example below, the combination of memorization and the replacement "/.sol[n-1]" inside the definition of sol[n] turns the code ...
0
votes
1answer
54 views

Replace bunch of numbers by letter

I want to do following: ArcTan[1/Sqrt[-1 + (4 Sqrt[2] x^2)/(5 xc^2)]] /.4 Sqrt[2]/5->a^2 I expect to have ...
6
votes
5answers
152 views

Setting value of a rule

How do I change the value of a rule? For example, If I have a = x->3, how do I reset the value of x/.a to 4, so that ...
5
votes
4answers
327 views

Long list of similar rules

Given the following: ...
7
votes
2answers
124 views

Why does ToRules return a Sequence?

Why does ToRules return a Sequence expression? Why doesn't it return directly what ...
1
vote
0answers
84 views

Multi-named Vertices (or Overlapping Vertices) in TreePlot/LayeredGraphPlot, or Merging Trees

If there is a series of rules from which a TreePlot/LayeredGraphPlot is made: ...
2
votes
2answers
152 views

How to apply # on different lists? [duplicate]

If I have list1 = {1, 2, 3, 4} list2 = {5, 6, 7, 8} How can I do so, that function[#1, #2] takes ...
0
votes
1answer
58 views

Refercing generic derivatives

I have a list of rules to the values of the derivatives of a generic (real valued, 3 variables) function, for example: df/dxdydz(0,0,0)-> 0.7. I would like to be able to generically reference this ...
1
vote
1answer
135 views

How to combine multiple rules? [closed]

I have 2 rules: Rule[#, SomeFunction[#]] &/@ SomeFunctionArgs; Rule[#, OtherFunction[#]] &/@ OtherFunctionArgs; I want to have one function that combines ...
3
votes
3answers
198 views

How to Update 2D List based on a binary array

I have 3 lists: numbers = {{1, 2}, {3, 4}, {5, 6}}; toUpdate = {{1, 0}, {0, 0}, {1, 1}}; newValues = {{10, 20}, {30, 40}, {50,60}}; I have to update the list ...
2
votes
1answer
87 views

Return the incomming expression on false

when I have an expression, that is unknown to mathematica, it is returned exactly the same, as it was called. For example SeeFlyingPigs[1,2] called on the line ...
9
votes
5answers
346 views

Select a particular rules out of a list

rule = {beta -> 4, alpha -> 2, x -> 4, z -> 2, w -> 0.8} Select[rule, beta] Select[rule, beta &] Select[rule, beta -> _ &] How would I ...
6
votes
1answer
228 views

Better replacement rules to perform this change of variables?

In cosmology, it is common to work with both a normal time variable t and a “conformal” time variable τ, which is defined such ...
7
votes
2answers
128 views

Is Replace doing double work?

In an answer to this question, rm -rf used the function Replace with level spefication to do a series of replacements inside out. As far as I can see, the order of substitutions when more than one ...
2
votes
1answer
186 views

replace RHS of rule

is there an elegant way of replacing the RHS of a rule with the value of some function of it. Say I have {{a -> 1.2}, {a -> 2.3}} and I want to replace ...
3
votes
3answers
106 views

How to select the value of a substitution rule for a particular variable?

Say I have the following lists of rules: ...
2
votes
1answer
53 views

How can I drop the “belongs to integers” part from Reduce output?

I have this Mathematica line: Reduce[1/(E^0)^x + 1/(E^1)^x + 1/(E^3)^x == 0, x] that gives the output: ...
1
vote
1answer
146 views

Transform a rule to a list of rules

How can this rule a -> {1, 2, 3} be transformed into this {a -> 1, a -> 2, a -> 3} by way of a function? ...
1
vote
3answers
95 views

How to distribute but skipping one level

I would like to write a Rule that mimics built-in Distribute[], but one that distributes ...
1
vote
3answers
145 views

Randomly changing matrix entries

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 ...