how I can get any point on Q - geometry

I have points A, B and C as in this picture:
link
I have the coordinates of A, B, and C. In this example, A = (1, 1), B = (4.5, 2), and C = (6, 5.5). Points Q1 and Q2 are on the line shown, where the distances from Q1 and Q2 to B are both equal to a number r (in this example, r = 3).
How I can get the coordinates of Q1 and Q2?
Thanks.

Let's assume you have some geometry primitives, such as vector subtraction and vector lengths. Then, you can compute Q2 as follows (in pseudo-code):
r = 3
v = C.minus(B)
w = B.minus(A)
u = v.scaleBy(length(w)) + w.scaleBy(length(v))
Q2 = B.plus(u.scaleBy(r / length(u))
Here, v.scaleBy(5) returns a vector that has the x and y coordinates of v multiplied by 5. The functions length, plus, and minus should be self-explanatory.

Related

String manipulation with dynamic programming

I have a problem where I have a string of length N, where (1 ≤ N ≤ 10^5). This string will only have lower case letters.
We have to rewrite the string so that it has a series of "streaks", where the same letter is included at least K (1 ≤ K ≤ N) times in a row.
It costs a_ij to change a single specific letter in the string from i to j. There are M different possible letters you can change each letter to.
Example: "abcde" is the input string. N = 5 (length of "abcde"), M = 5 (letters are A, B, C, D, E), and K = 2 (each letter must be repeated at least 2 times) Then we are given a M×M matrix of values a_ij, where a_ij is an integer in the range 0…1000 and a_ii = 0 for all i.
0 1 4 4 4
2 0 4 4 4
6 5 0 3 2
5 5 5 0 4
3 7 0 5 0
Here, it costs 0 to change from A to A, 1 to change from A to B, 4 to change from A to C, and so on. It costs 2 to change from B to A.
The optimal solution in this example is to change the a into b, change the d into e, and then change both e’s into c’s. This will take 1 + 4 + 0 + 0 = 5 moves, and the final combo string will be "bbccc".
It becomes complicated as it might take less time to switch from using button i to an intermediate button k and then from button k to button j rather than from i to j directly (or more generally, there may be a path of changes starting with i and ending with j that gives the best overall cost for switching from button i ultimately to button j).
To solve for this issue, I am treating the matrix as a graph, and then performing Floyd Warshall to find the fastest time to switch letters. This will take O(M^3) which is only 26^3.
My next step is to perform dynamic programming on each additional letter to find the answer. If someone could give me advice on how to do this, I would be thankful!
Here are some untested ideas. I'm not sure if this is efficient enough (or completely worked out) but it looks like 26 * 3 * 10^5. The recurrence could be converted to a table, although with higher Ks, memoisation might be more efficient because of reduced state possibilities.
Assume we've recorded 26 prefix arrays for conversion of the entire list to each of the characters using the best conversion schedule, using a path-finding method. This lets us calculate the cost of a conversion of a range in the string in O(1) time, using a function, cost.
A letter in the result can be one of three things: either it's the kth instance of character c, or it's before the kth, or it's after the kth. This leads to a general recurrence:
f(i, is_kth, c) ->
cost(i - k + 1, i, c) + A
where
A = min(
f(i - k, is_kth, c'),
f(i - k, is_after_kth, c')
) forall c'
A takes constant time since the alphabet is constant, assuming earlier calls to f have been tabled.
f(i, is_before_kth, c) ->
cost(i, i, c) + A
where
A = min(
f(i - 1, is_before_kth, c),
f(i - 1, is_kth, c'),
f(i - 1, is_after_kth, c')
) forall c'
Again A is constant time since the alphabet is constant.
f(i, is_after_kth, c) ->
cost(i, i, c) + A
where
A = min(
f(i - 1, is_after_kth, c),
f(i - 1, is_kth, c)
)
A is constant time in the latter. We would seek the best result of the recurrence applied to each character at the end of the string with either state is_kth or state is_after_kth.

please explain me this code about matrix in python

Hey I was trying to write a program to find the sum of two matrices A and B and store the sum in another matrix C but I am not getting this code of Declaring Matrices with for loop
r = int(input('Enter no. of rows of matrix A'))
c = int(input('Enter the number of colm. of matrix B'))
r1 = int(input('Enter the number of rows of Matrix B'))
c1 = int(input('Enter the number of colm. of Matrix B'))
#declaring of matrices
A = [[0 for x in range(c)]for x in range(r)]
B = [[0 for x in range(c1)]for x in range(r1)]

How do I solve two equations where one equation has a variable that takes a range of values?

Two equations ---
c + w + u = 50;
c-w/4 = 39
In the above, 'u' takes integer values from 0 to 11. (Why/How the 11? 11 = 50-39).
I need to output a table of values for c and w for each value of u starting with u = 0. The corresponding value of u must also appear across each row of values for c and w.
How do I write a VBA code for this?
Many thanks in advance!
Do a little algebra first:
c-w/4 = 39
c = 39+w/4
c+w+u = 50
39+w/4+w+u = 50
w/4+w+u = 50-39
w/4+w = 11-u
1.25*w = 11-u
w = (11-u)/1.25
Put the u values in column A. In B1 enter:
=(11-A1)/1.25
and copy down. In C1 enter:
=39+B1/4
and copy down.
The w values are in column B and the c values are in column C.

I want to solve a system of equations containing a*x

Is it possible to solve a system of three equations in Excel, that contain x*y??
Let's suppose that my unknowns are a,b,x
The equations are
a + b = 1
a * x - 20y = 0
10x * a - 20a + b = 0
Is there a way to express the multiplier that is one of my unknowns??
here you can take z=x*a then your equations become:
a + b = 1
z - 20y = 0
10z - 20a + b = 0
Now solve these equation using simple method given here. You get value of z and a. Now you can find value x. So finally you get value of a, b, z, y

How to perform FST (Finite State Transducer) composition

Consider the following FSTs :
T1
0 1 a : b
0 2 b : b
2 3 b : b
0 0 a : a
1 3 b : a
T2
0 1 b : a
1 2 b : a
1 1 a : d
1 2 a : c
How do I perform the composition operation on these two FSTs (i.e. T1 o T2)
I saw some algorithms but couldn't understand much. If anyone could explain it in a easy way it would be a major help.
Please note that this is NOT a homework. The example is taken from the lecture slides where the solution is given but I couldn't figure out how to get to it.
Since you didn't specify the input format, I'm assuming that 0 is the initial state, any integers that appear in the second column but not the first are accepting states (3 for T1 and 2 for T2), and each row is an element of the transition relation, giving the the previous state, the next state, the input letter and the output letter.
Any operation on FSTs needs to produce a new FST, so we need states, an input alphabet, an output alphabet, initial states, final states and a transition relation (the specifications of the FSTs A, B and W below are given in this order). Suppose our FSTs are:
A = (Q, Σ, Γ, Q0, QF, α)
B = (P, Γ, Δ, P0, PF, β)
and we want to find
W = (R, Σ, Δ, R0, RF, ω) = A ∘ B
Note that we don't need to determine the alphabets of W; the definition of composition does that.
Imagine running A and B in series, with A's output tape fed as B's input tape. The state of the combined FST is simply the combined states of A and B. In other words, the states of the composition are in the cross product of the states of the individual FSTs.
R = Q × P
In your example, the states of W would be pairs of integers:
R = {(0,0), (0,1), ... (3, 2)}
though we could renumber these and get (for example):
R = {00, 01, 02, 10, 11, 12, 20, 21, 22, 30, 31, 32}
Similarly, initial and accepting states of the composed FST are the cross products of those in the component FSTs. In particular, R accepts a string iff A and B both accept the string.
R0 = Q0 × P0
RF = QF × PF
In the example, R0 = {00} and RF = {32}.
All that remains is to determine the transition relationship ω. For this, combine each transition rule for A with every transition rule for B that might apply. That is, combine each transition rule of A (qi, σ) → (qj, γ) with every rule of B that has a "γ" as the input character.
ω = { ((qi,ph), σ) → ((qj, pk), δ) : (qi, σ) → (qj, γ) ∈ α,
(ph, γ) → (pk, δ) ∈ β}
In the example, this means combining (e.g.) 0 1 a : b of T1 with 0 1 b : a and 1 2 b : a of T2 to get:
00 11 a : a
01 12 a : a
Similarly, you'd combine 0 2 b : b of T1 with those same 0 1 b : a and 1 2 b : a of T2, 0 0 a : a of T1 with 1 1 a : d and 1 2 a : c of T2 &c.
Note that you might have unreachable states (those that never appear as a "next" state) and transitions that will never occur (those from unreachable states). As an optimization step, you can remove those states and transitions. However, leaving them in will not affect the correctness of the construction; it's simply an optimization.
If you are more amenable to graphical explanations, the following set of slides provides incremental, graphical examples of the composition algorithm in practice, and also includes discussion of epsilon transitions in the component transducers. Epsilon transitions complicate the composition process, and the algorithm described in outis answer may not generate the correct result in this case, depending on the semiring being used.
See slides 10~35 for some graphical examples:
http://www.gavo.t.u-tokyo.ac.jp/~novakj/wfst-algorithms.pdf
T1 and T2
Composition of T1 and T2
The states of the composition T are pairs of a T1 state and a T2 state. T satisfies the following conditions:
its initial state is the pair of the initial state of T1 and the initial state
of T2
Its final states are pairs of a final state of T1 and a final state of T2
There is a transition t from (q1, q2) to (r1, r2) for each pair of transitions T1 from q1 to r1 and T2 from q2 to r2 such that the output label of T1 matches the input label of T2. The transition T takes its input label from T1, its output label from T2, and its weight is the combination of the weights of T1 and T2 done with the same operation
that combines weights along a path.
Since there are no weights we can ignore this. Above was picked up exactly from a following beautiful paper. Link here

Resources