I have two numbers that are samples of two different quantities (it doesn't really matter what it is). They are both fluctuating with time. I have samples for these values from two different points in time. Call them a0, a1, b0, b1. I can use the differences (a1-a0, b1-b0) the difference and sum of the differences ( (a1-a0)-(b1-b0) ) ( (a1-a0) + (b1-b0) ) )
My questions is how do you determine when both of them are descending in an fashion that doesn't hard code any constants. Let me explain.
I want to detect when both of these quantities have decreased by a certain amount but that amount may change if I change the quantities I'm sampling so I can't hard code a constant.
I'm sorry if this is vague but that's really all the information I have. I was just wondering if this is even solvable.
if ( a1 - a0 < 0)
if( b1 - b0 < 0) {
//... descending
}
or:
if ( a1 - a0 + b1 - b0 < a1 - a0) // b1 - b0 is negative
if( a1 - a0 + b1 - b0 < b1 - b0) { // a1 - a0 is negative
//... descending
}
To add a threshold is simple:
if ( a1 - a0 < -K)
if( b1 - b0 < -K) {
//... descending, more than K
}
or:
if ( a1 - a0 + b1 - b0 < a1 - a0 - K) // b1 - b0 is less than -K
if( a1 - a0 + b1 - b0 < b1 - b0 - K) { // a1 - a0 is less than -K
//... descending more than K
}
Related
I am working on a drawing application. I summerize the main question to a tiny scenario:
User draws the line A1-B1 and also the point M1. Now user moves the line A1-B1 to a new position A2-B2 while the point M1 also should move with the line (like a rigid body). How can I calculate the new position of point M2?
I know it is possbile with complex calucations on lines or circles conjuction and finally detemrining if the new point is on the right or left of line (this question) etc. But as I want to recalculate live on any steps of dragging, I guess there should be a shortcut and light solution which all modeling softwares use. Is there really a shortcut formula rahter than the line or circle conjuctions?
To summerize the problem again I am looking for a light function with given A1,B1,A2,B2,M1 and looking for M2 position (two separate formula for x and y)
M2 = f(A1,B1,A2,B2,M1)
My guess: I think finding the center and the angle of rotation is one of the best options but I don't know again if there is a shotcut function to find them.
Edit: I prefer to implement it in an online website, so if there is a ready javascript library, it would be helpful, However I am now just looking for the mathematical logic.
Start with A1 and B1.
D = B1 - A1
C1 = D/|D|
Rotate C1 90 degrees counter-clockwise to get C'1:
C1 = (cx, cy)
C'1 = (-cy, cx)
Now take M1 - A1 and measure it against C1 and C'1:
j = (M1 - A) · C1
k = (M1 - A) · C'1
Now it's easy to prove:
M1 = A1 + j C1 + k C'1
So j and k tell you where M1 is, given A1 and B1.
When you get A2 and B2, use them to construct C2, rotate it to get C'2, and then you'll have:
M2 = A2 + j C2 + k C'2
I'm assuming points are objects which have x and y values. Like this:
A1 = {x: 100, y:100}
Here's a lightweight solution:
function reposition(A1,A2,B1,B2,M1)
{
//Getting the Angle Between Lines
var angle = Math.atan2(B2.y-B1.y, B2.x-B1.x)-Math.atan2(A2.y-A1.y, A2.x-A1.x);
//Rotating the point
var xdif = M1.x-A1.x; //point.x-origin.x
var ydif = M1.y-A1.y; //point.y-origin.y
//Returning M2
return {x:B1.x + Math.cos(angle) * xdif - Math.sin(angle) * ydif, y: B1.y + Math.sin(angle) * xdif + Math.cos(angle) * ydif}
}
I think we all should be familiar of the arithmetic swap algorithm, that swaps two variables without using a third variable. Now I found out that there are two variations of the arithmetic swap. Please consider the following:
Variation 1.
int a = 2;
int b = 3;
a = a + b;
b = a - b;
a = a - b;
Variation 2.
int a = 2;
int b = 3;
b = b - a;
a = a + b;
b = a - b;
I want to know, why are there two distinct variations of the arithmetic swap and why do they work? Are there also other variations of the arithmetic swap that achieve the same result? How are they related? Is there any elegant mathematical formula that justifies why the arithmetic swap works the way it does, for all variations? Is there anything related between these two variations of the two arithmetic swap, like an underlying truth?
Break each variable out as what it represents:
a = 2
b = 3
a1 = a + b
b1 = a1 - b = (a + b) - b = a
a2 = a1 - b1 = (a + b) - a = b
a = 2
b = 3
b1 = b - a
a1 = a + b1 = a + (b - a) = b
b2 = a1 - b1 = b - (b - a) = a
There's not underlying truth other than the fact that the math works out. Remember that each time you do an assignment, it's effectively a new "variable" from the math side.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I have an equation which I want to simplify in order to retrieve the solution. But the equation contains absolute in it. How can it be more simplified?
d = abs(X²-vs²)/2*a1 + abs(vf²-X²)/2*a2
abs is the absolute of the value inside the parenthesis
X is the unknown and we know d, vs, a1, vf and a2
So in the end the equation would be X = ....
A solution for this would be to solve the 4 following equations:
d = (X²-vs²)/2*a1 + (vf²-X²)/2*a2
d = (vs²-X²)/2*a1 + (vf²-X²)/2*a2
d = (X²-vs²)/2*a1 + (X²-vf²)/2*a2
d = (vs²-X²)/2*a1 + (X²-vf²)/2*a2
And then see which solutions are compatible with the initial equation.
The main idea is to rewrite the one abs(A) ... = 0 equation into 2 with conditions:
abs(A) ... = 0
is equal to
A ... = 0 when A >= 0
-A ... = 0 when A < 0
In your case we can turn then initial equation into three . Let's suppose that
vs**2 < vf**2
so we have 3 cases:
X <= vs so abs(X**2 - vs**2) = -(X**2 - vs**2) ; (vf**2 - X**2) = vf**2 - X**2
vs < X <= vf so abs(X**2 - vs**2) = X**2 - vs**2 ; (vf**2 - X**2) = vf**2 - X**2
vf < X so abs(X**2 - vs**2) = X**2 - vs**2 ; (vf**2 - X**2) = -(vf**2 - X**2)
Ans we have three easy to solve equations with conditions on X:
d = -(X**2 - vs**2) / 2 / a1 + (vf**2 - X**2)/2 * a2 when X <= vs
d = (X**2 - vs**2) / 2 / a1 + (vf**2 - X**2)/2 * a2 when vs < X <= vf
d = (X**2 - vs**2) / 2 / a1 - (vf**2 - X**2)/2 * a2 when vf < X
Solve all of them, but take X if and only if it met the condition. In case that vs**2 > vf**2 you have 3 a bit different conditions:
X <= vf
vf < X <= vs
vs < X
and equations will be
d = -(X**2 - vs**2) / 2 / a1 + (vf**2 - X**2)/2 * a2 when X <= vf
d = -(X**2 - vs**2) / 2 / a1 - (vf**2 - X**2)/2 * a2 when vs < X <= vf
d = (X**2 - vs**2) / 2 / a1 - (vf**2 - X**2)/2 * a2 when vf < X
By definition:
abs(x) = x if x >= 0, &
-x if x <=0
So turn this equation into 4 equations, for all combinations of each abs term positive or negative, i.e. + +, + -, - +, - -
Solve each of the four equations, and discard any solutions that don't satisfy the sign assumption about the abs term of that particular equation after you plug the answer back into the abs term. Any solutions remaining that do satisfy the abs sign assumption are the actual answers.
I was watching https://www.youtube.com/watch?v=UPo-M8bzRrc&index=21&list=PL4BBB74C7D2A1049C,(CS 61B Lecture 21: Hash Tables) and the example the professor gave was
You have a two letter word, each letter falls between a-z
public class Word{
public static final int LETTERS = 26, WORDS = LETTERS * LETTERS;
private String word;
public int hashCode(){
return LETTERS * (word.charAt(0)-'a') + word.charAt(1) - 'a';
}
}
Is there a way to prove(mathematical?) that each possible word will map to a different value between 0 and 675?
I've proved that the range will be between 0 and 675(give "aa" and "zz", but unsure about how to prove uniqueness.
The formula to get the hash code is:
hash = 26 * (A - 'a') + (B - 'a')
= 26 * (A - 97) + (B - 97) // 'a' == 97 in ASCII
= 26A + B - 27*97
So what we need to prove is that 26A + B has distinct values for any A and B in range <97; 122> (decimal values for <'a'; 'z'>). We ignore the constant `27 * 97 part, as it would not change the reasoning.
Let's look at the opposite statement - when the hash code would not be distinct? It would not be distinct when change in A would be compensated by the change in B. So the following would need to be true:
26 * A1 + B1 = 26 * A2 + B2
Let's assume that A2 = A1 + 1:
26 * A1 + B1 = 26 * (A1 + 1) + B2
= 26 * A1 + B2 + 26
Which means:
B1 = B2 + 26
B1 - B2 = 26
Which is impossible, because B is the char code for letters in range <'a'; 'z'>. And this range, in decimal ASCII values, is 25 (122-97). The required compensation by B would increase for every other A1 - A2 difference.
So, by proving the opposite is impossible, we've proved that hash code is unique for that characters.
Let's say I have two 4-dimensional vectors (i.e. a and b) as follows:
a = {a1, a2, a3, a4}
b= {b1, b2, b3, b4}
How do I compute the Euclidean distance between these vectors?
The euclidian distance calculus is independent of dimensions.
In your case, the euclidian distance between a and b can be written as:
d(a,b) = sqrt( sum_{ i=1 } ^ { 4 } (a[ i ] - b[ i ])^2 )
Or, more specifically:
d(a,b) = sqrt( (a1 - b1)^2 + (a2 - b2)^2 + (a3 -b3)^2 + (a4 - b4)^2 )
public static float ndistance(float[] a, float[] b) {
float total = 0, diff;
for (int i = 0; i < a.length; i++) {
diff = b[i] - a[i];
total += diff * diff;
}
return (float) Math.sqrt(total);
}
The function/method/code above will calculate the distance in n-dimensional space. a and b are arrays of floating point number and have the same length/size or simply the n. Since you want a 4-dimension, you simply pass a 4-length array representing the data of your 4-D vector.