2

I have a rather complicated function H(x), and I'm trying to solve for the value of x such that H(x) = constant. I would like to do this with an interpolation object generated from a discrete interval and the corresponding output of H(interval), where other inputs are held constant. I denote the interpolation object f.

My problem is that the call function of the interpolation object accepts an array_like, so passing a symbol to f(x) to use sage's solver method is out of the question. Any ideas of how to get around this?

I have interpolation function f. I would like to solve the equation f(x) == sageconstant forx.

    from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
    import numpy as np

    #Generating my interpolation object
    xint = srange(30,200,step=.1)
    val = [H(i,1,.1,0,.2,.005,40) for i in srange(30,299,step=.1)]
    f = IUspline(xint,val,k=4)

    #This will yield a sage constant
    eq_G(x) = freeB - x 

    #relation that I would like to solve
    eq_m(x) = eq_G(39.9) == f(x)
    m = solve(eq_m(x),x)

The above code (f(x) to be more specific) generates

"TypeError: Cannot cast array data from dtype('0') to dtype('float64') according to the rule 'safe'.

edit: Any function H(x) will result in the same error, hence it doesn't matter what H(x) is. For simplicity (I wasn't kidding when I said H is complicated), try H(x) = x. Then the block will read:

    from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
    import numpy as np

    #Generating my interpolation object
    xint = srange(30,200,step=.1)
    H(x) = x
    val = [H(i) for i in srange(30,299,step=.1)]
    f = IUspline(xint,val,k=4)

    #This will yield a sage constant
    eq_G(x) = freeB - x 

    #relation that I would like to solve
    eq_m(x) = eq_G(39.9) == f(x)
    m = solve(eq_m(x),x)
  • Please provide code that allows others to reproduce the error. The currently provided code has `srange(30, 200, step=.1)` for `xint` but `srange(30, 299, step=.1)` for `val`, and this `200` vs `299` is what causes an error for anyone copy-pasting it. Then `freeB` is not defined. As a general rule always make sure the question is well posed by copy-pasting into a fresh Sage session and then copy-pasting from that session the exact input, output, and error message. Maybe it doesn't matter what `freeB` is but it matters whether others can reproduce the error to explore and help solve the problem. – Samuel Lelièvre May 01 '19 at 00:52

1 Answers1

0

When working with numpy and scipy, prefer Python types to Sage types.

Instead of Sage Integers and Reals, use Python ints and floats.

Maybe you can fix your code like this.

from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
import numpy as np

# Generate interpolation object
xint = srange(30,200,step=.1)
xint = [float(x) for x in xint]
val = [float(H(i,1,.1,0,.2,.005,40)) for i in srange(30,299,step=.1)]
f = IUspline(xint,val,k=4)

# This will yield a Sage constant
eq_G(x) = freeB - x 

# relation that I would like to solve
eq_m(x) = eq_G(39.9) == f(x)
m = solve(eq_m(x),x)
Samuel Lelièvre
  • 3,212
  • 1
  • 14
  • 27
  • Generating the interpolation object is not the problem, so converting xint to python integers does not change anything. The real issue is that the interpolation object will only accept array_like objects, which makes it impossible to pass a symbol to `f` to use for solving for `x` in the relation `eq_m(x) = eq_G(39.9) == f(x)` – Tim Copeland Apr 30 '15 at 17:43