Processing math: 100%
Ask Your Question
2

how to define symbolic function on functions?

asked 4 years ago

Performant Data gravatar image

I recently started using Sage in earnest, and was trying to define a function on functions. But:

inner_product(f,g) = integral(f(x) * g(x), (x,0,1))
inner_product

returns:

(f, g) |--> 1/3

Not surprisingly, trying to use it, it behaves as described:

e1(x) = 1
inner_product(e1,e1)

returns:

1/3

And curiously:

inner_product(f,g) = integral(f(x) * 1, (x,0,1))
inner_product

returns:

(f, g) |--> 1/2

What's going on here?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 3 years ago

rburing gravatar image

The reason for the results is that callable symbolic expressions are defined by interpreting the input variables as symbolic variables, and calling will amount to substitution of values into those variables in the expression on the right-hand side. I agree this limitation is not obvious and often confusing for beginners.

Indeed, when f and g are symbolic variables, and x is a symbolic variable, then f(x) and g(x) will both evaluate to x, and integral(f(x)*g(x), (x,0,1)) hence evaluates to integral(x^2, (x,0,1)) which is indeed 1/3.

As explained in the other answer, what you want is a Python function instead.

Preview: (hide)
link
2

answered 4 years ago

cav_rt gravatar image

Your expressions are ill defined. A python function does the job

def inner_product(f,g): return integral(f*g, (x,-pi,pi))

Thus

inner_product(sin(3*x),sin(3*x))

returns π.

See here for symbolic computation.

Preview: (hide)
link

Comments

Thanks, I'll have a look. But what I really want to know is why I got the above results.

Performant Data gravatar imagePerformant Data ( 4 years ago )

I looked over your symbolic computation link but don't see anything there relevant to my question.

Performant Data gravatar imagePerformant Data ( 4 years ago )

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 4 years ago

Seen: 981 times

Last updated: Jun 10 '21