【Excel VBA】
Excel VBA can use third-party library functions. After creating or obtaining a third-party library, in the Excel VBA programming environment, select Tools → References to open the References dialog box, as shown in Figure 10-1. Find the library to be referenced in the list, or click the Browse button to locate the library file, then click OK to complete the reference. The referenced third-party library can be viewed using the Object Browser.
Figure 10-1 "References" Dialog Box
To introduce vector and matrix calculations in VB, the author once wrote a dynamic link library Math3D.dll in VB. Below, we reference it in the Excel VBA programming environment and encode tests. The file path is Samples\ch10\Excel VBA\ThirdPartyLibraryFunctions.xlsm. Math3D.dll is in the same directory. The following code requires referencing it first.
Sub Test()
Dim vctFirst As Vector3D
Dim vctSecond As Vector3D
Dim mtxFirst As Matrix3D
Dim mtxSecond As Matrix3D
Set vctFirst = New Vector3D
Set vctSecond = New Vector3D
Set mtxFirst = New Matrix3D
Set mtxSecond = New Matrix3D
' First vector
vctFirst.x = 10#
vctFirst.y = 78.5
vctFirst.Z = 102.9
' Second vector
vctSecond.x = 109.2
vctSecond.y = 82.5
vctSecond.Z = 180.8
' Output the first and second vectors in the Immediate Window
Dim strFirst As String
Dim strSecond As String
strFirst = "First vector: " & Str(vctFirst.x) & " " & Str(vctFirst.y) & " " & Str(vctFirst.Z)
strSecond = "Second vector: " & Str(vctSecond.x) & " " & Str(vctSecond.y) & " " & Str(vctSecond.Z)
Debug.Print
Debug.Print strFirst
Debug.Print strSecond
Debug.Print
' Length of the first vector
Dim strLen As String
strLen = "Length of the first vector: " & vctFirst.GetLength
Debug.Print strLen
' Vector operations
Dim vctAdd As Vector3D
Dim vctSub As Vector3D
Dim dblMulDot As Double
Dim vctMulCro As Vector3D
Dim vctDev As Vector3D
Set vctAdd = vctFirst.Add(vctSecond)
Set vctSub = vctFirst.Subtract(vctSecond)
dblMulDot = vctFirst.MultDot(vctSecond)
Set vctMulCro = vctFirst.MultCross(vctSecond)
Set vctDev = vctFirst.Devide(2)
Set vctDev = vctFirst.Devide(2)
' Output operation results
Dim strAdd As String
Dim strSub As String
Dim strMulDot As String
Dim strMulCro As String
Dim strDev As String
strAdd = "Vector sum: " & Str(vctAdd.x) & " " & Str(vctAdd.y) & " " & Str(vctAdd.Z)
strSub = "Vector difference: " & Str(vctSub.x) & " " & Str(vctSub.y) & " " & Str(vctSub.Z)
strMulDot = "Vector dot product: " & Str(dblMulDot)
strMulCro = "Vector cross product: " & Str(vctMulCro.x) & " " & Str(vctMulCro.y) & " " & Str(vctMulCro.Z)
strDev = "First vector divided by 2: " & Str(vctDev.x) & " " & Str(vctDev.y) & " " & Str(vctDev.Z)
Debug.Print strAdd
Debug.Print strSub
Debug.Print strMulDot
Debug.Print strMulCro
Debug.Print strDev
End Sub
Running the procedure outputs the calculation results in the Immediate Window.
First vector: 10 78.5 102.9
Second vector: 109.2 82.5 180.8
Length of the first vector: 129.81009205759
Length of the first vector: 129.81009205759
【Python】
In Python, to reference third-party libraries (also called third-party modules or packages), you first need to install the library, then import it in the code that uses it. NumPy and pandas introduced in Chapter 7 are both third-party packages; readers can refer to the corresponding content to learn their usage.