2
$\begingroup$

It seems that a/b is always replaced with the form: a * b^-1, Times[a, Power[b, -1]]. I tried Hold, Unevaluated, HoldAll and this always happens.

Thus a/b /. {Times -> Minus} leads to a - b^-1 and any_expression /. {Divide -> Minus} is always unchanged

How can I replace Divide in a/b with Minus to get a-b?

$\endgroup$
3
  • 2
    $\begingroup$ How about: repl = Times[x_, Power[y_, -1]] -> x - y; . For testing: {a/b, c a/(b d)} //. repl $\endgroup$
    – Syed
    Dec 21, 2022 at 8:14
  • 3
    $\begingroup$ This was asked before few times, you need to use the FullForm of the expression. But why not just do the following: expr /. Numerator[expr]/Denominator[expr] -> Numerator[expr] - Denominator[expr] where expr is your rational expression you show. This way you do not have to worry about it. Is the above the only example you are looking to change or you have more complicated cases? !Mathematica graphics $\endgroup$
    – Nasser
    Dec 21, 2022 at 8:14
  • 3
    $\begingroup$ Related: mathematica.stackexchange.com/q/84908/1871 $\endgroup$
    – xzczd
    Dec 21, 2022 at 8:37

1 Answer 1

6
$\begingroup$

I won't be surprised if there's a duplicate for this, but I can't find it, so let me write an answer. The following solution works on toy examples, but semanic-matching problem usually need to be studied case by case, so don't be surprised if it doesn't work well on your real problem:

divide…to…minus[expr_] := 
 ToBoxes@expr /. FractionBox[a_, b_] :> RowBox[{a, "-", b}] // ToExpression

(a d)/(b c) // divide…to…minus
(* -b c + a d *)

See the posts under the tag for more info.

$\endgroup$
0

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.