We have a calculation in code that calculates some adjusted value like this
a = Math.Round(b, 3) + Math.Round(b * c1, 3) + Math.Round(b * c2, 3);
The problem is that now I need to do reversed calculation. I have values for a,c1& c2 and need to find value of b. Is that possible?
Assuming b, c1 and c2 are all positive, you have an increasing function so you can binary search over it to find b. There may exist a formula to get b directly, but this should work fast enough as well. Here is a python example:
c1 = 0.0125
c2 = 0.0517
a = 0.155
def op(b, c1, c2):
return round(b, 3) + round(b * c1, 3) + round(b * c2, 3)
minb = 0
maxb = a
while (minb + 0.00001 < maxb):
b = (minb + maxb) / 2
estimate = op(b, c1, c2)
if estimate > a:
maxb = b
elif estimate < a:
minb = b
else:
break
print(b, op(b, c1, c2))
Related
I want to calculate the Position(s) that have the same distance to PositonA and PositionB.
Example:
PositionA: (3,2)
PositionB: (5,4)
Distance: 5
If I'm not mistaken there are two possibilities, but I don't know how to calculate either mathematically.
Update:
Based on mrk's answer, I adapted the question, although it was not clear to me before that it is then a circle in three-dimensional space. Thank you very much for that.
I have added an image to clarify the two-dimensional variant.
The goal is to calculate the best escape route based on the positions of the two attackers. In this example the distance is fixed at 5, but later variable.
From your conditions you get three equations with three variables (X,
Y, Z) that are the coordinates of the points P for which the conditions
hold. Based on your points A (A1, A2, A3) and B (B1, B2, B3).
The first equation reflects the fact, that the Point P has the same distance to both A and B
Math.Pow(X - A1, 2) + Math.Pow(Y - A2, 2) = Math.Pow(X - B1, 2) + Math.Pow(Y - B2, 2)
The second equations reflects the fact, that the Point P has a distance d (= 5) from Point A
Math.Pow(X - A1, 2) + Math.Pow(Y - A2, 2) + Math.Pow(Z, 2) = Math.Pow(d, 2)
The second equations reflects the fact, that the Point P has a distance d (= 5) from Point B
Math.Pow(X - B1, 2) + Math.Pow(Y - B2, 2) + Math.Pow(Z, 2) = Math.Pow(d, 2)
This leaves you with a system of quadratic equations. You can now solve it by either solving equation 1 for X and inserting into equation 2 and solving this equation for Y then inserting the terms for X and Y into equation 3, in this manner you can solve for Z. Now you go the same way back, inserting the solution for Z in equation 2 and solving for Y and finally inserting the solution for Y and Z in equation 1 solving for X. (This might sound confusing at first, here is a more detailed description of the approach with an easier hands-on example)
An example how a system of quadratic equations is solved in C# can be found here.
Note:
The distance of 5 can be thought of as a sphere around your points A and B. In 2D this would result in 2 solutions where the circles cross each other. In 3D, as you can see in the picture, this will result in an infinite number of possible solutions, since the 2 spheres overlap, resulting in a circle.
This might help you to choose an appropriate approach to your task, since you will need to add at least one more condition to get the specific coordinates of a point.
Update:
The task now changed to finding the best escape route, given two
attackers. One on either side of our player.
Without caring much about the values of the distances, it is obvious, that we want to escape orthogonal to the connection of Attacker 1 (A) and Attacker 2 (B), to maximize the distance to both:
The vector for the escape route is thus given by the scalar product:
(A1-B1) * (X-0.5*(B1-A1)) + (A2-B2) * (Y-0.5*(B2-A2)) = 0
Since we only care about the direction to escape you can set X or Y as you like and solve for the other variable.
In case you do care, introduce a second equation that includes the condition you care about, such as length of your new vector and solve for both X and Y
With the help of mrk's answer the result given in the form of C# and less mathematics.
public static Vector2 GetEscapeVector(this float distance, Vector2 one, Vector2 two)
{
var center = (one + two) / 2;
// rotate by 90° which equals to 1.5708F radians
var rotated = Vector2.Transform(center, Matrix3x2.CreateRotation(1.5708F));
var escapeVector = Vector2.Normalize(rotated) * distance;
return escapeVector;
}
Is later used like this:
currentPosition + 20F.GetEscapeVector(one, two)
or
currentPosition - 20F.GetEscapeVector(one, two)
Pythagorean theorem:
The squared distance of a point (x,y,z) to PositionA is
(x-3)² + (y-2)² + z²
Similarly, the squared distance to PositionB is
(x-5)² + (y-4)² + z²
You know that both are equal to 5²
Can you take it from there?
If your problem is limited to 2D this method does what you're looking for:
private Tuple<PointF, PointF> SameDistancePoints(PointF p1, PointF p2, float distance)
{
// Calculate the coefficients for the equation
// x^2 + y^2 + a1x + b1y + c = 0
// which represents the circle of all the points that are at distance d from p1:
// (x-p1.X)^2 + (y-p1.Y)^2 = distance^2
// which is
// x^2 -2p1.X*x + (p1.X)^2 + y^2 -2p1.Y*y + (p1.Y)^2 - distance^2 = 0
float a1 = -2 * p1.X;
float b1 = -2 * p1.Y;
float c1 = (float)Math.Pow(p1.X, 2) + (float)Math.Pow(p1.Y, 2) - (float)Math.Pow(distance, 2);
// do the same for p2
float a2 = -2 * p2.X;
float b2 = -2 * p2.Y;
float c2 = (float)Math.Pow(p2.X, 2) + (float)Math.Pow(p2.Y, 2) - (float)Math.Pow(distance, 2);
// Now we have the system with the 2 equations:
// x^2 + y^2 + a1x + b1y + c1 = 0
// x^2 + y^2 + a2x + b2y + c2 = 0
// subtracting the second equation from the first we get
// (a1-a2)x + (b1-b2)y + c1 - c2 = 0
// from which
// y = (c2-c1)/(b1-b2) - (a1-a2)x/(b1-b2)
// y = d - ex
float d = (c2 - c1) / (b1 - b2);
float e = (a1 - a2) / (b1 - b2);
// replacing the last equation in the first one of the system:
// x^2 + (d-ex)^2 + a1x + b1(d-ex) + c1 = 0;
// x^2 + d^2 - 2dex +(ex)^2 + a1x + b1d - b1ex + c1 = 0
// (1+e^2)x^2 + (a1-2de-b1e)x + d^2 + b1d + c1 = 0
// which can be written as
// a3x^2 + b3x + c3 = 0
// where
float a3 = 1 + (float)Math.Pow(e, 2);
float b3 = a1 - 2 * d * e - b1 * e;
float c3 = (float)Math.Pow(d, 2) + b1 * d + c1;
// now it's simlple
float delta = (float)Math.Pow(b3, 2) - 4 * a3 * c3;
if (delta < 0)
return null;
float x1 = (-b3 + (float)Math.Sqrt(delta)) / (2 * a3);
float y1 = d - e * x1;
float x2 = (-b3 - (float)Math.Sqrt(delta)) / (2 * a3);
float y2 = d - e * x2;
return new Tuple<PointF, PointF>(new PointF(x1, y1), new PointF(x2, y2));
}
I'm just struggled with question about that:
I need to write a program that get 3 positive digits from the user and print all the 3 digits numbers that can be created from them. I'm not allowed to use recursion.. any ideas?
thanks
Providing that a, b, c are given digits, e.g.
int a = 1;
int b = 2;
int c = 3;
the implementation (C#) could be
String report = String.Join(Environment.NewLine,
new HashSet<int>() {
100 * a + 10 * b + c,
100 * a + 10 * c + b,
100 * b + 10 * a + c,
100 * b + 10 * c + a,
100 * c + 10 * a + b,
100 * c + 10 * b + a,
});
Console.Write(report);
The output is
123
132
213
231
312
321
Note, that for (a = 1, b = 2 and c = 1) you'll get only
121
112
211
I doubt if this solution will be accepted by your professor (even if it doesn't have any recursion), but you can use it as a test when elaborating your own routine.
int a = 3, b = 9, c = 2;
double e = 11, f = 0.1;
e = (b + c) / a * a;
Now, I want to now what the result of e is. When I do the math in my head, I get the result to be 11/9 = 1,22222222
BUT
when I run the program in the compiler, I simply get 9. Which way to think is right?
There are 2 thing going wrong,
your misunderstanding your order of operations
your integers are truncating
so, what the compiler is doing is
9+2 = 11
11/3 = 3.666 truncated to 3
3*3 = 9
Use parenthesis, and don't use ints, use all doubles.
double a = 3, b = 9, c = 2, d;
double e = 11, f = 0.1;
string s = "AB", t = "BA", v;
e = (b + c) / (a * a);
if a, b, and c all have to be ints, than you can cast them
int a = 3, b = 9, c = 2, d;
double e = 11, f = 0.1;
string s = "AB", t = "BA", v;
e = ((double)b + (double)c) / ((double)a * (double)a);
now this could just easily be done with
e = (double)(b + c) / (double)(a * a);
and that's because both operations in parenthesis leave no remainder, but it is a bad practice to rely on those kind of coincidences.
All your input variables are integers, so the arithmetic is done as integer arithmetic. Also all arithemtic operators in C# are evaluated left to right, so
e = (b + c) / a * a is equivalent to e = ((b + c) / a ) * a
(b + c ) / a * a = ((9 + 2) / 3) * 3
= (11 / 3) * 3
= 3 * 3
= 9
The expression that I think you want is
e = (double)(b + c) / (a * a);
For details on operator precendence in C# see this .NET documentation
The compiler is quite correct - you may want to review how operator precedence in C# works.
You have two choice.Use doubles instead of integers, or use explicit cast and convert one of your values to double
e = (b + c) / (double)(a * a);
This statement :
(b + c) / a * a
evaluated like:
(int) / (int)
If you divide two integers, how could you get double results ? Then your result converting double with implicit type conversion.But result is already '1' without the floating points,and conversion is meaningless in this case.
But when you do something like this:
e = (b + c) / (double)(a * a);
Returning value from b + c promoted to double and then (double / double) division is performing.So you get the correct results.
I am trying to perform a Calculus Cross Product calculation using Linq and trying to figure out the pattern for the below code:
static void Main(string[] args)
{
double[] a = { 1, -1, -1 };
double[] b = {.5,1,.5};
var cross = from x in a
from y in b
select new {x,y};
List<double> LeftSide = new List<double>();
foreach (var c in cross) {
Console.WriteLine("x = " + c.x + " y = " + c.y);
double res = c.x * c.y;
Console.WriteLine("");
LeftSide.Add(res);
}
double i = LeftSide[5] - LeftSide[7];
double j = LeftSide[2] - LeftSide[6];
double k = LeftSide[1] - LeftSide[3];
Console.WriteLine("("+ i + "i) - (" + j + "j) +(" + k + "k)" );
Console.ReadLine();
}
Once I cross join the a and b, I need to perform the following calculations:
double i = LeftSide[5] - LeftSide[7];
double j = LeftSide[2] - LeftSide[6];
double k = LeftSide[1] - LeftSide[3];
This works and I get the desired output, put I know it can be written more efficiently. I am looking for any suggestions, to point me in the right direction.
Note: This is not a homework question, but is related to Calculus III Cross Products. I am a CS Major
You are making this way, way, way too complicated. The cross product of vectors (a0, a1, a2) and (b0, b1, b2) is (a1 * b2 - a2 * b1, a2 * b0 - a0 * b2, a0 * b1 - a1 * b0). So just compute that:
double[] a = { 1.0, -1.0, -1.0 };
double[] b = { 0.5, 1.0, 0.5 };
double[] cross =
{
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
};
And you're done in a single statement. There's no need to involve LINQ.
You are defining cross as a sequence of {x,y} elements, only to convert it to a sequence of x*y elements in the next step. This is redundant; you could generate your LeftSide immediately using:
double[] a = { 1, -1, -1 };
double[] b = { .5, 1, .5 };
var LeftSide =
(
from x in a
from y in b
select x * y
).ToArray();
Imagine that a - b < c (a, b, c are C# doubles). Is it guaranteed that a < b + c?
Thanks!
EDIT
Let's say that the arithmetical overflow doesn't occur unlike the following example:
double a = 1L << 53;
double b = 1;
double c = a;
Console.WriteLine(a - b < c); // Prints True
Console.WriteLine(a < b + c); // Prints False
Imagine that Math.Abs(a) < 1.0 && Math.Abs(b) < 1.0 && Math.Abs(c) < 1.0
No. Suppose a = c, a very large number, and b is a very small number. It's possible that a - b has a representation less than a, but a + b is so close to a (and bigger) that it still ends up being most precisely representable as a.
Here's an example:
double a = 1L << 53;
double b = 1;
double c = a;
Console.WriteLine(a - b < c); // Prints True
Console.WriteLine(a < b + c); // Prints False
EDIT:
Here's another example, which matches your edited question:
double a = 1.0;
double b = 1.0 / (1L << 53);
double c = a;
Console.WriteLine(a - b < c); // Prints True
Console.WriteLine(a < b + c); // Prints False
In other words, when we subtract a very small number from 1, we get a result less than 1. When we add the same number to 1, we just get 1 back due to the limitations of double precision.
no not always:
double a = double.MaxValue;
double b = double.MaxValue;
double c = 0.1;
Console.WriteLine(a - b < c); // True
Console.WriteLine(a < b + c); // False
This link speaks about floating-point arithmetic properties, and could be very interesting:
FLOATING-POINT FALLACIES
In particular, search for Properties of Relations