Finding the Projection without using projection matrix always but to use some symmetry - raytracing

I am interested in finding the t2' with the help of t1, t1' and t2.
Actually I am using some projection matrix T (this will be used to project point x and y which is clear in the Image attached) on x and y to get the points t1 and t2. Again I have to use the matrix T on x' and y' to find the t1' and t2'. But I don't want to exactly use the matrix T to do this just to avoid the lots of multiplication.
Is there any way to find t2' with the help of t1' and the result followed in the case of t1 and t1'.
T*x = t1
T*y = t2
t*x' = t1'
I am attaching picture for better visibility of the question.

If there is a linear relationship between the variables, you can use that same relationship after applying T because T, being a linear transformation, will preserve those relationships.
So, let's assume that the relationship you have verifies y - x = y' - x'. This assumption is consistent with the one you wrote in the comment because there y - x = (0, 1) and y'- x' = (0, 1) too.
So, applying T to both sides of the equation we have: T(y - x) = T(y' - x'). Now, using that T preserves linear combinations (i.e., T(u + sv) = T(u) + sT(v) for all vectors u,v and scalars s), we get: Ty - Tx = Ty' - Tx'. With your variable names we get: t2 - t1 = t2' - t1'. Solving for t2' leads to t2' = t2 - t1 + t1'.
If the actual relationship between x, y, x' and y' happens to be different but still linear you can apply the same reasoning to it.

Related

Probability of a disjunction on N dependent events in Prolog

Does anybody know where to find a Prolog algorithm for computing the probability of a disjunction for N dependent events? For N = 2 i know that P(E1 OR E2) = P(E1) + P(E2) - P(E1) * P(E2), so one could do:
prob_disjunct(E1, E2, P):- P is E1 + E2 - E1 * E2
But how can this predicate be generalised to N events when the input is a list? Maybe there is a package which does this?
Kinds regards/JCR
The recursive formula from Robert Dodier's answer directly translates to
p_or([], 0).
p_or([P|Ps], Or) :-
p_or(Ps, Or1),
Or is P + Or1*(1-P).
Although this works fine, e.g.
?- p_or([0.5,0.3,0.7,0.1],P).
P = 0.9055
hardcore Prolog programmers can't help noticing that the definition isn't tail-recursive. This would really only be a problem when you are processing very long lists, but since the order of list elements doesn't matter, it is easy to turn things around. This is a standard technique, using an auxiliary predicate and an "accumulator pair" of arguments:
p_or(Ps, Or) :-
p_or(Ps, 0, Or).
p_or([], Or, Or).
p_or([P|Ps], Or0, Or) :-
Or1 is P + Or0*(1-P),
p_or(Ps, Or1, Or). % tail-recursive call
I don't know anything about Prolog, but anyway it's convenient to write the probability of a disjunction of a number of independent items p_m = Pr(S_1 or S_2 or S_3 or ... or S_m) recursively as
p_m = Pr(S_m) + p_{m - 1} (1 - P(S_m))
You can prove this by just peeling off the last item -- look at Pr((S_1 or ... or S_{m - 1}) or S_m) and just write that in terms of the usual formula, writing Pr(A or B) = Pr(A) + Pr(B) - Pr(A) Pr(B) = Pr(B) + Pr(A) (1 - Pr(B)), for A and B independent.
The formula above is item C.3.10 in my dissertation: http://riso.sourceforge.net/docs/dodier-dissertation.pdf It is a simple result, and I suppose it must be an exercise in some textbooks, although I don't remember seeing it.
For any event E I'll write E' for the complementary event (ie E' occurs iff E doesn't).
Then we have:
P(E') = 1 - P(E)
(A union B)' = A' inter B'
A and B are independent iff A' and B' are independent
so for independent E1..En
P( E1 union .. union En ) = 1 - P( E1' inter .. inter En')
= 1 - product{ i<=i<=n | 1 - P(E[i])}

Calculate the displacement coordinates of a semi-articulated truck

As shown in the image below, I'm creating a program that will make a 2D animation of a truck that is made up of two articulated parts.
The truck pulls the trailer.
The trailer moves according to the docking axis on the truck.
Then, when the truck turns, the trailer should gradually align itself with the new angle of the truck, as it does in real life.
I would like to know if there is any formula or algorithm that does this calculation in an easy way.
I've already seen inverse kinematics equations, but I think for just 2 parts it would not be so complex.
Can anybody help me?
Let A be the midpoint under the front axle, B be the midpoint under the middle axle, and C be the midpoint under the rear axle. For simplicity assume that the hitch is at point B. These are all functions of time t, for example A(t) = (a_x(t), a_y(t).
The trick is this. B is moving directly towards A with the component of A's velocity in that direction. Or in symbols, dB/dt = (dA/dt).(A-B)/||A-B|| And similarly, dC/dt = (dB/dt).(B-C)/||B-C|| where . is the dot product.
This turns into a non-linear first-order system in 6 variables. This can be solved with normal techniques, such as https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods.
UPDATE: Added code
Here is a Python implementation. You can replace it with https://rosettacode.org/wiki/Runge-Kutta_method for your favorite language and your favorite linear algebra library. Or even hand-roll that.
For my example I started with A at (1, 1), B at (2, 1) and C at (2, 2). Then pulled A to the origin in steps of size 0.01. That can be altered to anything that you want.
#! /usr/bin/env python
import numpy
# Runga Kutta method.
def RK4(f):
return lambda t, y, dt: (
lambda dy1: (
lambda dy2: (
lambda dy3: (
lambda dy4: (dy1 + 2*dy2 + 2*dy3 + dy4)/6
)( dt * f( t + dt , y + dy3 ) )
)( dt * f( t + dt/2, y + dy2/2 ) )
)( dt * f( t + dt/2, y + dy1/2 ) )
)( dt * f( t , y ) )
# da is a function giving velocity of a at a time t.
# The other three are the positions of the three points.
def calculate_dy (da, A0, B0, C0):
l_ab = float(numpy.linalg.norm(A0 - B0))
l_bc = float(numpy.linalg.norm(B0 - C0))
# t is time, y = [A, B, C]
def update (t, y):
(A, B, C) = y
dA = da(t)
ab_unit = (A - B) / float(numpy.linalg.norm(A-B))
# The first term is the force. The second is a correction to
# cause roundoff errors in length to be selfcorrecting.
dB = (dA.dot(ab_unit) + float(numpy.linalg.norm(A-B))/l_ab - l_ab) * ab_unit
bc_unit = (B - C) / float(numpy.linalg.norm(B-C))
# The first term is the force. The second is a correction to
# cause roundoff errors in length to be selfcorrecting.
dC = (dB.dot(bc_unit) + float(numpy.linalg.norm(B-C))/l_bc - l_bc) * bc_unit
return numpy.array([dA, dB, dC])
return RK4(update)
A0 = numpy.array([1.0, 1.0])
B0 = numpy.array([2.0, 1.0])
C0 = numpy.array([2.0, 2.0])
dy = calculate_dy(lambda t: numpy.array([-1.0, -1.0]), A0, B0, C0)
t, y, dt = 0., numpy.array([A0, B0, C0]), .02
while t <= 1.01:
print( (t, y) )
t, y = t + dt, y + dy( t, y, dt )
By the answers I saw, I realized that the solution is not really simple and will have to be solved by an Inverse Kinematics algorithm.
This site is an example and it is a just a start, although it still does not solve everything, since the point C is fixed and in the case of the truck it should move.
Based on this Analytic Two-Bone IK in 2D article, I made a fully functional model in Geogebra, where the nucleus consists of two simple mathematical equations.

SICP - Which functions converge to fixed points?

In chapter 1 on fixed points, the book says we can find fixed points of certain functions using
f(x) = f(f(x)) = f(f(f(x))) ....
What are those functions?
It doesn't work for y = 2y when i rewrite it as y = y/2 it works
Does y need to get smaller everytime? Or are there any general attributes that a function has to have to find fixed points by that method?
What conditions it should satisfy to work?
According to the Banach fixed-point theorem, such a point exists iff the mapping (function) is a contraction. That means that, for example, y=2x doesn't have fixed point and y = 0,999... * x has. In general, if f maps [a,b] to [a,b], then |f(x) - f(y)| should be equal to c * |x - y| for some 0 <= c < 1 (for all x, y from [a, b]).
Say you have:
f(x) = sin(x)
then x = 0 is a fixed point of the function since:
f(0) = sin(0) = 0
f(f(0)) = sin(sin(0)) = sin(0) = 0
Not every point along x is a fixed point of sin, only 0 is.
Different functions have different fixed points, if at all. You can find more on fixed points of functions at Wikidpedia

how to calculate a quadratic equation that best fits a set of given data

I have a vector X of 20 real numbers and a vector Y of 20 real numbers.
I want to model them as
y = ax^2+bx + c
How to find the value of 'a' , 'b' and 'c'
and best fit quadratic equation.
Given Values
X = (x1,x2,...,x20)
Y = (y1,y2,...,y20)
i need a formula or procedure to find following values
a = ???
b = ???
c = ???
Thanks in advance.
Everything #Bartoss said is right, +1. I figured I just add a practical implementation here, without QR decomposition. You want to evaluate the values of a,b,c such that the distance between measured and fitted data is minimal. You can pick as measure
sum(ax^2+bx + c -y)^2)
where the sum is over the elements of vectors x,y.
Then, a minimum implies that the derivative of the quantity with respect to each of a,b,c is zero:
d (sum(ax^2+bx + c -y)^2) /da =0
d (sum(ax^2+bx + c -y)^2) /db =0
d (sum(ax^2+bx + c -y)^2) /dc =0
these equations are
2(sum(ax^2+bx + c -y)*x^2)=0
2(sum(ax^2+bx + c -y)*x) =0
2(sum(ax^2+bx + c -y)) =0
Dividing by 2, the above can be rewritten as
a*sum(x^4) +b*sum(x^3) + c*sum(x^2) =sum(y*x^2)
a*sum(x^3) +b*sum(x^2) + c*sum(x) =sum(y*x)
a*sum(x^2) +b*sum(x) + c*N =sum(y)
where N=20 in your case. A simple code in python showing how to do so follows.
from numpy import random, array
from scipy.linalg import solve
import matplotlib.pylab as plt
a, b, c = 6., 3., 4.
N = 20
x = random.rand((N))
y = a * x ** 2 + b * x + c
y += random.rand((20)) #add a bit of noise to make things more realistic
x4 = (x ** 4).sum()
x3 = (x ** 3).sum()
x2 = (x ** 2).sum()
M = array([[x4, x3, x2], [x3, x2, x.sum()], [x2, x.sum(), N]])
K = array([(y * x ** 2).sum(), (y * x).sum(), y.sum()])
A, B, C = solve(M, K)
print 'exact values ', a, b, c
print 'calculated values', A, B, C
fig, ax = plt.subplots()
ax.plot(x, y, 'b.', label='data')
ax.plot(x, A * x ** 2 + B * x + C, 'r.', label='estimate')
ax.legend()
plt.show()
A much faster way to implement solution is to use a nonlinear least squares algorithm. This will be faster to write, but not faster to run. Using the one provided by scipy,
from scipy.optimize import leastsq
def f(arg):
a,b,c=arg
return a*x**2+b*x+c-y
(A,B,C),_=leastsq(f,[1,1,1])#you must provide a first guess to start with in this case.
That is a linear least squares problem. I think the easiest method which gives accurate results is QR decomposition using Householder reflections. It is not something to be explained in a stackoverflow answer, but I hope you will find all that is needed with this links.
If you never heard about these before and don't know how it connects with you problem:
A = [[x1^2, x1, 1]; [x2^2, x2, 1]; ...]
Y = [y1; y2; ...]
Now you want to find v = [a; b; c] such that A*v is as close as possible to Y, which is exactly what least squares problem is all about.

Finding the point which minimizes the distance given a point in space and constraints?

I have a question regarding an algorithm:
We have a fixed point in 2D space let's call it S(x,y) and the length of two links joining (L1 and L2). These two links are connected at a common joint called E(x,y). And we have another point in the space which is end point of the L2 which we call F(x,y).
So we L1 have two end points S and E where as L2 has E and F.
When we are given a point P(x,y) in space. How can we find the coordinate of F(x,y) which is closest to P? I wanted to find the angle of θ1 and θ2 which takes the links L1 and L2 to that point?
See this link to get the graphical representation of my problem
See this pic http://postimage.org/image/qlekcv1qz/, where you will be able to see the real problem I have right now.
So I have formulated this as optimization problem. Where the Objective function is:
* arg min |P-F|
with constraints θ1 and θ2 where θ1 ∈ [ O , π] and θ2 ∈ [ O , π/2].
So we have,
* xE = xS + L1 * Cosθ1 and yE = yS + L1 * Sinθ1
* xF = xE + L2 * Cos (θ1 + θ2 ) and yF = yE + L2 * sin ( θ1 + θ2)
Here we have length of L1 = 105 and L2 = 113.7 and Point S is the origin i.e xS = O and yS = O.
Can you give a hint how code up my function or any optimization problem which gives me the values of θ1 and θ2, such that the distance between Point F and point P is minimized.
So if I understand correctly, your description is equivalent of having two rigid rods of length L1 and L2, with one end of L1 fixed at S, the other end connected to L2 by a flexible joint (at some undefined point E), and you want to get the other end of L2 (point F) as close to some point P as possible. If this is the case then:
If |L1-L2| < |P-S| < |L1+L2| then F = P
If |L1-L2| > |P-S| then F = S + (P-S)*|L1-L2|/|P-S|
If |P-S| > |L1+L2| then F = S + (P-S)*|L1+L2|/|P-S|
Is that what you want?
See imnage
http://postimage.org/image/l1ktt0qtb/
If point P is closer to point S than the distance |L1-L2| (assuming they are unequal), then point F cannot 'reach' point P, even with the angle at E bent to 180 ndegrees. Then the closest you can get is somewhere on the the circle with radius |L1-L2| and centre S. In this case the best F is given by the vector with direction (P-S), and magnitude |L1-L2|, my case 2 above and Figure A below. Note that if L1=L2 this will never be the case.
If point P is further from point S than the distance |L1+L2|, then point F cannot 'reach' point P, even with the angle at E straightened to 0 degrees. Then the closest you can get is aomewhere on the the circle with radius |L1+L2| and centre S. In this case the best F is given by the vector with direction (P-S), and magnitude |L1+L2|, my case 3 above and Figure B below.
If point P is betwen the two limiting circles, then there will be two solutions (one as shown in Figure 3 below, and the other with L1 and L2 reflected in the mirror line formewd by the vector P-S. In this case the 'best' F is equal to point P.
If you want to know the angles Theta1 and Theta 2 then that is a different question (I see you have added that now).
Use the cosine rule for triangles with no right angle.
The rule is
C = acos[(a^2 + b^2 - c^2)/(2ab)]
where a triangle has sides of length a,b, and c, and C is the angle between sides a and b. You are trying to produce a triangle with sides l1, l2, and d=|S-P|, which will be possible so as long as no two of the lengths are shorter (in sum) than the third one.
By substituting l1, l2, and d for a,b, anc c appropriately you will be able to solve for each of the internal angles, A, B, and C. Then you can use these angles A,B,C plus the angle between the vector P-S and horizontal (call that D perhaps?) to calculate your theta1 and theta2.

Resources