#Pkg.add("SymPy")
using SymPy, LinearAlgebra
@vars a b c
(a, b, c)
#we can optionally specify domain with e.g.
x,y,z = symbols("x, y, z", real=true) # also integer, positive, negative...
(x, y, z)
expr = x+2y # note that we do not need to specify 2*y.. this is Julia specific
expr2 = x*expr
expanded_expr = SymPy.expand(x*expr) # Attenction that without the star it gives wrong results!
factor(expanded_expr)
f = (x+1)^2 # power is ^ and not ** .. again, Julia specific
g = x^2-2x+1
f-g
simplify(f-g)
expr3 = subs(expr, x=>1, y=>z^3) # substitution can be either numeric or symbolic
solve(expr2,y) # by default solve the equation f(.) = 0
solve(expr2,x)
Eq(expr2,2) # create an equation object
solve(Eq(expr2,2),y)
solve([expr,g],[x,y]) # solve systems of equations in multiple variables
1-element Array{Tuple{Sym,Sym},1}: (1, -1/2)
M = [[1,2,x] [4,5,x]]
N = [[x,2] [3,4] [5,6]]
M*N
det(M*N)
d = [a,b,3]
e = [x,y,2]
k = d' * e
j = d * e'
f = a*x^2-4x+3c
diff(f,x)
solve(diff(f,x),x) # find FOC
integrate(f,x)
integrate(f,(x,2,4)) # definite integral between x=2 and x=4
@vars t k r p NPV # time, planting costs, interest rate, timber price, Net Present Value (as symbol)
(t, k, r, p, NPV)
@symfuns S # generic function for stock
(S,)
NPVf = ( p * S(t) * exp(-r*t)-k)/ (1-exp(-r*t))
# Derivative of NPV with respect to time
NPVt = diff(NPVf,t)
# Equation of derivative of NPV equal to zero
NPVteq = Eq(NPVt,0)
# Rewriting the equation with the value derivative as dependent variable (V' = pS')
Vt = solve(NPVteq,p * sympy.Derivative(S(t), t)) # V' = f(pS,r,k,t)
# Setting the "symbol" NPV equal to the NPV function
# (because we want to keep in the final output the result in term of NPV)
NPVeq = Eq(NPVf,NPV)
# Solve the equation NPV==NPVb with respect to k, which has a unique solution as a function of NPV
k_NPV = solve(NPVeq, k)[1] # k = f(NPV)
# Now substitute k
VtNPV = subs(Vt[1], k=>k_NPV) # V' = f(NPV)
# Now simplify
VtNPV2 = simplify(VtNPV) # pS' = f(pS, NPV, r)
# Now expand
SymPy.expand(VtNPV2) # pS' = f(pS, NPV, r), the Faustmann formula