0
$\begingroup$

i want a module or function or switch or If (whatever works), what asks if a function is an e function or a polynomial(a hole number one or fractorial), a exponential function, a logarithm function and so on. How can i do this? And where can i find all the build-in-questions for this? I only found PolynomialQ so far. But i am not able to put it in a Switch. Maybe It's better with If? Thanks. The Iam Switch is just here to look good. I wanted to create functionQ like this. The Iam Switch is from the book "Programming with Mathematica" by Paul Wellin.

fuu[x_] := 2 x - 5 x^3
PolynomialQ[fuu[x], x]
True

Iam[expr_] := Switch[expr,
                                _Integer, "I am an integer",
                                _Rational, "I am rational",
                                _Real, "I am real",
                                _Complex, "I am complex",
                                _, "I am not a number"]

functionQ[f_] := Switch[f,
                                _PolynomialQ[f, x], "I am polynomial",
                                _, "mystical"]
Head[fuu[x]]
Plus

Iam[fuu[x]]
I am not a number

functionQ[fuu[x]]   
mystical
$\endgroup$
5
  • $\begingroup$ What would a function like Sqrt[x] be? If x is 2 then Sqrt[x] is an irrational number, if x is 4 then Sqrt[x] is an integer. $\endgroup$ – Ymareth Jun 2 '15 at 9:06
  • $\begingroup$ And what is the reason for wanting this check? $\endgroup$ – Ymareth Jun 2 '15 at 9:07
  • $\begingroup$ i am working on an module which shall perfom a curve sketching with an arbitrary function. I want to check first what kind of function i have. I don't know if i will need it. But it would be nice to at least give a message that states what kind of function i have, if it is a special function. - sorry for the factorial english :D $\endgroup$ – baloo Jun 2 '15 at 9:11
  • $\begingroup$ maybe it is useless.. $\endgroup$ – baloo Jun 2 '15 at 9:17
  • $\begingroup$ I think you may find this question relevant to your problem. $\endgroup$ – m_goldberg Jun 2 '15 at 11:56
1
$\begingroup$

PolynomialQ isn't the head of your function, so your pattern won't match it in your switch statement. Instead, you can can use a conditional pattern with the /; operator like so:

functionQ[f_] := Switch[f,
    _/;PolynomialQ[f, x], "I am polynomial",
    _, "mystical"
]

functionQ[fuu[x]]

(* "I am polynomial" *)

You can use this operator in the form pattern/;condition, which only attempts to match pattern if condition evaluates to True.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.