I need to make the output of a variable positive, for both negative and positive inputs

88 Views Asked by At

I need to make the output of a variable positive, for both negative and positive inputs.

Since I am using it in the linear optimization problem, no squares, self-division or self-multiplication is allowed.

Need a mathematical solution, No prebuilt functions of programming languages are allowed

3

There are 3 best solutions below

0
On BEST ANSWER

$$|5 x_1-7x_2| \leq 10$$ is equivalent to the two linear constraints $$5 x_1-7x_2 \leq 10 \text{ and } -(5 x_1-7x_2) \leq 10$$

0
On

Let the input value be $x$. Then compare $x$ with $0$, if it is greater than $0$ we are done. If it is less than $0$, return $-x$.

0
On

Define a function $$f(x)=\mid x\mid=\left\{\begin{array}{ll}x&\text{if }x>0\\-x&\text{otherwise}\end{array}\right..$$