5
$\begingroup$

In Mathematica, how can I replace instances of delta t in an expression with a, while keeping instances of delta and t separate when they appear alone? For example, delta^2 t should be replaced with a delta, not delta^2 t. The following code doesn't work as expected; it results in delta^2 t instead of a delta.

replaceRule = delta t -> a;
expr = delta^2 t;
newExpr = expr /. replaceRule;
Print[newExpr];

The output is:

delta^2 t

instead of:

a delta

What am I doing wrong, and how can I achieve the correct replacement?

$\endgroup$

1 Answer 1

5
$\begingroup$
$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

replaceRule = delta^n1_. t^n2_. -> (n3 = Min[n1, n2];
    delta^(n1 - n3) t^(n2 - n3) a^n3);

expr = {delta^2 t, delta^3 t^5, delta^5 t^3};

newExpr = expr /. replaceRule

(* {a delta, a^3 t^2, a^3 delta^2} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.