The positive definiteness will be handled with the NCAlgebra Suite 5,0 from here
With the codes for convexHull and ExtractElements given here you can proceed as follows
<< NC`
<< SDP`
PosChar[p_, c_] := ToExpression[StringJoin[ToString[p], ToString[c]]]
SymmetricalMatrix[name_, dim_] := Module[{dummy, vars = {}, i, j, k, c}, dummy = Table[0, {dim}, {dim}];
For [i = 1; k = 1, i <= dim, i++, For[j = i, j <= dim, j++,
c = PosChar[name, k];
dummy[[i, j]] = c;
dummy[[j, i]] = c;
vars = Append[vars, c];
k = k + 1]];
{dummy, vars}
]
p = 2 a1^2 b1^2 + a2^2 b1^2 + a3^2 b1^2 + 2 a1 a2 b1 b2 + 2 a3 a4 b1 b2 + a1^2 b2^2 + 2 a2^2 b2^2 + a4^2 b2^2 + 2 a1 a3 b1 b3 + 2 a2 a4 b1 b3 - 4 a2 a3 b2 b3 + 4 a1 a4 b2 b3 + a1^2 b3^2 + 2 a3^2 b3^2 + a4^2 b3^2 + 4 a2 a3 b1 b4 - 4 a1 a4 b1 b4 + 2 a1 a3 b2 b4 + 2 a2 a4 b2 b4 + 2 a1 a2 b3 b4 + 2 a3 a4 b3 b4 + a2^2 b4^2 + a3^2 b4^2 + 2 a4^2 b4^2;
vars = Variables[p]
CCV = convexHull[p]
(* {{2, 0, 0, 0, 2, 0, 0, 0}, {2, 0, 0, 0, 0, 2, 0, 0}, {2, 0, 0, 0, 0, 0, 2, 0}, {0, 2, 0, 0, 2, 0, 0, 0}, {0, 2, 0, 0, 0, 2, 0, 0}, {0, 2, 0, 0, 0, 0, 0, 2}, {0, 0, 2, 0, 2, 0, 0, 0}, {0, 0, 2, 0, 0, 0, 2, 0}, {0, 0, 2, 0, 0, 0, 0, 2}, {0, 0, 0, 2, 0, 2, 0, 0}, {0, 0, 0, 2, 0, 0, 2, 0}, {0, 0, 0, 2, 0, 0, 0, 2}} *)
BV = createBasis[CCV]
(* {{0, 0, 0, 1, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 1}, {0, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 1}, {0, 1, 0, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 1, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 1, 0}, {1, 0, 0, 0, 0, 1, 0, 0}, {1, 0, 0, 0, 1, 0, 0, 0}} *)
Z = FormMonomials[vars, BV]
(* {a4 b4, a4 b3, a4 b2, a3 b4, a3 b3, a3 b1, a2 b4, a2 b2, a2 b1, a1 b3, a1 b2, a1 b1} *)
{matM, varsM} = SymmetricalMatrix[m, Length[Z]];
V = Z.matM.Z
{eV, cV} = ExtractElements[V, vars]
{ep, cp} = ExtractElements[p, vars];
comp = Complement[ep, eV]
If[comp == {},
{eVp, cVp} = ExtractElements[V - p, vars];
vars2 = Variables[cVp];
sol = Quiet@Solve[cVp == 0, vars2][[1]];
matB0 = matB /. sol;
Print[MatrixForm[Chop[matB0]]];
y = Variables[matB0];
Print[y];
G = matB0;
f = Total[y];
abc = SDPMatrices[f, G, y];
SetPrecision[abc, 50];
{Y, X, S, flags} = SDPSolve[abc];
Print[Flatten[Y]];
Print[PositiveDefiniteMatrixQ[X[[1]]]];
Print[PositiveDefiniteMatrixQ[S[[1]]]]
];
NOTE
The module createBasis was omitted because it is quite involved. It's purpose is to find the monomials which squared produce the powers found in $f$. As can be verified the polynomial is SOS.
LAST NOTE
As requested, the needed module createBasis
ConvexDepenentQ[corners_, cand_] := Module[{w, ws}, w = Array[ws, Length@corners];
1 == Length@
FindInstance[
w.corners == cand && Total[w] == 1 &&
And @@ Table[w[[i]] >= 0, {i, Length@w}], w]];
ConvexReduce[data_] := Module[{corners, ncorners, test}, corners = data;
Do[ncorners = Delete[corners, Position[corners, data[[i]]]];
test = ConvexDepenentQ[ncorners, data[[i]]];
If[test, corners = ncorners];, {i, Length@data}];
corners];
convexHull[data_] := Module[{corners, rd}, corners = {};
Do[corners =
Join[corners,
Select[data,
Min[data[[;; , i]]] == #[[i]] ||
Max[data[[;; , i]]] == #[[i]] &]];, {i, Length@data[[1]]}];
corners = DeleteDuplicates@corners;
rd = Delete[data, First@Position[data, #] & /@ corners];
Do[If[ConvexDepenentQ[corners, rd[[i]]], ,
AppendTo[corners, rd[[i]]]], {i, Length@rd}];
ConvexReduce@DeleteDuplicates@corners];
CierreConvexo[data_List, n_] := Module[{}, Return[{True, convexHull[data]}]]
MaxExps[coefs_] :=
Module[{i, n, r}, n = Dimensions[coefs];
If[Length[n] > 0,
For[i = 1; r = {}, i <= n[[2]], i++, r = Append[r, Max[Take[coefs, {1, n[[1]]}, {i}]]]]; Return[r],
Return[coefs]]]
GeneraBase[data_] := Module[{n, m, i, expmax, expbase, j, k, nn, m0, p},
expmax = Ceiling[MaxExps[data]/2];
expmax = Floor[MaxExps[data]/2];
n = Length[expmax];
For[i = 1; m = 1, i <= n, i++, m = m*(expmax[[i]] + 1)];
expbase = Table[0, {n}, {m}];
For[i = 1; m0 = m, i <= n, i++, p = 1; nn = m0/(expmax[[i]] + 1);
While[p <= m, For[j = 0, j <= expmax[[i]], j++,
For[k = 1, k <= nn, k++, expbase[[i, p]] = j; p = p + 1]]];
m0 = nn]; Return[Transpose[expbase]]
]
Clear[createBasis]
createBasis[P_List] := Module[{S, nlp, ncp, nls, ncs, sos, ones, zeros, II, CC, BB, P0, m1, l1, l2, limits, si, m2, MM, X, f, s = {}, i},
S = GeneraBase[P];
{nlp, ncp} = Dimensions[P];
If[nlp <= 10, {sos, P0} = CierreConvexo[P, 1], {sos, P0} =
CierreConvexo[P, 2]];
{nlp, ncp} = Dimensions[P0];
If[Length[Dimensions[S]] == 1, nls = 1;
ncs = ncp, {nls, ncs} = Dimensions[S]];
ones = Table[1, {nlp}, {1}];
zeros = Table[0, {nlp}, {1}];
II = IdentityMatrix[nlp];
CC = Join[
Join[Join[{Table[0, {ncp}]}, {{0}}, 2], 1/nlp Transpose[ones],
2], {{1}}, 2][[1]];
BB = Join[Transpose[ones], {{1}}, 2][[1]];
m1 = Join[Join[Join[1/2 P0, -ones, 2], II, 2], zeros, 2];
l1 = Table[{-1, 1}, {ncp + 1}];
l2 = Table[{0, 1}, {nlp + 1}];
limits = 10^20 Join[l1, l2];
For[i = 1, i <= nls, i++,
If[nls > 1, si = S[[i]], si = S];
m2 = Join[Join[Join[{-si}, {{1}}, 2], Transpose[zeros], 2], {{1}}, 2];
MM = Join[m1, m2];
X = Quiet[LinearProgramming[CC, MM, BB, limits, Reals]];
f = CC.X;
If[f > 0, AppendTo[s, si]]]; Return[s]
]
PLUS LAST NOTE
Added the method FormMonomials
FormMonomials[vars_, basefinal_] :=
Module[{n, m, p, s = {}, i, j, vj, nv, k, dimv},
dimv = Length[basefinal];
If[dimv == 0, Return[s], {n, m} = Dimensions[basefinal];
For[i = 1, i <= n, i++, p = 1;
For[j = 1, j <= m, j++, vj = vars[[j]]; nv = Length[vj];
If[nv > 1,
For[k = 1, k <= nv, k++, p = p*vj[[k]]^basefinal[[i, j]]],
p = p*vars[[j]]^basefinal[[i, j]]]]; s = Append[s, p]];
Return[s]]]