In mathematics, a set can be write in two(or more) form: one is {3x | x in [0,1]}
, the other {x | P(x)}
, where P is called a propositional function or predicate. Since Mathematica have a great power of dealing with quantifiers, once you can write a set in the second form above, you can do plenty of things and tricks that you would have thought to have nothing to do with programming before.
As we may write down the range of a function f in math this way: {f(x) | x in Dom f}
, however, f(x)
is not a "predicate", it's more closely to be thought as an object, so putting it into Mathematica programming might be difficult. What's lucky, we can write it using another way! That is{ y | ∃x such that y=f(x)}
, where ∃x such that y=f(x)
is a propositional function of x. From now, you can use the build-in functions Reduce
to get the all possible values of y.
For example, if you have a function y = x^3 + x + 6 in math,
and you want to find its range(w.r.t whole domain of f) or image of some proper set of its domain, try to use the the quantifier-family, ie Reduce
, ForAll
and Exists
.
For example, here is the way to find the image of a set [1,15]
Reduce[Exists[x, 1 <= x <= 15, y == x^3 + x + 6], y, Reals]
Output: 8 <= y <= 3396
or find a range (w.r.t the whole domain of that function)
Reduce[Exists[x, y == (x^2+x)/(4-x)], y, Reals]
Output: y <= -9 - 4 Sqrt[5] || y >= 1/(-9 - 4 Sqrt[5])
After you become sophisticated with quantifiers, you are on a highly level of programming.
Finding the multiples of 6 among 1~100.
Reduce[Exists[x, x \[Element] Integers, y == 2 x] &&
Exists[x, x \[Element] Integers, y == 3 x] &&
1 <= y <= 100, y, Integers]
Output: y == 6 || y == 12 || y == 18 || y == 24 || y == 30 || y == 36 || y == 42 || y == 48 || y == 54 || y == 60 || y == 66 || y == 72 || y == 78 || y == 84 || y == 90 || y == 96
Finding the distance between a fixed point and an arbitrary curve.
This example is also very cool. Given a fixed point, take (3,4)
for example and, an arbitrary curve defined by an equation, take x^3 + y^3 + 2 x^2 y - 7 x y^2 + 8 x - 7 y + 46 == 0
. How to get the distance between the point and the graph?
First we need to insure that the point does not belong to the graph.
x^3 + y^3 + 2 x^2 y - 7 x y^2 + 8 x - 7 y + 46 == 0 /. {x -> 3, y -> 4}
Output: False
And we now try to get the set of every distance. This set collects all the distance from every points in the graph to the fixed point we picked.
Reduce[Exists[{x, y},
x^3 + y^3 + 2 x^2 y - 7 x y^2 + 8 x - 7 y + 46 == 0 &&
Sqrt[(x - 3)^2 + (y - 4)^2] == d], d, Reals]
Output: d >= Root[-3228313885871685449 + 3580719856462877084 #1^2 -
1341308868741195007 #1^4 + 229192171361605440 #1^6 -
21541588953050597 #1^8 + 1444606027764877 #1^10 -
58640497566169 #1^12 + 1358762455214 #1^14 - 17611698786 #1^16 +
102703185 #1^18 &, 6]
% //N
Output: d >= 1.33688
Perfect!
Another logic to do the same thing is:
N[Reduce[ForAll[{x, y},
x^3 + y^3 + 2 x^2 y - 7 x y^2 + 8 x - 7 y + 46 == 0,
Sqrt[(x - 3)^2 + (y - 4)^2] >= dMin]], 10]
And we get: dMin <= 1.336884157
Have fun playing with quantifiers!