[英]Converting an If-Else statement into a Formula
I have code as follows: 我的代码如下:
`if (a <= 10){
z = 5;
} else {
z = -1;
}`
I figured out that when s(10 - a) = |10 - a| / (10 - a)
我发现当
s(10 - a) = |10 - a| / (10 - a)
s(10 - a) = |10 - a| / (10 - a)
where it outputs a 1 or -1. s(10 - a) = |10 - a| / (10 - a)
输出1或-1。 It outputs 1
if a < 10
and -1
if a > 10
. 如果
a < 10
则输出1
如果a > 10
则输出-1
。
Then, I just solve the linear equation z = s(10 - a) * m + b
, to find constants m and b. 然后,我只求解线性方程
z = s(10 - a) * m + b
,找到常数m和b。
5 = 1 * m + b
and -1 = -1 * m + b
Which outputs b = 2, m = 3
. 5 = 1 * m + b
和-1 = -1 * m + b
其中输出b = 2, m = 3
。
Then this can be modeled as z = 3 * s(10 - a) + 2
. 然后,这可以被建模为
z = 3 * s(10 - a) + 2
。
Now the question becomes more tricky. 现在这个问题变得更加棘手。 What if I have two variables in nested if statements?
如果我在嵌套的if语句中有两个变量怎么办? Such as:
如:
`if (a <= 10){
if(b <= 3){
z = 3;
} else {
z = 1;
}
} else {
if(b <= -5){
z = -11;
} else {
z = 4;
}
}`
I tried to solve this using another series of linear equations. 我试图用另一系列线性方程来解决这个问题。
3 = A * s(10 - a) + B * s(3 - b) + C
1 = A * s(10 - a) + B * s(3 - b) + C
-11 = A * s(10 - a) + D * s(-5 - b) + C
4 = A * s(10 - a) + D * s(-5 - b) + C
with A, B, C, D as constants. 以A,B,C,D为常数。 However, this isn't giving me the right answer.
但是,这并没有给我正确的答案。 What am I doing wrong?
我究竟做错了什么?
An if statement can be transformed into a formula by using the following trick: we need to find a formula that's 1 if the if
statement is true and 0 otherwise. 可以使用以下技巧将if语句转换为公式:如果
if
语句为true, if
需要查找公式为1,否则为0。 We can use the signum function for this: 我们可以使用signum函数:
f(x, y) = (sign(y - x) + 1) / 2
f(x, y) is 1 if x < y and 0 if x > y. 如果x <y,则f(x,y)为1,如果x> y,则f(x,y)为0。 The inverse g(x, y) = 1 - f(x, y).
逆g(x,y)= 1-f(x,y)。
So with those two formulas we can easily put together the whole thing: 因此,使用这两个公式,我们可以轻松地将整个事物组合在一起
f(a, 10) * (f(b, 3) * 3 + g(b, 3) * 1) + g(a, 10) * (f(b, -5) * -11 + g(b, -5) * 4)
A general equation of the form: 形式的一般方程式:
((z2+z1)/2) + (|z2-z1|/2)*f(a,b)
where f(a,b) = |ab|/(ab)
其中
f(a,b) = |ab|/(ab)
In english: (midpoint between 2 given z values) + (distance from midpoint to either z value)*|ab|/(ab)
英语:(
(midpoint between 2 given z values) + (distance from midpoint to either z value)*|ab|/(ab)
trying this on the original example: 在原始示例上尝试此操作:
if (a <= 10){
z = 5;
} else {
z = -1;
}
you get: 你得到:
z1=5
z2=-1
z1=5
z2=-1
f(a,b)=f(10,a)=|10-a|/(10-a)
plugging these in... 插入这些......
((5-1)/2) + (|5-(-1)|/2)*|10-a|/(10-a)
simplifying to your original z = 3 * s(10 - a) + 2
简化为原始
z = 3 * s(10 - a) + 2
When applying this to nested conditional: 将此应用于嵌套条件时:
if (a <= 10) {
... // z1
} else {
... // z2
}
for z1
i get z1 = 2 + |3-b|/(3-b)
对于
z1
我得到z1 = 2 + |3-b|/(3-b)
for z2
i get -3.5 + 7.5*(|-5-b|/(-5-b))
. 对于
z2
我得到-3.5 + 7.5*(|-5-b|/(-5-b))
。 z1
seems ok but z2
doesn't seem to work since if you tried b=0
you have z2 = -3.5 - 7.5*(1)
but since 0>-5
you would expect z2 = 4
since: z1
似乎没问题但是z2
似乎没有用,因为如果你试过b=0
你有z2 = -3.5 - 7.5*(1)
但是因为0>-5
你会期望z2 = 4
因为:
if (b <= -5) {
z = -11;
} else {
z = 4;
}
to get the correct expression i swapped the definition of f(a,b) = |ab|/(ab)
to f(a,b) = |ba|/(ba)
the new result being z2 = -3.5 + 7.5*(|b+5|/(b+5))
and testing b=0
gives the correct result of 4
. 为了得到正确的表达式,我将
f(a,b) = |ab|/(ab)
的定义交换为f(a,b) = |ba|/(ba)
新结果为z2 = -3.5 + 7.5*(|b+5|/(b+5))
和测试b=0
给出4
的正确结果。 This reduces the nested conditional to look like the simpler problem 这减少了嵌套条件看起来像更简单的问题
if (a <= 10) z = 2 + |3-b|/(3-b)
else z = -3.5 + 7.5*(|b+5|/(b+5))
which assuming you know b
you can apply the same method above used for the simple case. 假设你知道
b
你可以应用上面用于简单情况的相同方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.