TITLE: Maps of primitive vectors and Conway's river, has anyone built this in SAGE?
QUESTION [6 upvotes]: I am attempting to teach number theory from John Stillwell's Elements of Number Theory in the upcoming semester. There are two sections (5.7 and 5.8) which describe the diagrammatic method for the derivation of primitive vectors which ultimately lead to a healthy understanding of values which the quadratic form $x^2-ny^2$ may attain for fixed $n$ and integers $x,y$.  The "river" is a particular path in this "tree of integral bases" which separates positive and negative values for the quadratic form. Here is an example:
an example from David Vogan of MIT
To be fair, there is a good discussion in Stillwell, my question is simply this:

Has anyone implemented a routine, command, etc. which produces some part of the integral tree of bases or the more interesting diagrams as shown in section 5.8 of Stillwell?

I'm more inclined to cover it if I can create examples without falling prey to the inevitable arithmetic mistakes I will make in the creation of such a diagram. Also, for the homework, it would really be nice for them to be able to play around with it without investing too much time. 
Thanks in advance for your help!

REPLY [2 votes]: I was surprised to learn, recently, that a simple idea ( made up, it appears, by Tito Piezas, without him knowing it was a neologism) allowed me to get the information in Conway's topograph with a fairly simple computer program, as long as what I wanted was to guarantee finding all solutions $(x,y)$ to $ax^2 + bxy+ cy^2 = n$ with integers $x,y > 0$ and $a,b,c,n$ fixed, $b^2 - 4ac>0$ but not a square. It seems the first answer where I displayed this material was If $d>1$ is a squarefree integer, show that $x^2 - dy^2 = c$ gives some bounds in terms of a fundamental solution. and has good explanations, while Tito's comments began with Does the Pell-like equation $X^2-dY^2=k$ have a simple recursion like $X^2-dY^2=1$?  in the thread  Does the Pell-like equation $X^2-dY^2=k$ have a simple recursion like $X^2-dY^2=1$?
There are a few programs involved for me, as one must tell the final program the fundamental solution to a relevant Pell equation. Anyway, you are welcome to the programs. For me, it means I can give all solutions, then draw the topograph when time permits. My first program on this is restricted to $x^2 - d y^2 = t,$ and it must be told the "fundamental" solution to $x^2 - d y^2 = 1.$
The output below are the two problems for which I posted topograph diagrams in my earlier answer
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

jagy@phobeusjunior:~/old drive/home/jagy/Cplusplus$ ./Pell 8

Pell unit 
3^2 - 8 * 1^2 = 1 

=========================================

jagy@phobeusjunior:~$ ./Pell_Target_Fundamental

  3^2 - 8 1^2 = 1

 x^2 - 8 y^2 = -7

Fri Apr 29 11:15:41 PDT 2016

x:  1  y:  1 ratio: 1  SEED 
x:  5  y:  2 ratio: 2.5  SEED 
x:  11  y:  4 ratio: 2.75
x:  31  y:  11 ratio: 2.818181818181818
x:  65  y:  23 ratio: 2.826086956521739
x:  181  y:  64 ratio: 2.828125
x:  379  y:  134 ratio: 2.828358208955223
x:  1055  y:  373 ratio: 2.828418230563003
x:  2209  y:  781 ratio: 2.82842509603073
x:  6149  y:  2174 ratio: 2.828426862925483
x:  12875  y:  4552 ratio: 2.828427065026362
x:  35839  y:  12671 ratio: 2.828427117038907
x:  75041  y:  26531 ratio: 2.828427122988202
x:  208885  y:  73852 ratio: 2.828427124519309
x:  437371  y:  154634 ratio: 2.82842712469444
x:  1217471  y:  430441 ratio: 2.828427124739511
x:  2549185  y:  901273 ratio: 2.828427124744667
x:  7095941  y:  2508794 ratio: 2.828427124745993
x:  14857739  y:  5253004 ratio: 2.828427124746145

Fri Apr 29 11:16:01 PDT 2016

 x^2 - 8 y^2 = -7

jagy@phobeusjunior:~$



jagy@phobeusjunior:~/old drive/home/jagy/Cplusplus$ ./Pell 2

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Pell unit 
3^2 - 2 * 2^2 = 1 

=========================================

jagy@phobeusjunior:~$ ./Pell_Target_Fundamental

  3^2 - 2 2^2 = 1

 x^2 - 2 y^2 = 7

Fri Apr 29 11:20:19 PDT 2016

x:  3  y:  1 ratio: 3  SEED 
x:  5  y:  3 ratio: 1.666666666666667  SEED 
x:  13  y:  9 ratio: 1.444444444444444
x:  27  y:  19 ratio: 1.421052631578947
x:  75  y:  53 ratio: 1.415094339622641
x:  157  y:  111 ratio: 1.414414414414414
x:  437  y:  309 ratio: 1.414239482200647
x:  915  y:  647 ratio: 1.414219474497681
x:  2547  y:  1801 ratio: 1.414214325374792
x:  5333  y:  3771 ratio: 1.41421373640944
x:  14845  y:  10497 ratio: 1.414213584833762
x:  31083  y:  21979 ratio: 1.414213567496246
x:  86523  y:  61181 ratio: 1.414213563034275
x:  181165  y:  128103 ratio: 1.414213562523907
x:  504293  y:  356589 ratio: 1.414213562392558
x:  1055907  y:  746639 ratio: 1.414213562377534
x:  2939235  y:  2078353 ratio: 1.414213562373668
x:  6154277  y:  4351731 ratio: 1.414213562373226

Fri Apr 29 11:20:39 PDT 2016

 x^2 - 2 y^2 = 7

jagy@phobeusjunior:~$ 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=