Классы решения задачи собственных значений для .NET

  • Published on
    13-Dec-2016

  • View
    213

  • Download
    1

Embed Size (px)

Transcript

Fomin_EigenSyst/Eigen_.docPAGE 2

: .NET: .., -

: " .NET " .

- EigenSystems - , , , Jacobi ( ) HouseholderQL ( ).

: ; , ; C# .NET Microsoft Visual Studio 2008, v. 9. 0. 21022. 8 RTM

Microsoft .NET Framework Version 3.5

Microsoft Visual C# 2008 91605-270-7986846-60210

Microsoft Windows Vista Business v. 6.0.6000 dllCSEigenSyst.cs: 17,356 K, ( cnslCSEigenSystTest.cs, wEigenTest.cs, fEigenTest.Designer.cs, fEigenTest.cs) 34,545 .

Fomin_EigenSyst/FominEigenCnslEXE.exeFomin_EigenSyst/FominEigenSolution.zipFominEigenSolution/cnslCSEigenSystTest/cnslCSEigenSystTest.csusing System;using System.Collections.Generic;using System.Text;using dllCSEigenSystem;

namespace cnslCSEigenSystTest{ class cnslEigenSystTest { static void Main (string [ ] args) { // // EigenSystems es; // Random rnd = new Random (); do { Console.Write (" = "); // int N = Int32.Parse (Console.ReadLine ()); // double [,] mtrx = new double [N, N], mtrxCopy = new double [N, N]; // // for ( int i = 0; i < N; i++ ) for ( int j = i; j < N; j++ ) mtrxCopy [i, j] = mtrxCopy [j, i] = mtrx [i, j] = mtrx [j, i] = 200 * rnd.NextDouble () - 100; try { // // if ( rnd.NextDouble () < 0.5 ) es = new Jacobi (mtrx); else es = new HouseholderQL (mtrx); // , // Console.WriteLine (es.GetType ().ToString ()); Console.WriteLine ("N={0}", N); Console.WriteLine ("Start at {0}", DateTime.Now.ToLongTimeString ()); // es.GetSolution (); } catch ( Exception e ) { Console.WriteLine (e.Message); return; } // Console.WriteLine ("End at {0}", DateTime.Now.ToLongTimeString ()); // (alpha) // (m) int alpha = rnd.Next (N), m = rnd.Next (N); double sum = 0.0; // // // , for ( int k = 0; k < N; k++ ) sum += (mtrxCopy [m, k] - (m == k ? es.EigenValues [alpha] : 0.0)) * es.EigenVectors [k, alpha]; Console.WriteLine ("zero(?)={0};iterations={1}", sum, es.Iterations); Console.WriteLine ( "Press esc to exit or any other key to reenter."); } // esc while ( ConsoleKey.Escape != Console.ReadKey (true).Key ); } }}

FominEigenSolution/cnslCSEigenSystTest/cnslCSEigenSystTest.csproj

Debug AnyCPU 8.0.50727 2.0 {227CE462-DFE0-40A4-9D78-5683BCDA450F} Exe Properties cnslCSEigenSystTest cnslCSEigenSystTest 2.0 true full false bin\Debug\ DEBUG;TRACE prompt 4 pdbonly true bin\Release\ TRACE prompt 4 {AFFF0377-EA02-44A2-B05E-878794E222E1} dllCSEigenSystem

FominEigenSolution/cnslCSEigenSystTest/Properties/AssemblyInfo.csusing System.Reflection;using System.Runtime.CompilerServices;using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information// associated with an assembly.[assembly: AssemblyTitle ("cnslCSEigenSystTest")][assembly: AssemblyDescription ("")][assembly: AssemblyConfiguration ("")][assembly: AssemblyCompany ("RSU Physics")][assembly: AssemblyProduct ("cnslCSEigenSystTest")][assembly: AssemblyCopyright ("Copyright RSU Physics 2007")][assembly: AssemblyTrademark ("")][assembly: AssemblyCulture ("")]

// Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type.[assembly: ComVisible (false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM[assembly: Guid ("09d6c2fd-ff7b-4b18-987b-ab96728e9bcc")]

// Version information for an assembly consists of the following four values://// Major Version// Minor Version // Build Number// Revision//[assembly: AssemblyVersion ("1.0.0.0")][assembly: AssemblyFileVersion ("1.0.0.0")]

FominEigenSolution/dllCSEigenSystem/dllCSEigenSyst.csusing System;using System.Collections.Generic;using System.Text;

namespace dllCSEigenSystem{ /// /// /// /// public abstract class EigenSystems { public const double tiny = 1e-30; /// /// /// const int defMaxIterations = 50; /// /// /// /// const int minMaxIterations = 10; /// /// /// double[,] curMatrix; /// /// /// int n; /// /// /// double[,] eigenVectors; /// /// /// double[] eigenValues; /// /// /// int maxIterations; /// /// /// /// public int MaxIterations { set { if (value >= minMaxIterations) maxIterations = value; } get { return maxIterations; } } /// /// /// protected int iterations; /// /// /// public double[,] CurMatrix { get { return curMatrix; } } /// /// /// public int N { get { return n; } } /// /// /// public double[,] EigenVectors { get { return eigenVectors; } } /// /// /// public double[] EigenValues { get { return eigenValues; } } /// /// /// public int Iterations { get { return iterations; } } /// /// . /// . /// /// /// , /// /// protected internal EigenSystems(double[,] matrix) { // if (matrix == null) throw new ArgumentNullException(" !"); // n = matrix.GetLength(0); if (n < 2) throw (new ArgumentOutOfRangeException( " !")); if (n != matrix.GetLength(1)) throw (new ArgumentOutOfRangeException( " !")); for (int row = 0; row < n; row++) for (int col = row + 1; col < n; col++) if (matrix[row, col] != matrix[col, row]) throw (new ArgumentOutOfRangeException( " !")); // curMatrix = matrix; // // eigenValues = new double[n]; eigenVectors = new double[n, n]; // // maxIterations = defMaxIterations; } /// /// /// public abstract void GetSolution(); /// /// /// public void Sort() { int k; double temp; for (int i = 0; i < n; i++) { k = i; temp = eigenValues[k]; for (int j = i + 1; j < n; j++) if (eigenValues[j] >= temp) { k = j; temp = eigenValues[k]; } if (k != i) { eigenValues[k] = eigenValues[i]; eigenValues[i] = temp; for (int j = 0; j < n; j++) { temp = eigenVectors[j, i]; eigenVectors[j, i] = eigenVectors[j, k]; eigenVectors[j, k] = temp; } } } } }

public class Jacobi : EigenSystems { int rotationNumber; int RotationNumber { get { return rotationNumber; } }

public Jacobi(double[,] matrix) : base(matrix) { for (int row = 0; row < N; row++) for (int col = 0; col < N; col++) EigenVectors[row, col] = row == col ? 1.0 : 0.0; }

void Rotate(double[,] a, double s, double tau, int i, int j, int k, int l) { double g = a[i, j], h = a[k, l]; a[i, j] = g - s * (h + g * tau); a[k, l] = h + s * (g - h * tau); }

public override void GetSolution() { double[] diagonal = new double[N], z = new double[N]; for (int row = 0; row < N; row++) { EigenValues[row] = diagonal[row] = CurMatrix[row, row]; z[row] = 0.0; } rotationNumber = 0; iterations = 0; double sum, threshold, g, h, s, t, theta, c, tau; for (int iteration = 1; iteration threshold) { h = EigenValues[col] - EigenValues[row]; if (Math.Abs(g) < 1e-15 * Math.Abs(h) ) t = CurMatrix[row, col] / h; else { theta = 0.5 * h / CurMatrix[row, col]; t = 1.0 / (Math.Abs(theta) + Math.Sqrt(1.0 + theta * theta)); if (theta < 0.0) t = -t; } c = 1.0 / Math.Sqrt(1.0 + t * t); s = t * c; tau = s / (1.0 + c); h = t * CurMatrix[row, col]; z[row] -= h; z[col] += h; EigenValues[row] -= h; EigenValues[col] += h; CurMatrix[row, col] = 0.0; for (int count = 0; count < row; count++) Rotate(CurMatrix, s, tau, count, row, count, col); for (int count = row + 1; count < col; count++) Rotate(CurMatrix, s, tau, row, count, count, col); for (int count = col + 1; count < N; count++) Rotate(CurMatrix, s, tau, row, count, col, count); for (int count = 0; count < N; count++) Rotate(EigenVectors, s, tau, count, row, count, col); rotationNumber++; } } for (int row = 0; row < N; row++) { EigenValues[row] = diagonal[row] += z[row]; z[row] = 0.0; } } throw (new ArgumentOutOfRangeException(string.Format( " " + " {0}!." + " MaxIterations.", MaxIterations))); } }

public class HouseholderQL : EigenSystems { int[] iterationsVector; double[] temp; public int[] IterationsVector { get { return iterationsVector; } } public HouseholderQL(double[,] matrix) : base(matrix) { iterationsVector = new int[N]; } /// /// Householder /// void Householder() { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) EigenVectors[i, j] = CurMatrix[i, j]; int l; double g, h, hh, f, scale; for (int i = N - 1; i > 0; i--) { l = i - 1; h = scale = 0.0; if (l > 0) { for (int k = 0; k

Recommended

View more >