Replace expressions with your proper names in Mathematica

I have some expressions in Mathematica that are defined in terms of other expressions. I want to take some functions of a larger expression, and then get the result in terms of subexpressions. Example:

In[78]:= e1 = x + y; e2 = 2^e1; In[80]:= D[e2, x] Out[80]= 2^(x + y) Log[2] 

I want the result to be 2^e1 Log[2] . I am currently using ReplaceAll as follows, but this is cumbersome in my actual application with approximately 20 subexpressions.

 In[81]:= D[e2, x] /. e1 -> E1 Out[81]= 2^E1 Log[2] 
+4
source share
2 answers

It is hard to get and save this form if you set e1 as x + y. Therefore, if you really do not need it, you can work with replacement rules instead.

 rul = {e1->x+y, e2->2^e1}; revrul = {x+y->e1}; InputForm[D[e2//.rul, x] /. revrul] Out[5]//InputForm= 2^e1*Log[2] 

Daniel Lichtblow Wolfram Research

+6
source

Your answer seems concrete due to the simple form of your e1 and e2 . One possibility is to define e2 as a function in terms of e1 without indicating that e1 :

 In[8]:= Clear[e1, e2]; e2[x_] := 2^e1[x] 

Then

 In[10]:= D[e2[x], x] Out[10]= 2^e1[x] Log[2] Derivative[1][e1][x] 

which is generally the right answer. As soon as you want to calculate it, you can specify a specific definition for e1 . You can also do this inside Block , so as not to introduce global definitions:

 In[11]:= Block[{e1}, e1[x_] := x + y; D[e2[x], x]] Out[11]= 2^(x + y) Log[2] 

I guess this approach can scale to more subexpressions.

NTN

+2
source

Source: https://habr.com/ru/post/1338326/


All Articles