My problem is just really simple,
I found somethng in the stackoverflow a same problem but it finds the largest number between 2 numbers
var c =(Math.sqrt( a*a + b*b - 2*a*b ) + a + b) / 2;
can somebody help me revised this equation so the lowest number should print out?
hi i have one solution
c = ((a + b) - sqrt((a - b) * (a - b))) / 2
Hope this will help you
Rewrite your code as below:
var c =((a + b) - Math.sqrt((a - b) * (a - b))) / 2;
As far as I know:
c = ((a + b) - sqrt((a - b) * (a - b))) / 2
equals
c = ((a + b) - (a - b)) / 2
equals
c = (a - a + b + b) /2 = b
or am I missing something?
Why don't you use the Math class for it? Like Math.min(a,b) ????
Related
I have a table with columns: a, b, x_1, x_2, x_3, x_4, x_5, y_1, y_2, y_3, y_4, y_5 and need to apply a formula such as: a * (x_i * (1 + (b / (1 - b)) ) + (y_i - x_i)) where i = {1,2,3,4,5}.
My problem is that I need to sum() the results of this formula partitioned w.r.t two the other columns a, b. So the select query will be like:
select(
col_1,
col_2,
func.sum(
a * (x_1 * (1 + (c / (1 - c)) ) + (y_1 - x_1))
).over(partition_by=[col_1, col_2]).label("sum1"),
func.sum(
a * (x_2 * (1 + (c / (1 - c)) ) + (y_2 - x_2))
).over(partition_by=[col_1, col_2]).label("sum2"),
...
func.sum(
a * (x_5 * (1 + (c / (1 - c)) ) + (y_5 - x_5))
).over(partition_by=[col_1, col_2]).label("sum5")
)
As you can see it is cumbersome to maintain and keep repeating the formula is error prone.
Is there a way to wrap this formula so that I can simply refer to formula like below or some way of abstracting it?
select(
col_1,
col_2,
func.sum(my_custom_formula(a, b, x_1, y_1)).over(partition_by=[col_1, col_2]).label("sum1"),
func.sum(my_custom_formula(a, b, x_2, y_2)).over(partition_by=[col_1, col_2]).label("sum2"),
...
func.sum(my_custom_formula(a, b, x_5, y_5)).over(partition_by=[col_1, col_2]).label("sum5")
)
You can hide even more of the implementation detail if you want. The below should give you directional idea:
def get_expression(T: MyTable, pos: int, c):
xn = getattr(T, f"x_{pos}")
yn = getattr(T, f"y_{pos}")
_expr = T.a * (xn * (1 + (c / (1 - c)) ) + (yn - xn))
expr = func.sum(_expr).over(partition_by=[T.col_1, T.col_2]).label(f"sum{pos}")
return expr
q = select(
T.col_1,
T.col_2,
get_expression(T, 1, c),
get_expression(T, 2, c),
get_expression(T, 3, c),
get_expression(T, 4, c),
get_expression(T, 5, c),
)
My own equation is a bit longer, but the following example shows perfectly where I struggle at the moment.
So far I have been using the let() and letsimp() function
to substitute longer terms in an equation,
but in this example they have no effect:
(%i1) eq: ((2*u+a^2+d) * y+x)/2*a = x;
2
a ((2 u + d + a ) y + x)
(%o1) ------------------------ = x
2
(%i2) let(2*u+a^2+d, %beta);
2
(%o2) 2 u + d + a --> %beta
(%i3) letsimp(eq);
2
a ((2 u + d + a ) y + x)
(%o3) ------------------------ = x
2
What is the preferred way to replace 2*u+a^2+d with %beta in this sample equation?
And why has letsimp() no effect?
Thank you very much!
letsimp applies only to "*" expressions. You could try subst.
I'm writing a simple octave program that calculate similarity between two images.
The relevant part for this question can be found here:
http://pastebin.com/gBxN7VbP
The function struct_comp was supposed to return some number between zero and one. But the command
ec = (corr + C) / (dp1 * dp2 + C);
is setting ec as 0 even when it should not be zero.
The disp commands are showing the values of all variables involved in this division. The output is:
C:
6.5536
dp1
97.663
dp2
47.686
corr
-290
(corr + C)
-283
(dp1 * dp2 + C)
4663.7
(corr + C) / (dp1 * dp2 + C)
0
Struct comp:
0
As you can see, the partial values (numerator and denominator) are right, but the division is returning zero.
Somebody knows what is happening here?
Thank you.
EDIT:
The two images used to generate this output were these:
http://lasca.ic.unicamp.br/~hilder/tux.jpg
http://lasca.ic.unicamp.br/~hilder/monalisa.jpg
But you can use any grayscale image to test, just change the name in the code.
i replaced the line
ec = (corr + C) / (dp1 * dp2 + C);
by
ec = double(corr + C) / double(dp1 * dp2 + C);
and it worked.
I am writing a multiple choice quiz using AS3. And, I want to display answer options in columns of 2 0r 3 such as:
A) Answer A B) Answer B C) Answer C
D) Answer D E) Answer E
I use this to accomplish the layout,
answerTextField.x=(j%3) * 200 ;
answerTextField.y= questionName.y + 20 + ((j % 2) * 20) ;
but it produces
A) Answer A E) Answer E C) Answer C
D) Answer D B) Answer B
How can we solve this problem or is there any other way to accomplish the layout.
I solved:
answerTextField.y= questionName.y + 20 + (int(j / 2) * 20) ;
I have an equation for a parabolic curve intersecting a specified point, in my case where the user clicked on a graph.
// this would typically be mouse coords on the graph
var _target:Point = new Point(100, 50);
public static function plot(x:Number, target:Point):Number{
return (x * x) / target.x * (target.y / target.x);
}
This gives a graph such as this:
I also have a series of line segments defined by start and end coordinates:
startX:Number, startY:Number, endX:Number, endY:Number
I need to find if and where this curve intersects these segments (A):
If it's any help, startX is always < endX
I get the feeling there's a fairly straight forward way to do this, but I don't really know what to search for, nor am I very well versed in "proper" math, so actual code examples would be very much appreciated.
UPDATE:
I've got the intersection working, but my solution gives me the coordinate for the wrong side of the y-axis.
Replacing my target coords with A and B respectively, gives this equation for the plot:
(x * x) / A * (B/A)
// this simplifies down to:
(B * x * x) / (A * A)
// which i am the equating to the line's equation
(B * x * x) / (A * A) = m * x + b
// i run this through wolfram alpha (because i have no idea what i'm doing) and get:
(A * A * m - A * Math.sqrt(A * A * m * m + 4 * b * B)) / (2 * B)
This is a correct answer, but I want the second possible variation.
I've managed to correct this by multiplying m with -1 before the calculation and doing the same with the x value the last calculation returns, but that feels like a hack.
SOLUTION:
public static function intersectsSegment(targetX:Number, targetY:Number, startX:Number, startY:Number, endX:Number, endY:Number):Point {
// slope of the line
var m:Number = (endY - startY) / (endX - startX);
// where the line intersects the y-axis
var b:Number = startY - startX * m;
// solve the two variatons of the equation, we may need both
var ix1:Number = solve(targetX, targetY, m, b);
var ix2:Number = solveInverse(targetX, targetY, m, b);
var intersection1:Point;
var intersection2:Point;
// if the intersection is outside the line segment startX/endX it's discarded
if (ix1 > startX && ix1 < endX) intersection1 = new Point(ix1, plot(ix1, targetX, targetY));
if (ix2 > startX && ix2 < endX) intersection2 = new Point(ix2, plot(ix2, targetX, targetY));
// somewhat fiddly code to return the smallest set intersection
if (intersection1 && intersection2) {
// return the intersection with the smaller x value
return intersection1.x < intersection2.x ? intersection1 : intersection2;
} else if (intersection1) {
return intersection1;
}
// this effectively means that we return intersection2 or if that's unset, null
return intersection2;
}
private static function solve(A:Number, B:Number, m:Number, b:Number):Number {
return (m + Math.sqrt(4 * (B / (A * A)) * b + m * m)) / (2 * (B / (A * A)));
}
private static function solveInverse(A:Number, B:Number, m:Number, b:Number):Number {
return (m - Math.sqrt(4 * (B / (A * A)) * b + m * m)) / (2 * (B / (A * A)));
}
public static function plot(x:Number, targetX:Number, targetY:Number):Number{
return (targetY * x * x) / (targetX * targetX);
}
Or, more explicit yet.
If your parabolic curve is
y(x)= A x2+ B x + C (Eq 1)
and your line is
y(x) = m x + b (Eq 2)
The two possible solutions (+ and -) for x are
x = ((-B + m +- Sqrt[4 A b + B^2 - 4 A C - 2 B m + m^2])/(2 A)) (Eq 3)
You should check if your segment endpoints (in x) contains any of these two points. If they do, just replace the corresponding x in the y=m x + b equation to get the y coordinate for the intersection
Edit>
To get the last equation you just say that the "y" in eq 1 is equal to the "y" in eq 2 (because you are looking for an intersection!).
That gives you:
A x2+ B x + C = m x + b
and regrouping
A x2+ (B-m) x + (C-b) = 0
Which is a quadratic equation.
Equation 3 are just the two possible solutions for this quadratic.
Edit 2>
re-reading your code, it seems that your parabola is defined by
y(x) = A x2
where
A = (target.y / (target.x)2)
So in your case Eq 3 becomes simply
x = ((m +- Sqrt[4 A b + m^2])/(2 A)) (Eq 3b)
HTH!
Take the equation for the curve and put your line into y = mx +b form. Solve for x and then determine if X is between your your start and end points for you line segment.
Check out: http://mathcentral.uregina.ca/QQ/database/QQ.09.03/senthil1.html
Are you doing this often enough to desire a separate test to see if an intersection exists before actually computing the intersection point? If so, consider the fact that your parabola is a level set for the function f(x, y) = y - (B * x * x) / (A * A) -- specifically, the one for which f(x, y) = 0. Plug your two endpoints into f(x,y) -- if they have the same sign, they're on the same side of the parabola, while if they have different signs, they're on different sides of the parabola.
Now, you still might have a segment that intersects the parabola twice, and this test doesn't catch that. But something about the way you're defining the problem makes me feel that maybe that's OK for your application.
In other words, you need to calulate the equation for each line segment y = Ax + B compare it to curve equation y = Cx^2 + Dx + E so Ax + B - Cx^2 - Dx - E = 0 and see if there is a solution between startX and endX values.