I want to know if it is possible to create a variable that is an operator. (Please note I only started programming Mathematica today) For example, the partial operator $\partial_x$ i.e.

partX = D[#,x]&

Ideally I would want to use it in the following way.


Which would return 2x. This is mostly so I can store it and manipulate it and use it when I want to.

On a related note, I was wondering if I could do this with lists as well.

del = {D[#,x]&, D[#,y]&, D[#,z]&}

Which would ideally return


I was wondering how this would work because I was also hoping to create operators like $\partial_x\partial_y$ by multiplying the variables for each individually i.e.

partXY = D[#,x]&*D[#,y]&


partX = D[#,x]&
partY = D[#,y]&
partXY = partX*partY

Please let me know if you can answer any or all of these questions. Thanks!

  • $\begingroup$ An easy way to do things like this is with ReplaceAll. Basically leave your terms undefined until you replace later. One thing to note, however, is that you'll need to generally use ** instead of * to avoid Mathematica's automatic commutativity. $\endgroup$ – b3m2a1 Dec 7 '17 at 4:41
  • $\begingroup$ Oh, that's interesting. So I just demarcate partials with Subscript[d,x] for example and later do ReplaceAll[Subscript[d,x]->D[#,x]&]? Would that work if I had something like say Subscript[d,x]*x^2? I assume it would then turn that expression to 2x? $\endgroup$ – Aakash Lakshmanan Dec 7 '17 at 4:47
  • $\begingroup$ Pretty much, except you'd want to start with Subscript[d,x]@x^2, rather than *. Or you could do ReplaceAll[Subscript[d,x]**a_:>D[a,x]] and start with NonCommutativeMultiply. By the way, use the back-tick, ` to get code in comments. $\endgroup$ – b3m2a1 Dec 7 '17 at 4:49
  • $\begingroup$ See also the Applications tab in the docs for NonCommutativeMultiply $\endgroup$ – b3m2a1 Dec 7 '17 at 4:51
  • $\begingroup$ What is the non-commutative multiplication for? Also, I didn't really understand ReplaceAll[Subscript[d,x]**a_:>D[a,x]], What is all the stuff in the middle? This is the code I attempted to write but it didn't work. k=Subscript[d,x] *x^2 ReplaceAll[Subscript[d,x]**a_:>D[a,x]] [k] $\endgroup$ – Aakash Lakshmanan Dec 7 '17 at 4:54

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.