"Algebraic" IF Statement Evaluation

May 19, 2009 22 Replies

I need to evaluate...



IF(t,1,0) (If t is true, output 1, if t is false, output 0 (numeric), t may be something like V11 >= 7, for example)



BUT I have no IF statement handling capability at this level of a simulation.



I do have algebraic capability.



Is there some cute way to do this algebraically?



...Jim Thompson



-- | James E.Thompson, P.E. | mens | | Analog Innovations, Inc. | et | | Analog/Mixed-Signal ASIC's and Discrete Systems | manus | | Phoenix, Arizona 85048 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at

formatting link
| 1962 |



"When the government fears the People, that is Liberty. When the People fear the Government, that is Tyranny." - attributed to Thomas Jefferson by his contemporaries



"The Constitution is not an instrument for the government to restrain the people, it is an instrument for the people to restrain the government -- lest it come to dominate our lives and interests." - Patrick Henry


For your example, V11>=3D7:

(abs(V11-7))/(V11-7) will be +1 for V11>7, -1 for V11

t=X/X

This will sorta work, but if x is 0 then you'll probably get errors, to a human it's the 0/0 = 1 thing not having used your emulator I can't be sure what'll happen of how to treat an error like this.

I don't have INT available, I do have ABS and some trig functions

...Jim Thompson

| James E.Thompson, P.E. | mens | | Analog Innovations, Inc. | et | | Analog/Mixed-Signal ASIC\'s and Discrete Systems | manus | | Phoenix, Arizona 85048 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at http://www.analog-innovations.com | 1962 | Stormy on the East Coast today... due to Bush\'s failed policies.

That's what I came up with, except I inserted a small "delta" (smaller than my step size) to avoid 0/0. I was hoping for something more compact :-(

...Jim Thompson

| James E.Thompson, P.E. | mens | | Analog Innovations, Inc. | et | | Analog/Mixed-Signal ASIC\'s and Discrete Systems | manus | | Phoenix, Arizona 85048 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at http://www.analog-innovations.com | 1962 | Stormy on the East Coast today... due to Bush\'s failed policies.

Do you have a unit step function (u(x)) available?

Paul Hovnanian paul@hovnanian.com ---------------------------------------------------------------------- Have gnu, will travel.

No :-(

...Jim Thompson

| James E.Thompson, P.E. | mens | | Analog Innovations, Inc. | et | | Analog/Mixed-Signal ASIC\'s and Discrete Systems | manus | | Phoenix, Arizona 85048 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at http://www.analog-innovations.com | 1962 | Stormy on the East Coast today... due to Bush\'s failed policies.

I don't have function SGN :-(

...Jim Thompson

| James E.Thompson, P.E. | mens | | Analog Innovations, Inc. | et | | Analog/Mixed-Signal ASIC\'s and Discrete Systems | manus | | Phoenix, Arizona 85048 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at http://www.analog-innovations.com | 1962 | Stormy on the East Coast today... due to Bush\'s failed policies.

y = V11 -7

(abs(y) + y)/(2*y)

P.S. If you want avoid awkward situations very near y == 0 you could do

(abs(y) + y)/(2* abs(y) + e)

where e is a very small positive number

are there any operators or functions that can give a different mumeric result depending on the truth of the input.

many systems have false as 0 and true as 1 (or less common -1)

else maybe something like (sgn( sgn(V11-7) *2 +1 ) + 1 ) /2

ABS has an intrinsic "if" that can do it. Spehro showed the method.

(numeric),

Do you have a tanh() function?

No, Though I'm fascinated. How would use of TANH() work in this situation?

...Jim Thompson

| James E.Thompson, P.E. | mens | | Analog Innovations, Inc. | et | | Analog/Mixed-Signal ASIC\'s and Discrete Systems | manus | | Phoenix, Arizona 85048 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at http://www.analog-innovations.com | 1962 | Stormy on the East Coast today... due to Bush\'s failed policies.

tanh(x) = (e^x - e^-x) / (e^x + e^-x), so for large |x| (for example, |x| > 3) it approximates x/abs(x) quite closely but avoids divide-by-zero issues. For snappier results, consider tanh(k*x), at the risk of overflow if k is too big.

.
jiw

So the equivalent of "SGN"?

...Jim Thompson

| James E.Thompson, P.E. | mens | | Analog Innovations, Inc. | et | | Analog/Mixed-Signal ASIC\'s and Discrete Systems | manus | | Phoenix, Arizona 85048 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at http://www.analog-innovations.com | 1962 | Stormy on the East Coast today... due to Bush\'s failed policies.

(numeric),

With tanh(Ax-B)/2 + 1/2 you may arbitrarily make close approximation to a unit step function (including shiftability). Or do interesting cross fade like things. Mostly pretty well behaved for numerical techniques as well.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required