Ask Your Question
0

sagemath simplification problem

asked 2025-12-02 14:13:08 +0100

coliss86 gravatar image

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 ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2025-12-07 07:52:42 +0100

dyne2meter gravatar image

updated 2025-12-07 08:01:38 +0100

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!

edit flag offensive delete link more

Comments

.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)
Max Alekseyev gravatar imageMax Alekseyev ( 2025-12-08 11:22:06 +0100 )edit

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.

dyne2meter gravatar imagedyne2meter ( 2025-12-08 12:35:44 +0100 )edit
Max Alekseyev gravatar imageMax Alekseyev ( 2025-12-09 01:25:03 +0100 )edit

Thanks for your answer ! I understand better !

coliss86 gravatar imagecoliss86 ( 2025-12-10 21:25:13 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2025-12-02 14:13:08 +0100

Seen: 77 times

Last updated: Dec 07