When is an element of a free module over a principal ideal domain contained in a basis? I'm trying to show the following:

Let $R$ be a principal ideal domain and let $M$ be free $R$-module of rank $n$. Let $Y=\{y_1,\ldots,y_n\}$ be a basis of $M$ and $x\in M$ with $x=y_1a_1+\cdots+y_na_n$. Then $x$ is contained in a base of $M$ if and only if $\gcd(a_1,\ldots,a_n)=1$.

I have shown that if $x$ belongs to a basis of $M$, then $\gcd(a_1,\ldots,a_n)=1$.
This is my idea for the other direction:
If $n=1$, then $a_1\in R^*$, and we are done. Now suppose $n\geq 2$. Since $\gcd(a_1,\ldots,a_n)=1$, there are $s_1,\ldots,s_n\in R$ such that $a_1s_1+\cdots+a_ns_n=1$, then using this equality I tried to get some non-zero $y\in M$ such that $\langle x\rangle\cap \langle y\rangle=0$. Supposing the existence of such $y$,  using Zorn's lemma you can get a maximal nonzero submodule $N$ such that $\langle x\rangle\cap N=0$, then I showed that $\langle x\rangle\oplus N=M$, and since $M$ is free and $R$ a principal ideal domain $N$ is free and has a basis $\{x_1,\ldots,x_m\}$, and thus $\{x,x_1,\ldots,x_m\}$ would be a basis of $M$.
My question is: is there such an $y$? Also, I would like to know whether this idea is too complicated, and if so, I want to know alternative approaches to it.
Thanks
 A: Let $R$ be a PID $M$ a free $R$-module of finite rank $n$. Let $\{x_1,\dots,x_k\}$ be $k\leq n$ elements in $M$, and denote $N$ their $R$-span in $M$. Then the claim is that the $k$-ple $\{x_1,\dots,x_k\}$ can be completed to a basis of $M$ over $R$ if and only if
$$
rk(N)=k\qquad\text{and}\qquad\text{$M/N$ is torsion-free.}
$$
Indeed, if $\{x_1,\dots,x_k,x_{k+1},\dots,x_n\}$ is an $R$-basis for $M$, then $M=N\oplus N^\prime$ where $N^\prime$ is the submodule of $M$ generated by$\{x_{k+1},\dots,x_n\}$ and $M/N\simeq N^\prime$. Thus $rk(N)=k$ because $rk(N^\prime)\leq n-k$ and $rk(N)+rk(N^\prime)=n$, and $M/N$ is free because it is isomorphic to a submodule of a free module.
Viceversa, consider the quotient map
$$
\pi:M\longrightarrow M/N.
$$
Let $\{z_1,\dots,z_r\}$ be a basis of $M/N$. Choose $y_i\in M$ such that $\pi(y_i)=z_i$ and let $N^\prime\subset M$ the submodule generated by the $y_i$'s. Since the $z_i$ are $R$-linearly independent so are the $y_i$, i.e. they form a basis of $N^\prime$.
For any $m\in M$ write
$$
\pi(m)=a_1z_1+\cdots+a_rz_r,\qquad a_i\in R.
$$
Then $m-\sum_{i=1}^ra_iy_i\in\ker(\pi)=N$, from which follows readily that $M=N\oplus N^\prime$ and thus $\{x_1,\dots,x_k,y_1,\dots,y_r\}$ is a basis for $M$ containing $\{x_1,\dots,x_k\}$. The claim is proved.
Now, let $x\in M$, $x\neq0$, and let $\{y_1,..,y_n\}$ be a basis of $M$. Then
$x=a_1y_1+\dots+a_ny_n$ for some $a_i\in R$. Let $d=\operatorname{gcd}(a_1,\dots,a_n)$.
If $d\neq1$, the element $y=\frac1dx\in M$ gives a torsion element in $M/Rx$.
On the other hand, suppose that $d=1$ and $z\in M/Rx$ is such that $rz=0$ for some non-zero $r\in R$. Choose $y\in M$ such that $\pi(y)=z$ and write $y=\sum_{i=1}^nb_iy_i$. Then $ry=sx$ for some $s\in R$, i.e.
$$
rb_i=sa_i,\qquad\text{for all $i=1,\dots,n$}
$$
Thus $r\operatorname{gcd}(b_i)=s$, i.e. $r$ divides $s$ in $R$. Therefore $y=\frac sr x\in Rx$ and $\pi(y)=z=0$, thus proving that $M/Rx$ is torsion-free.
A: Let $R$ be a PID and $M$ a free $R$-module of any rank, finite or infinite.
Then $x\in M$ belongs to some basis of $M$ iff there exists a linear functional
$g\colon M\to R$ such that $g(x)=1$.
Moreover, let $(e_t\mid t\in T)$ be a basis of $M$, and $x=\sum_{t\in T}\xi_t e_t$
(where $\xi_t\neq 0$ for only finitely many $t\in T$);
then $x$ belongs to some basis of $M$ iff $\gcd\{\xi_t\mid t\in T\}=1$.
Proof.
Necessity.
Suppose there is a basis $(e_t\mid t\in T)$ of $M$ such that $x=e_s$ for some $s\in T$.
For each $t\in T$ choose an $\alpha_t\in R$, taking care to choose $\alpha_s=1$.
There exists a unique linear functional $g$ on $M$
such that $g(e_t)=\alpha_t$ for every $t\in T$.
Since $g(x)=g(e_s)=1$, we have a linear functional $g$ as required.
Sufficiency.
Let $g$ be a linear functional on $M$ so that $g(x)=1$.
We claim that $M=Rx\oplus\ker(g)$.
If $y\in M$, then $g(y-g(y)x)=0$, thus $y-g(y)x\in\ker(g)$;
this shows that $M=Rx+\ker(g)$.
If $\xi x+z=0$ for some $\xi\in R$ and $z\in\ker(g)$,
then $\xi=g(\xi x+z)=0$, thus also $z=0$;
this shows that the sum $Rx+\ker(g)$ is direct.
Now $\ker(g)$, as a submodule of a free module over a PID, is free,
so we can extend $x$ to a basis of $M$ by any basis of $\ker(g)$.
The reformulation in terms of the coordinates $\xi_t$ of $x$
with respect to a basis $(e_t\mid t\in T)$.
Every linear functional $g$ on $M$
is uniquely determined by the values $\alpha_t=g(e_t)\in R$, $t\in T$,
which can be chosen arbitrarily.
The value of $g$ on $x$ is $g(x)=\sum_{t\in T}\alpha_t\xi_t$
(a finite linear combination).
Since every linear combination of $\xi_t$'s
is divisible by $d=\gcd\{\xi_t\mid t\in T\}$,
which is itself a linear combination of $\xi_t$'s (because $R$ is a PID),
it is clear that there exists a linear functional $g$ such that $g(x)=1$ iff $d=1$.
Done.
You may enjoy proving the following generalization.
Let $R$ and $M$ be as above, and $x_1,\ldots,x_n\in M$.
Then $(x_1,\ldots,x_n)$ can be extended to a basis of $M$
iff there exists a linear map $h\colon M\to R^n$
such that $h(x_1)=e_1$, $\ldots$, $h(x_n)=e_n$,
where $(e_1,\ldots,e_n)$ is the standard basis of $R^n$.
Moreover, let $(u_t\mid t\in T)$ be a basis of $M$,
and $x_i=\sum_{t\in T}\xi_{it} u_t$ for $1\leq i\leq n$ and $t\in T$.
Then $(x_1,\ldots,x_n)$ can be extended to a basis of $M$
iff the $\gcd$ of the determinants of all $n\!\times\! n$ submatrices
of the $\{1,\ldots,n\}\!\times\! T$-matrix $[\xi_{it}]$
is $1$.
(Since there exists a finite subset $S$ of $T$
such that $\xi_{it}=0$ for $1\leq i\leq n$ and $t\in T\setminus S$,
the condition in fact involves only finitely many $n\!\times\! n$ matrices.)
A: I like to prove things like this as algorithmically as possible,
using matrices and row and column operations that are as elementary
as possible.
I construct an invertible matrix $A$ such that $A \matrix(a_1,\ldots,a_n)^T$ = $(1,0,\ldots,0)^T$. Let $B$ be the linear map corresponding to $A$ relative to the basis $\{y_1,\ldots,y_n\}$.
Then the desired basis is $\{x = B^{-1}y_1,\ldots,B^{-1}y_n\}$.
If $n = 1$, take $A = \matrix(1/a_1)$.
If $n = 2$, let $c = \gcd(a_1,a_2) = 1$ and choose $s_1$ and $s_2$ such that
$s_1 a_1 + s_2 a_2 = c$. Take
$$A = \begin{bmatrix} s_1 \ \ \ \ s_2 \\ -a_2/c \ \ a_1/c \end{bmatrix}.$$
Then $A \matrix(a_1,a_2)^T = \matrix(c,0)^T$. In this step,
$c = 1$, but we write it as $c$ for use in the induction step.
$A$ is invertible because its determinant is $1$.
If $n > 2$, use induction on the number of nonzero entries in $\matrix(a_1,\ldots,a_n)$.
If there is only $1$ nonzero entry, then it is a $\gcd$ of the entries and
in fact $1$ by the choice made in the previous step. Apply the most
elementary row operation of interchanging rows to move the nonzero entry
to the first entry. Row interchange operations expressed in matrix
form correspond to multiplication on the left of the matrix $A$ constructed
in previous stages of the induction. The product remains invertible.
If there are $2$ or more nonzero entries, first interchange rows so that
$a_1$ and $a_2$ are nonzero (this will actually make the row interchange
in the above step never occur). Apply the $n = 2$ step to the first
$2$ coordinates (more precisely, to the first $2$ rows) (augment the
$2 \times 2$ matrix with a $(n-2) \times (n-2)$ identity matrix).
Now $c = \gcd(a_1,a_2)$ is not necessarily a unit, but $A$ is
invertible because we divided its second row by $c$ to make its determinant
$1$. Most importantly, reducing
$\matrix(a_1,a_2,a_3,\ldots)^T$ to $\matrix(c,0,a_3,\ldots)^T$
leaves the $gcd$ of the entries unchanged, so the induction works.
All the matrices constructed correspond to not-always-elementary row
operations when they are used to multiply column vectors on the left.
Some are non-elementary since they use a linear combination of rows
while for pure elementary row operations only the special linear
combination $unit * row_1 + r_2 * row_2$ is permitted.
If $R$ is Euclidean, then the Euclidean algorithm essentially gives
a construction of the $2 \times 2$ $A$ as a product of matrices for
elementary row operations, so the final $A$ is also such a product.
I don't know of any practical algorithms for constructing the
$2 \times 2$ $A$ for PIDs that are not Euclidean.
This is the dual of a special case of the algorithm for reduction to
Hermite normal form.  In the general case of it, we start with an
arbitrary matrix instead of a column vector, and do reversible (left)
row operations on it to get a triangular matrix (with some complications
for the matrix not being square).  Hermite normal form is what you
can naturally get by doing reversible (right) column operations
instead. There is also Smith normal form.  It is what you can naturally
get by doing both reversible row operations and reversible column
operations -- a diagonal matrix.  All of the elementary theory of
modules over PIDs can be read off from these matrices.
