New answers tagged

0

Surely the simplest way to do this is just to check whether the imaginary part is zero: Im[z] == 0 This will return true if z is a real number and false if z is complex.


2

tl;dr Use this modified valueQ function. SetAttributes[valueQ, {HoldFirst}]; valueQ[h_[args__]] := With[{eval = args}, ! Hold[Evaluate[h[eval]]] === Hold[h[eval]] ]; How ValueQ works. I learned from @halirutan's answer in ValueQ returns false positive for one argument type only that ValueQ simply tests whether an expression is equal to ...


3

f[x] === HoldForm[f[x]] does not provide the desired result, because HoldForm stays attached to f[x] on the right side of the expression. Thus, the left and right sides never are identical. Instead use, f[x] === Unevaluated[f[x]] (* True *) And, when the function does something, g[x_] := x^2 g[x] === Unevaluated[g[x]] (* False *) Unevaluated prevents ...


16

Others have argued in the comments that this behaviour makes sense mathematically, and I fully agree. But further than that, it is also very practical. Mathematica's functions are usually designed to give reasonable results for edge cases in the sense that if you put these functions together and write some more complex calculation, this compound function ...



Top 50 recent answers are included