GAMS maximum element - max

I have to get GAMS to find the maximum element of a set. This should result in some linear regression model, where the objective is not the least squares but the least maximum deviation.
My data points are the (x(p), y(p)) points (There is Set p / p1*p1000 / ; given). I managed to solve the regression model as described by amsterdamoptimization:
Variables
m Slope
b Constant
objVal Objective Value
;
Equations
objFun Objective Function
lin(p) Regression Model
;
objFun .. objVal =n= 0;
lin(p) .. y(p) =e= m * x(p) + b;
option lp=ls;
Model Regression / objFun, lin / ;
Solve Regression minimizing objVal using lp;
But what I should hand in is something like
Variables
m Slope
b Constant
objVal Objective Value
;
Equations
objFun Regression Model
;
objFun .. objVal =e= smax(p, abs( y(p) - (m * x(p) + b) ));
Model Regression / objFun / ;
Solve Regression minimizing objVal using lp;
Of course you can read this, but GAMS hates it:
2031 Solve Regression minimizing objVal using lp;
**** $51,59,256
Error Messages
51 Endogenous function argument(s) not allowed in linear models
59 Endogenous prod smin smax require model type "dnlp"
256 Error(s) in analyzing solve statement. More detail appears
Below the solve statement above
Yep, it is homework, nevertheless I'm completely stuck.

Turns out, maximum can be handled easily:
objFun .. objVal =e= smax(p, abs( y(p) - (m * x(p) + b) ));
Is equivalent to
objFun(p) .. objVal =g= abs( y(p) - (m * x(p) + b) );
Now GAMS doesn't like abs() used in linear models for obvious reasons, but remember x > abs(y) means (x > y) AND (x > -y), so:
objFun1(p) .. objVal =g= y(p) - (m * x(p) + b);
objFun2(p) .. objVal =g= -( y(p) - (m * x(p) + b) );
Which is finally (canonical conformity):
objFun1(p) .. objVal + x(p) * m + b =g= y(p);
objFun2(p) .. objVal - x(p) * m - b =g= -y(p);

Related

Allowing inputs to be matrix or vectors or scalar in octave

I am doing the coursera machine learning logistic regression assignment and I wrote the top piece of code.
Instructions
function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
% J = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly
g = zeros(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
g = (1 / (1 + exp(-1 * z)) ) .^ 1;
%g = (1 + exp(-1 * z)) .^ -1;
% =============================================================
end
My Code --- Breaks when inputting a vector or matrix
g = (1 / (1 + exp(-1 * z)) ) .^ 1;
I found this code on some github ---- Works for all cases
g = (1 + exp(-1 * z)) .^ -1;
The issue is the output works when my input is a scalar. My code breaks when it becomes a vector. May i know what am i missing as it appears to me to be the same
You need to change / which is a matrix right division to be an element-wise division (./)
g = (1 ./ (1 + exp(-1 * z)) ) .^ 1;
The use of / will work just fine for scalars, but when one of the inputs is a matrix (in this case the right side of the operator), the meaning changes and you will get either an error or unexpected results.

How to solve this equation for solving "Finding duplicate in integer array"

I was looking at the problem and the discussion here: Easy interview question got harder: given numbers 1..100, find the missing number(s)
One of the user provided a solution using following equation.
k1 + k2 = x
k1^2 + k2^2 = y
Substituting provides (x-k2)^2 + k2^2 = y
I am trying to solve this equation further and come up with a C program to solve the problem of finding duplicates.
Inspite of spending lot of time I couldn't solve this equation to get k1 or k2 one side. I always ended up with k1 or k2 on both side of equation.
Any help is appreciated.
Expand the equation
(x - k2)^2 + k2^2 = y
and get
x^2 - 2xk2 + 2k2^2 = y
or
2k2^2 - 2xk2 + x^2 - y = 0
Now use the formula for solving the quadratic equation az^2 + bz + c = 0 which is (-b +/- sqrt(b^2 - 4ac))/2a. Only that in our case z=k2. So
k2 = (2x +/- sqrt(4x^2 - 8(x^2 - y))) / 4
or
k2 = (x +/- sqrt(x^2 - 2(x^2 - y))) / 2
= (x +/- sqrt(2y - x^2)) / 2
and you can put
k2 = (x + sqrt(2y - x^2)) / 2
k1 = (x - sqrt(2y - x^2)) / 2.

How can I ask Wolfram Alpha to rearrange an equation?

I have an equation (parentheses are used because of VBA code)
Y=(P/(12E((bt^3)/12))*A
and i know every variables but not "b". Is there any way how to ask Wolfram Alpha to "redefine" (not solve) equation so I can see something like following: I tried to do it manually (but result is not OK)
b=((P/EY)*12A))/t^3
I wish to see how right equation will look.
Original equation is on picture below
where
equation in [,] I simplified by A
I'm not sure if there's a way to tell Wolfram|Alpha to rearrange for a particular variable; in general it will usually try to rearrange for x or y.
If I substitute b for x in your equation and use the following query:
solve Y - (P/(12E((xt^3)/12))*A) = 0
then Wolfram Alpha returns the result you're looking for: x (b) expressed in terms of the other variables. Specifically:
x = A P / (E t^3 Y) for tY != 0 and AP != 0
I know that your question was about Wolfram Alpha, that you do not want to "solve", but here is one way you could do it in Mathematica using your real question. I renamed I into J because I is a reserved symbol in Mathematica for the imaginary unit.
J = b t^3/12;
expr = (P / (12 E J) ) (4 L1^3 + 3 R ( 2 Pi L1^2 + Pi R^2 + 8 L1 R ) + 12 L2 (L1 + R)^2)
Solve[ Y == expr , b]
Result
{{b -> (P (4 L1^3 + 12 L1^2 L2 + 24 L1 L2 R + 6 L1^2 \[Pi] R + 24 L1 R^2 + 12 L2 R^2 + 3 \[Pi] R^3))/(E t^3 Y)}}

Why `x = x*(1.5f-(xhalf*x*x));` can be a Newton Method iteration?

Ok, by far, I guess many people know the famous fast inverse square root (see more on Writing your own square root function and 0x5f3759df)
Here is the code
float FastInvSqrt(float x) {
float xhalf = 0.5f * x;
int i = *(int*)&x; // evil floating point bit level hacking
i = 0x5f3759df - (i >> 1); // what the fuck?
x = *(float*)&i;
x = x*(1.5f-(xhalf*x*x)); // one Newton Method iteration
return x;
}
Ok, i do NOT need to know more how magic 0x5f3759df is.
What I don't understand is why x*(1.5f-(xhalf*x*x)) is a Newton Method iteration?
I tried analyse but just can't get it.
So let's assume r is the real number and x is the inverse sqrt of r.
1 / (x^2) = r, then f(x) = r*(x^2)-1 and f'(x) = 2 * r * x
So one iteration should be x1 = x - f(x)/f'(x) = x / 2 + 1 / (2 * r * x), right?
How comes x * (1.5 - ((r / 2) * x * x))? (note I replaced xhalf with r / 2 here)
Edit
Ok f(x) = x^2 - 1/r is another form, let me calculate
f(x) = x^2 - 1 / r
f'(x) = 2 * x
So x1 = x - (f(x)/f'(x)) = x - (x^2 -(1 / r))/(2*x) = x / 2 + 1 / (2 * r * x), still it is quite different from the formula used in the code, right?
Wikipedia says function is (using your variable names):
f(x) = 1/x2 - r
Then we have:
f'(x) = -2/x3
And iteration is:
x - f(x)/f'(x) =
x - (1/x2 - r)/(-2 / x3) =
x + x3 /2 * (1/x2 - r) =
x + x/2 - r/2 * x3 =
x * (1.5 - r/2 * x * x)
And this is what you see in code.
Newton's method is defined in terms of the iteration
xi+1 = xi - f(xi) / f'(xi)
(where f'(x) is the first derivative of f(x)). To find the inverse root of r, one needs to find the zero of the function f(x) = x - 1/sqrt(r) (or, equivalently, f(x) = x2 - 1/r). Simply take the derivative, plug it in to the definition of the iteration step, simplify, and you have your answer.
Actually, the exact form used in the code comes from using a third equivalent form:
f(x) = x-2 - r
See this article for the detailed steps of the derivation. It's also derived in the Wikipedia article on fast inverse square root.

How to reflect a line over another line

For a collision algorithm I am developing, I need to find out how to reflect a line over another.
Line 1:
y=ax+b
Line 2:
y=cx+d
Line 3:
(a result of reflecting line 1 over line 2) y=ex+f
Is there any algebraic way to determine e and f in terms of a, b, c, and d?
I have run over this exact same problem before. Stay with me here...
This problem involves two parts:
1. Find the point at which they intersect
to find where two lines intersect, we use the two equations of the lines:
y = M1x + B1
y = M2x + B2
Using substitution:
M1x + B1 = M2x + B2
M1x - M2x = B2 - B1
x(M1 - M2) = B2 - B1
x = (B2 - B1) / (M1 - M2)
To find the y value, just plug it in:
y = M1x + B1
2. Find the slope of the line from the other two slopes.
The second is far trickier. Using trigonometry, it is not impossible.
Let L1 be the "base line." (With a slope of M1)
Let L2 be the line that is to be reflected over the "base line." (With a slope of M2)
Let L3 be our resulting line. (With a slope of M3)
The equation I used is as follows:
double M3 = ((2 * M1) + (M2 * pow(M1, 2)) - M2) / (2 * M1 * M2 - pow(M1, 2) + 1);
Straight from my C code.
It is important to note that both slopes should be defined. You can use L'Hopital's rule to get an equation when one of the slopes is approaching infinity.
ONWARD WITH THE EXPLANATION!
Here is a crude drawing of three lines.
L2 is reflected over L1, resulting in L3. Drawing is not exact.
The angle between L1 and L2, as well as L2 and L3, is labelled as R.\
Here are the facts:
M1 = tan(A1)
M2 = tan(A2)
M3 = tan(A3)
This comes from the definition of tangent.
A3 = R + A1
This is a little trickier to see, but if you draw a horizontal line at the point of intersection it becomes obvious.
Thus, our goal is to find tan(A3). To accomplish this, we need to find R. As we can see, R can be found in a triangle with A2 and the supplement of A1 as the other angles. Thus, we know:
R + (180 - A1) + A2 = 180
R - A1 + A2 = 0
R = A1 - A2
Let's take the tangent of both sides:
tan(R) = tan(A1 - A2)
From trigonometry, we know:
tan(R) = (tan(A1) - tan(A2)) / (1 + tan(A1)tan(A2))
R = arctan((tan(A1) - tan(A2) / (1 + tan(A1)tan(A2))
Arctan being inverse tangent. From our earlier formula, A3 = R + A1, we get:
A3 = arctan((tan(A1) - tan(A2) / (1 + tan(A1)tan(A2)) + A1
A3 = arctan((M1 - M2) / (1 + M1*M2)) + A1
But we don't want A3. We want tan(A3). So again, we take the tangent of both sides.
tan(A3) = M3 = tan(arctan((M1 - M2) / (1 + M1*M2)) + A1)
M3 = tan(arctan((M1 - M2) / (1 + M1*M2))) + tan(A1) / (1 - tan(arctan((M1 - M2) / (1 + M1*M2))) * tan(A1))
Unfortunately, that's disgustingly hideous. Replacing tangents with slopes and simplifying, we get
M3 = ((M1 - M2) / (1 + M1*M2)) + M1 / (1 - ((M1 - M2)/(1 + M1*M2)) * M1)
M3 = (M1 - M2 + M1*(1 + M1*M2)) / (1 + M1*M2 - M1*M1 + M1*M2)
M3 = (M1^2 * M2 + 2*M1 - M2) / (1 + 2*M1*M2 - M1^2)
Which is the exact same as the formula above. Sorry about all the ugly math. When M2 is completely vertical, you can use L'Hopital's rule to get
M3 = (M1^2 - 1) / 2*M1
If anyone is so inclined, check my math. But I'm tired right about now.
Assuming the two lines are not parallel to each other
Step 1:
First find the intersection of the line y = ax + b with line y = cx + d , that is by solving comes out to be
m = (d - b) / (c - a)
Step 2:
The final line has point of the form (x , ex + f) , so wjat we know is the line joining the point and the corresponding image is perpendicular to the mirror line AND the the midpoint of the first point and its image lies on the mirror line. Solving for first requirement ....
(Slope of line joining point and its image) * (Slope of the mirror line) = -1
we get ...
c * (e*pt + f - a*n - b)/( pt - n ) = -1 -----> The first equation .
Then the midpoint of the point and its image lie on the central line , i.e.
Y coordinate of the midpoint - ( c* x coordinate of midpoint + d) = 0
y coordinate of midpoint = (a*n + e*pt) / 2 and x coordinate = ( pt + n) / 2
putting it above we get...
(a*n + e*pt)c - c( pt + n) - 2d =0 ----> second equation
3.
now the point and its image make equal angles from the intersection point .... a simple way of saying that angle between mirror line , point line and image line , point line being equal ... therefore ... the tangent of angle between lines mI and mM is equal to that of mM and mP
equating we get
( mM + mP ) / ( 1 + mp*mM) =( mI - mM )/ (1 + mI*mM)
where mM = c , mI = e, and mP = a -----> third equation
put it in their respective
places you get three equations in three unknowns , pt , e and f and solve ... just x in place of n earlier and there you have your e , f in terms of a , b , c , d.
Solve it yourselves ....
However if they are parallel its simple , you have two equations in two variables use the midpoint method
Yet another method:
Matrix of affine transformation for reflection relatively to line y=ax+b (works for non-vertical lines!).
Let's pa = 1+a^2, ma = 1-a^2, then matrix is (from Nikulin's Computer Geometry book)
ma/pa 2a/pa 0
2a/pa -ma/pa 0
-2ab/pa 2b/pa 1
So we can get two arbitrary points at second line, apply this transform, and calculate new line equation

Resources