# Re: [R] Solve for x in Ax=B with vectors, not matrices

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Mon 23 Jan 2006 - 05:27:22 EST

On 22-Jan-06 Lapointe, Pierre wrote:
> Hello R-helpers,
>
> What I have: I am working with vectors not matrice:
>

```>#Basic equations

> A <-c(-20,-9,-2)

```
> x <-c(0.17,0.22,0.61)
> B <- crossprod(A,x)
>
```># R matrix multiplication works with vectors

> A%*%x==B      # Is true...

```
>
> Question: If x is unknown and A and B are known,
> how do I solve for x in R?
> solve(A,B) won't work because A is not a square matrix
>
> solve(A,B)
> Error in solve.default(A, B) : 'A' (3 x 1) must be square
>
> I understand that I might have many solutions but note
> that the sum of x is 1 and all x are positive (x are
> weightings in % of the total).

For the example you have given, in "classical" vector algebra notation the equation is

A.x = B [ = -6.6 in this case ]

where A and x are two vectors.

Note the explanataion resulting from ?"%*%"

```     Multiplies two matrices, if they are conformable.
If one argument is a vector, it will be coerced
to a either a row or column matrix to make the
two arguments conformable. If both are vectors it
will return the inner product.

```

If this is the interpretation you intend, and if the above is a typical problem of yours, then if you divide by the "lengths" of A and x you will get an equation

V.y = cos(u)

where V (corresponding to A) and y (corresponding to x) are unit vectors, and cos(u) corresponds to B.

Now you want to find solutions y from this equation. You are in the first instance looking for all vectors y which are at a fixed angle u in (0,pi) to the vector V (the "elevation", if you like), which you can find by choosing another angle v (the "azimuth", say) arbitrarily in (0,2*pi). All such angles give a solution of V.y = cos(u), and the endpoints of the vectors describe a circle.

You can get back to x by re-scaling, and now you want the solutions such that the sum of the elements is 1

```(which defines a plane). There are either two of these
(where the circle cuts the plane, or infinitely many
(if the circle in question lies in the plane).

```

Does this outline point in s useful direction?

Best wishes,
Ted.

E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
```Date: 22-Jan-06                                       Time: 18:27:18
------------------------------ XFMail ------------------------------

______________________________________________
```
R-help@stat.math.ethz.ch mailing list