Ask Your Question
2

How to use splines in substitutions

asked 2024-10-16 12:11:43 +0100

dex gravatar image

Hello. I'm working on geodesics on a manifold for which the metric functions are only given in terms of numerical functions. I obtained the approximations of the functions using splines, which work very well. When it comes time to substitute for the metric functions, however, I run into a problem.

Here's a minimal example that reproduces the problem: Make straight line spline:

spltest=spline([(0,0), (1,1), (2,2)])

Create expression:

eq(r) = 2 * function('nu')(r)

Substitute:

eq.subs({nu(r): spltest(r)})

which throws out errors: TypeError: cannot evaluate symbolic expression to a numeric value TypeError: unable to simplify to float approximation

When spline is replaced with a PolynomialRing.lagrange_polynomial, the substitution works fine, but Lagrange polynomials do not behave well with the form of the function I need, and therefore cannot be used in general.

Any advice for making numeric data work with equations?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2024-10-17 20:33:28 +0100

Max Alekseyev gravatar image

updated 2024-10-17 20:36:14 +0100

The thing is that spltest is not a symbolic function and cannot be called on symbolic arguments such as r. That is, spltest(r) you are trying to substitute is undefined.

To overcome this issue, one can define a symbolic function that will call spltest for numerical evaluation, but until then remains "unevaluated". Here is an example:

def spltest_evalf(self, x, parent=None, algorithm=None): return spltest(x)
spltest_func = function("spltest_func", nargs=1, evalf_func=spltest_evalf)
new_eq = eq.subs({nu(r): spltest_func(r)})
print( new_eq ) 
print( new_eq(1) )
print( new_eq(1).n() )

which prints:

r |--> 2*spltest_func(r)
2*spltest_func(1)
2.00000000000000

For details on the functions machinery, see https://doc.sagemath.org/html/en/refe...

edit flag offensive delete link more

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: 2024-10-16 12:11:43 +0100

Seen: 135 times

Last updated: Oct 17