TransWikia.com

The number 1 evaluates inside HoldForm sometimes

Mathematica Asked by Mattia Landoni on April 20, 2021

If I run the following code:

Do[
 Print["*** ", HoldForm[x] /. x -> a, " ***"];
 Print["Replace: ", (HoldForm[x] /. x -> a) /. {n1 -> 1, n2 -> 2, n3 -> 3}];
 Print["FullForm: ", FullForm[a]];
 , {a, {n1 n2, -n1 n2, n1 n2 n3}}
 ]

I get

    *** n1 n2 ***
    Replace: 1 x 2
    FullForm: Times[n1,n2]
    *** -n1 n2 ***
    Replace: -2
    FullForm: Times[-1,n1,n2]
    *** n1 n2 n3 ***
    Replace: 2 x 3
    FullForm: Times[n1,n2,n3]

I kind of understand what is going on, but I would like the output in the second case to be -1 x 2 and in the third case 1 x 2 x 3. How do I do that?

One Answer

It is enough to write the substitution rule as

{n1 -> HoldForm[1], n2 -> 2, n3 -> 3}

If later one wants the expression evaluated, it will be enough to use HoldForm[] twice.

Answered by Mattia Landoni on April 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP