From: mokambo on
Hello,

In Mathematica when a function returns unevaluated the "return value"
is the name of the function with arguments.
How do we control such output? Can we suppress it? How do we change
the usual string for something like "N/A"?
Advise please.

Alex

From: Albert Retey on
Hi,

> In Mathematica when a function returns unevaluated the "return value"
> is the name of the function with arguments.

you should understand that a function definition is nothing but a
replacement rule in Mathematica. Evaluation means that all stored
replacement rules are tried on a given expression until it doesn't
change anymore. The result that you see is not a return value but the
original expression because no pattern will match and no "function call
is executed".

> How do we control such output? Can we suppress it? How do we change
> the usual string for something like "N/A"?

what you see is no string but an expression. There are plenty ways to
change the behavior of Mathematica in this respect, but they differ a
lot in what they really do. To give you a useful answer you would need
to explain a little better what you try to achieve and why you don't
want to "see" the unevaluated expressions. Here are two possibilities
which behave very different:

1) for a given function, add a rule that replaces "everything else" with
the desired "unevaluated" result:

ClearAll[f];
f[x_]:=x^2;
f[___]:="N/A";

test:

f[a]
f[a,b]
f[a,b]//Head

2) define a format rule. Note that this will only change the appearance
in the notebook, not the actual "return value":

ClearAll[g];
Format[g[___]]:="N/A";
g[x_]:=x^2;

test:

g[a]
g[a,b]
g[a,b]//Head


hth,

albert

From: mokambo on
Thanks Albert and Bob,

To give an example, the multiplicative inverse of 3 computed with

PowerMod[3, -1, 288]

does not evaluate and prints the message:

PowerMod::ninv:3 is not invertible modulo 288

Following your suggestions I tried:

PowerMod[___] := "N/A";

but it does not work unless you unprotect the symbol (a bad idea)

Attributes[PowerMod]
= {Listable, Protected, ReadProtected}

So I searched through the docs and found:

Check[PowerMod[3, -1, 288], "N/A"]

which still prints the error message (can be suppressed with Quiet)
but "returns" N/A as desired :)

Thanks!


From: Rui on
On Apr 22, 4:31 am, mokambo <alexandrepassosalme...(a)gmail.com> wrote:
> Hello,
>
> In Mathematica when a function returns unevaluated the "return value"
> is the name of the function with arguments.
> How do we control such output? Can we suppress it? How do we change
> the usual string for something like "N/A"?
> Advise please.
>
> Alex

Totally agree with Albert.
I wanted to note too that Mathematica is symbolic. That means that
some results you expect to get are symbols and numbers combined in a
certain way (Imagine expanding (x+1)^2 to get x^2+2x+1 as a result)
Therefore, you cannot tell Mathematica something like
"All results that are of the form something[something] should be
replaced by "N/A""
because you would get "N/A" in everything you do.

However, I'm guessing perhaps it is possible (don't know if it makes
sense) to tell Mathematica to return "N/A" every time it tries to
replace an expression and finds no rule to do so. Perhaps setting $Pre
to a function that stores the expression unevaluated and $Post to
another one that compares the result and decides what to do. Or
perhaps with a general Format, not sure.

From: Bob Hanlon on
Include the definition

f[x_] = "N/A"

This will cover any case not otherwise addressed by the definitions for f.


Bob Hanlon

---- mokambo <alexandrepassosalmeida(a)gmail.com> wrote:

=============
Hello,

In Mathematica when a function returns unevaluated the "return value"
is the name of the function with arguments.
How do we control such output? Can we suppress it? How do we change
the usual string for something like "N/A"?
Advise please.

Alex