Argmax function in C++? - c++

I am trying to make armgax function in C++
For example,
C = wage * hour * shock + t + a * R + (1 - delta) * d - d_f - a_f - fx1 * (1 - delta) * d;
double result;
result = (1 / (1 - sig)) * pow(pow(C, psi) * pow(d, 1 - psi), (1 - sig));
Suppose every variable except for 'd_f' and 'a_f' are given and both 'd_f' and 'a_f' some number(double) in [0, 24].
If I want to get the combination of 'd_f' and 'a_f' that maximizes 'result', is there a proper function that I can use?
Thanks.

Related

How do I generate Bezier Curves and NURBS in C++ and import it as an igs?

I am new to C++ NURBS libary. I learnt generating line (by CLine, from nurbs.h ) and save it as igs. But in case of
multiple control points, how to generate a curve ? Every other tutorial using graphics.h
(putpixel), but couldnt find anything about igs.
This should be a simple problem. But I have no idea which function can help me here.
Thanks in advance.
We have 4 control points here to begin with.
for (float t = 0.0; t <= 1.0; t += 0.2) {
double xt = 0.0, yt = 0.0;
xt = pow(1 - t, 3) * x[0] + 3 * t * pow(1 - t, 2) * x[1] + 3 * pow(t, 2) * (1 - t) * x[2]
+ pow(t, 3) * x[3];
yt = pow(1 - t, 3) * y[0] + 3 * t * pow(1 - t, 2) * y[1] + 3 * pow(t, 2) * (1 - t) * y[2]
+ pow(t, 3) * y[3];
count = count + 1;
//Math::Vector4f c(xt, yt, 0);
for (int i = 1; i < 3; i++) {
listt[i][0]= xt;
listt[i][1]= yt;
Math::Vector4f a(listt[i][0], listt[i][1],0);
myvector.push_back (&a);
}
}
......
.....
igs.Write("test.igs");
--- This is to create the points, but after that I dont know how to use the points to create a Bezier curve .

Hello I am new to sympy and I am trying to solve a couple of non-linear equation

here is the problem:
constants:
enter image description here
dependent value functions:
def v_ego(self,ego_vel_x,ego_a_ini_x,t):
v_ego = ego_vel_x + ego_a_ini_x * t
return v_ego
def x_obj(self,x_obj_ini,obj_vel_x,obj_a_ini_x,t):
x_obj = x_obj_ini + obj_vel_x * t + 0.5 * obj_a_ini_x * t ** 2
return x_obj
def x_ego(self,x_ego_ini,ego_vel_x,ego_a_ini_x,t):
x_ego = x_ego_ini + ego_vel_x * t + 0.5 * ego_a_ini_x * t ** 2
return x_ego
def y_obj(self,y_obj_ini,obj_vel_y,obj_a_ini_y,t):
y_obj = y_obj_ini + obj_vel_y * t + 0.5 * obj_a_ini_y * t ** 2
return y_obj
def y_t(self):
y_t = math.sqrt(self._r_t ** 2 - self._l_f ** 2) - (self._w_ego / 2)
return y_t
def y_r(self,ego_vel_x,ego_a_ini_x,t):
y_r = math.sqrt(max(0, ((self.v_ego(ego_vel_x,ego_a_ini_x,t) ** 2 / (self
._Mu_rt * self._g)) ** 2 - self._l_c ** 2)))
return y_r
def y_min(self,ego_vel_x,ego_a_ini_x,t):
y_min = max(self.y_t(), self.y_r(ego_vel_x,ego_a_ini_x,t))
return y_min
def r_min(self,ego_vel_x,ego_a_ini_x,t):
r_min = max(self._r_t, math.sqrt(self._l_f ** 2 + (self.y_min(ego_vel_x,ego_a_ini_x,t) + self._w_ego / 2) ** 2))
return r_min
tts, delta_t = sym.symbols('tts,delta_t')
e_10 = sym.Eq(math.atan((self.x_obj(x_obj_ini, obj_vel_x, obj_a_ini_x, tts + delta_t) - self.x_ego(x_ego_ini,ego_vel_x,ego_a_ini_x,tts)+ self._l_f) / (self.y_min(ego_vel_x, ego_a_ini_x, tts) - self.y_obj(y_obj_ini, obj_vel_y, obj_a_ini_y,tts + delta_t))) - ((self.v_ego(ego_vel_x, ego_a_ini_x, tts) * delta_t) / self.r_min(ego_vel_x, ego_a_ini_x, tts) - (math.asin(min(1.0, self._l_f / self.r_min(ego_vel_x, ego_a_ini_x, tts))))), 0)
e_11 = sym.Eq((self.x_obj(x_obj_ini, obj_vel_x, obj_a_ini_x, tts + delta_t) - self.x_ego(x_ego_ini, ego_vel_x,ego_a_ini_x,tts)+ self._l_f) ** 2 + (self.y_min(ego_vel_x, ego_a_ini_x, tts) - self.y_obj(y_obj_ini, obj_vel_y, obj_a_ini_y,tts + delta_t)) ** 2 - (self.r_min(ego_vel_x, ego_a_ini_x, tts)) ** 2, 0)
print(sym.solve([e_10, e_11], (tts, delta_t)))
I am getting TypeError: cannot determine truth value of Relational
These are the equations:
non linear equations that I am trying to solve
and these are the dependent values that need to be calculated:
dependent functions
any help is appreciated
When you use min with a SymPy expressions, it will complain if it can't figure out what the min is, e.g. min(x,y) -> TypeError: cannot determine truth value of Relational.
Since you are only selecting a minimum of two values, the function is easy to write as a Piecewise as in the following example where the equation x - min(y,z) is being solved.
Replace max with a similar rewrite, too.

C++: Get value of variable after performing divide and assignment operator:

I am trying to get a value of a variable "pesoil" before its converted to to mm month^-1
pesoil = beta * rnsoil + CPAIR * RHOAIR * exp_h * vpdo / r_as;
pesoil /= (beta + PSY * exp_h * (1.0 + r_ss / r_as))); // W m^-2
pesoil *= (etimes * ndays[dm]) / LAMBDA; // convert to mm month^-1
can I do it like this? or will this mess up value stored in "pesoil"
epotential_W = (pesoil /= (beta + PSY * exp_h * (1.0 + r_ss / r_as))); // W m^-2

C++ and Eigen: How do I handle this 1x1 matrix?

Consider this excerpt:
for(int i = 0; i < 600*100*100; i++) {
( 1 / 2 * (1 - a) / a * x.transpose() * y * (z + (1 - a) *
z.transpose() * y(i) / z.sum() ) * x.transpose() * z );
}
In the code above, x, y, z are objects of the class MatrixXd in Eigen and a is a double. Through these multiplications, eventually the outcome is a scalar. The entire forloop took less than a second.
However, if I change my code:
for(int i = 0; i < 600*100*100; i++) {
F(i) = F(i) + ( 1 / 2 * (1 - a) / a * x.transpose() * y * (z + (1 - a) *
z.transpose() * y(i) / z.sum() ) * x.transpose() * z );
}
The forloop then takes 6 seconds. F is an ArrayXd. I'm trying to update each element of F through a loop and in each iteration I would do a series of simple matrix multiplications (which would result in a scalar).
I'm not sure what's wrong. How can I speed it up? I tried to use .noalias() but that didn't help. This could have to do with the fact that the outcome of the series of matrix multiplication results in a 1x1 MatrixXd and Eigen is having issues adding a MatrixXd to a number.
Update
Per #mars, I tried eval():
for(int i = 0; i < 600*100*100; i++) {
( 1 / 2 * (1 - a) / a * x.transpose() * y * (z + (1 - a) *
z.transpose() * y(i) / z.sum() ) * x.transpose() * z ).eval();
}
And it takes ~6 seconds as well. Does that mean there's no way to optimize?
Also, I used -O3to compile.

C++ What is wrong with this version of the quadratic formula?

In my book, it asks me the following question This is for compsci-1
What is wrong with this version of the quadratic formula?
x1 = (-b - sqrt(b * b - 4 * a * c)) / 2 * a;
x2 = (-b + sqrt(b * b - 4 * a * c)) / 2 * a;
The equation your code is translating is:
which of course is not the solution for quadratic equations. You want a solution for this equation:
What's the difference? In the first one you compute the numerator, then you divide by two, then you multiply by a. That's what your code is doing. In the second one you compute the numerator, then you compute the denominator, finally you divide them.
So with additional variables:
num1 = -b - sqrt(b * b - 4 * a * c);
num2 = -b + sqrt(b * b - 4 * a * c);
den = 2 * a;
x1 = num1 / den;
x2 = num2 / den;
which can of course be written as:
x1 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
Where you have to plug in those parenthesis in order to force the denominator to be computed before the division. As suggested in the comment by #atru.