Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
0 votes

Hi! Always staying on the question: what is the correct way to define a post_process function of the type post_process(equation,operator) that applies a generic operator (for example the derivation) to an equation? For example, such that once invoked

test := A = B;
dTest = post_process(test,$\partial_{\mu\nu}$);

I can get dTest as

$$\partial{\mu\nu}{A} = \partial{\mu\nu}{B} $$ Thanks, Mattia

related to an answer for: Apply an operator to an equation
in General questions by (410 points)

1 Answer

0 votes

First of all, don't call this post_process; that's a reserved function which Cadabra will call after every manipulation step.

To do this, you need to somehow indicate where you want the 'argument' of the operator to go, otherwise a \partial_{\mu\nu} not acting on anything will be replaced with zero before it even makes it to the function. Try something like the code below:

import cdb.core.manip as manip
\partial{#}::PartialDerivative;

def appop(ex, op):
   t1=manip.get_lhs(ex)
   t2=manip.get_rhs(ex)
   o1:= @(op);
   o2:= @(op);
   for arg in o1:
      if arg.name=='#':
         arg.replace(t1)
   for arg in o2:
      if arg.name=='#':
         arg.replace(t2)
   ex.top().replace( $@(o1) = @(o2)$ )
   return ex

You can then do

ex:= A = B;
appop(ex, $\partial_{\mu\nu}{#}$);

to get the result

$$\partial_{\mu\nu}{A} = \partial_{\mu\nu}{B}$$

You may want to add some error checking code for when you feed it a single expression, not an equation.

by (83.1k points)

Thanks for the answer, Mattia

...