# Save a result containing HoldForm to a file

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?

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!"

• That works. I don't know why I didn't use := at the first place. I will think about it. Thank you. Dec 16, 2014 at 19:25
• 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. Dec 16, 2014 at 19:55
• @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? Dec 16, 2014 at 19:59
• Ok, I will edit my question. Dec 16, 2014 at 20:01

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]

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

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]]

• No, x and y are variables as in a polynomial. The variable test is a polynomial. Dec 16, 2014 at 19:19