开发者

Mathlink and delayed evaluation/variables

开发者 https://www.devze.com 2023-03-24 16:53 出处:网络
I have a scalar function written in Fortran that I\'m using in Mathematica via a small mathlink module. I want this function to behave as close to a native Mathematica function as possible, including

I have a scalar function written in Fortran that I'm using in Mathematica via a small mathlink module. I want this function to behave as close to a native Mathematica function as possible, including its handling of symbolic constants.

A lot of times I wish to write some expression in mathematica with symbolic constants/variables that will be replaced latter on with a list of replacement rules. However, Mathematica immediately tries to evaluate my mathlink function with the symbolic arguments in place and it obviously fails. For example, even the following simple expression will fail

extf[a]/.a->5

Is there a way to delay the evaluation of the function until all symbolic arguments have been replaced with numbers?

One not so elegant way I have used with some success (but not acceptable If I wish to include this function in a general purpose mathematica package to share with coworkers) is to evaluate everything with a "dummy" reference to the external function and then use a replacement rule to swap all dummy references for the real reference at the same time I evaluate all the other numerical constants. e.g.

dummyf[开发者_高级运维a]/.{a->5,dummyf->extf}


Would something using the Mathematica pattern matcher be useful? Note that you need to omit any definition for holddummyf when the input is non-numeric.

dummyf[a_?NumericQ] := extf[a]
holddummyf[a_?NumericQ] := extf[a]
dummyf[a_] := holddummyf[a]

[In]  dummyf[0.3]
[Out] extf[0.3]

[In]  dummyf[b]
[Out] holddummyf[b]

[In]  % /. b -> 5
[Out] extf[5]

I guess the other question to ask is whether having extf in Fortran is strictly necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消