Table of Contents

29.3.5 Vector class

The Vector class from the gb.gsl component implements a vector with real or complex coordinates or components. A vector can be used like a read/write array. The Vector class has two properties and five methods.

29.3.5.1 Vector term

A vector can be represented as a row or column vector as follows

Vector

29.3.5.2 Properties

The Vector class has two properties:

PropertyData typeDescription
CountIntegerReturns the number of coordinates or components of a vector.
HandlePointerReturns a pointer to the internal GSL vector object.

Table 29.3.5.2.1 : Properties of the Vector class

29.3.5.3 Methods

The Vector class has these five methods:

MethodReturn typeDescription
ConjDot ( Vector As Vector ) VariantReturns the complex conjugate scalar product between the current vector and another vector.
Copy ( ) VectorA copy of the vector is returned.
Dot ( Vector As Vector ) VariantReturns the scalar product between the current vector and another vector.
Norm ( )FloatThe magnitude or the length of a vector is returned.
ToString ([ Local As Boolean ])StringThe vector is returned as a string representation - depending on the 'Local' parameter.

Table 29.3.5.3.1 : Methods of the Vector class

Note:

29.3.5.4 Creating vectors

There are different ways to create vectors:

(A) Declaring a variable of data type Vector with direct value assignment.
(B) Declaring a variable of data type Vector and later assigning the coordinates in an array.
(C) Creating a copy of a vector.
(D) Converting a character string from a suitable (input) component into a vector as partial source text in a project.

Example A

Public voA As New Vector(3, False)

Example B

    Dim voD As Vector
  
    voD = New Vector(4, False)
    voD = [-3.4, 5, -6, 2]
'-- Print voD.ToString(True)

Example C

    Dim voA, voB As Vector

    voA = New Vector(3, False)
    voA = [5, -6, 2]
    voB = voA.Copy()
'-- Print voB.ToString(True)

29.3.5.5 Operations and relations

The following operations are explained for vectors:

Dim voA, voB, voC As Vector 
 
  voA = [1, 1, 2] 
  voB = [5, -4, 3] 
  voC = [3, 6, 7] 
 
  Print "Location vector OA: "; voA.ToString(True) 
  Print "Location vector OB: "; voB.ToString(True) 
  Print "Location vector: "; voC.ToString(True) 
 
  Print "-----------------------------------------" 
 
  Print "Vector sum: OA+OB = " & (voA + vob).ToString(True) 
  Print "Vector difference: OA-OB = " & (voA - voB).ToString(True)  
  Print "Scalar product: OA" & String.Chr(&H22C5) & "OB = z = "; voA.Dot(voB)
  Print "Comparison: OA not the same as a copy of OB? » "; voA <> voB.Copy() 
  Print "Comparison: OC equals OB? » "; voc = voB 

Read in the IDE console:

Ortsvektor OA: [1 1 2] 
Ortsvektor OB: [5 -4 3] 
Ortsvektor OC: [3 6 7] 
----------------------------------------- 
Vector sum: OA+OB = [6 -3 5] 
Vector difference: OA-OB = [-4 5 -1] 
Scalar product: OA⋅OB = z = 7
Comparison: OA not the same as a copy of OB? » True 
Comparison: OC equals OB? » False

29.3.5.6 Project

The following project implements the fourth variant (D) for creating a vector by converting a character string from a suitable (input) component.

Public Sub Form_Open()
 
  ...
 
' Set decimal separator after locale
  If Left$(Format$(0, ".0")) = "," Then
    txbInputA.Text = "1,0|1,0|2,0"
  Else
    txbInputA.Text = "1.0|1.0|2.0"
  Endif
 
End
 
Public Sub CheckInput(sAllowed As String) '-- Concept: Charles Guerin  
 Select Case Key.Code
 Case Key.Left, Key.Right, Key.BackSpace, Key.Delete, Key.End, Key.Home, Key.Enter, Key.Return
      Return
 Default
      If Key.Text And If InStr(sAllowed, Key.Text) Then
         Return
      Endif
 End Select
 Stop Event 
End
 
Public Sub TBGroup_KeyPress() '-- Applies to the *TextBox group* TBGroup consisting of 3 text boxes!
 
  If Left$(Format$(0, ".0")) = "," Then
    CheckInput("+-,|0123456789")
  Else
    CheckInput("+-.|0123456789")
  Endif
 
End
 
Public Function Convert2Vector(sInput As String) As Vector '-- Input string → Vector
  Dim sSubject, sPattern As String
  Dim vVector As Vector
  Dim aArray As String[]
 
  sSubject = sInput
  sSubject = Replace(sSubject, " ", "")
  sSubject = Replace$(sSubject, ",", ".")
 
  sPattern = "^([-+]?[0-9]*\\.?[0-9]+[|])([-+]?[0-9]*\\.?[0-9]+[|])([-+]?[0-9]*\\.?[0-9]+)$"
 
  If sSubject Match sPattern Then   
     aArray = Split(sInput, "|")
     vVector = [Val(aArray[0]), Val(aArray[1]), Val(aArray[2])] 
     Return vVector
  Else
     Return Error.Raise("Input error in the coordinates!")
  Endif
 
End

Almost all properties and methods of the Vector class are used in the project: the three location vectors define a triangle in space, some of whose properties are calculated.

Arbeit mit Vektoren

Figure 29.3.5.6.1: Demonstration of working with vectors

You can use the IsVector(txbInputA.Text) function, for example, to check whether the character string read from a text box for point A can be interpreted as a point in a Cartesian coordinate system.

The above function is not used in the <uuuu>project. However, the same search pattern is used in a regular expression as in the Convert2Vector(..) function.

Private Function IsVector(sInput As String) As Boolean
  Dim sSubject, sPattern As String
 
  sSubject = sInput
  sSubject = Replace(sSubject, " ", "")
  sSubject = Replace$(sSubject, ",", ".")
 
  sPattern = "^([-+]?[0-9]*\\.?[0-9]+[|])([-+]?[0-9]*\\.?[0-9]+[|])([-+]?[0-9]*\\.?[0-9]+)$"
 
  If sSubject Not Match sPattern Then Return False
 
  Return True
 
End

The calculation of the cross product V1✘V2 of two vectors is realised by this function:

Public Function SetAxB(vV1 As Vector, vV2 As Vector) As Vector '-- Cross product: vV1✘vV2
 
  Dim fx, fy, fz As Float
 
  fx = vV1[1] * vV2[2] - vV2[1] * vV1[2]
  fy = vV1[0] * vV2[2] - vV2[0] * vV1[2]
  fz = vV1[0] * vV2[1] - vV2[0] * vV1[1]
 
  Return [fx, - fy, fz]
 
End

The complete, sufficiently commented source code can be found in the download area.

Download

Chapter & Projects

Download