Vector Operations in IronPython QuickStart Sample
Illustrates how to perform operations on Vector objects, including construction, element access, arithmetic operations in IronPython.
This sample is also available in: C#, Visual Basic, F#.
Overview
This QuickStart sample demonstrates the fundamental operations available for working with vector objects in Numerics.NET. It shows how to perform common mathematical operations on vectors efficiently and elegantly.
The sample covers:
- Basic vector arithmetic including addition, subtraction, and scalar multiplication
- Computing vector norms (1-norm, 2-norm, infinity norm)
- Calculating dot products between vectors
- Finding maximum and minimum elements and their indices
- Applying mathematical functions to vectors using Map operations
- In-place operations for better performance
- Different ways to construct and manipulate vectors
Each operation is demonstrated with clear examples and shows both operator syntax and equivalent method calls. The sample illustrates how to chain operations efficiently and how to use both static and instance methods for vector manipulation. Special attention is paid to performance considerations, such as when to use in-place operations versus creating new vector instances.
The code
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 Extreme Numerics.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)