User Tools

Site Tools


k29:k29.3:k29.3.5:start

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

  • A vector is a special n-tuple. A tuple is a finite list of n mathematical objects from different mathematical object classes.
  • For the Vector class in Gambas, the mathematical object classes are either real or complex numbers. If complex numbers are used, this must be specified explicitly.
  • The real or complex numbers ai in a row or column vector are called coordinates or components.
  • The magnitude or length of a vector is called its (Euclidean) norm.

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:

  • The copy of a vector is an independent vector object.
  • If the 'Local' parameter has the value True, numbers are output in the local notation, while the default value False formats the string so that it can be evaluated by the Eval(..) function.

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:

  • addition, subtraction and scalar multiplication (scalar product).
  • Direct comparison and checking for inequality exist as relations between 2 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.

  • Specification: The three vectors are position vectors in a Cartesian coordinate system.
  • The coordinates of the vectors are entered in the format: x|y|z in a text box - x, y and z are real numbers (→ Figure 29.3.5.6.1) and read from the formatted string.
  • An input alphabet is specified and checked for the three text boxes so that incorrect entries are significantly reduced (→ CheckInput(..) and TBGroup_KeyPress()).
  • A conversion function Convert2Vector(..) uses a regular expression, among other things, to check whether only valid entries were made in the three text boxes. If this is the case, the character string is split and converted into a vector.
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

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k29/k29.3/k29.3.5/start.txt · Last modified: 17.02.2024 by emma

Page Tools