Data Analysis Mathematics Linear Algebra Statistics
New Version 8.1!

Supports .NET 6.0. Try it for free with our fully functional 60-day trial version. QuickStart Samples

# Basic Vectors QuickStart Sample (IronPython)

Illustrates the basic use of the Vector class for working with vectors in IronPython.

```import numerics

from System import Array

# The Vector class resides in the Extreme.Mathematics.LinearAlgebra
# namespace.
from Extreme.Mathematics import *

#/ Illustrates the use of the Vector class in the
#/ Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
#/ Mathematics Library for .NET.

#
# Constructing vectors
#

# Option #1: specify the number of elements. All
# components are set to 0.
v1 = Vector.Create[float](5)
# Option #2: specify the components:
v2 = Vector.Create(1.0, 2.0, 3.0, 4.0, 5.0)
# Option #3: specify the components as a double array.
# By default, the components are copied to a storage
# area internal to the Vector.
components = Array[float]([ 1, 2, 3, 4, 5 ])
v3 = Vector.Create(components)
### In Python, you can also use a list:
### v3 = Vector([ 1, 2, 3, 4, 5 ])
# Option #4: same as above, but specify whether
# to copy the components, or use the array as
# internal storage.
v4 = Vector.Create(components, False)
# Changing a value in the original vector changes
# the resulting vector.
print 'v4 = {0:.4f}'.format(v4)
components = 1
print 'v4 = {0:.4f}'.format(v4)
# Option #5: same as #4, but specify the length of
# the Vector. The remaining elements in the component
# array will be ignored.
v5 = Vector.Create(4, components, False)

### In IronPython, you can also create vectors from a list:
##v6 = Vector([1,2,3,4,5])

#
# Vector properties
#

# The Length property gives the number of components
# of a Vector:
print 'v1.Length =', v1.Length
# The ToArray method returns a double array
# that contains the components of the vector.
# This is always a copy:
components = v2.ToArray()
print 'Effect of shared storage:'
print 'v2 =', v2
components = 1
print 'v2 =', v2

#
# Accessing vector elements
#

# The Vector class defines an indexer property that
# takes a zero-based index.
print 'Assigning with private storage:'
print 'v1 =', v1
# You can assign to this property:
v1 = 7
print 'v1 =', v1
# The vectors v4 and v5 had the copy parameter in the
# constructor set to false. As a result, they share
# their component storage. Changing one vector also
# changes the other:
print 'Assigning with shared storage:'
print 'v4 =', v4
v4 = 7
print 'v4 =', v4

# The SetValue method sets all components of a vector
# to the same value:
v1.SetValue(1)
print 'v1 = {0:.4f}'.format(v1)
# The SetToZero method sets all components to 0:
v1.SetToZero()
print 'v1 = {0:.4f}'.format(v1)

#
# Copying and cloning vectors
#

# A shallow copy of a vector constructs a vector
# that shares the component storage with the original.
# This is done using the ShallowCopy method:
print 'Shallow copy vs. clone:'
v7 = v2.ShallowCopy()
# The clone method creates a full copy.
v8 = v2.Clone()
# When we change v2, v7 changes, but v8 is left
# unchanged.
print 'v2 =', v2
v2 = -2
print 'v7 =', v7
print 'v8 =', v8
# We can give a vector its own component storage
# by calling the CloneData method:
print 'CloneData:'
v7.CloneData()
# Now, changing the original v2 no longer changes v7:
v2 = 4
print 'v7 =', v7
# The CopyTo method copies the components of a Vector
# to a variety of destinations. It may be a Vector:
print 'CopyTo:'
v5.CopyTo(v1)
print 'v5 = {0:.4f}'.format(v5)
print 'v1 = {0:.4f}'.format(v1)
# You can specify an index where to start copying
# in the destination vector:
v5.CopyTo(v1, 1)
print 'v1 = {0:.4f}'.format(v1)
# Or you can copy to a double array:
v5.CopyTo(components)```