Home > Blockchain >  simplify algebraic expressions in R
simplify algebraic expressions in R

Time:10-18

I would like to solve a first order condition given an expression in R. I have successfully taken the derivative but, I suspect because I am taking the derivative of a bunch of call objects. I cannot get my expressions to simplify.

In the example code below I outline three functions, combine them with arithmetic and then take the derivative with respect to my K variable. The associated output has also been placed below.

Q = quote(K^(a)*L^(1-a))
P = quote(wL x)
MC = quote(wL K)
eval(parse(text=R-MC))


profit = substitute(Q*(P-MC), list(Q=Q,P=P,MC=MC))
D(profit,'K')

Output:

K^((a) - 1) * (a) * L^(1 - a) * (wL   x - (wL   K)) - K^(a) * 
    L^(1 - a)

Note that the (wL x - (wL K)) in the above output ought to simplify to (x - K). While the output is correct, I am afraid that if I went much further or tried to solve for a particular first order condition, I want to be working with the most simplified expressions possible

My question: Is there a function or means by which I can simplify expressions either prior to or once they have been mathematically evaluated?

My desired output (or any algebraic equivalent):

K^((a) - 1) * (a) * L^(1 - a) * (x - K) - K^(a) * 
    L^(1 - a)

CodePudding user response:

Using the Ryacas0 package (also see the Ryacas package) we can write:

library(Ryacas0)

der <- D(profit,'K')
e <- as.expression(der)
Simplify(e)

giving:

yacas_expression(K^(a - 1) * a * L^(1 - a) * x - K^(a - 1) * a * L^(1 - a) * K - L^(1 - a) * K^a)
  • Related