We can write the Lagrangian (with $n$ generalized coordinates) using the following expression:
$$\mathcal{L(q_i,\dot{q_i},t)}=\mathcal{L}_0(q_i,t)+\mathcal{L}_1(q_i,\dot{q_i},t)+\mathcal{L}_2(q_i,\dot{q_i},t)$$
where $$\mathcal{L}_0=\vec{k_1}.\vec{q}+k_2,$$ is a function with no $\dot{q_i}$ terms,($k_1\in\mathbb{R}^n,k_2\in \mathbb{R}$), $$\mathcal{L}_1=\vec{a}.\vec{\dot{q}},$$ is a linear function on $\dot{q_i}$,$\left(a=a(q_i,t)\right)$, and $$\mathcal{L}_2=b_i\dot{q_i}^2+c_i\dot{q_i}\dot{q_j},$$ is a quadratic function on $\dot{q_i}$, ($b=b(q_i,t),c=c(q_i,t), i=1,2,...n,j=1,2,...n$).
By this way I can transform the previous expression for $\mathcal{L}$ on:
$$\mathcal{L(q_i,\dot{q_i},t)}=\mathcal{L}_0(q_i,t)+{a}.{\dot{q}}+\frac{1}{2}\dot{q}^tT\dot{q}$$
where $T$ is the kinectic energy tensor.
We have the pre-hamiltonian as,
$$\mathcal{h(q_i,\dot{q_i},p_i,t)}=\vec{\dot{q}}.\vec{p}-\mathcal{L(q_i,\dot{q_i},t)}$$
which can be written as
$$\mathcal{H(q_i,p_i,t)}=\frac{1}{2}(p-a)^tT^{-1}(p-a)-\mathcal{L}_0(q_i,t)$$
My question is about the procedure to go from the Lagrangian tensor to this last expression, in a way of algebric operations. Could you write this algebric operations?