Extreme Optimization >
QuickStart Samples >
Symmetric Matrices QuickStart Sample (C#)
Extreme Optimization QuickStart Samples
Symmetric Matrices QuickStart Sample (C#)
Illustrates the use of the SymmetricMatrix class
(Extreme.Mathematics.LinearAlgebra namespace) in C#.
VB.NET code
Back to QuickStart Samples
using System;
namespace Extreme.Mathematics.QuickStart.CSharp
{
// The SymmetricMatrix class resides in the
// Extreme.Mathematics.LinearAlgebra namespace.
using Extreme.Mathematics.LinearAlgebra;
/// <summary>
/// Illustrates the use of the SymmetricMatrix class in the
/// Extreme.Mathematics.LinearAlgebra namespace of the Extreme
/// Optimization Mathematics Library for .NET.
/// </summary>
class SymmetricMatrices
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// 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:
SymmetricMatrix s1 = new SymmetricMatrix(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:
double[] components = new double[]
{
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
};
SymmetricMatrix s2 = new SymmetricMatrix(5,
components, MatrixTriangleMode.Upper);
Console.WriteLine("s2 = {0:F0}", s2);
// You can also create a symmetric matrix by
// multiplying any matrix by its transpose:
GeneralMatrix m = new GeneralMatrix(3, 4, new double[]
{
1, 2, 3,
2, 3, 4,
3, 4, 5,
4, 5, 7
});
Console.WriteLine("m = {0:F0}", m);
// This calculates transpose(m) times m:
SymmetricMatrix s3 = SymmetricMatrix.FromOuterProduct(m);
Console.WriteLine("s3 = {0:F0}", 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):
SymmetricMatrix s4 =
SymmetricMatrix.FromOuterProduct(m,
MatrixOperationSide.Right);
Console.WriteLine("s4 = {0:F0}", s4);
//
// SymmetricMatrix methods
//
// The GetEigenvalues method returns a vector
// containing the eigenvalues.
Vector l = s4.GetEigenvalues();
Console.WriteLine("Eigenvalues: {0:F4}", l);
// The ApplyMatrixFunction calculates a function
// of the entire matrix. For example, to calculate
// the 'sine' of a matrix:
SymmetricMatrix sinS =
s4.ApplyMatrixFunction(new RealFunction(Math.Sin));
Console.WriteLine("sin(s4): {0:F4}", sinS);
// Symmetric matrices don't have any specific
// properties.
// You can get and set matrix elements:
s3[1, 3] = 55;
Console.WriteLine("s3[1, 3] = {0:F0}", s3[1, 3]);
// And the change will automatically be reflected
// in the symmetric element:
Console.WriteLine("s3[3, 1] = {0:F0}", s3[3, 1]);
//
// Row and column views
//
// The GetRow and GetColumn methods are
// available.
Vector row = s2.GetRow(1);
Console.WriteLine("row 1 of s2 = {0:F0}", row);
Vector column = s2.GetColumn(2, 3, 4);
Console.WriteLine("column 3 of s2 from row 4 to ");
Console.WriteLine(" row 5 = {0:F0}", column);
Console.Write("Press Enter key to exit...");
Console.ReadLine();
}
}
}
Copyright 2004-2007,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, and Visual
Studio.NET are registered trademarks of Microsoft Corporation