4
$\begingroup$

Im trying to use LibraryLink to do some calculations in C but part of the expression i want to calculate is an Interpolating Function. C cant use that obviously so I'm trying to shift it to a data type that i can use.

So first question, does an interpolated function exist as an equation as in y = ax^2 + bx + c, if yes, can that be extracted, if not, can it be used with something like InterpolatingPolynomial to create an equation.

The most obvious solution is to create an interpolating polynomial to begin with but the existing code is quite extensive so it would involve a lot of modifications, which is fine i guess but i thought i would check if anyone had any ideas first.

Thanks in advance =)

$\endgroup$
  • 1
    $\begingroup$ One of the problem with this approach is that the best way for evaluating the interpolating polynomial is usually not explicitly writing out the formula in C. Question: Were you planning to simply paste that equation into C code or were you looking to pass an interpolating function to some LibraryLink code dynamically? If the latter: You can call back to Mathematica from LibraryLink and ask it to evaluate the interpolating function, but it's not going to be very fast. $\endgroup$ – Szabolcs Jan 12 '15 at 23:25
  • $\begingroup$ You can also extract the underlying data from an InterpolatingFunction and re-implement the interpolation method in C. Try ifun["Methods"] or google for "InterpolatingFunctionAnatomy". $\endgroup$ – Szabolcs Jan 12 '15 at 23:27
  • $\begingroup$ The function in C (actually C++ now that i think about it) is a numerical differential equation solver so it is only passed the equation once and it computes multiple times (probably thousands) so i think you're right, the best approach is to interpolate the base data in the C++ code. And yeah the end goal is optimisation so calling back to mathematica isn't so desirable. $\endgroup$ – Nicholas Gaffney-Henderson Jan 12 '15 at 23:37
  • $\begingroup$ Is it a variable time step solver? If it' a fixed time step solver, you could pre-generate all the data you need in Mathematica, in a single step, and pass them to the C side. $\endgroup$ – Szabolcs Jan 12 '15 at 23:50
  • $\begingroup$ I haven't decided yet but most likely ill be using adaptive step size. I need to work with stiff systems occasionally so restraining myself to static step sizes now would make that excruciating at a later point. $\endgroup$ – Nicholas Gaffney-Henderson Jan 12 '15 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.