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.
Related
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 .
I need to compute an expression that looks like: (A - B * C) / D, where their types are: signed long long int A, B, C, D; Each number can be really big (not overflowing its type). While B * C could cause overflow, at same time expression (A - B * C) / D would fit in. How can I compute it correctly?
For example: In the equation (Ax + By = C), assuming A * x is overflowing, but y = (C - A * x) / B could fit in. No need to use BigInteger or double data type.
You can transform the equation to do the division first while accounting for the remainders:
Assume / is integer division and everything is infinite precision:
x == x / y * y + x % y
(A - B * C) / D
((A/D * D + (A%D)) - (B/D * D + (B%D)) * (C/D * D + (C%D))) / D
(A/D * D - B/D * D * C/D * D - (B/D * D * (C%D) + (B%D) * C/D * D) + (A%D) - (B%D) * (C%D)) / D
(A/D * D - B/D * D * C/D * D) / D - (B/D * D * (C%D) + (B%D) * C/D * D) / D + ((A%D) - (B%D) * (C%D)) / D
(A/D - B/D * C/D * D) - (B/D * (C%D) + (B%D) * C/D) + ((A%D) - (B%D) * (C%D)) / D
A/D - B/D * C - B/D * (C%D) - (B%D) * C/D) + ((A%D) - (B%D) * (C%D)) / D
Assuming D is not too small and not too big then x / D and x % D are small and we can do this:
using T = signed long long int;
T compute(T a, T b, T c, T d) {
T a1 = a / d, a2 = a % d;
T b1 = b / d, b2 = b % d;
T c1 = c / d, c2 = c % d;
T m1 = b1 * c, m2 = b1 * c2, m3 = b2 * c1, m4 = b2 * c2;
T s1 = a1 - m1 - m2 - m3, s2 = a2 - m4;
return s1 + s2 / d;
}
The critical part is the multiplication for m1 through m4. The range of numbers b and c that overflow while the result should have fit is rather small I believe.
I think you could change the order of the operations so it will look like:
A/D - (B/D)*C
The result should remain the same.
Since you mentioned gcd lets try that as alternative answer:
using T = signed long long int;
T gcd(T a, T b);
T compute(T a, T b, T c, T d) {
// use gcd for (b * c) / d
T db = gcd(b, d);
T dc = gcd(c, d / db);
T dm = db * dc;
// use gcd on a for (a - b*c) / dm
T da = gcd(a, dm);
// split da for use on (b * c) / da
db = gcd(b, da);
dc = da / db;
T dr = d / da;
return ((a / da) - (b / db) * (c / dc)) / dr;
}
Only works if d has enough factors in common with a, b and 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.
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.
I'm trying to overload the operator to divide two complex numbers
Testing with 3+2i / 4 - 3i
complex g(3, 2);
complex f(4,-3);
cout << g / f << endl;
I added * -1.0 since we go
(4*4) + (3 * -3)i^2 in math which is 25
((3*4) + (3 * -3)* -1) is my intent
Testing I get -0.545455 - 1.72727i
While before I added the * 1.0 I got
0.24 +0.76i
Which is was very close to
0.24 + 0.68i
the answer
complex complex :: operator/ (complex& x) {
complex conjugate = x.conj();
double j = (real * conjugate.real) + (imag * conjugate.imag); // real
double u = (real * conjugate.imag) + (imag * conjugate.real); // imag
double h = (((conjugate.imag * imag)* -1.0) + (real * conjugate.real)) + ( (real*conjugate.imag) + (imag * conjugate.real));
return complex(j/h,u/h);
}
This is wrong:
double j = (real * conjugate.real) + (imag * conjugate.imag); // real
should be -, not +.
This is right:
double u = (real * conjugate.imag) + (imag * conjugate.real); // imag
Although both j and u are meaninglessly named.
What's going on here?
double h = (((conjugate.imag * imag)* -1.0) + (real * conjugate.real)) + ( (real*conjugate.imag) + (imag * conjugate.real));
The denominator is just x*conjugate which is:
double denom = x.real * x.real + x.imag * x.imag;
Side-note, you want to take your argument by reference to const.