![]() ![]() Waitbar(((i - 1) * nsizes + j) / (nrepeats * nsizes), h) Me圎rrMsgTxt("The second input argument must be a column vector of proper size.") Įtimes(j, 1) = etimes(j, 1) + toc(start) Įtimes(j, 2) = etimes(j, 2) + toc(start) Įrror('"x'' * A * x" is not equal to "quadraticform(A, x)"') ![]() Me圎rrMsgTxt("The first input argument must be a quadratic matrix.") Me圎rrMsgTxt("Too many output arguments.") Me圎rrMsgTxt("Two input arguments required.") Void mexFunction(int nlhs, mxArray *plhs, int nrhs, const mxArray *prhs) I've written the function "quadraticform" in C, as a Matlab extension: // y = quadraticform(A, x) So, I think that the calculation of quadratic forms cannot be done faster than O(n^2), and since this can also be achieved by the formula y=x'*A*x, there would be no real advantage of a special "quadraticform" function. But this does not really help: the running time of the calculation of y=x'*A*x is still O(n^2). However, there is a faster way to sum our n*(n+1)/2 different terms: for every i, we can factor out x(i), which means that only n*(n-1)/2+3*n multiplications is enough. It is easy to see that the naive calculation of x'*A*x, that is, calculating z=A*x and then y=x'*z, also needs n*(n+1) multiplications. Each has two floating-point multiplications, so a naive method would need n*(n+1) multiplications in total. Due to symmetry, every term appears twice in the sum, except for those where i equals j. A is symmetric: A(i,j) = A(j,i) for all i and j. Y=x'*A*x can be written as a sum of n^2 terms A(i,j)*x(i)*x(j), where i and j runs from 1 to n (where A is an nxn matrix). ![]() I couldn't find such a built-in function, and I have an idea why. ![]()
0 Comments
Leave a Reply. |