XLPack 5 reverse communication versions
For the routines which require external functions, from the XLPack version 5, the reverse communication versions are newly supported in addition to the existing callback versions.
1. Existing (callback version) routines
For example, let’s compute the following integral.
∫ 1/(1 + x^2) dx [0, 4]
The typical progam using Qk15 is as follows:
Function F(X As Double) As Double
F = 1 / (1 + X ^ 2)
End Function
Sub Ex_Qk15()
Dim A As Double, B As Double, Result As Double
A = 0: B = 4
Call Qk15(AddressOf F, A, B, Result)
Debug.Print "S =", Result
End Sub
When the quadrature Qk15 needs an integrand value, it calls (back) the user supplied external routine F which compute the integrand value at the given point x. Qk15 returns the control to the caller program when the computation of the value of integral has been completed.
2. Reverse communication version
In contrast to the existing routine, if the reverse communication routine needs an integrand value, it returns to the caller routine with the value of point x and requests to reenter with the necessary function value at x. This procedure repeats until the final value of integral has been obtained.
The program for above example using reverse communication version is as follows:
Sub Ex_Qk15_r()
Dim A As Double, B As Double, Result As Double
Dim XX As Double, YY As Double, IRev As Long
A = 0: B = 4
IRev = 0
Do
Call Qk15_r(A, B, Result, XX, YY, IRev)
If IRev = 1 Then YY = 1 / (1 + XX ^ 2)
Loop While IRev <> 0
Debug.Print "S =", Result
End Sub
The name of reverse communication version routine is “name of callback version routine” + “_r”. For example, the reverse communication version of QK15 is Qk15_r.
IRev is the control variable for the reverse communivation. It should be initialized to 0 before calling the reverse communication routine. When the computation has been completed and the final result has been obtained, IRev=0 will be returned. When the additional information is necessary, IRev<>0 will be returned. In the latter case, the value of IRev and required action depends on the routine.
In the case of Qk15_r, XX and YY are the additional arguments for reverse communication. In the case of IRev=1, the function value at XX should be stored in YY and should call Qk15_r again.
3. Pros and cons of reverse communication version
The advantages of the reverse communication version are as follows:
- It will be safer because the AddressOf operator is not used. Since VBA cannot check the validity of the interface to the external routine when AddressOf operator is used, Excel can easily crash if there is an interface error.
- There may be more flexibility of programming. Since the arguments of user supplied external routine in the case of callback version are strictly defined, there may be some difficulty to pass some additional information.
On the other hand, the following disadvantage is considered.
- The calling sequence of the routine is more complicated than that for callback version. The program therefore may become complicated.
The same computation result will be obtained by the callback version and the reverse communication version. Please use the appropriate one depending on the conditions.