Re:$ (a,b,c,d)=(pr+qs, qr-ps,pr-qs,ps+qr) $ In the diophantine equation: $ a^2+b^2=c^2+d^2 $, I know that the solutions are parametrized by $ (a,b,c,d)=(pr+qs, qr-ps,pr-qs,ps+qr) $ where $p,q,r,s \in  {Z}$ are arbitrary. I have been having a hard time solving $(p,q,r,s)$ meaning writing them as a combination of the integers $a,b,c,d$).Can you please help me find the solution?
 A: To start I'd suggest removing any common factor of all of $a,b,c,d$ and then at the end you can multiply each of $p,q,r,s$ by that removed factor.
From your setup $(a,b,c,d)=(pr+qs, qr-ps,pr-qs,ps+qr)$ follows
$$pr=(a+c)/2 \\ qs=(a-c)/2 \\ qr=(d+b)/2 \\ ps=(d-b)/2 \tag{1}$$
First we see we must have $a,c$ either both even or both odd, and the same for $b,d$. This you can simply arrange to do because you may switch $a,b$ in the equation $a^2+b^2=c^2+d^2$, or similarly switch $c,d$.
Now note that from looking at say $pr$ and $qr$ we know that $r$ is a common factor of the two right sides from (1). I don't know if this always works, but it seems one choice is to make $r$ equal to the gcd of those two right sides. Then once $r$ is decided on, the other values $p,q,s$ can be found from equations $(1)$.
If you notice that for example the right sides of equations for $pr,qr$ happen to be coprime, then you know that it must be $r=\pm 1$ (no need to try both signs, since the negative only leads to each of $p,q,r,s$ getting their signs changed).
Here's an example where there weren't any such coprime right sides. Consider
$$7^2+22^2=23^2+2^2.$$
We get $pr=15,\ qs=-8,\ qr=12,\ ps=-10.$
From this we only know $r$ is a divisor of $3$, $s$ is a divisor of $2$, $p$ is a divisor of $5$, and $q$ is a divisor of $4$. That technically leaves a lot of combinations to try, most going nowhere. But starting with for example $r$ being a divisor of $3$ and deciding to take it as actually equal to $3$ (i.e. $\gcd(15,12)$ ) we calculate then $p=5,\ q=4,\ s=-2$ and this choice of $p,q,r,s$ indeed leads back to $(a,b,c,d)=(7,22,23,2).$
ADDED: One can get to the parametrization by use of the "fraction fact" that if $r,s$ are positive integers and the fraction $r/s$ is in reduced form $p/q$ with $\gcd(p,q)=1$ and $p,q$ positive, then there is a unique positive integer $k$ such that $r=kp,\ s=kq.$
So looking at equations $(1)$ we note we've already assumed that $a,c$ are equal mod $2$ as are $b,d$. We may switch sides if necessary and assume also $a>c,d>b$ so that, if we write $w,x,y,z$ for the four right sides in $(1)$ in that order, we have $wx=yz$ following from $a^2+b^2=c^2+d^2,$ and each of $w,x,y,z$ is positive.
Now from $w/y=z/x=p/q$ with $p,q$ positive and coprime, we can apply the fraction fact to define $w=rp,y=rq,z=sp,x=sq.$ Then we have
$$ a=w+x=pr+qs,\\ b=y-z=qr-ps,\\ c=w-x=pr-qs,\\ d=z+y=ps+qr.$$
This matches the required form. Note that if originally each of $a,b,c,d$ is odd we may switch some terms and get conceivably different parameter values to get the same solution. In other words the parametrization may not be unique.
