I am working on a notebook on sagemath 10.7.
I don’t understand howto to simplify a function. Here is my notebook
var('a h')
f(x)=x^2-x+3
t(a)=(f(a+h)-f(a))/h
a=1
t(a).simplify_full()
It outputs: h+1 ==> I’m ok
However, when going further, the following did not output a simple equation:
h=0
ta(x)=t(a)*(x-a)+f(a)
print(ta(x).simplify_full())
It prints (h + 1)*x - h + 2 but it should be x+2.
What am I doing wrong ?
There is extensive documentation on symbolic expressions at
Symbolic Expressions
In particular, some examples treat the difference between setting a variable equal to another variable or constant (as you do in the fourth line of your first block of code) and using the .subs() method which lexically substitutes symbols into an expression. Here’s how I used the .subs() method to derive what you are seeking:
var('a h')
f(x) = x^2 - x + 3
t(a) = (f.subs(x=a + h) - f(x=a))/h
t # have a look
t(a).subs(a=1).simplify_full() # we should get h + 1
ta(x) = t(a) * (x - a) + f.subs(x=a)
ta.simplify_full() # optional, to have a look
v = ta.subs(a=1).simplify_full()
v # have a look
v.subs(h=0) # gives x + 2, which is what we want
Disclaimer: I’m learning about all this, myself, so I’m not sure if my result is as general as we would like it to be. I think the .subs method is what you want for doing the kind of algebraic simplifications that are required. In a limit expression such as in this example, we don’t want h to go to zero before we are ready!
.subs() is needed for plugging in parameter values, but for direct arguments of functions, it’s an overkill. Compare your code to:
var('x a h')
f(x) = x^2 - x + 3
t(a) = (f(a + h) - f(a))/h
print(t(1).simplify_full()) # we should get h + 1
ta(x) = t(a) * (x - a) + f(a)
ta.simplify_full() # optional, to have a look
v = ta.subs(a=1).simplify_full()
print(v) # have a look
v.subs(h=0)
Thanks, Max. Something I was doing using just the arguments, before I tried .subs, was not working all the way, but I must have inserted some inconsistency assigning a value to expansion point a that I did not catch, hence the overkill in trying to get the result.
Thanks for your answer ! I understand better !