0
$\begingroup$

in this code cumavg1,cumstd1,giuper1,gilower1 are compiled. however, i dont know how to compile ei and fi to further speed up the calculation. any help will be appreciated.

ax = RandomReal[{1, 2}, 12000];
res = RandomReal[{1, 2}, 12000];
cumavg1 = 
  Compile[{{testdata, _Real, 1}, {i, _Integer}}, 
   Mean@testdata[[1 ;; i]]];
cumstd1 = 
  Compile[{{testdata, _Real, 1}, {i, _Integer}}, 
   If[i > 2, StandardDeviation@testdata[[1 ;; i]], 0]];
giuper1 = 
  Compile[{{testdata, _Real, 1}, {i, _Integer}}, 
   cumavg1[testdata, i] + cumstd1[testdata, i], 
   Parallelization -> True, 
   CompilationOptions -> {"InlineExternalDefinitions" -> True, 
     "InlineCompiledFunctions" -> True}];
gilower1 = 
  Compile[{{testdata, _Real, 1}, {i, _Integer}}, 
   cumavg1[testdata, i] - cumstd1[testdata, i], 
   Parallelization -> True, 
   CompilationOptions -> {"InlineExternalDefinitions" -> True, 
     "InlineCompiledFunctions" -> True}];
ei[testdata_, i_, wb_, stress_] := 
  If[stress[[i]] >= gilower1[testdata, i] && 
    stress[[i]] <= giuper1[testdata, i], 
   N[wb Abs[cumavg1[testdata, i] - stress[[i]]]], 
   Min[N[Abs[stress[[i]] - giuper1[testdata, i]] + 
      wb Abs[giuper1[testdata, i] - cumavg1[testdata, i]]], 
    N[Abs[gilower1[testdata, i] - stress[[i]]] + 
      wb Abs[cumavg1[testdata, i] - gilower1[testdata, i]] ]]];

fi[testdata_, i_, wb_, stress_, tol_] := 
  If[cumavg1[testdata, i] > tol, ei[testdata, i, wb, stress]/
   cumavg1[testdata, i], (ei[testdata, i, wb, stress])^2];

testing fi by:

Total[Table[
   1./Length@ax  fi[ax, i, 0.1, res, 10.^-6], {i, 
    Length@ax}]] // AbsoluteTiming

my try:

ei1 = Compile[{{testdata, _Real, 
     1}, {i, _Integer}, {wb, _Real}, {stress, _Real, 1}}, 
   If[stress[[i]] >= gilower1[testdata, i] && 
     stress[[i]] <= giuper1[testdata, i], 
    wb Norm[cumavg1[testdata, i] - stress[[i]]], 
    Min[Norm[stress[[i]] - giuper1[testdata, i]] + 
      wb Norm[giuper1[testdata, i] - cumavg1[testdata, i]], 
     Norm[gilower1[testdata, i] - stress[[i]]] + 
      wb Norm[cumavg1[testdata, i] - gilower1[testdata, i]] ]], 
   Parallelization -> True, 
   CompilationOptions -> {"InlineExternalDefinitions" -> True, 
     "InlineCompiledFunctions" -> True}]; but i get could not  complete external evaluation. 
$\endgroup$
  • $\begingroup$ What have you tried so far? What difficulties have you encountered when trying to write compiled versions of those functions? $\endgroup$ – MarcoB Jul 4 '18 at 13:05
  • 1
    $\begingroup$ Could you explain what you are trying to achieve with your code? Perhaps it could be simplified by a different approach. $\endgroup$ – MarcoB Jul 4 '18 at 21:46
  • $\begingroup$ @MarcoB please see the edit and sorry for the late response!!! $\endgroup$ – user49047 Jul 5 '18 at 9:15
  • 3
    $\begingroup$ There is absolutely no point in compiling Mean and StandardDeviation. Moreover, your program will run several times faster if you compute quantities like cumavg1[testdata, i] only once for each i and store it in a temporary variable. $\endgroup$ – Henrik Schumacher Jul 5 '18 at 15:31
  • 1
    $\begingroup$ @HenrikSchumacher's suggestion is called memoization. In Mathematica, you can just do f[x_] := f[x] = (function definition) $\endgroup$ – barrycarter Jul 7 '18 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.