1
$\begingroup$

Given

lyapexp[n_] := (Hold[Compile[{{kMin, _Integer}, tol, r, u0, u1},
      Module[{
        relErr, ohs,
        res = {0., 0.},
        hs = Table[.0, {n}],
        k = 0,
        nzs = Table[.0, {n}],
        ss = Table[.0, {n}],
        ws = IdentityMatrix[n]/2,
        zs = IdentityMatrix[n]/2,
        success = 1.0,
        uPrev = u0,
        uCurr = u1
        },
       While[True,
        k++;
        zs = Dnn[r, uCurr, uPrev].ws;
        Do[
         (Module[{nzi},
            Do[(
               zi -= zi.zj/Norm[zj, 2] zj) /. zj :> zs[[1 ;; n, j]]
             , {j, i - 1}];
            nzi = Norm[zi];
            nzs[[i]] = nzi;
            ws[[1 ;; n, i]] = zi/nzi]) /. zi :> zs[[1 ;; n, i]]
         , {i, n}];
        ohs = hs;
        ss += Log[nzs];
        hs = ss/k;
        relErr = Norm && relErr < tol,
         res = {hs, 1.0};
         Break[];
         ];
        {uCurr, uPrev, success} = nn[r, uCurr, uPrev, success];
        If[success == 0,
         res = {0., 0.};
         Break[];
         ];
        ];
       res
       ], CompilationOptions -> {"InlineExternalDefinitions" -> True},
       RuntimeOptions -> "Speed", 
      CompilationTarget -> "C"]] //. {Pattern[p, _IdentityMatrix] :> 
      Block[{}, p /; True], 
     HoldPattern[e_ /. (p_ :> r_)] :> 
      Block[{}, (Hold[e] /. p :> r) /; True]}) //. 
  HoldPattern[Hold[expr_]] :> expr

why is MainEvaluate used in CompilePrint[lyapexp[2]]?

EDIT

Now (after SetSystemOptions["CompileOptions" -> "CompileReportExternal" -> True]) it's

lyapexp[n_] := (Hold[Compile[{{kMin, _Integer}, tol, r, u0, u1},
      Module[{
        relErr, ohs,
        res = {0., 0.},
        hs = Table[.0, {n}],
        k = 0,
        nzs = Table[.0, {n}],
        ss = Table[.0, {n}],
        ws = {{1., 0.}, {0., 1.}},
        zs,
        success = 1.0,
        uPrev = u0,
        uCurr = u1
        },
       While[True,
        k++;
        zs = Dnn[r, uCurr, uPrev].ws;
        Do[
         (Module[{nzi},
            Do[(
               zi -= zi.zj/Norm[zj, 2] zj) /. zj :> zs[[1 ;; n, j]]
             , {j, i - 1}];
            nzi = Norm[zi];
            nzs[[i]] = nzi;
            ws[[1 ;; n, i]] = zi/nzi]) /. zi :> zs[[1 ;; n, i]]
         , {i, n}];
        ohs = hs;
        ss += Log[nzs];
        hs = ss/k;
        relErr = Norm[1 - ohs/hs, 2];
        If[k >= kMin && relErr < tol,
         res = Prepend[hs, 1.0];
         Break[];
         ];
        {uCurr, uPrev, success} = nn[r, uCurr, uPrev, success];
        If[success == 0,
         res = Prepend[Table[0., {n}], 0.];
         Break[];
         ];
        ];
       res
       ], CompilationOptions -> {"InlineExternalDefinitions" -> True},
       RuntimeOptions -> "Speed", 
      CompilationTarget -> "C"]] //. {Pattern[p, _IdentityMatrix] :> 
      Block[{}, p /; True], 
     HoldPattern[e_ /. (p_ :> r_)] :> 
      Block[{}, (Hold[e] /. p :> r) /; True]}) //. 
  HoldPattern[Hold[expr_]] :> expr

So, how do I do replacements in my Compile'd code correctly?

$\endgroup$
1
  • $\begingroup$ There is a BIG help from SetSystemOptions["CompileOptions" -> "CompileReportExternal" -> True] $\endgroup$ Commented Sep 27, 2014 at 14:55

1 Answer 1

0
$\begingroup$

The correct code is:

lyapexp[n_] := 
 ReleaseHold[Replace[Hold[Compile[{{kMin, _Integer}, tol, r, u0, u1},
      Module[{
        relErr, ohs,
        res = {0., 0.},
        hs = Table[.0, {n}],
        k = 0,
        nzs = Table[.0, {n}],
        ss = Table[.0, {n}],
        ws = {{1., 0.}, {0., 1.}},
        zs,
        success = 1.0,
        uPrev = u0,
        uCurr = u1
        },
       While[True,
        k++;
        zs = Dnn[r, uCurr, uPrev].ws;
        Do[
         (Module[{nzi},
            Do[(
               zi -= zi.zj/Norm[zj, 2] zj) /. zj :> zs[[1 ;; n, j]]
             , {j, i - 1}];
            nzi = Norm[zi];
            nzs[[i]] = nzi;
            ws[[1 ;; n, i]] = zi/nzi]) /. zi :> zs[[1 ;; n, i]]
         , {i, n}];
        ohs = hs;
        ss += Log[nzs];
        hs = ss/k;
        relErr = Norm[1 - ohs/hs, 2];
        If[k >= kMin && relErr < tol,
         res = Prepend[hs, 1.0];
         Break[]
         ];
        {uCurr, uPrev, success} = nn[r, uCurr, uPrev, success];
        If[success == 0,
         res = Prepend[Table[0., {n}], 0.];
         Break[]
         ];
        ];
       res
       ], CompilationOptions -> {"InlineExternalDefinitions" -> True},
       RuntimeOptions -> "Speed", 
      CompilationTarget -> "C"]] //. {Pattern[p, _IdentityMatrix] :> 
      Block[{}, p /; True], 
     HoldPattern[e_ /. (p_ :> r_)] :> 
      Block[{}, (Hold[e] /. p :> r) /; True]}, 
   HoldPattern[Hold[expr_]] :> expr, Infinity]]
$\endgroup$
1
  • $\begingroup$ This code is still incorrect in it's misusing of n, but as far as I remember answers my own question. $\endgroup$ Commented Sep 29, 2014 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.