# How to solve quadratic equation in 4 variables?

I am trying to solve the equation $a^2 + b^2 + c^2 + d^2 = \alpha$ where alpha is a constant and $a, b, c$, and $d$ are the $4$ variables. I need to find some integer values of $a, b, c$ and $d$ which satisfies this equation

I know that I can make $a, b$, and $c$ as $0$ and then $d = \sqrt\alpha$ but this is one very trivial solution.

Another approach which I have in mind is to set $a, b$ as constant and then solve for $c$ and $d$ using brute-force method but that again is very computation intensive method of doing this.

Is there any better way of solving this

• This is the equation of a sphere in four dimensions. There are infinitely many solutions, so your question is not very clear Jun 1 '16 at 11:14
• I basically want to find some random point (a1,b1,c1,d1) which satisfies this Jun 1 '16 at 11:15
• are you looking for real or integer solutions? Jun 1 '16 at 11:16
• there is a nontrivial theorem on numbers that can be represent as sum of 4 squares: en.wikipedia.org/wiki/Lagrange's_four-square_theorem Jun 1 '16 at 11:20
• I think this is a good reference.
– N74
Jun 1 '16 at 13:35

The integer solutions to $a^2+b^2+c^2+d^2=n$ for a positive integer $n$ can be found by using the norm for the Hurwitz integers, see here.

There is an infinity number of solutoins, but you could set $a=b=c=d =\sqrt(\alpha/4)$.

As @Aditya Singh state in a comment to his question "I basically want to find some random point (a1,b1,c1,d1) which satisfies this" I suggest the following algorithm to find (more or less) random points:

1. Take/sample a value for $a$ between $\pm \sqrt(\alpha)$
2. Take/sample a value for $b$ between $\pm \sqrt(\alpha - a^2)$
3. Take/sample a value for $c$ between $\pm \sqrt(\alpha - a^2 - b^2)$
4. Finally $d = \pm \sqrt(\alpha -a^2 - b^2 - c^2)$

in R:

alpha <- 5

a1 <- runif(1, min = -sqrt(alpha), max = sqrt(alpha))
b1 <- runif(1, min = -sqrt(alpha - a1^2), max = sqrt(alpha - a1^2))
c1 <- runif(1, min = -sqrt(alpha - a1^2 - b1^2), max = sqrt(alpha - a1^2 - b1^2))
d1 <- sqrt(alpha - a1^2 - b1^2 - c1^2) * sample(c(-1, 1), 1)

a1^2 + b1^2 + c1^2 + d1^2 #gives 5


As he later stated in another comment, that he need integers as solution, he might want to read https://stackoverflow.com/questions/11732555/how-to-find-all-possible-values-of-four-variables-when-squared-sum-to-n or https://cs.stackexchange.com/questions/2988/how-fast-can-we-find-all-four-square-combinations-that-sum-to-n.

• Or he could set $$\begin{cases} a=\sqrt\alpha\cos(29)\\ b=\sqrt\alpha\sin(29)\cos\left(\sqrt\pi\right)\\ c=\frac{\sqrt{3\alpha}}2\sin(29)\sin\left(\sqrt\pi\right)\\ d=-\frac{\sqrt\alpha}2\sin(29)\sin\left(\sqrt\pi\right)\end{cases}$$ How is this observation sensible? The poster said in the comments he needs to find random points.
– user228113
Jun 1 '16 at 11:28

You need 4 equations to solve for 4 variables, or in general n equations to solve a n degree polynomial. But if you have no other equations, only thing that can be done is putting values and verifying

You can chose any value for $b,c,d$ and find: $$a=\pm \sqrt{\alpha-(b^2+c^2+d^2)}$$

obviously $a$ is a real number only if $(b^2+c^2+d^2)\le \alpha$.

• Yeah. But I have mentioned this solution already in question but for 2 variables and then solving for c and d. Is there any other alternative? Jun 1 '16 at 11:16
• In your question you say that we can set $a,b$ and solve for $c,d$. This a bit different. Obviously you can also solve, in the same manner, for $b$ or $c$ or $d$, but the point is that a solution does depend on the other three variables that can be chosen as we want ( at random). Jun 1 '16 at 11:21

To find a "random point" $(a,b,c,d)$ (as you say in a comment) satisfying $a^2+b^2+c^2+d^2= \alpha$, uniformly on the hypersphere surface:

• generate $W,X,Y,Z$ independently each normally distributed $N(0,1)$
• find $r = \sqrt{ W^2+X^2+Y^2+Z^2}$
• set $a=\dfrac{\sqrt{\alpha}}{r}W$, $b=\dfrac{\sqrt{\alpha}}{r}X$, $c=\dfrac{\sqrt{\alpha}}{r}Y$, $d=\dfrac{\sqrt{\alpha}}{r}Z$

but these are unlikely to be integers

• Thanks for your reply. But I need them to be integers Jun 1 '16 at 11:39