Quadratic form in R

480 views Asked by At

This is probably straightforward, elementary and whatever, but I can't manage to get it. I have 2 Nx1vectors u and w, which are composed by both negative and positive values. I am trying to compute w'u u'w , which should be a quadratic form. I should be able to write this like

t(w)%*%u%*%t(u)%*%w

However sometimes I get a negative value, depending on the values in the two vectors. This is not possible, since that thing is a quadratic form. I tried with

crossprod(w, u)%*%crossprod(u, w)

and

crossprod(w, u)*crossprod(u, w)

which gives positive and equal results. However, since I am dealing with Nx1vectors, I should also be able to write it as

`sum(w*u)^2`

which gives a positive value but different from the ones above.

So I guess I am doing something wrong somewhere. So, the question is: how can I express w'u u'w which is valid for both vectors and matrices ?

EDIT: here a csv file with the original vectors to reproduce exactly the same issue

0

There are 0 answers