![]() |
|
|
#1 |
|
Sep 2004
13·41 Posts |
I reduced my problem to this almost linear program:
minimize max ( abs(a*1+b*3-c), abs(a*2+b*5-c), abs(a*3+b*7-c), abs(a*5+b*11-c), abs(a*7+b*14-c), abs(a*8+b*15-c), abs(a*10+b*19-c) ) on ax + by = c I can get rid of the abs, minimize max ( a*1+b*3-c, -a-3b+c a*2+b*5-c, -2a-5b+c a*3+b*7-c, -3a-7b+c a*5+b*11-c, -5a-11b+c a*7+b*14-c, -7a+14b+c a*8+b*15-c, -8a-15b+c a*10+b*19-c, -10a-19b+c ) on ax + by = c How do I get rid of the max? Someone said the answer is: min z s.t. z >= a*x_i + b*y_i - c, 1<=i<=T z >= c - a*x_i + b*y_i, 1<=i<=T Thanks! Last fiddled with by Joshua2 on 2011-03-04 at 10:21 |
|
|
|
|
|
#2 | |
|
"Richard B. Woods"
Aug 2002
Wisconsin USA
22×3×641 Posts |
I don't see how you can get rid of the max function, or something functionally equal to it, as long as your goal is, as your title says, "minimize maximum error". How else is the "maximum error" to be determined?
You can use some algorithm that avoids using "max" by using comparisons and conditional branches ... but that's just what the max function does, so all you accomplish that way is to pretend that you're not using a "max" function just because the character string ("m", "a", "x") doesn't appear in your code. Inside the max function, what goes on is just comparisons and conditional branches to assignment statements. function max(a,b); if a > b then return a else return b; function max(a,b,c); if a > b then {if a > c then return a else return c} else {if b > c return b else return c}; . . . Quote:
z s.t. z >= a*x_i + b*y_i - c, 1<=i<=T z >= c - (a*x_i + b*y_i), 1<=i<=T is just z = -infinity; for i = 1 to T by 1; m = a*x_i + b*y_i - c; if m > z then z = m; if -m > z then z = -m; end for; return z; Last fiddled with by cheesehead on 2011-03-07 at 22:59 |
|
|
|
|
|
|
#3 | |
|
Bamboozled!
"๐บ๐๐ท๐ท๐ญ"
May 2003
Down not across
270268 Posts |
Quote:
Code:
#include <stdint.h>
int32_t max (int32_t a, int32_t b)
{
return b - (a-b) * ((a-b) >> 31);
}
Moral 1: there is more than one way to skin a cat. Moral 2: there is a long history behind writing straight-line code to be found within the SIMD machine community, which is where I learned this kind of trick. CUDA coders aiming to squeeze every drop of performance try really hard to have uniform program flow and uniform memory accesses in their kernels. Paul Last fiddled with by xilman on 2011-03-08 at 11:36 Reason: Fixed a sign error |
|
|
|
|
|
|
#4 |
|
Oct 2007
Manchester, UK
53×11 Posts |
Shouldn't that be (a-b) for the first bracket?
Nice trick btw. Edit: Or add it rather than subtract it to keep both brackets the same. Last fiddled with by lavalamp on 2011-03-08 at 18:16 |
|
|
|
|
|
#5 | |
|
"Richard B. Woods"
Aug 2002
Wisconsin USA
170148 Posts |
Quote:
2 - (56-2)*((56-2)>>31) = 2 - (54)*((54)>>31) = 2 - (54)*(0) = 2 ? Moral 3: One of us has failed to either compile or test correctly before publishing our result. If >> is not a right shift, it's me. |
|
|
|
|
|
|
#6 |
|
Oct 2007
Manchester, UK
53·11 Posts |
Ah, maybe it was meant to be a at the start then.
All equivalent: Code:
return b - (b-a) * ((a-b) >> 31); return b + (a-b) * ((a-b) >> 31); return a - (a-b) * ((a-b) >> 31); |
|
|
|
|
|
#7 | ||
|
Bamboozled!
"๐บ๐๐ท๐ท๐ญ"
May 2003
Down not across
2·17·347 Posts |
Quote:
![]() Quote:
Still, at least two of us think it's a nice trick. At least one of us (me) thinks its a lousy way to write code unless you really need that last drop of performance. Almost all the time it takes longer to write, test and debug the code in the first place than you ever get back at runtime. Paul |
||
|
|
|
|
|
#8 |
|
"Richard B. Woods"
Aug 2002
Wisconsin USA
1E0C16 Posts |
If I hadn't tested, I wouldn't have moralized about it. :-)
Last fiddled with by cheesehead on 2011-03-08 at 18:59 |
|
|
|
|
|
#9 |
|
Aug 2006
22·3·499 Posts |
It's a good way to write library code. It's a bad way, generally, to write most anything else.
|
|
|
|
|
|
#10 | |
|
"Bob Silverman"
Nov 2003
North of Boston
5×17×89 Posts |
Quote:
max(a,b) = [abs(a+b) + abs(a-b)]/2 min (a,b) = [abs(a+b) - abs(a-b)]/2 |
|
|
|
|
|
|
#11 |
|
Aug 2004
italy
113 Posts |
|
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Minimize |n-2^x*3^y| over the integer | a1call | Information & Answers | 93 | 2016-09-10 17:34 |
| Maximum memory for P-1 in XP 32-bit | willmore | Software | 4 | 2009-10-14 02:45 |
| Maximum theoretical MPG | TimSorbet | Lounge | 9 | 2008-07-14 22:45 |
| Maximum B1 for P+1 = 4294967295? | Andi47 | GMP-ECM | 13 | 2007-05-11 14:29 |
| Good way to minimize factoring algoritm twice? | HiddenWarrior | Math | 7 | 2004-03-10 13:39 |