0
$\begingroup$

This is the workflow I have. Basically, I want the outputs of the polynomials I computed are in a specific order. (It is not the same one here, this is just an example.).

f[1] = 1;
f[2] = x;
f[3] = x + y;
f[n_] := Expand[f[n - 1]*(x + 2 y - 3)]

f[4]

-3 x + x^2 - 3 y + 3 x y + 2 y^2

monomials = MonomialList[f[4]]

{x^2, 3 x y, -3 x, 2 y^2, -3 y}

First /@ CoefficientRules[f[4]]

{{2, 0}, {1, 1}, {1, 0}, {0, 2}, {0, 1}}

Sort@(First /@ CoefficientRules[f[4]])

{{0, 1}, {0, 2}, {1, 0}, {1, 1}, {2, 0}}

permutation = Ordering[First /@ CoefficientRules[f[4]]]

{5, 4, 3, 2, 1}

SortedMonomials = monomials[[permutation]]

{-3 y, 2 y^2, -3 x, 3 x y, x^2}

result=Plus @@@ HoldForm[Evaluate[SortedMonomials]]

-3 y+2 y^2-3 x+3 x y+x^2

But when I use Save to save the last result above, it will be result = HoldForm[-3*y + 2*y^2 - 3*x + 3*x*y + x^2] in the saved .txt file.

I want the content in the file to be result = -3*y + 2*y^2 - 3*x + 3*x*y + x^2.

Is there any way to achieve my requirement, only use MMA?

$\endgroup$
1
$\begingroup$

Is there some reason you are avoiding SetDelayed? That will work like this:

x = "Fail!";
test := y + x

Save["foo.m", test]
FilePrint["foo.m"]
test := y + x

x = "Fail!"

If for some reason that won't work you can use Unevaluated:

Remove[x, y, test]

y = "Fail!";
test = Unevaluated[y + x];

Save["bar.m", test]
FilePrint["bar.m"]
test = y + x

y = "Fail!"
$\endgroup$
4
  • $\begingroup$ That works. I don't know why I didn't use := at the first place. I will think about it. Thank you. $\endgroup$ – user565739 Dec 16 '14 at 19:25
  • $\begingroup$ I see the reason. In fact, I am defining a sequence of polynomials. Each of these polynomials will be determined by some computation. The outputs of these computations are polynomials. I tried to rearrange the terms and use HoldForm to make sure they appear as I expected. In this case, using SetDelayed is not the point, because I still need to use HoldForm. But after the whole computation are done and the sequence of polynomials are defined, I want to save the results into a file. $\endgroup$ – user565739 Dec 16 '14 at 19:55
  • 1
    $\begingroup$ @user565739 Would you edit your question to give a condensed yet complete example of your workflow? I am not sure I understand. What exactly are the "results" and in what way are they the product of computation? $\endgroup$ – Mr.Wizard Dec 16 '14 at 19:59
  • $\begingroup$ Ok, I will edit my question. $\endgroup$ – user565739 Dec 16 '14 at 20:01
1
$\begingroup$

Since you are saving to a text file, how about convert to string first, for example:

pl = ToString[#] &[HoldForm[test = x + y]]; Save["file.txt", pl]

$\endgroup$
1
$\begingroup$

One can use Block to prevent Plus from evaluating until after result is defined.

SortedMonomials = {-3 y, 2 y^2, -3 x, 3 x y, x^2};

Block[{Plus},
 result = Plus @@ SortedMonomials;
 ]

Save["/tmp/foo.txt", result]

FilePrint["/tmp/foo.txt"]

result = -3*y + 2*y^2 - 3*x + 3*x*y + x^2

$\endgroup$
0
$\begingroup$

Assuming you have values for x and y defined, hence the need for HoldForm:

x = 1;
y = 2;

Block[{x, y},
 test = y + x;
 Save["file.txt", test]]
$\endgroup$
1
  • $\begingroup$ No, x and y are variables as in a polynomial. The variable test is a polynomial. $\endgroup$ – user565739 Dec 16 '14 at 19:19

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.