import numerics
from math import sin
from System import Array
from Extreme.Mathematics import *
# The SymmetricMatrix class resides in the Extreme.Mathematics.LinearAlgebra
# namespace.
from Extreme.Mathematics.LinearAlgebra import *
# Illustrates the use of the SymmetricMatrix class in the
# Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
# Mathematics Library for .NET.
# Symmetric matrices are matrices whose elements
# are symmetrical around the main diagonal.
# Symmetric matrices are always square, and are
# equal to their own transpose.
#
# Constructing symmetric matrices
#
# Constructing symmetric matrices is similar to
# constructing general matrices. See the
# BasicMatrices QuickStart samples for a more
# complete discussion.
# Symmetric matrices are always square. You don't
# have to specify both the number of rows and the
# number of columns.
#
# The following creates a 5x5 symmetric matrix:
s1 = Matrix.CreateSymmetric[float](5)
# Symmetric matrices access and modify only the
# elements on and either above or below the
# main diagonal. When initializing a
# symmetric matrix in a constructor, you must
# specify a triangleMode parameter that specifies
# whether to use the upper or lower triangle:
components = Array[float]([ \
11, 12, 13, 14, 15, \
21, 22, 23, 24, 25, \
31, 32, 33, 34, 35, \
41, 42, 43, 44, 45, \
51, 52, 53, 54, 55 ])
s2 = Matrix.CreateSymmetric(5, components, MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor)
print "s2 = {0:.0f}".format(s2)
# You can also create a symmetric matrix by
# multiplying any matrix by its transpose:
m = Matrix.Create(3, 4, Array[float]([ \
1, 2, 3, 2, \
3, 4, 3, 4, \
5, 4, 5, 7 ]), \
MatrixElementOrder.ColumnMajor)
print "m = {0:.0f}".format(m)
# This calculates transpose(m) times m:
s3 = SymmetricMatrix[float].FromOuterProduct(m)
print "s3 = {0:.0f}".format(s3)
# An optional 'side' parameter lets you specify
# whether the left or right operand of the
# multiplication is the transposed matrix.
# This calculates m times transpose(m):
s4 = SymmetricMatrix[float].FromOuterProduct(m, MatrixOperationSide.Right)
print "s4 = {0:.0f}".format(s4)
#
# SymmetricMatrix methods
#
# The GetEigenvalues method returns a vector
# containing the eigenvalues.
l = s4.GetEigenvalues()
print "Eigenvalues: {0:.4f}".format(l)
# The ApplyMatrixFunction calculates a function
# of the entire matrix. For example, to calculate
# the 'sine' of a matrix:
sinS = s4.ApplyMatrixFunction(sin)
print "sin(s4): {0:.4f}".format(sinS)
# Symmetric matrices don't have any specific
# properties.
# You can get and set matrix elements:
s3[1, 3] = 55
print "s3[1, 3] =", s3[1, 3]
# And the change will automatically be reflected
# in the symmetric element:
print "s3[3, 1] =", s3[3, 1]
#
# Row and column views
#
# The GetRow and GetColumn methods are
# available.
rom = s2.GetRow(1)
row = s2[1,:]
print "row 1 of s2 =", row
column = s2.GetColumn(2, 3, 4)
column = s2[3:5, 2]
print "column 3 of s2 from row 4 to row 5 =", column