Why is it incorrect to define an "impure" function?

288 Views Asked by At

While doing a math question on piecewise functions, I came across an answer which seemed wrong to me but I could not explain it mathematically.

The question states (summarised):

A lottery dealer makes 6 cents on each ticket sold. If the lottery dealer sells more than 25,000 tickets, she gets an extra 2 cents for every ticket after the 25,000th ticket. Express her profits as a function of the number of sold tickets.

The correct (or expected) answer was the piecewise function: $$ f(x) = \begin{cases} 6x, & \text{when } 0 \le x \le 25000 \\ 8x-50000, & \text{when } x \gt 25000 \end{cases} $$

However, another answer I saw was:

$$ \text{let } x \text{ be the number of tickets sold } \le 25000 \\ \text{let } z \text{ be the number of tickets sold } \gt 25000 \\ f(x) = 6x + 8z $$

I found this answer intuitively incorrect. To me, all mathematical functions are pure (a concept from programming). In particular, all values in a function should not be determined by anything other than it's inputs.

In this case, $f(x)$ is defined with an extra $z$ which is not given as an input.

But this is a concept from programming, not mathematics. After reading the definition of a function on Wikipedia, I don't see anything that explicitly says that additional variables are allowed or not allowed in function definitions.

Is there something I missed in the definition or is the second answer acceptable?

EDIT

To clarify and amplify the problem, here is another example. Let's say I want to define a function that adds $x$ to the current hour (in 24 hours). For example,

$$ \text{let } t \text{ be the current 24 hour time, } 0 \le t \le 23, t \in \mathbb{Z} \\ f(x) = t + x $$

Similar as before, this introduces a variable $t$ that is not given as an input to $f(x)$.

My question is this: What part of the mathematical definition of a function disallows this?

3

There are 3 best solutions below

0
On BEST ANSWER

I'm going to address this part of your question, since the other answers have addressed the other parts:

To clarify and amplify the problem, here is another example. Let's say I want to define a function that adds $x$ to the current hour (in 24 hours). For example,

$$ \text{let } t \text{ be the current 24 hour time, } 0 \le t \le 23, t \in \mathbb{Z} \\ f(x) = t + x $$

Similar as before, this introduces a variable $t$ that is not given as an input to $f(x)$.

My question is this: What part of the mathematical definition of a function disallows this?

Well, some might object that the notion of "the current time" doesn't have any mathematical meaning. But let's set that objection aside.

The definition you've given is, in fact, the definition of a function, but it's the definition of a pure function. Specifically, given the time and place that I'm writing this, the function that you've defined is $f(x) = x + 17$. An hour from now, the definition will refer to a different function instead: namely, the function $f(x) = x + 18$.

All right, but let me give another attempt at defining an impure function. This time, I'm going to write something that really is an invalid definition:

$$g(x) = x + (\text{the current year, as of the time that this function is being evaluated})$$

I've attempted to define one function, $g$, where $g(10)$ is $2028$ right now, but will change to be $2029$ next year, and so on.

So, what part of the mathematical definition of a function disallows this? Well, here's one standard definition of a function:

Given sets $X$ and $Y$, a function $X \to Y$ consists of a subset $s$ of $X \times Y$, such that each element of $X$ appears exactly once as the left-hand component of an element of $s$.

To a software developer, a mathematical function isn't like a subroutine (or "function") at all. Instead, a mathematical function is essentially a mere list of input and output values (and each valid input must be listed exactly once). And there simply is no way for a mere list of input and output values to be an "impure function", because once you've determined what the function is, you've determined all of its outputs, too.

2
On

In $f(x) = 6x + 8z$ you have used $x$ to mean both the number of tickets sold in total and the number if below $25000$ (and presumably $0$ otherwise). This is not satisfactory

We could attempt a recovery. First let $x$ be the number in total and $y$ the number in excess of $25000$, which from the question would give $f(x)=6x+2y(x)$

We can actually write down $y$ explictly as $y=\max(x-25000,0)$ and that would give a single line closed form of $f(x)=6x+2\max(x-25000,0)$ which can be written as $$f(x)=\max(8x-50000,6x)$$

5
On

This other answer is indeed a little clumsy but not unrecoverable.

The problem indisputably has a single input variable, the number of tickets sold, which is denoted by $x$.

Now consider

$$ \text{let } \color{green}y \text{ be the number of tickets sold } \le 25000 \\ \text{let } z \text{ be the number of tickets sold } \gt 25000 \\ f(x) = 6\color{green}y + 8z $$ or better

$$ \text{let } y\color{green}{(x)} \text{ be the number of tickets sold } \le 25000 \\ \text{let } z\color{green}{(x)} \text{ be the number of tickets sold } \gt 25000 \\ f(x) = 6y\color{green}{(x)} + 8z\color{green}{(x)}.$$

The "impurity" is gone (though we can still discuss the intelligibility of the definitions of $y$ and $z$).

The main sin here was to use $x$ with two different meanings. Introducing intermediate dependent variables/functions is harmless.


Update (after the EDIT):

It is common practice to use literal constants in function definitions. They are considered to belong to the context and any variation of their values are not relevant to the discussion in progress, so they can be implied.

For your example the correct defintion could be

$$f:x\in\mathbb R, t\in\{0,1,\cdots23\}:f(x,t)=x+t$$ but for conciseness omission of the second argument is allowed. (Think of $f'(x)$ vs. $\dfrac{\partial f(x,t)}{\partial x}$ when you don't care about the influence of time.)

The notation $f_t(x)$ can also be used, with the intent to stress that $t$ plays a different kind of role.