New Version 6.0!

Try it for free with our fully functional 60-day trial version.

Download now!

QuickStart Samples

Vector Operations QuickStart Sample (IronPython)

Illustrates how to perform operations on Vector objects, including construction, element access, arithmetic operations in IronPython.

C# code Visual Basic code F# code Back to QuickStart Samples

import numerics

from math import exp

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

# Illustrates operations on Vector objects from the
# Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
# Mathematics Library for .NET.


# For details on the basic workings of Vector 
# objects, including constructing, copying and
# cloning vectors, see the BasicVectors QuickStart
# Sample.
#
# Let's create some vectors to work with.
v1 = Vector([1, 2, 3, 4, 5])
v2 = Vector([1, -2, 3, -4, 5])
v3 = Vector([3, 2, 1, 0, -1])

#
# Vector Arithmetic
#

# The Vector class defines overloaded addition, # subtraction, and multiplication and division
# operators:
print "v1 =", v1
print "v2 =", v2
print "Basic arithmetic:"
v = -v1
print "-v1 =", v
v = v1 + v2
print "v1 + v2 =", v
v = v1 - v2
print "v1 - v2 =", v
# Vectors can only be multiplied or divided by
# a real number. For dot products, see the
# DotProduct method.
v = 5 * v1
print "5 * v1 =", v
v = v1 * 5
print "v1 * 5 =", v
v = v1 / 5
print "v1 / 5 =", v

# For each operator, there is a corresponding
# static method. For example: v1 + v2 is
# equivalent to:
v = Vector.Add(v1, v2)
# v1 - v2 corresponds to:
v = Vector.Subtract(v1, v2)
# You can also apply these methods to Vector objects.
# In this case, they change the first operand.
print "v3 =", v3
v3.Add(v1)
# Note that this is different from the += operator!
# The += operator creates a Vector.Create object, # whereas the Add method above does not.
print "v3+v1 -> v3 =", v3
# This method is overloaded so you can directly
# add a scaled vector:
v3.Add(-2, v1)
print "v3-2v1 -> v3 =", v3
# Corresponding to the * operator, we have the
# scale method:
v3.Multiply(3)
print "3v3 -> v3 =", v3
print 

#
# Norms, dot products, etc.
#
print "Norms, dot products, etc."
# The dot product is calculated in one of two ways:
# Using the static DotProduct method:
a = Vector.DotProduct(v1, v2)
# Or using the DotProduct method on one of the two
# vectors:
b = v1.DotProduct.Overloads[DenseVector](v2)
print "DotProduct(v1, v2) = {0} = {1}".format(a, b)
# The Norm method returns the standard two norm 
# of a Vector:
a = v1.Norm()
print "|v1| =", a
# .the Norm method is overloaded to allow other norms, # including the one-norm:
a = v1.Norm(1)
print "one norm(v1) =", a
# ...the positive infinity norm, which returns the
# absolute value of the largest component:
a = v1.Norm(float.PositiveInfinity)
print "+inf norm(v1) =", a
# ...the negative infinity norm, which returns the
# absolute value of the smallest component:
a = v1.Norm(float.NegativeInfinity)
print "-inf norm(v1) =", a
# ...and even the zero norm, which simply returns
# the number of components of the vector:
a = v1.Norm(0)
print "zero-norm(v1) =", a
# You can get the square of the two norm with the
# NormSquared method.
a = v1.NormSquared()
print "|v1|^2 =", a
print 
			
#
# Largest and smallest elements
#
# The Vector class defines methods to find the
# largest or smallest element or its index.
print "v2 =", v2
# The Max method returns the largest element:
print "Max(v2) =", v2.Max()
# The AbsoluteMax method returns the element with
# the largest absolute value.
print "Absolute max(v2) =", v2.AbsoluteMax()
# The Min method returns the smallest element:
print "Min(v2) =", v2.Min()
# The AbsoluteMin method returns the element with
# the smallest absolute value.
print "Absolute min(v2) =", v2.AbsoluteMin()
# Each of these methods has an equivalent method
# that returns the zero-based index of the element 
# instead of its value, for example:
print "Index of Min(v2) =", v2.MinIndex()

# Finally, the Apply method lets you apply
# an arbitrary function to each element of the
# vector:
v1.Apply(exp)
print "Exp(v1) =", v1
# There is also a static method that returns a 
# Vector.Create object:
v = Vector.Apply(exp, v2)