Questions tagged [rule]

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

Filter by
Sorted by
Tagged with
0
votes
1answer
49 views

Problem with replacement rules

I would like to apply substitution rules to an object I have defined, but I must be doing something wrong. Here are the relevant pieces of code. First I have defined a function ...
3
votes
2answers
45 views

Concisely operate on RHS of list of rules

I have a length-N list of rules l = {a -> 5, b -> 1, c -> 42} and an length-N vector of scalars... dl = {-1, .2, 5} ...
0
votes
0answers
41 views

Delayed Rule right-hand-side mis-evaluation

Outline I am trying to create a rule that will give me a minus sign if the number of times A is applied on X is odd where A is some οperator and X a random quantity. Some of the configurations of ...
1
vote
1answer
38 views

Filter out terms that include any combination of F[X] and G[-X]

I have the following problem which is closely linked to a previous question I asked some time ago (Replacement rule for terms that are separated by operators?). In this specific instance, suppose I ...
1
vote
1answer
32 views

Replace expressions

Let's consider the following example ...
0
votes
2answers
65 views

Suppress arguments in output for pretty print

I would like to define a rule that suppress any arguments of a function for visibility. I tried: rule = f_[x__] -> f While this works well with individual ...
0
votes
0answers
35 views

Implementing a generic product rule

I'd like to implement a generic rule for expanding differential forms by the product rule. For instance, I'd like to transform the following PDE: $\phi \frac{\partial \phi}{\partial t} = \phi \nabla^...
1
vote
2answers
64 views

Correctly differentiate wrt product of variables

I have a function f(x) for which I would need to differentiate and then evaluate it to some product x = y*z. Naively, Mathematica does not accept: D[f[y*z], y*z] ...
6
votes
2answers
290 views

Is there a simple way to apply a function to the RHS of a substitution?

Suppose I have solved a system of equations, resulting in a solution of the form: ...
1
vote
0answers
65 views

Graph of a list of rules with coefficient

I am trying to transform an expression to a list of rules and after that plot its graph. The expression has the following form ...
8
votes
2answers
434 views

A simple problem about a Rule example

The documentation says that "Rule represents a rule that transforms lhs to rhs" and gives the following example: ...
2
votes
1answer
39 views

Replace sub-expressions matching a pattern unless located under specific heads

Suppose I have some rule, e.g. Re[x_] :> (x + Conjugate[x])/2. I want to apply it to all matching sub-expressions in some expression, excluding any sub-...
0
votes
1answer
30 views

Remove inequalities from set of rules

I solved a set of equation and use reduce on the set of solutions. Changing the equal sign to arrows I get some set of rules but some of the entries are inequalities. E.g. ...
1
vote
1answer
41 views

Drop-down menu to globally change definition of a function `t[x]`?

If have a function t[x_] which is used extensively in a long notebook, but which has a variety of definitions, and which is used in a variety of ways. I'm looking ...
0
votes
1answer
63 views

defining rule for derivative on arbitrary function

Is there a way in Mathematica for defining rules for derivatives on undefined functions? For example, I would like to define a rule such that for any function $f(x,y)$, when I take the $x$-derivative ...
2
votes
1answer
47 views

How to construct multible rules for replacement at once?

After generating a list for which I used //N, I want to replace some values with the exact one. What I had in mind is to generate something like: ...
0
votes
0answers
16 views

KeyAbsent when using Association in a replacement [duplicate]

I am trying to replace derivatives with other functions. I expected as = Association[fx -> dfx] Derivative[1][fx][x] /. Derivative[1][y_][_] -> as[y] to ...
1
vote
1answer
51 views

Can I get mapping as output from SatisfiabilityInstances?

Consider the code: SatisfiabilityInstances[Implies[a, b], {a, b}, All] (* output *) {{True, True}, {False, True}, {False, False}} I would like to obtain the ...
1
vote
1answer
29 views

Selecting only the terms whose exponents add up to a specific number

Suppose I have the expression from a truncated series: ...
0
votes
0answers
31 views

CoefficientRules for huge expressions

Is there some idea to get the coefficients for a very complicated, huge, and explicit polynomial expression to avoid insufficient memory problem? I am trying to use GroebnerBasis`DistributedTermsList ...
1
vote
3answers
96 views

Pattern with two lists

I have two lists with multiple array elements: ...
2
votes
2answers
59 views

Creating an elementwise replacement rule

I apologise for the simple question, but I've trying to learn to write my code in a more idiomatic way, after having learned Mathematica mostly through trial and error for a long time. Say I have ...
4
votes
1answer
67 views

Inconsistency in getting Keys, Values and Query for nested Rules

Querying nested Rule expressions of the form a -> v -> b // FullForm Rule[a,Rule[v,b]] for ...
1
vote
1answer
37 views

Using rule with derivative

Assuming I have an expression like: D[a[t, r], r] + D[D[a[t, r], r],r] + D[D[a[t, r], t],r] I would like to change variables, by expression everything in terms of ...
4
votes
1answer
114 views

A single argument pattern definition applies to multiple-argument patterns?

Consider defining a pattern rule, such as myFun[x_]:=x As far as I understand Mathematica syntax, this rule means ...
7
votes
2answers
181 views

Replacement Rule for Functions

Attempting to write a rule that will substitute an undetermined function of several variables by some other undeterminate functions (also, of the same several variables). I can do it in an inelegant ...
3
votes
2answers
97 views

Convert a list of lists into a list of rules

I have some trouble converting an imported list looking like this: {{a, b}, {c, d}, {e, f}} into a list of rules looking like this: ...
1
vote
2answers
59 views

Replacement rule for terms that are separated by operators?

My main problem is how one can create a "transcending between operators rule" in the same term according to the minimal example bellow. Suppose I need to perform a replacement of the form ...
1
vote
1answer
188 views

Convert String in Expression to Integer

I have reviewed many string replacement Q&A but I have been unable to apply them to this case (MWE): ...
2
votes
3answers
55 views

Using rule output as function

Im trying to define a function using the output from RSolve, however i cant get it to work properly. I have a RSolve that output something like: ...
1
vote
2answers
77 views

How to make functional rules not depend on an argument?

Consider the following: rule = f[x] -> Sin[x] if I now do, say: f[y] /. rule this does not work, because the rule wants ...
0
votes
0answers
45 views

Imposing conditions on argument for a function definition

Suppose that I wish to use "Piecewise" to merge two different interpolating functions (obtained via a NDSolve of some ODE), defined over negative and positive arguments respectively, like: ...
1
vote
3answers
92 views

Is there a possibility of adding a second condition, or an else clause to $/;$?

Let tally=RandomInteger[{0, 1}, 10]; In the code below, I'm trying to output a list where each component $C_n=\sum^n_{i=1} \text{tally}_i / n$ The problem with my ...
1
vote
1answer
46 views

Rule based list manipulation

I'm trying to restructure the following expression: start = Table[{1, 2, 3, 4}, 5]; Join[{ToString /@ {first, second, third, fourth}},start] Into this: ...
4
votes
1answer
173 views

Delete zeros from a sparse array [duplicate]

Why doesn't SparseArray automatically remove zero entries? The code ...
2
votes
2answers
58 views

Expand a recursion relation

Hermite Polynoms have the following recursion relation: $$ H_{n+1}(x) = 2x H_n(x) - 2n H_{n-1}(x), $$ which can be rearranged to be $$ x H_n(x) = \frac{1}{2} H_{n+1}(x) - n H_{n-1}(x).$$ Is it ...
0
votes
3answers
74 views

Transformation rule for a sum of functions

When applying the rule: exp[c[x] + d[x] + e[x]] //. exp[a_[x] + b_[x]] -> exp[a[x]] + exp[b[x]] I would like to get ...
0
votes
2answers
85 views

Splits an expression into two parts if possible

The series The series is: $\text{expr}_{1} \pm \text{expr}_{2} ...$ $\pm$ represents plus or minus sign. The number of terms in the series can be 1 or more than 1. Each term $\text{expr}_{n}$ is an ...
0
votes
0answers
39 views

Defining a function that transforms an expression with rules

Question I try to define a function that apply a set of rules to an expression in Mathematica 11.3. However, the function returns a result that is not the same as directly applying the rule to the ...
1
vote
1answer
80 views

Simplify rules with variable number of arguments

Problem How to simplify the following rules? The rules simplify (single, double, triple ...) summations of Kronecker delta. Replacement should occur if any summation and the Kronecker delta share an ...
1
vote
1answer
60 views

Make Mathematica apply the same rule exhaustively in simplification

Use Case Mathematica evaluate the partial derivative as: $$\frac{\partial}{\partial A_{abc}}\sum _{j=1}^J \sum _{k=1}^K \log \left(\sum _{l=1}^L A_{jkl} B_{jkl}\right) = \sum _{j=1}^J \sum _{k=1}^K \...
2
votes
1answer
115 views

Generating Manipulate variables from a list of rules

I have the following list of rules: ...
5
votes
1answer
42 views

Overriding TwoWayRule for in-place element swapping

I'd like to make \[TwoWayRule] work for things other than graph edges, for example, for swapping i.e. Replace, ReplaceAll, StringReplace, etc: ...
2
votes
2answers
177 views

Wrestling with substitution rules

Summary I am struggling with substitution rules. Example Here are several cases which are problematic: ...
6
votes
2answers
136 views

How to use ReplaceAll and RuleDelayed with an Association? [duplicate]

I expected that something like this ...
3
votes
2answers
50 views

Using elements of a list as SparseArray patterns: “can't be used as part specification”

I don't have much experience with SparseArray, and the search here did not give me an answer to this. I'm trying to numerically compute a certain spectrum, and the ...
6
votes
2answers
153 views

How can I get new random values with each blank in ReplaceAll?

Is there any way to get different values for different blanks with ReplaceAll[]? For example, I would like to do something like: ...
2
votes
1answer
54 views

Why ContourPlot shows nothing when use “ReplaceAll”? [closed]

When I use ReplaceAll in ContourPlot, it shows me nothing: ...
1
vote
1answer
82 views

Symbol Level pattern matching and rule replacement

I want to parse a Mathematica Notebook to get some information regarding the relative use of symbols. So first things first: ...