Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
Now I have two binary decision variables X1 and X2. If I intend to define a constraint that X2 could be 1 if and only if X1 is 1. In other word, X2 is a sequel to X1. How could I define this kind of logic in linear programming?
Any hint will be appreciated, many thanks in advance!
I am a bit confused by your question.
x1 = 1 <=> x2 = 1
is the same as
x1=x2
I think you want
x1 = 1 => x2 = 1
i.e. no "if and only if" but only "if". This can be written as:
x2 >= x1
The reverse
x1=0 => x2=0
is of course just as trivial:
x2 <= x1
Also note that LPs don't have binary variables (LP refers to continuous variables). Binary variables can be used only in (mixed) integer programs.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In C I put in a big number, like 1e-12, in float data and add 1. It's gave me a correct answer.
In C++ I made the same, but when I add 1 to 1e-12, it returns me 1.
float a = 1e-12;
std::cout << "The number is : " << a + 1 << std::endl;
Output:
The number is: 1
I don't have any error messages. The program just returns the wrong result.
(!! 1e-12 + 1 is not equal to 1!!)
Compilers by default take some short-cuts when doing floating-point math. (They typically have a command-line switch to enforce the rules)
In this case, the rule is that the compiler should store the double value 1e-12 as a float in a, and then, in the output statement, add 1 to the stored value.
The optimization is probably that one of the compilers never stored the value; instead, it added 1 to the double value 1e-12. With the higher precision initial value there are more low bits in the fraction part, and that will affect the result of adding 1.
So the results can be different, depending on how the compiler treats those values.
That's just handwaving, though; if this is really important to you, look at the machine code that the compiler generates in both cases to see what's being done differently.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have some results whose relevance decreases with distance. I want to weight the result array elements with constants whose distribution is close to normal or folded normal. At start I want to generate an array with N constants starting from 1 to 0.01 by a function.
The result should be something like the following, ending with a number close to 0.01.
const double normalDistWeight[] = {
1.000, 0.997, 0.994, 0.989, 0.984, 0.977, 0.970, 0.961, 0.951, 0.939,
0.926, 0.910, 0.893, 0.874, 0.853, 0.830, 0.805, 0.778, 0.750, 0.719,
0.687, 0.654, 0.619, 0.584, 0.548, 0.512, 0.476, 0.440, 0.405, 0.370,
0.337, 0.305, 0.274, 0.246, 0.219, 0.194, 0.171, 0.150, 0.131, 0.114,
0.098, 0.085, 0.073, 0.063, 0.054, 0.047, 0.040, 0.035, 0.030, 0.027
};
Unfortunately I can't use any third party libraries or C++11 features, only plain C++.
Edit: Oh, I was over-thinking it... It's just a simple Gaussian error, so exp(-x^2) should work.
It appears to me that all you want is an array of values of the Gaussian function corresponding to uniformly spaced points on the positive half-axis, up to a point where the value is about 0.01.
This is straight-forward. The Gaussian function is f(x) = exp(−x2), like this:
In the chosen expression, we already have f(0) = 1, so all that remains is to find the final point x where we have f(x) = 0.01. Invert: x = √−log(0.01) ≈ 2.15.
So all you need to do is evaluate f on uniformly spaced points on the interval [0, 2.15].
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I'm looking for a way to interpolate values from some 2D scattered data. I have a 3d points that represent a terrain from which I want to interpolate intermediate points. For input (X,Y) coordinates I need Z (height) value.
This article on wikipedia may also help you understand my wishes. There is a library in matlab called triscateredinterp that I think it does what I want.
What is a lightweight way to accomplish this interpolation in C++?
I don't think you need 3D interpolation (triscateredinterp). You have data based on 2D inputs; the 3rd dimension is your output. If I understand correctly you want to provide a point in 2D (something between the original points, and interpolate the value.
Light weight? nearest neighbor!; then bi-linear interpolation; then bi-cubic (and others). The first is simple, the others require an increasing amount of math.
Bi-linear: For each point to be interpolated, find the nearest 3 points to your X and Y:
lat long Altitude
X1 Y1 A1
X2 Y2 A2
X3 Y3 A3
Make these matrices:
X1 Y1 1 A1
X = X2 Y2 1 Y = A2
X3 Y3 1 A3
B is the interpolation coefficients we will calculate for those three nearest points (and can be re-used for all points in the area)
B1
B = B2
B3
The matrix equation is: X*B = Y
You could use brut force:
Multiply both sides by XT: XT*X*B = XT*Y
Take the inverse of XT*X: B = (XT*X)^-1 *XT*Y.
Yes 3x3 matrix inversion. Tying this back to a C++ question, you might use Boost for your matrix operations.
Here is another similar C++ question: Solving a system of equations programmably?
One problem that can arise from the bi-linear technique is that as your interpolated point becomes closer to a different set of 3 values you can get some jumps (how would you interpolate 4 points in a saddle configuration?)
One good method for scattered points is Natural Neighbor Interpolation.
You can check the implementation available in CGAL for example : http://doc.cgal.org/latest/Interpolation/index.html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 4 pin values defined P1_1, P1_2, P1_3, P1_4, with bit values. (1/0)
I want to combine them into one byte value, example:
0000 0101 (3 LSB are the pins)
How can i do this?
Something like
value = (P1_1<<3)|(P1_2<<2)|(P1_3<<1)|(P1_4);
Disclaimers: this is pretty ugly example. In your actual code you probably want some kind of constants defined for shift (so you are able to do the reverse operation without using more magic literal values).
Also note that this will have unexpected results if one of P constants is not 0 or 1. In this case one might use something like !!P1_x instead of P1_x.
bitset<4> temp;
temp[0] = P1_1;
temp[1] = P1_2;
temp[2] = P1_3;
temp[3] = P1_4;
unsigned char value = static_cast<unsigned char>(temp.to_ulong());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I had seen OpenGL statement to draw a line using two points. However, my requirement is to draw a line using the following detail
a point on a line
Direction Vector
Im developing function in c++ using openGL library.
Any help is most appreciated.
The answer depends on the semantics of what you've termed a direction vector.
In the computer graphics context I would normally take that term to mean a unit vector facing in the specified direction. Whereas in a mathematics context you might simply mean the relative vector that results from subtracting the two points' coordinates.
[Using P1 and P2 to represent the required two points, and V for the vector].
In the former case, you also need a specify a length for the vector, so you'll need:
P2 = P1 + n * V
whereas in the latter case, it's just trivially
P2 = P1 + V
Just make that two-point line a very long one, say 10000 to each direction from your point-on-a-line:
void drawLinePointDirection(Point P, Vector D) {
Point A = P + 10000*D;
Point B = P - 10000*D
drawLineTwoPoints(A, B);
}
assuming D is unit length.