2

I am trying to convert Mathematica output into equations I can plug into C. I am presently trying to convert expressions of the form

Derivative[1,0,0][f][t,x,y]

where f is function with an arbitrary name (e.g. it may be called f, g, func, etc), into expressions of the form

f_Dt

where f is the symbol for the original function, e.g. I want the general rule to map Derivative[1,0,0][g][t,x,y] to g_Dt as well. Is there a way of doing this? For example, applying

expression/.{Derivative[1,0,0][f_][t,x,y]->f_Dt}

(where "expression" is arbitrary) doesn't work.

To expand the scope of this question, more generally I would like to transform

Derivative[1,0,0][f][t,x,y]

to not just f_Dt, but also expressions like

f$D

or

fD

(i.e. expressions that don't just include the underscore after f).

  • Well if it's Derivative[1,0,0][f][t,x,y] then Derivative[1,0,0][f_][T,R,Y] clearly won't match that. Maybe you wanted Derivative[1,0,0][f_][a_, b_, c_]? By the way, why are you trying to transform it into something that looks like f_Dy? If you look at the FullForm[f_Dy] you get Pattern[f, Blank[Dy]] which is a pattern matching object and thus something that can't be used as a function name or anything like that. – b3m2a1 Dec 11 '19 at 18:34
  • @b3m2a1 Unfortunatelyt the [T,R,Y] is a typo (which I've corrected). I want to transform to something like f_Dy as that is the general name for a function derivative I am using in my C code. – physics_researcher Dec 11 '19 at 18:47
  • 1
    Can you give us a sample expression where this fails? I'm almost certain your pattern just isn't quite flexible enough for what you want. Like you expect one form of the pattern but it turns out you have a different one. – b3m2a1 Dec 11 '19 at 18:48
  • In version 10.1 Derivative[1, 0, 0][foo][t, x, y] /. {Derivative[1, 0, 0][f_][t, x, y] :> f_Dt} yields foo_Dt along with a warning message that in this case can be ignored. – Mr.Wizard Dec 11 '19 at 20:25
  • @Mr.Wizard For some reason that approach isn't working for more complicated algebraic expressions that also include derivatives of other functions (e.g. the expression includes derivatives of a function f, h, etc) .I'm also concerned by the warning Mathematica raises. – physics_researcher Dec 19 '19 at 14:47
  • (1) Can you provide one or more of these cases as an example? (2) The particular warning is because usually one does not want a named Pattern on the right side of a Rule; it's a common mistake like using = in place of == within Solve. However if that is actually what one wants it can be safely ignored; see (210799) for example. – Mr.Wizard Dec 19 '19 at 15:10
  • Usually, when you want to convert Mathematica expressions into C, you want to turn it into a string. I suggest something like "f_Dt" instead of plain f_Dt which is interpreted by Mathematica as a pattern named f with head Dt. – QuantumDot Dec 19 '19 at 19:50
  • @QuantumDot The problem with that approach is then I will covert, e.g. expression like Derivative[1,0,0][g][t,x,y] to "f_Dt", instead of "g_Dt" – physics_researcher Dec 19 '19 at 21:31

0 Answers0