Say i'm trying to evaluate the Polynomial:
x^2 + 1
Using the Fast Fourier transform method for evaluating co-efficients. Now i can change this into matrix/vector form using the co-effcient as inputs for the fast fourier transform:
so:
x^2 + 1 = <1, 0, 1, 0>
This is done by using the coefficient value e.g 1 = 1, 0x^1 = 0, X^2 = 1 and so on
Now we get to the bit where i'm totally confused. I'm meant to use the vandermonde matrix :Vandermonde matrix ~ Wiki to evaluate these values into FFT Form using the matrix:
1 1 1 1
1 i-1-i
1-1 1-i
1-i 1 i
The output of
fft(1,0,1,0)
is
(2,0,2,0)
Now thats the step i don't quite understand, how did we use that matrix to get (2,0,2,0)?
First, your Vandermonde matrix is incorrect. The (4,3) entry should be -1, not 1, since the fourth row should be (-i)0, (-i)1, (-i)2, (-i)3. Note in particular that
(-i)*(-i) = (-1)2 * i2 = i2 = -1.
With this correction, the result follows from multiplying the Vandermonde matrix by the column vector (1,0,1,0).
Maybe you could explain what your overall goal is here. I have never heard of FFTs being used to evaluate polynomials. They are used to multiply polynomials, or to convolve signals (an equivalent task), but I wouldn't bother unless the polynomials/signals have a large number of terms. x2 + 1 isn't large. 16 terms is not large, and even 64 or 256 terms is probably better done by straightforward O(N2) techniques.
Discrete Fourier Transforms use the matrix Mij = ωij where ω is the Nth complex root of 1 and column/row numbering goes from 0 to N-1.
Fast Fourier Transforms never use this matrix directly, they are heavily optimized to use a divide-and-conquer technique (Cooley-Tukey algorithm) to calculate the end result through stages of 2x2 DFTs in series and parallel.
If you write your vector as [0,1,0,1] instead of [1,0,1,0], I think you will see that if you multiply that by the matrix you gave, you'll get [0,2,0,2]. (Although you have an error, it's
1 1 1 1
1 i-1-i
1-1 1-1
1-i-1 i
) There must be some convention in the program you are using which reverses the order of the vector's coefficients.
Related
I have a data frame which contains decimal numbers in 5 columns. Try to think it as an excel file. For every column I find the sum. I provide an example below:
The problem is that I want to keep the optimal lines/observations that they will:
Have individual sum as much closer to 0 - for the col1, col2 and col3
AND simultaneously the col4 and col5 to sum (again individually) the closer to (let's say 3).
The example has created with a random sample, so it does not contain negative numbers, but I provided it as an example.
I think that problems of this case are solved via a Solver in R. I would like to find a code that solves the above problem.
I used before the package lpSolve with the function lp to solve linear programming problems with constraints. It will be easy to set up as you don't appear to have many constraints. However, because it is a single objective function, you need to define the objective function probably as the minimum difference of the sum of your 2 branches (read: Cols 1 to 3, and Cols 4 to 5).
Potentially there would be other methods but this one should be rather easy.
Hopefully this will help.
Regards
I think there are two obvious ways of doing this.
I'll write down the mathematical models that make more formal my interpretation of your problem.
Quadratic Formulation
The first is a least squares approach. Let
x(i) = 1 if row i is selected
0 otherwise
Then we can write:
min sum(j, w(j)*d(j)^2)
d(j) = sum(i, a(i,j)*x(i)) - t(j)
d(j) : free variable (can be substituted out if wanted)
where
t(j) : target sum for column j (0 and 3 in your example)
w(j) : weight for column j (choose 1 if there is no good reason to use something else)
a(i,j) : your data matrix (or data frame)
This is called a MIQP model (Mixed Integer Quadratic Programming). There are MIQP solvers available for R.
Linear Formulation
Instead of least squares we can choose to use least absolute deviations. A high-level model can look like:
min sum(j, w(j)*|d(j)|)
d(j) = sum(i, a(i,j)*x(i)) - t(j)
d(j) : free variable (can be substituted out if wanted)
To make this a proper MIP (Mixed Integer Programming) model we need to make everything linear. One possible formulation is:
min sum(j, w(j)*y(j))
d(j) = sum(i, a(i,j)*x(i)) - t(j)
-y(j) <= d(j) <= y(j) (we can write this as two inequalities)
d(j) : free variable
y(j) >= 0 (positive variable)
There are many MIP solvers available for use with R.
For complex valued data, I am finding it hard to ensure that the covariance matrix is positive definite. Taking an example,
P =
10.0000 +10.0000i 0 0
0 10.0000 +10.0000i 0
0 0 10.0000 +10.0000i
I can check for positive definiteness of P using the cholesky or the eigenvalues explained below.
(A)
[R1,p1] = chol(P)
R1 =
[]
p1 =
1
Since p1 > 0, A is not positive definite
(B) Using eigen values : if the eigenvalues are positive, then P should be positive definite.
[r p]=eig(P)
r =
1 0 0
0 1 0
0 0 1
p =
10.0000 +10.0000i 0 0
0 10.0000 +10.0000i 0
0 0 10.0000 +10.0000i
However, doing svd(P) gives all positive eigenalues !!
Where am I going wrong and what should I do to prevent the P matrix from becoming non positive definite. During run time and real world scenarios it is very hard to ensure the postive definiteness of P. Is there a hack or a way out? Thank you very much
Checking positive definiteness on complex matrices:
First of all, an answer to this question at math.stackexchange says that:
A necessary and sufficient condition for a complex matrix A to be
positive definite is that the Hermitian part
A_H = 1/2·(A+A^H) is positive definite,
where A^H denotes the conjugate transpose.
Why P matrix becomes non-positive definite:
Then, on the question of why P loses its "positive-definiteness", the usual culprit is floating point representation/arithmetic.
Standard Kalman filter algorithm can show numerical stability problems in some sensitive operations as taking the inverse of matrix S when calculating Kalman gain, or when applying optimizations as using the simplified expresion for error covariance on the update step P+ = (I - K·H)·P-.
There are other sources of error, as a buggy implementation or using wrong data (e.g. defining process/measure covariance matrices that are not positive definite themselves).
How to avoid the problem:
I will focus in the first source of error: numerical stability.
There are a number of alternatives commonly used to make Kalman filters more stable and avoid the covariance matrix problem:
Correct small errors in P on each iteration (formally not correct, but works nicely). I have successfully used the simplistic P = 1/2 (P + P') with real matrices in the past, but there are more ellaborate schemes.
Use a square-root Kalman filter, or any other formulation that improves stability. Since they keep and update a kind of square root of P, the assymetries are not a problem anymore. They also reduce the positive definite problem in general.
Go to 64 bit FP arithmetic. Much more stable than using single precision.
Improve numerical conditioning by scaling some variables. Large magnitude differences between numbers worsen the inaccuracies, so if P mixes in the diagonal values in the order of 10^+3 with other values in the order of 10^-6, then change the unit of the latter variable to micro-whatever to reduce the gap.
Using one or a combination of these factors could work. However, I have not worked with Kalman filters in the complex domain, so let me know how it works for your case.
I have an initial frame and a bounding box around some information. I have a transformation matrix T, for which I want to use to transform this bounding box.
I could easily apply the transformation and draw it in the output frame, but I would like to apply the transformation over a sequence of x frames, can anyone suggest a way to do this?
Aly
Building on #egor-n comment, you could compute R = T^{1/x} and compute your bounding box on frame i+1 from the one at frame i by
B_{i+1} = R * B_{i}
with B_{0} your initial bounding box. Depending on the precise form of T, we could discuss how to compute R.
There are methods for affine transforms - to make decomposition of affine transform matrix to product of translation, rotation, scaling and shear matrices, and linear interpolation of parameters of every matrix (for example, rotation angle for R and so on). Example
But for homography matrix there is no single solution, as described here, so one can find some "good" approximation (look at complex math in that article). Probably, some limitations for possible transforms could simplify the problem.
Here's something a little different you could try. Let M be the matrix representing the final transformation. You could try interpolating between I (the identity matrix, with 1's on the diagonal and 0's elsewhere) using the formula
M(t) = exp(t * ln(M))
where t is time from 0 to 1, M(0) = I, M(1) = M, exp is the exponential function for matrices given by the usual infinite series, and ln is the similar natural logarithm function for matrices given by the usual infinite series.
The correctness of the formula depends on the type of transformation represented by M and the type of transformations allowed in intermediate steps. The formula should work for rigid motions. For other types of transformations, various bad things might happen, including divergence of the logarithm series. Other formulas can be used in other cases; let me know if you're using transformations other than rigid motions and I can give some other formulas.
The exponential and logarithm functions may be available in a matrix library. If not, they can be easily implemented as partial sums of infinite series.
The above method should give the same result as some quaternion methods in the case of rotations. The quaternion methods are probably faster when they're available.
UPDATE
I see you mention elsewhere that your transformation is a homography (perspectivity), so the method I suggested above for rigid motions won't work. Instead you could use a different, but related method outlined in ftp://ftp.cs.huji.ac.il/users/aristo/papers/SYGRAPH2005/sig05.pdf. It goes as follows: represent your transformation by a matrix in one higher dimension. Scale the matrix so that its determinant is equal to 1. Call the resulting matrix G. You want to interpolate from the identity matrix I to G, going through perspectivities.
In what follows, let M^T be the transpose of M. Let the function expp be defined by
expp(M) = exp(-M^T) * exp(M+M^T)
You need to find the inverse of that function at G; in other words you need to solve the equation
expp(M) = G
where G is your transformation matrix with determinant 1. Call the result M = logp(G). That equation can be solved by standard numerical techniques, or you can use Matlab or other math software. It's somewhat time-consuming and complicated to do, but you only have to do it once.
Then you calculate the series of transformations by
G(t) = expp(t * logp(G))
where t varies from 0 to 1 in steps of 1/k, where k is the number of frames you want.
You could parameterize the transform over some number of frames by adding a variable with a domain greater than zero but less than 1.
Let t be the frame number
Let T be the total number of frames
Let P be the original location and orientation of the object
Let theta be the total rotation angle
and translation be the vector [x,y]'
The transform in 2D becomes:
T(P|t) = R(t)*P +(t*[x,y]')/T
where R(t) = {{Cos((theta*t)/T),-Sin((theta*t)/T)},{Sin((theta*t)/T),Cos((theta*t)/T)}}
So that at frame t_n you apply the transform T(t) to the position of the object at time t_0 = 0 (which is equivalent to no transform)
I'm basically looking for a summation function that will compute multinomials given the number of variables and a degree.
Example
2 Variables; 2 Degrees:
x^2+y^2+x*y+x+y+1
Thanks.
See Knuth The Art of Computer Programming, Vol. 4, Fascicle 3 for a comprehensive answer.
Short answer: it's enough to generate all multinomial expressions in n variables with degree exactly d. Then, for your problem, you can either put together the answers with degrees ≤d, or add a dummy variable "1".
The problem of generating all expressions with degree exactly d is thus simply one of generating all ordered partitions (i.e., all nonnegative integer solutions to x1 + ... + xn = d), and this can be done with a simple backtracking algorithm. ("Depth-first search")
Given N variables, and a maximum degree of D, you have an array of D slots to fill with all possible combinations of variables.
[_, _, ..., _, _]
You are allowed to fill the slots with any of the N variables any number <= D times total. Since multiplication is commutative, it suffices to not care about ordering of variables. As such, this problem is reduced to generating (1) partitions of an integer and (2) subsets of a set.
I hope this is at least a start to your solution.
This also seems to be a Dynamic programming variant of the 0-1 Knapsack problem. Here we would be interested in all possible leaves of the decision tree.
I wonder if it is possible (and if it is then how) to re-present an arbitrary M3 matrix transformation as a sequence of simpler transformations (such as translate, scale, skew, rotate)
In other words: how to calculate MTranslate, MScale, MRotate, MSkew matrices from the MComplex so that the following equation would be true:
MComplex = MTranslate * MScale * MRotate * MSkew (or in an other order)
Singular Value Decomposition (see also this blog and this PDF). It turns an arbitrary matrix into a composition of 3 matrices: orthogonal + diagonal + orthogonal. The orthogonal matrices are rotation matrices; the diagonal matrix represents skewing along the primary axes = scaling.
The translation throws a monkey wrench into the game, but what you should do is take out the translation part of the matrix so you have a 3x3 matrix, run SVD on that to give you the rotation+skewing, then add the translation part back in. That way you'll have a rotation + scale + rotation + translate composition of 4 matrices. It's probably possible to do this in 3 matrices (rotation + scaling along some set of axes + translation) but I'm not sure exactly how... maybe a QR decomposition (Q = orthogonal = rotation, but I'm not sure if the R is skew-only or has a rotational part.)
Yes, but the solution will not be unique. Also you should rather put translation at the end (the order of the rest doesn't matter)
For any given square matrix A there exists infinitely many matrices B and C so that A = B*C. Choose any invertible matrix B (which means that B^-1 exists or det(B) != 0) and now C = B^-1*A.
So for your solution first decompose MC into MT and MS*MR*MSk*I, choosing MT to be some invertible transposition matrix. Then decompose the rest into MS and MR*MSk*I so that MS is arbitrary scaling matrix. And so on...
Now if at the end of the fun I is an identity matrix (with 1 on diagonal, 0 elsewhere) you're good. If it is not, start over, but choose different matrices ;-)
In fact, using the method above symbolically you can create set of equations that will yield you a parametrized formulas for all of these matrices.
How useful these decompositions would be for you, well - that's another story.
If you type this into Mathematica or Maxima they'll compute this for you in no time.