Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

/. can be used to replace variables but how can one replace operators.

For example in a * b, * can be replaced by + or x or / etc. Is there some function to achieve this?

share|improve this question
1  
Answering your deleted comment.try Fu[a_, b_, o_] := Module[{}, Unevaluated[a + b] /. Plus -> o]; Fu[4, 2, Times] –  belisarius Apr 7 '13 at 20:16
    
thanks...this would do. –  Rorschach Apr 8 '13 at 2:25
add comment

3 Answers 3

up vote 16 down vote accepted

Perhaps use the full names for the operators:

a * b /. Times -> Plus

a + b

share|improve this answer
add comment

FullForm prints the expression with no special syntax (e.g. Plus instead of +).

FullForm[a*b]
(* Times[a, b] *)

So you change the expression's head.

Table[fun @@ (a*b),
 {fun, {Plus, Subtract, Divide, Dot, Cross}}]
(* {a + b, a - b, a/b, a.b, a\[Cross]b} *)
share|improve this answer
add comment

Just to complement: be careful when your symbols have values already assigned.
Compare:

a = 2; b = 3; rule = Plus -> Times;

a + b /. rule
ReleaseHold[Hold[a + b] /. rule]
Unevaluated[a + b] /. rule
Defer[a + b] /. rule
(*
 5
 6
 6
 a b
*)
share|improve this answer
    
c = Defer[a + b]; How come c + 3 doesn't return 8? –  BoLe Apr 7 '13 at 17:09
    
@BoLe: execute c + 3 // FullForm and see. –  J. M. Apr 7 '13 at 17:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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