Can any one help to get the general formula, when i=1 it should be zero('0') for other numbers(2,3,4....5) it should be one('1'). 'i' starts from 1.Please someone help. Thanks in advance.
Asked
Active
Viewed 465 times
-5
-
Need this general formula for the program in python. – Delwin Aug 27 '12 at 10:31
-
Erm... is `i = i == 1 ? 0 : i` classic not relevant here? – raina77ow Aug 27 '12 at 10:33
-
@Rupak: I tried there but i didnt get. – Delwin Aug 27 '12 at 10:33
-
@Delwin Then why `mathematica` is mentioned in tags, but `python` is not? – raina77ow Aug 27 '12 at 10:33
-
@raina77ow: I have some printing lines before this so i need formula not condition. I have put condition already. – Delwin Aug 27 '12 at 10:35
-
@Delvin Well, (i == 1 ? 0 : 1) is a template expression; you can use it just as you use (2 * i), for example. And, may I say, if you need to (heavily) modify the value you print right when you print it, you're doing it wrong already. – raina77ow Aug 27 '12 at 10:37
-
Please show what you've tried so far? – moopet Aug 27 '12 at 14:21
-
Do "other numbers" include 0, -1, -2, ...? sgn(i-1) returns a ternary-valued step function, i>1 returns a binary-valued step function, and i != 1 returns an impulse, as does abs(sgn(i-1)). Which do you want? (See also http://stackoverflow.com/questions/1986152/why-python-doesnt-have-a-sign-function) – Dave Aug 27 '12 at 14:45
2 Answers
2
Try this
result = (i != 1) ? 1 : 0
seems there is some ambiguity as to what language you are using but anything that supports the ternary operator will work in this way you just may have to tweak the syntax.
If you cant access ternary operators then pseudo-code is
result = 1;
if (i == 1) {
result = 0;
}
In python this would be
result = 1
if i == 1:
result = 0

mathematician1975
- 21,161
- 6
- 59
- 101
-
@Delwin see edit - i believe this is the right python syntax – mathematician1975 Aug 27 '12 at 10:44
-
The Python syntax should work. Python also has a ternary syntax, so `result = 0 if i == 1 else 1` would work too. – DSM Aug 27 '12 at 14:07
0
-
The 'code' here is not Mathematica. In truth, *If Mathematica then *`sgn(i-1)` *will generate an error*. – High Performance Mark Aug 27 '12 at 10:33
-
There is no code here. I provided generic mathematical formula to calculate what OP required - maybe I confused tags, but for me term 'mathematica' means just math, not any programming environment. – Arvo Aug 27 '12 at 10:40
-
`sgn(i-1)` is no more 'generic mathematical formula` than it is Mathematica. – High Performance Mark Aug 27 '12 at 11:36
-
1I don't understand, what is your problem with sign function. This function returns required result for given arguments set (integers starting from 1) and corresponds to tag ('math') which OP used. Sure this is not code, but OP asked 'Can any one help to get the general formula'... – Arvo Aug 27 '12 at 12:18
-
HPM, my CRC handbook has been in the basement for 40 years (so I can't look it up at work), but as I recall "sgn(i-1)" is in fact as generic a mathematical expression as "abs(i-1)". The OP cited Wikipedia which says the same thing. If you disagree then edit the Wikipedia article, and be prepared to be reverted. – Dave Aug 27 '12 at 14:54