4
$\begingroup$

I apologize if my syntax is bad. I looked around, but didn't see anything on this in the stack exchange.

I would like to be able to transform a classical real-valued polynomial to a tropical polynomial. The rule set that we're using are: "a+b"=max(a,b), "a+b+c+..."=max(a,b,c,...) "ab"=a+b, "abc..."=a+b+c+...

I've defined those operations in the code below, but the problem I'm running into is that I can't convince Mathematica to take more than 2 expressions and separate it into separate terms as I would like. Here's my definitions:

troptoclass[x_ + y_]:=max[x,y] troptoclass[x_ y_]:=x+y troptoclass[x_^n_Integer]:=n*x

Here's what happens when I try the operations when it's more than 2 expressions:

tropical addition of multiple terms

or:

enter image description here

I think it boils down to having Mathematica be able to perform multiple operations in one statement, and also recognition. Is what I'm trying to do here possible in Mathematica? I'm a beginner with this program, any help is greatly appreciated.

$\endgroup$
2
  • $\begingroup$ The simplest way is to do something like a + b c /. {Plus -> Max, Times -> Plus}; you would need to treat Power separately for a general (tropical) polynomial. $\endgroup$ May 17 at 19:58
  • $\begingroup$ @J.M. thanks! I just asked, what does the /. mean in the code? I'm getting ready to try it now. $\endgroup$
    – kingdras
    May 17 at 20:02

2 Answers 2

5
$\begingroup$

I assume that you want to replace Addition by "Max" and Multiplication by addition. It is not clear how a negative term should be treated. For the time being I assume that it will be included in a factor.

Toward this aim, we define following rules:

Plus -> Max
Times -> Plus

In addition we need a rule for powers:

Power[x1_, x2_] -> Sum[x1, x2]

All together:

rules = { Plus -> Max, Times -> Plus, Power[x1_, x2_] -> Sum[x1, x2]};

Now we can give an example:

x + y + 2 x y + x^4 /. rules
(* Max[x, 4 x, y, 2 + x + y] *)
$\endgroup$
4
  • $\begingroup$ Thank you and @J.M. for such a quick response! What does the /. mean exactly? $\endgroup$
    – kingdras
    May 17 at 20:01
  • $\begingroup$ You might want to use a more restrictive pattern for Power, e.g. Power[a_, n_Integer?Positive]. $\endgroup$ May 17 at 20:01
  • $\begingroup$ @king, anytime you encounter an unfamiliar symbol on the front end (in this case, /.), highlight it and press F1 to see more detail about it. $\endgroup$ May 17 at 20:02
  • $\begingroup$ Great, I'll keep that in mind. And this method worked like a charm! Thank you for the help :) $\endgroup$
    – kingdras
    May 17 at 20:09
4
$\begingroup$

I'm not entirely sure if this is what you're looking for, but it seems you would just need to replace your x_ with x__

f[x__] := Total[{x}]
f[a, b, c, d, e]
(*a + b + c + d + e*)

f2[x__] := x /. Times -> Plus
f2[a b c]
(*a + b + c*)
$\endgroup$
1
  • $\begingroup$ Appreciate the help. Daniel beat ya to it :P $\endgroup$
    – kingdras
    May 17 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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