1

So I want to translate the following Python code into VBA (just toy example to illustrate the issue, details omitted).

def numerical_integration(fun, a, b):
    """

    :param fun: callable 
    :param a, b: float
    :return: float
    """
    # do something based on both fun and a, b
    res = ...
    return res


def h(x, k):
    """
    helper func
    :param x: float, main arg
    :param k: float, side param
    :return: float
    """
    # calc based on both x and k
    res = ...
    return res


def main_function(k):
    """

    :param k: float 
    :return: float
    """
    a, b = 0.0, 1.0
    res = numerical_integration(fun=lambda x: h(x, k), a=a, b=b)
    return res

The problem is that I don't know how to properly pass something like a "partial" function as argument in VBA. I found how to pass "entire" func in VBA in this post. The idea is to pass the function as a string and then let VBA evaluate the string (something like a Python eval equivalent I guess?). But I don't know how this is gonna help if my function is partial i.e. I want it to be a func on only the first param like lambda x: f(x, k) where the second param is in the context in the main func.

Thanks for any sugguestions in advance.

2

2 Answers 2

1

I don't know Python and "partial" func, but I've try to use an object to replace it. I've created a class module trying to mimic the "partial" func, having optional parameters and default value.

Public defaultX As Double
Public defaultY As Double

Public Function h(Optional x As Variant, Optional y As Variant) As Double

    ' The IsMissing() function below only works with the Variant datatype.

    If (IsMissing(x)) Then
        x = defaultX
    End If
    If (IsMissing(y)) Then
        y = defaultY
    End If

    h = x + y

End Function

Then I've decided to use the "CallByName" function in numerical_integration. And here's the main module code :

Public Function numerical_integration(fun As clsWhatEver, funName As String, a As Double, b As Double) As Integer

    Dim r1 As Double, r2 As Double

    ' CallByName on the "fun" object, calling the "funName" function.
    ' Right after "vbMethod", the parameter is left empty
    ' (assuming default value for it was already set).
    r1 = CallByName(fun, funName, VbMethod, , a)
    r2 = CallByName(fun, funName, VbMethod, , b)

    numerical_integration = r1 + r2

End Function

Public Function main_function(k As Double) As Double
    Dim res As Double

    Dim a As Double, b As Double
    a = 0#
    b = 1#

    Dim oWE As New clsWhatEver
    ' Sets the default value for the first parameter of the "h" function.
    oWE.defaultX = k

    res = numerical_integration(oWE, "h", a, b)

    main_function = res

End Function
Sign up to request clarification or add additional context in comments.

Comments

0

I'm really unsure what you are asking but if you are trying to pass the result of the first function as an argument for the second then...

Function numerical_integration(fun, a, b)
    numerical_integration = 1
End Function

Function h(x, k)
    h = 0
End Function

Sub main()
    Debug.Print numerical_integration(h(1, 2), 3, 4)
End Sub

(And yes, these should all be strongly-typed variables)

2 Comments

No, not the result. But rather passing lambda x: h(x,k), the mapping per se. The numerical_integration function accepts a function/mapping as input, and returns the integral value, i.e. f -> int f(x)dx from a to b.
I still don't know if I understand the question, but have you checked the CallByName function? learn.microsoft.com/en-us/office/vba/Language/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.