Return equality from Mathematica function

夙愿已清 提交于 2019-12-02 05:56:42

You can use Defer to do this:

In[5]:= printableEqual[x_, y_] := Defer[x == y];
In[6]:= printableEqual[1, 2]
Out[6]= 1 == 2

Another trick is to just use Unevaluated:

In[1] := Print[Unevaluated[1 == 1]]
1==1

Usually one uses HoldForm for this sort of thing. HoldForm is a head that works like Hold, in that it doesn't evaluate its contents, but it's not displayed when it's printed as output, like so:

In[1]:= HoldForm[x == 3]
Out[1]= x == 3

In[2]:= HoldForm[x == x]
Out[2]= x == x

As with Hold, you can interpolate things into a HoldForm using With or function argument substitution, like so:

In[3]:= PrintableEqual[x_, y_] := HoldForm[x == y]

In[4]:= PrintableEqual[x, x]
Out[4]= x == x

However, this will mean that the arguments are evaluated before substitution, like so:

In[5]:= PrintableEqual[x + x, 2 x]
Out[5]= 2 x == 2x

If you don't want this to happen, you can use SetAttributes and HoldAll:

In[6]:= SetAttributes[PrintableEqual, {HoldAll}]

In[7]:= PrintableEqual[x + x, 2 x]
Out[7]= x + x == 2 x

Note that HoldForm is always there, it's just not displayed in output form:

In[8]:= PrintableEqual[x, x] // InputForm
Out[8]= HoldForm[x == x]

If you want to evaluate things, use ReleaseHold:

In[9]:= ReleaseHold@PrintableEqual[x, x]
Out[9]= True

Another thing you can do is to is use Grid[] to align all of your equalities - the added advantage is that since you don't actually create expressions with Equal[], you don't have to prevent their evaluation.

In[1]:= Grid[Table[{LHS[i],"\[LongEqual]",RHS[i]},{i,2}],
             Alignment -> {Right,Center,Left}]
Out[1]= LHS[1] == RHS[1]
        LHS[2] == RHS[2]

On a similar vein, you could manually typeset using

printableEqual[LHS_, RHS_] := Row[{LHS, " \[LongEqual] ", RHS}]

or more generally

printableEqual[LHS_, mid___, RHS_] := Row[Riffle[{LHS, mid, RHS}, " \[LongEqual] "]]

By the way, the output from the printableEqual[] defined above can be converted back to a real Expression using ToExpression[ToString[#]]& or something like

toRealEqual[Row[lst_List]] := Equal@@lst[[1;;-1;;2]] /; OddQ[Length[lst]] && Union[lst[[2;;-2;;2]]] == {" \[LongEqual] "}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!