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

learn more… | top users | synonyms

3
votes
1answer
62 views

Rule for derivatives

I have a differential equation of the form $$f'(x)=g(x,f(x))$$ (where $g$ is has a known explicit form, e.g. $x f(x)$) and I have an expression which contains several derivatives of $f$. I want to ...
0
votes
0answers
40 views

Generate rule that replaces if string matches pattern

I have let's say a function f[x], but for x I pass a string. And I want to replace that string with a number, if that string ...
4
votes
1answer
45 views

What is the function of OrderedQ in these expressions?

Could someone please let me why the output of the two expressions change with OrderedQ in place. The first expression is as follows: ...
4
votes
0answers
39 views

Possible bug with `KeyValuePattern` and `Optional`

I expect to get 0 on every line of output of this: ...
3
votes
1answer
58 views

Why does syntax highlighting in `Set` and `Rule` not color pattern names on the RHS?

The mathematica front-end colors x on the right hand side of x_ :> x and f[x_] := x but ...
4
votes
4answers
146 views

How to scope `Pattern` labels in rules/set?

Module[{x}, f@x_ = x; p@x_ := x; {x, x_, x_ -> x, x_ :> x} ] ?f ?p gives ...
6
votes
2answers
83 views

Association matching still not working perfectly?

Up until 10.4 you could not match on internal Association structure at all: http://mathematica.stackexchange.com/a/119542/6804 Now you can, but it seems flawed ...
5
votes
2answers
97 views

Defining a symbolic inner product

So I have some rules for a symbolic inner product that work for quite a lot of cases: ...
0
votes
2answers
46 views

How to replace the argument of a function?

For the following expression, b'[x y/z]+z b''[x y/z] I want to replace the argument of b such that the above expression becomes ...
0
votes
1answer
39 views

Replace argument of delayed function before it evaluates [duplicate]

How should I use replace so that f[x_] := x^2 Replace[f[3], 3 -> 1] OUT = 9 evaluates to 1, instead of 9? This way, the replacement of the argument is done ...
1
vote
1answer
67 views

What are rules in Mathematica and what are its uses? [closed]

I'm new to Mathematica. I have been going through the official reference and came across this article on rules to which I was led by a Google search on how to ...
1
vote
1answer
41 views

Why doesn't recursive transformation stop when rules no longer match?

In an exercise where I was trying to pick out consecutive words starting with the same letter, i.e. I wanted: ...
0
votes
1answer
62 views

Pattern which will transform a given expression [closed]

Find the Pattern which when applied to this expression expr = {a^2, a^b, x^4, (x - 1)^2}; yields this result: ...
1
vote
0answers
43 views

Rule/Replace/Patterns [closed]

I need help with doing this question. given: Sin[x+y] Cos[x+y+z] Define using Patterns a rule (specifically a list of 2 rules ) , the two adding rules for trigonometric functions sin (a + b )...
0
votes
1answer
76 views

Module output rules like Solve FindRoot etc built-in functions

How do I print rules {x -> values} as an output from my module? I'd like it formatted just like those from the built-in functions, so that users can use Out[ ] or % to directly apply the rules. ...
1
vote
0answers
45 views

Select and use only one solution in a list solutions [closed]

I would like to use and plot only one of the two solutions of a quadratic equation. In particular this is my equation ...
4
votes
2answers
240 views

How do I encode this replacement rule?

I'm pulling a 5x5 array from a larger array. The small sub-array (call it S) can contain any combination of the symbols A, B or 0. What I'd like to do is: if the array contains any matching pairs ...
3
votes
1answer
112 views

Replace list element with element from another list based on condition

I Have a list on integers and another list of "duplicates". E.g. start={{3},{1},{2},{4}}; duplicates={{1,4,7},{5,6}} I would like to substitute each element of ...
1
vote
0answers
56 views

How to use the function Predict[] with variable-length dataset?

I can use the function Predict[] with pre-defined dataset successfully. A = {1, 2, 3, 4, 5, 6, 7, 8, 9} -> {1, 2, 3, 4, 5, 6, 7, 8, 9}^3; p = Predict[A] ...
4
votes
2answers
221 views

Pattern Matching for Rules [duplicate]

Consider a set of rules, e.g. {a -> aa, b -> bb, c -> cc, d -> dd, e -> ee} I want to remove from this list all patterns of the form e->_ and to ...
6
votes
3answers
188 views

How to prevent substitution in a part of an expression?

Background: I am trying to write a program that can execute scheme in Mathematica. Now I have things like {"+", 3, {"-", 2, {"'", {"*", 1, 3}}}}. The syntax ...
0
votes
2answers
57 views

RSolve - basic question about using rules as functions

I think this is basic question, but I can't find it in Help... (I am begginer :P) I have this expression ...
0
votes
2answers
74 views

How does one exchange an option with another one?

Consider an expression with a list of options at its end, like ...
1
vote
1answer
54 views

Rule for manipulate an expression

like as my precedent post I have this Expression: ...
0
votes
1answer
35 views

Replacement of a group of variables in trigonometric expression

I would like to make simplification of trigonometric expressions by using a replacement a group of variables. Here the equation I have to simplify : ...
5
votes
1answer
109 views

How to make a postordered ReplaceAll that operates just once per subtree?

The answers to this question provide code for a function I'll call postOrderReplaceAll to replace from the inside-out: ...
1
vote
2answers
63 views

Using a rule to increase precision [closed]

I'm sure this is a very simple question, but I can't work out how to do it. Construct a rule to set x=0.32 to a precision of 20 digits. The rule is to be applied ...
3
votes
2answers
42 views

Setting initial conditions of a domain grid?

So I'm writing some code to try and produce a cellular automaton which will model an oscillatory chemical reaction, but I'm trying to set up the initial conditions of the grid so that I can vary them ...
8
votes
6answers
278 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
36 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 ...
7
votes
1answer
123 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
202 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
238 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
2answers
119 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 ...
4
votes
2answers
63 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
47 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
112 views

why doesn't the replacement rule work?

I have the following code: ...
3
votes
2answers
120 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
213 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
36 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
182 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 rule = f[x] -> y,...
5
votes
3answers
186 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
96 views

How avoid error message with Range?

Evaluating Range[1, n] /. n -> 3 produces an error message ...
0
votes
1answer
55 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
63 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
196 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
72 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
298 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 ...
7
votes
4answers
225 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 ...