# How to find the domain and range of a function with Mathematica?

I'm studying calculus and in some exercises I am asked to find the domain and range of a function. Does Mathematica have already a built-in function for this?

I can imagine some ways of doing so, but at the moment I want to know if there's a built-in function or some easy way of doing it.

-

If you have a domain, you can often find a range using Interval.

Examples:

In[1]:= Sin@Interval[{0, 2 Pi}]
Out[1]= Interval[{-1, 1}]

In[2]:= Sin@Interval[{0, Pi}]
Out[2]= Interval[{0, 1}]

-
I'd love to see this answer expanded. +1 –  Mr.Wizard Jul 7 '12 at 9:32
@Mr.Wizard I think there are so many additional comments on the same thing that there is not much I can expand on. I was travelling again, so couldn't react earlier. –  Szabolcs Jul 9 '12 at 7:10

I think that the closest thing to a built in function, as you ask for, is the the built-in WolframAlpha functionality. For example:

WolframAlpha["domain of x+x/(x(x^2-1))",
{{"Result", 1}, "Output"}]
WolframAlpha["range of x/(x(x^2-1))",
{{"Result", 1}, "Output"}]

HoldComplete[x < -1 || -1 < x < 0 || 0 < x < 1 || x > 1]

HoldComplete[y < -1 || y > 0]

-

Interval is a proper way for your task, but sometimes it is not the way to go. There are functions which can return neither their ranges nor domains, e.g. function yielding k-th non-trivial zero of the Riemman zeta function. E.g. ZetaZero[1000]//N yields 0.5 + 1419.42 I, while N[ ZetaZero @ Interval[{1000, 10000}]]  yields ZetaZero[Interval[{1000, 10000}]], however since it is weakly monotonic on the critical line with respect to the imaginary part (i.e. 0.5 + t I, where t is a real number, )

N[ ZetaZero @ {1000, 10000}]

{0.5 + 49.7738 I, 0.5 + 236.524 I}


It is known that ( purely mathematically speaking) the domain of ZetaZero are integer numbers, but there are certain limitations of its implementation in Mathematica, e.g. there is no way to find its domain, only a kind of "divide and conquer" approach, e.g.

ZetaZero[10^7] // N

ZetaZero[10^7 + 1] // N

0.5 + 4.99238*10^6 I

ZetaZero::largp: Argument 10000001 in ZetaZero[10000001] is too large for this
implementation. >>
ZetaZero[10000001]


There is a second argument in ZetaZero[k, t] representing the k-th zero with imaginary part greater than t. Neither this works with Interval, though it is Listable with respect to the second argument :

ZetaZero[105, Interval[{15., 35.5}]]
ZetaZero[105, {15., 35.5}]

ZetaZero[105, Interval[{15., 35.5}]]
{1/2 + 247.137 I, 1/2 + 253.07 I}


There are similar issues with e.g. PrimePi ( the Mathematica counterpart of the prime counting function $\pi(x)$) and Prime.

PrimePi[ Interval[{10, 100}]]

PrimePi[ Interval[{10, 100}]]


but

PrimePi[{10, 100}]

{4, 25}

Plot[PrimePi[x], {x, 10, 100}, AxesOrigin -> {0, 0}, PlotStyle -> Thick]


The maximal evaluatable argument for PrimePi is 25 10^13 -1, while with Prime there is a bigger problem, like e.g. an apparently extensible domain :

Prime @ {# + 1, #, # + 1} & 7783516045221

Prime::largp: Argument 7783516045222 in Prime[7783516045222] is too large for
this implementation. >>
{Prime[7783516045222], 249999997909357, 249999997909367}


You can find related details here : What is so special about Prime ?.

There are infinitely many primes, however values of $\pi(x)$ are known up to $x = 10^{23}$, look at e.g. Prime Counting Function on MathWorld .

-
Well, since these functions are expected to only take integer arguments, Interval[] is certainly not expected to interact nicely with them... –  Ｊ. Ｍ. Jul 7 '12 at 9:50
Well, PrimePi evaluates on real arguments, e.g. PrimePi[1000. Pi] yields 446. –  Artes Jul 7 '12 at 10:15
What do I need to know for understanding your answer? Your answer is amazing but I read it almost as if I were reading Japanese (I don't know japanese). Notice that this is not a critic, I just need references for understanding it. –  Gustavo Bandeira Jul 7 '12 at 21:55
@GustavoBandeira My intentions were 1.to explain certain limitations of using Interval. 2. to show limitations of built-in functions, especially with respect to their domains. 3. to demonstrate that sometimes representing of mathematical objects is far from high-fidelity. There are 4 links where you can read more on these issues. I think that careful reading of the M documentation concerning the functions which I mentioned would be very helpful. If there are other doubts you should specify them more strictly. I hope I could help more and possibly edit and update my answer. –  Artes Jul 7 '12 at 22:11
@GustavoBandeira I think that MathWorld is an interesting repository of knowledge. If you have more detailed questions, try to ask them and I'll add a bit more extended clarifications. –  Artes Jul 7 '12 at 22:21
show 1 more comment

You can often find the domain of a function f[x] using Reduce. Assuming the domain is a subset of the real numbers, the syntax would be

Reduce[Abs[f[x]] < Infinity, x, Reals]


Here are some examples:

(1)

A piecewise defined function:

Clear[f]; f[x_] := Piecewise[
{
{-1.5, x < 0},
{3, x > 1},
{Undefined, 0 < x < 1}
}]

Plot[f[x], {x, -1, 2}]


Reduce[Abs[f[x]] < Infinity, x, Reals]

(* ==> x <= 0 || x >= 1 *)


This means x has to be less than 0 or larger than 1.

(2)

A function with singularities:

Clear[f]; f[x_] := 1/Sin[x]

Plot[f[x], {x, -4 Pi, 4 Pi}]


Reduce[Abs[f[x]] < Infinity, x, Reals]

(*
==>
C[1] \[Element]
Integers && (-Pi + 2 Pi C[1] < x < 2 Pi C[1] ||
2 Pi C[1] < x < Pi + 2 Pi C[1])
*)

Last[%] /. C[1] -> n

(*
==> -Pi + 2 n Pi < x < 2 n Pi ||
2 n Pi < x < Pi + 2 n Pi
*)


On the last line I've just made the conditions easier to read by introducing an integer n instead of the default constant C[1].

-
Haha. You also solved another doubt I had: "How to make piecewise defined functions on Mathematica" I just didn't ask it because I could solve using a manual method but here it is. Thanks. –  Gustavo Bandeira Jul 7 '12 at 21:50