Take the 2-minute tour ×
MathOverflow is a question and answer site for professional mathematicians. It's 100% free, no registration required.

I'm writing a computer program and I need to fit some IF..ELSE condition into mathematic model, so I can't use regular programming constructs. For example, how would I turn this into mathematic equation (or inequation):

if (3ax + 5by + 8cz >= 320) then
    w = 1.0;
    w = 0.8;

I understand how to express the domain of w to accept only values 0.8 and 1.0 by using:

(w - 1.0) * (w - 0.8) = 0

But I haven't got a clue how to transform the above IF statement. Is it even possible?

P.S. I'm rather new to math overflow, so I'm not sure which tags to assign to this question. Please feel free to re-tag appropriately.

share|improve this question

closed as no longer relevant by Yemon Choi, S. Carnahan, Andy Putman, Scott Morrison May 21 '10 at 5:43

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

This is missing some context. What kind of expressions can you use? For example, if you have min/max then you can, otherwise you might be out of luck... –  François G. Dorais Mar 19 '10 at 1:33
François, I'm not sure yet. Depending on the answers I get here I need to decide what kind of mathematic model should be used to solve my problem. I currently only have linear inequations in, and one min, so it can be solved with Linear Programming. But I need to add those IFs into model, so it will probably turn into something else. –  Milan Babuškov Mar 19 '10 at 2:40
I voted to close. This appears to be a programming question rather than a math question (its mathematical content is basically precalculus). This site is intended for research level mathematics questions. I recommend asking it on stackoverflow. –  Andy Putman May 21 '10 at 4:11

4 Answers 4

up vote 6 down vote accepted

You can express piecewise functions by using the unit step function. Your example would be:

$ w = 0.8 + 0.2 \times \textrm{unit_step}(3 a x + 5 b y + 8 c z - 320) $

Note that the unit step is usually defined to have a value of $ \frac{1}{2} $ at 0, but this is not generally an issue.

It may be more convenient to replace the unit step with the signum function, or with the function $ \mathrm{s}(x) = \frac{x}{|x|} $ which is equal to it almost everywhere; this is left as an exercise to the reader.

share|improve this answer

In the example you give, $w$ is simply a function of $a$,$x$,$b$,$y$,$c$, and $z$ (i.e., $w = f(a,x,b,y,c,z)$), so would the following be what you are looking for:

$f(a,x,b,y,c,z) = \begin{cases}1 & \text{if }3ax + 5by + 8cz \geq 320 \\ 0.8 & \text{otherwise}\end{cases}$

I would usually write $f$ as a multi-part definition, but cannot seem to figure out how to do that on mathoverflow (in Latex I would usually use an array).

share|improve this answer
I took the liberty of trying to fix your latex. –  Scott Morrison Mar 19 '10 at 2:21
If I'm understanding your remark, Travis, what you're looking for are the \begin{cases}...\end{cases} commands in LaTeX. –  Cam McLeman Mar 19 '10 at 7:45
Thank you. I had been using the \begin{arrary}...\end{array} in latex but that did not seem to work on mathoverflow –  Travis Service Mar 19 '10 at 12:52
\begin{array}..\end{array} should, for the most part, no longer be used in newly written LaTeX. AMSLaTeX provides for considerably better alternatives. –  Mariano Suárez-Alvarez Mar 19 '10 at 18:35

You can use indicators or characteristic functions of sets. For a set $A$, you can define:

${\bf 1}_A(x)$ equals $1$ if $x\in A$, and equals $0$ if $x\notin A$.

Or, for a condition $A$ you can define ${\bf 1}_A$ to be $1$ if $A$ holds and $0$ if it does not. Then you can write things like

$f(x)=3\cdot {\bf 1}_{x\le 2}+10\cdot {\bf 1}_{x>2}$

for a function that is equal to $3$ for $x\le 2$ and to $10$ for $x>2$

share|improve this answer

There are many conventions. My favorite is the Iverson bracket where [foo] is 1 if foo is true and 0 otherwise.

For a slightly more general notation you can write $f(x)=\begin{cases}1,&\text{foo is true}\\\\0,&\text{foo is false}\end{cases}$

The characteristic function (or indicator function) is also used, as are various step functions, e.g., the Heaviside step function H or the sign function sgn.

share|improve this answer

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