3
$\begingroup$

Suppose I have the polynomial given by the determinant of this matrix:

matry = {{1 - R, 3, -3}, {1, 0 - R, 0}, {0, 1, 0 - R}}

I am feeding the Mathematica output into a Perl program, and when I evaluate

Det[matry], I get the output:

              2    3
 -3 + 3 R + R  - R

The polynomial spans over two lines - no good. So I try

CForm[ Det[matry] ], and I get:

-3 + 3*R + Power(R,2) - Power(R,3)

Is there an easy way to get a nice output of something like -R^3 + R^2 + 3*R - 3 in one line of plain text?

$\endgroup$
1
  • 4
    $\begingroup$ I think you want ToString[InputForm[Det[matry]]]? $\endgroup$
    – march
    Commented Jun 10, 2015 at 5:01

2 Answers 2

2
$\begingroup$

Thank you march for my solution:

ToString[InputForm[Det[matry]]]
$\endgroup$
2
  • 4
    $\begingroup$ Just a gentle suggestion: always check Mathematica's help files first. They are wonderful. I always go there first when I have a question, and that's in fact how I figured out the answer to your question (although, admittedly, I was aware that there was some function that did what you needed in this case). Note that InputForm will not work in general, as it is specific to Mathematica input form. For instance, Sqrt[x] is the input form for the square root. In the case of polynomials, however, this solution works. $\endgroup$
    – march
    Commented Jun 10, 2015 at 5:15
  • $\begingroup$ Duly noted, thanks! $\endgroup$
    – Kolibrie
    Commented Jun 10, 2015 at 5:25
2
$\begingroup$

Since you are cutting and pasting output into perl, you may find this easier than explicitly converting your output expression into input form string.

  1. Select the cell with your output.
  2. Bring up the contextual menu (right mouse click)
  3. Select Convert To > Raw Input Form

converting

The result will be

result

$\endgroup$
1
  • $\begingroup$ Thank you for the suggestion - I wasn't aware of this either. However the other solution is more useful because I am reading (not copying/pasting) many unique polynomials from calling Mathematica commands in Perl. $\endgroup$
    – Kolibrie
    Commented Jun 10, 2015 at 19:19

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.