0

Given a complex number with rational components, I want to check if its square root also has rational components, and if so calculate the value. For example, given $-\frac{119}{225}+\frac{8}{15}i$ I want to get $\frac{1}{3}+\frac{4}{5}i$.

For some context, I'm writing an implementation of the Scheme programming language, which has exact and inexact numbers. A square root of an exact number should be returned as an exact number itself, if possible. And in this context, exact basically means rational, because everything else has to be expressed as floating point numbers which are inexact.

Elektito
  • 109
  • 1
    There is a formula for the cartesian form of the square roots of a complex number, in terms of the usual real square root $\sqrt\bullet:[0,\infty)\to[0,\infty)$. The formula is $$\sqrt{x+iy}=\begin{cases}\pm\left(\sqrt{\frac{x+\sqrt{x^2+y^2}}2}+i\frac{y}{\lvert y\rvert}\sqrt{\frac{-x+\sqrt{x^2+y^2}}2}\right)&\text{if }y\ne 0\ \pm\sqrt x&\text{if }y=0\land x\ge0\ \pm i\sqrt x&\text{if }x<0\land y=0\end{cases}$$ – Sassatelli Giulio Aug 30 '23 at 13:35
  • It can be derived by solving $(a+ib)^2=a^2-b^2+2iab=x+iy$, which then becomes the system $\begin{cases}a^2+(-b^2)=x\ a^2(-b^2)=-\frac14y^2\end{cases}$. This provides four solutions $(a,b)$, and depending on whether or not $y>0$ or $y<0$ you have to pick the pairs where $a,b$ have the same sign or opposite sign, respectively. – Sassatelli Giulio Aug 30 '23 at 13:41
  • Thanks. I guess I can use that to work out if both components are rational or not. Can you post this as an answer so I can accept it? – Elektito Aug 30 '23 at 14:05
  • Already implemented it, and it seems to be working just fine! – Elektito Aug 30 '23 at 14:42
  • I won't. If anybody wants to expand my comment into an answer, they can. – Sassatelli Giulio Aug 30 '23 at 17:20
  • I was not looking for how the square root of a complex number is calculated in general. I had already read that on wikipedia. Now maybe working out how to get from there to my specific question is obvious to others, but it was not to me. The comment above, even it it's also a way of calculating root of a complex number in general, did make things work for me. The answers on the other question, did not. – Elektito Aug 30 '23 at 18:22
  • @Elektito The formula in the first comment here is the same one as in this answer under the duplicate question, and is equivalent to the one in the top-voted answer under that question. – dxiv Aug 31 '23 at 03:16
  • Yes, but the comment also isn't a complete answer to my question. It needs expansion, which Sassatelli Guilio, as noted above, does not want to spend time doing (perfectly fine by me). I might be able to do it myself, but I'm not quite sure how to do that. I've already formulated it as a program, but for here I should somehow formulate it as math, which is not my forte exactly! :) – Elektito Aug 31 '23 at 05:28
  • @Elektito If you had read all answers under the duplicate question you'd have found what you are asking for, in several shapes and forms. If that's still not what you are after, then you'll need to provide more context as to what exactly your background is, what the shortcomings are that you find in the other answers, and what kind of different answer you are looking for. – dxiv Aug 31 '23 at 06:48
  • I thought I'd put that in title and question: I needed to be able to detect when the root also has rational components. And also calculate it. And the context is that I'm writing it as a program, so I can't really work with irrational numbers, or even properly detect them. But if you still think it's not appropriate go ahead and close the question. I have gotten what I wanted already. I don't feel like explaining myself even more. And no wonder this place is considered toxic to newcomers by everyone. I do thank
    Sassatelli Giulio for pointing me to the right direction though.
    – Elektito Aug 31 '23 at 06:56

1 Answers1

1

$-\frac{119}{225}+\frac{8}{15}i=\frac{1}{225}(-119+120i)$

Let us use the classical method to find the two complex numbers $\pm(a+bi)$ satisfying $$(a+ib)^2=-119+120i.$$ The equation is equivalent to the system $\begin{cases} a^2-b^2&=&-119& \quad \text{real parts}\\ 2ab&=&120& \quad \text{imaginary parts}\\ a^2+b^2&=&\sqrt{119^2+120^2}=169& \quad \text{absolute values} \end{cases}$

The solutions are $$a+bi=\pm(5+12i)$$

As $225=15^2,$ we get$$-\frac{119}{225}+\frac{8}{15}i=\left(\frac{1}{15}(5+12i)\right)^2=\left(\frac{1}{3}+\frac{4}{5}i\right)^2,$$ the other square root of the given number is opposite, e.g. $\left(-\frac{1}{3}-\frac{4}{5}i\right).$

user376343
  • 8,311
  • Is my conclusion correct that, given rational $p$ and $q$, the square root of $p+qi$ has rational parts, if and only if $p^2+q^2$ is a square? – Elektito Sep 01 '23 at 14:33
  • ... $p^2+q^2$ is a square of a rational number. – user376343 Sep 01 '23 at 17:03
  • Is it? I must be really rusty in math! :D So...how can we get a test our of this for my question? I've already implemented this of course, but in my implementation I'm sorta cheating using the type system. I want to see if there's a more mathematical way of checking whether the square root also has rational components. – Elektito Sep 02 '23 at 05:06