This is my class assignment and I don't know how I can construct a precedence graph for :
S1: x=0
S2: x=x+1
S3: y=2
S4: z=y
S5: x=x+2
S6: y=x+z
S7: z=4
I thought it would be like:
S1 goes to S2, S5, and S6
S2 goes to S5 and S6
S3 goes to S4 and S6
S4 goes to S6
S5 goes to S6
S7 goes to S6
but S7 doesn't go to S6 in the answer.
It looks S7 goes to S6 because the right hand side of S6 has "z".
Please help me out this problem.
The example of precedence graph
s7 can not goooo to s6 because once a program is executed it can be reversed.
Moreover s1 can go to s3 s4 or any number below it but s2 can not refer back to s1.
I hope you understand that :)
Related
I'm trying to calculate angular velocity of each axis by movement of end-effector,
If S1 and S2 is finite screw, and S2 had infinitesimal movement from S1.
Also, let S1_ be (-)array of S1 and instantaneous screw of S1 is St1
So If I triangle product S2 and S1_ (S2△S1_=St1), It becomes almost instantaneous screw of S1 (I believe)
What I want to calculate is, if St1 is instantaneous screw, than can I calculate the angular velocity of each axis by using inverse jacobian with [ (J^-1)*St1 = answer ]?
(jacobian is from S1, if S1=S1_6△S1_5△S1_4△S1_3△S1_2△S1_1, (the robot has 6 axis),
jacobian matrix 'J' = [Su1_1, Su1_2, Su1_3, Su1_4, Su1_5, Su1_6], Su is for 'unit twist')
When you look at the kinematics recursively from the base to the end effector you have
vi = vi-1 + si ui
where vi is the velocity screw of each link, vi-1 is the velocity screw of the previous link, si is the unit screw of the joint axis, and ui the joint speed.
So the end effector has a final velocity screw of
v6 = s1 u1 + s2 u2 + s3 u3 + s4 u4 + s5 u5 + s6 u6
and I think you are asking on how to find the vector of joint speeds u = (u1, u2, u3, u4, u5, u6)
So you compose the 6×6 jacobian matrix J, by combining the individual joint axis unit screws in columns
J = [ s1 s2 s3 s4 s5 s6]
and invert the kinematics
v6 = J * u ⇒ u = J-1 v6
I'm using the following code to produce a quaternion from XYZ Euler-Angles in radians:
c1 = Math.cos( x / 2 )
c2 = Math.cos( y / 2 )
c3 = Math.cos( z / 2 )
s1 = Math.sin( x / 2 )
s2 = Math.sin( y / 2 )
s3 = Math.sin( z / 2 )
quaternion = [
c1 * c2 * c3 - s1 * s2 * s3,
s1 * c2 * c3 + c1 * s2 * s3,
c1 * s2 * c3 - s1 * c2 * s3,
c1 * c2 * s3 + s1 * s2 * c3,
]
from:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm
This produces a quaternion that first rotates around the z, then y, then finally the x-axis - Z-Y-X. Is it possible to alter this formula so that it rotates around the axes in a different order? What I'm looking for is the opposite, so X-Y-Z.
If anyone's interested...
Yes you can, turns out on this occasion I just needed to swap the plus and minus signs around to get an X-Y-Z order (traditionally written ZYX).
Like so...
[
c1 * c2 * c3 + s1 * s2 * s3,
s1 * c2 * c3 - c1 * s2 * s3,
c1 * s2 * c3 + s1 * c2 * s3,
c1 * c2 * s3 - s1 * s2 * c3
]
Three.js has a full list of the different formulas for various xyz orders - in the function THREE.Quaternion.setFromEuler
https://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/steps/index.htm
The following program works exactly as expected:
data = input("Input the length of three sides of a triangle: ").strip()
if data.count(',') != 2:
print("You did not enter the lengths of three sides: ")
a, b, c = data.split(', ')
def triangle_type(s1, s2, s3):
if s1 == s2 == s3:
return print("The values entered represent an equilateral triangle.")
elif s1 != s2 and s1 != s3 and s2 != s3:
return print("The values entered represent a scalene triangle.")
else:
if s1 == s2 or s1 == s3 or s2 == s3:
return print("The values entered represent an isosceles triangle.")
triangle_type(int(a), int(b), int(c))
However, if I change the second condition to the following:
elif s1 != s2 != s3:
it does NOT work as expected.
If I input 3, 6, 3 I get:
Input the length of three sides of a triangle: 3, 6, 3
The values entered represent a scalene triangle.
This is incorrect. This should clearly match condition #3 (isosceles).
It works correctly with the first version of the code.
I am confused on two things:
1) Why does the second version work improperly?
2) If the second version is invalid, then why does the first condition work
correctly?
Given the above behavior with the != operator, I would expect to have to treat a == b == c like a == b and a == c and b == c, but I don't have to.
When you chain comparisons in Python, it ands them together, and it only does as many comparisons as you have comparison operators. So
if s1 == s2 == s3:
is equivalent to
if s1 == s2 and s2 == s3:
That works because if both of those conditions are True, then s1 must also be equal to s3 (because they're both equal to s2).
However
if s1 != s2 != s3:
is equivalent to
if s1 != s2 and s2 != s3:
As you've discovered, for the values 3, 6, and 3, the above two conditions are True, but the third assumed condition (s1 != s3) is not True. This doesn't work as you expect because Python isn't making that third comparison.
The chained version is equivalent to s1 != s2 and s2 != s3; it does not require that s1 != s3, because inequality is not transitive.
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 3 years ago.
Improve this question
How do I calculate velocity for bouncing objects with mass?
f1 = ( (m1-m2)/(m1+m2) )*v1 + ( (m2*2 )/(m1+m2) )*v2
f2 = ( (m1*2 )/(m1+m2) )*v1 + ( (m2-m1)/(m1+m2) )*v2
Doesn't work if both objects collide while heading in the same direction
Any help will be appreiciated
For 1d objects
v1 = (u1(m1-m2) + 2m2u2)/(m1+m2)
v2 = (u2(m2-m1) + 2m1u1)/(m1+m2)
Where v1 and v2 are the velocities after and u1 and u2 are velocities before, with m1 and m2 being the mass of each.
Which is the same but simplified version of the formula you have given. BTW your the code block has an error on line 1 5th var is b2 should be m2
This will give the correct velocities if the bodies are moving in the same direction. All I can think that you may be doing wrong is not including the sign. If two bodies are moving in the same direction then the two velocities input u1 and u2 will have the same sign. For all other collisions they will each have a different sign.
If you are applying the above in a 2D situation you will need to modify the formula. This is the solution for circles where the point of contact is along the angle 'p' between them.
//
// u1 * cos(d1 - p) * (m1 - m2) + 2 * m2 * u2 * cos(d2 - p)
//V1x = -------------------------------------------------------- * cos(p) + u1 * sin(d1 - p) * cos(p + PI / 2)
// m1 + m2
// u1 * cos(d1 - p) * (m1 - m2) + 2 * m2 * u2 * cos(d2 - p)
//V1y = -------------------------------------------------------- * sin(p) + u1 * sin(d1 - p) * sin(p + PI / 2)
// m1 + m2
And do the same for the other object
v1x,v1y are the resulting x,y components of the velocity
u1,u2 is velocity.
m1, m2 is the mass
d1 , d2 us the direction of movement in radians.
p is the angle of contact. This is the angle from the center of first
object to the center of second object and the point of contact is on
the line this angle describes.
I am trying to write a program that takes two strings s1 and s2 as arguments. The function should check whether s2 contains s1 and if it does, the program should write the position in s2 at which the first letter of s1 occurs.
I want to check: String.substring(s2, size s2 - size s1, size s1) = s1.
And I then need to do a recursion on (size s2 - 1) so that s2 gets smaller by one after each comparison and thereby "move the s1" comparison of s2 one letter to the left.
I have no problems with recursions like:
fun recurison 0 = 0
| recurison n = n + (n - 1)
But when i shall interact with other functions or String.substring it feels messy. How should I think when I'm trying to write some recursive stuff? Can you give me a hint on the problem? I love to come up with the solutions myself but need help to point out the right road.
First, there is already the function String.isSubstring : string -> string -> bool which does what you want. But since you are interested in implementing such a function yourself recursively you just have to think about the following:
What is the base-case (i.e., when does the recursion end)?
What is the step-case (i.e., how to get a solution from a smaller solution)?
Since in your description you started to check for substrings from the right and then move to the left, the base-case is when you arrive at the leftmost position (i.e., 0). As for the step-case, after you checked position i you have to check position i - 1.
First you might start with a function that handles the case of checking position i.
fun substringat s1 s2 i =
if size s1 + i > size s2 then false
else String.substring (s2, i, size s1) = s1;
Then the skeleton for the recursion
fun substringfrom s1 s2 i =
if i < 0 then ~1
else if substringat s1 s2 i then i
else substringfrom s1 s2 (i - 1);
Finally we initialize everything appropriately
fun substring s1 s2 = substringfrom s1 s2 (size s2 - 1);
To avoid some unnecessary checks we could combine all this to
fun substring s1 s2 =
let
val l1 = size s1;
val l2 = size s2;
fun substringat s1 s2 i =
if l1 + i > l2 then false
else String.substring (s2, i, l1) = s1;
fun substringfrom s1 s2 i =
if i < 0 then ~1
else if substringat s1 s2 i then i
else substringfrom s1 s2 (i - 1);
in
substringfrom s1 s2 (l2 - 1)
end