pnorm[A_, p_] := Module[{m, n, x, y, f, ans, ret},
{m, n} = Dimensions[A];
y = Array[x, n];
f = Norm[A.y, p]/Norm[y, p] // FullSimplify;
ans = Maximize[f, Array[x, n]] // N;
ret = Part[ans, 1];
ret
]
findD[L_, c_, \[Lambda]_, p_] := Module[{B, M, w, pp, ans},
M = Array[B, Dimensions[L]];
w = Tr[c.(Transpose[M])];
pp = pnorm[L - M, p] // FullSimplify;
ans = Minimize[Hold[pp + \[Lambda] w],
Flatten[Array[B, Dimensions[L]]]] // N;
ans
]
findD[{{3, 8}, {2, 5}}, {{9, 1}, {3, 6}}, .5, 3]
During evaluation of In[44]:= NMinimize::nnum: The function value 6.59387 +1/((Abs[<<1>>]^3+<<1>>^3)/(Abs[<<1>>]^3+<<1>>^3))^(1/3) is not a number at {B$33209[1,1],B$33209[1,2],B$33209[2,1],B$33209[2,2]} = {0.643476,0.766972,0.280303,0.964763}. >>
During evaluation of In[44]:= NMinimize::nnum: The function value 1/((Abs[<<1>>]^3+<<1>>^3)/(Abs[<<1>>]^3+<<1>>^3))^(1/3)+0.5 (9 B$33209[1,1]+B$33209[1,2]+3 B$33209[2,1]+6 B$33209[2,2]) is not a number at {B$33209[1.,1.],B$33209[1.,2.],B$33209[2.,1.],B$33209[2.,2.]} = {0.643476,0.766972,0.280303,0.964763}. >>
During evaluation of In[44]:= NMinimize::nnum: The function value 1/((Abs[<<1>>]^3+<<1>>^3)/(Abs[<<1>>]^3+<<1>>^3))^(1/3)+0.5 (9 B$33209[1,1]+B$33209[1,2]+3 B$33209[2,1]+6 B$33209[2,2]) is not a number at {B$33209[1.,1.],B$33209[1.,2.],B$33209[2.,1.],B$33209[2.,2.]} = {0.643476,0.766972,0.280303,0.964763}. >>
During evaluation of In[44]:= General::stop: Further output of NMinimize::nnum will be suppressed during this calculation. >>
Minimize[
Hold[pp$33209 + 0.5 w$33209], {B$33209[1., 1.], B$33209[1., 2.],
B$33209[2., 1.], B$33209[2., 2.]}]
The code above is trying to find a matrix $\mathbf M$ which minimizes $\|\mathbf L-\mathbf M\|_p + \lambda\,\mathrm{tr}(\mathbf C\mathbf M^\top)$. However, the usage of Minimize
in the findD
function is giving me problems. Does anyone know how to fix the "not a number" problem?
Thank you!
_?NumericQ
in your function definition $\endgroup$