Message Boards Message Boards


[✓] Keep x/y (Divide[x,y]) from turning into Times[x, Power[y,-1]]?

Posted 2 years ago
4 Replies
4 Total Likes

I would like to have a way to keep x/y in the Divide[x,y] notation, but it seems to go immediately to the standard form

qTimes[x, Power[y,-1]]

There does not seem to be any attribute that controls this as there is for properties such as commutative and associative, etc. Similarly, you have



Times[2, Times[Rational[1, 3], Power[x, -1]]] 

when I might want to distinguish between 2/(3x) and (2/3)(1/x) etc. I know this is normally a feature, not a bug, but there are times when I'd like to have more control over conversion to standard form. Is there any way to do this?? Much thanks. - Elaine

4 Replies

It depends on what you want to do with it. You may use Inactive[Divide][x, y], or define your own operator, with its rules:

 Format[myDivide[x_, y_]] := DisplayForm@FractionBox[x, y]

I guess you should make your own Divide with it's own rules:

MyDivide[x_, y_] MyDivide[a_, b_] ^:= MyDivide[x a, y b]
MyDivide[x_, y_] + MyDivide[a_, b_] ^:= MyDivide[b x + a y, b y]
Format[MyDivide[x_, y_]] := DisplayForm[FractionBox[x, y]]

You can now define your own definitions for multiply and plus et cetera, see example above…

Thanks all, but I'm sorry, I wasn't very clear in my question. I meant I wanted to start from the standard infix notation x/y before converting to some internal notation such as MyDivide. I don't want the end user (not me) to have to know about something such as MyDivide or Inactive. I want them to be able to write x/y and, after I apply a translation function, get MyDivide[x,y]. I also want them to be able to write x y^(-1) and have it stay that way, so I can't just convert Times[x, Power[y, -1]] to MyDivide. Something like Inactive or Inactivate would be great if I could set it as an attribute, but that doesn't seem possible, and if I wrap the whole expression, e.g. FullForm[Inactivate[x/y]], it's already too late as I get Inactive[Times][x, Inactive[Power][y, -1]].

I know I could do string manipulations, but I'm trying to avoid having to write my own parser.

OK, I think I was going about this backwards. If I let Times[x, Power[y, -1]] be the standard representation for divide, then I can do ToExpression[StringReplace[<exp>], "^"->"myPower"] and end up with either x/y or x myPower[y, -1].

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract