A pattern that leads to regular continued fractions of quadratic irrationals The following expression can be obtained by converting the continued fraction of quadratic irrationals to single fraction.
$$
\sqrt{N} = \frac{b\sqrt{N}+aN}{a\sqrt{N}+b}
$$
The equation holds for any values of $a$ and $b$. However, by calculating from the regular continued fraction, $a$ and $b$ are restricted to unique pair for $N$.
While investigating how to infer these $a$ and $b$ from $N$, I found the following properties under limited conditions. (not proven)
Let $N$ be expressed as follows.
$$
N=p^2\pm q \ \ \ \ (p,q \in \mathbb{N}, 1 < q<p)
$$
When $q$ divides $2p$, we obtain $a$ and $b$ as follows.
$$
a = \frac{2p}{q},\ b=ap\pm1
$$
I would like to know why this is happening. If it is already known, please point it out. Thank you.

concrete example
The regular continued fraction of $\sqrt{7}$ is
$$
\sqrt{7} = 2 + \frac{1}{1+\frac{1}{1+\frac{1}{1+\frac{1}{2+\sqrt{7}}}}}
$$
Return to the single fraction.
$$
\sqrt{7} = \frac{8\sqrt{7}+21}{3\sqrt{7}+8}
$$
$(a,b)=(3,8)$ is uniquely determined for $N=7$.
On the other hand, $7$ can be expressed as follows.
$$
7=p^2-q=3^2-2
$$
Since $q=2$ divides $2p=6$, we can obtain $a$ and $b$ as follows.
$$
a = \frac{6}{2} = 3,\ \  b = 3\cdot3-1 = 8
$$
a,b in N=1~50
 A: your matrix is the generator of the (oriented)  automorphism group of  the quadratic form $x^2 - N y^2.$  Given  the smallest positive (nonzero) $u,v$  solving the Pell equation $u^2 - N v^2 = 1,$   your matrix  is
$$
A =
\left(
\begin{array}{cc}
u & Nv \\
v & u
\end{array}
\right)
$$
and your fraction is
$$   \frac{u \sqrt N  + v N}{v \sqrt N + u}   $$
Without matrices, taking integers $x,y,$   we can confirm that
$$ (ux + Nv y)^2 - N(vx + uy)^2 = x^2 - N y^2   $$
This identity is the generalization of "Vieta Jumping,"  which is the phrase used in contests  for automorphisms of any form $f(x,y)=x^2 - kxy + y^2 .$  Let's see, for the general indefinite form  $g(x,y) = a x^2 + bxy + c y^2$  with $ \Delta = b^2 - 4ac > 0$  but $\Delta$ not a square,  there is an expression for the generating automorphism    using nontrivial solutions to $ \sigma^2 - \Delta \tau^2  = 4.$      Also, some forms have automorphisms of determinant $-1;$  in your $x^2 - N y^2 $  such a thing is $(x,y)   \mapsto (-x,y) $
