0

I am new to Rcpp so I apologize in advance if this question is simple to answer. I searched on the web but couldn't find much help and I am hoping the savviness in this forum can help me!

I have an existing code in R using Rcpp, and I need to add to this code the following. I have a quadratic function in two variables, f(x, y), and I need to find the zeros of it:

f(x, y) = (x + by + c)' W (x + by + c)

where the unknowns are x and y. That is, I am interested in finding the set of pairs (x, y) that satisfy f(x , y)=0.

Note: This is a simulation exercise where I need to find the zeros of this function for different values of a, b, c and W. Therefore, I need to code this in a mechanical way (cannot just find the solution, for instance, by graphical inspection). Both variables are continue, and I don't want to work with a grid for (x,y) to see when f(x,y)=0. I need a more general/optimization solution. I don't really know what values (x,y) can take.

coatless
  • 20,011
  • 13
  • 69
  • 84
DSSY
  • 1
  • 2
    So, you need a numerical optimization routine. Try [RcppNumerical](https://github.com/yixuan/RcppNumerical#numerical-optimization). – coatless Jan 31 '17 at 22:44
  • Well, I don't need to minimize the function. I need to find the zeros or roots... – DSSY Feb 01 '17 at 16:15
  • Demerit points for cross-posting rcpp-devel, particularly as you failed to give any more relevant details. – Dirk Eddelbuettel Feb 01 '17 at 22:28
  • What do the notations stand for in your function? Are x, y scalars? Or vectors? – yixuan Feb 02 '17 at 00:29
  • I cross posted to reach out to more people. I thought rcpp-devel may be a more specific audience. I really need help! – DSSY Feb 02 '17 at 14:26
  • Good question. In the general problem, x and y are vectors, and W is a matrix. Dimensions are such that f(x,y) is scalar. I can simplify the problem and assume everything is scalar, including W. It will give a quadratic form in two scalar variables. – DSSY Feb 02 '17 at 14:41

1 Answers1

2

Before diving into the numerical part I think you should define this question in a better way. Here I assume that x, y, and c are vectors, and b is a scalar.

A quick observation is that, if W is positive definite, then f(x, y) = 0 implies that x + by + c = 0. If both x and y are free variables, then the solution is not unique. For example, if (x, y) is a solution, then (x - b, y + 1) (element-wise operations) is also a solution.

If W is indefinite, then the equation also has multiple solutions. I just give a very simple example here. Imagine that W is a 2x2 diagonal matrix with 1 and -1 on the diagonal. Then as long as x + by + c = (t, t)' for any t, the function value is exactly zero.

In short, under my assumption on the notations, the equation has infinite number of solutions. I believe you need additional restrictions to make it unique.

yixuan
  • 774
  • 3
  • 6
  • Thank you for your reply. I know the equation has no unique solution. That is why I am interested in finding the **set** of pairs (x, y) that satisfy f(x , y)=0. W is a non-singular positive semi-definite matrix. There is a lot of math behind the problem, which I cannot write here (derivations are long and complex). I tried to give you the essence of the problem: find the roots of a quadratic form in two variables. This solution should contemplate all possible cases, as you would do with one variable based on the discriminant. I hope this helps. – DSSY Feb 02 '17 at 20:49
  • 3
    "W is a non-singular positive semi-definite matrix." -- Doesn't this imply that W is actually positive definite? – yixuan Feb 03 '17 at 15:11