Return equality from Mathematica function

后端 未结 4 1165
小蘑菇
小蘑菇 2021-01-23 16:51

I have a function that returns equalities, which I want to print, for example, x==y, or 2x+5==10. These usually have no meaning for mathematica, it cannot simplify it furhter.

4条回答
  •  终归单人心
    2021-01-23 17:20

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

提交回复
热议问题