1

I can do this right now, but I'd love to make the argument a list. (I have a compiled implementation of Linear Solve from here https://mathematica.stackexchange.com/a/91881/73342).

symbolicLHS = {{Derivative[1, 0][a][x], b}, {c, d}};
symbolicRHS = {Derivative[1, 0][a][y], e};
LHSwitharguments = 
  symbolicLHS /. {Derivative[1, 0][a][___] -> arg1, b -> arg2, 
    c -> arg3, d -> arg4, e -> arg5};
RHSwitharguments = 
  symbolicRHS /. {Derivative[1, 0][a][___] -> arg1, b -> arg2, 
    c -> arg3, d -> arg4, e -> arg5};
compiledFunc1 = 
  Compile[{{arg1, _Real}, {arg2, _Real}, {arg3, _Real}, {arg4, \
_Real}, {arg5, _Real}}, 
   LinearSolve[LHSwitharguments, RHSwitharguments], 
   CompilationOptions -> {"InlineExternalDefinitions" -> True}];

This works, but not without a bunch of warnings "Part::partd: Part specification arg1[[1]] is longer than depth of object." What's the best way to go about this?

symbolicLHS = {{Derivative[1, 0][a][x], b}, {c, d}};
symbolicRHS = {Derivative[1, 0][a][y], e};
LHSwitharguments = 
  symbolicLHS /. {Derivative[1, 0][a][___] -> arg1[[1]], 
    b -> arg1[[2]], c -> arg1[[3]], d -> arg1[[4]], e -> arg1[[5]]};
RHSwitharguments = 
  symbolicRHS /. {Derivative[1, 0][a][___] -> arg1[[1]], 
    b -> arg1[[2]], c -> arg1[[3]], d -> arg1[[4]], e -> arg1[[5]]};
compiledFunc2 = 
  Compile[{{arg1, _Real, 1}}, 
   LinearSolve[LHSwitharguments, RHSwitharguments], 
   CompilationOptions -> {"InlineExternalDefinitions" -> True}];
compiledFunc2[{1, 2, 3, 4, 5}]

Thanks!

NathanRL
  • 127
  • 6

2 Answers2

1

I found Indexed, which is like Part without the warning message. In the compiled code, Indexed turns into Part. Leaving this up in case anyone knows a smoother way of going about this.

symbolicLHS = {{Derivative[1, 0][a][x], b}, {c, d}};
symbolicRHS = {Derivative[1, 0][a][y], e};
LHSwitharguments = 
  symbolicLHS /. {Derivative[1, 0][a][___] -> Indexed[arg1, 1], 
    b -> Indexed[arg1, 2], c -> Indexed[arg1, 3], 
    d -> Indexed[arg1, 4], e -> Indexed[arg1, 5]};
RHSwitharguments = 
  symbolicRHS /. {Derivative[1, 0][a][___] -> Indexed[arg1, 1], 
    b -> Indexed[arg1, 2], c -> Indexed[arg1, 3], 
    d -> Indexed[arg1, 4], e -> Indexed[arg1, 5]};
compiledFunc2 = 
  Compile[{{arg1, _Real, 1}}, 
   LinearSolve[LHSwitharguments, RHSwitharguments], 
   CompilationOptions -> {"InlineExternalDefinitions" -> True}];
compiledFunc2[{1, 2, 3, 4, 5}]
NathanRL
  • 127
  • 6
1

Indexed isn't bad. You can also use Compile`GetElement:

Hold[symbolicLHS = {{Derivative[1, 0][a][x], b}, {c, d}};
    symbolicRHS = {Derivative[1, 0][a][y], e};
    LHSwitharguments = 
     symbolicLHS /. {Derivative[1, 0][a][___] -> arg1[[1]], b -> arg1[[2]], 
       c -> arg1[[3]], d -> arg1[[4]], e -> arg1[[5]]};
    RHSwitharguments = 
     symbolicRHS /. {Derivative[1, 0][a][___] -> arg1[[1]], b -> arg1[[2]], 
       c -> arg1[[3]], d -> arg1[[4]], e -> arg1[[5]]};] /. Part -> Compile`GetElement //
   ReleaseHold;

compiledFunc2 = Compile[{{arg1, _Real, 1}}, LinearSolve[LHSwitharguments, RHSwitharguments], CompilationOptions -> {"InlineExternalDefinitions" -> True}]; compiledFunc2[{1, 2, 3, 4, 5}]

Notice you cannot replace Part with Compile`GetElement in cases like a[[1]]=…. Here is an example about handling these cases.

xzczd
  • 65,995
  • 9
  • 163
  • 468