C#,数值计算——计算实对称矩阵所有特征值和特征向量的雅可比(Jacobi)方法与源程序

本文主要是介绍C#,数值计算——计算实对称矩阵所有特征值和特征向量的雅可比(Jacobi)方法与源程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// Computes all eigenvalues and eigenvectors of
    /// a real symmetric matrix by Jacobi's method.
    /// </summary>
    public class Jacobi
    {
        private int n { get; set; }
        private double[,] a;
        private double[,] v;
        private double[] d;
        private int nrot { get; set; }
        private double EPS { get; set; }


        /// <summary>
        /// Computes all eigenvalues and eigenvectors of a real symmetric matrix
        /// a[0..n - 1][0..n - 1]. On output, d[0..n - 1] contains the eigenvalues of a
        /// sorted into descending order, while v[0..n - 1][0..n - 1] is a matrix whose
        /// columns contain the corresponding normalized eigenvectors.nrot contains
        /// the number of Jacobi rotations that were required.Only the upper triangle
        /// of a is accessed.
        /// </summary>
        /// <param name="aa"></param>
        /// <exception cref="Exception"></exception>
        public Jacobi(double[,] aa)
        {
            this.n = aa.GetLength(0);
            this.a = aa;
            this.v = new double[n, n];
            this.d = new double[n];
            this.nrot = 0;
            this.EPS = float.Epsilon;

            double[] b = new double[n];
            double[] z = new double[n];

            for (int ip = 0; ip < n; ip++)
            {
                for (int iq = 0; iq < n; iq++)
                {
                    v[ip, iq] = 0.0;
                }
                v[ip, ip] = 1.0;
            }
            for (int ip = 0; ip < n; ip++)
            {
                b[ip] = d[ip] = a[ip, ip];
                z[ip] = 0.0;
            }
            for (int i = 1; i <= 50; i++)
            {
                double sm = 0.0;
                for (int ip = 0; ip < n - 1; ip++)
                {
                    for (int iq = ip + 1; iq < n; iq++)
                    {
                        sm += Math.Abs(a[ip, iq]);
                    }
                }
                //if (sm == 0.0)
                if (Math.Abs(sm) <= float.Epsilon)
                {
                    eigsrt( d,  v);
                    return;
                }
                double tresh;
                if (i < 4)
                {
                    tresh = 0.2 * sm / (n * n);
                }
                else
                {
                    tresh = 0.0;
                }
                for (int ip = 0; ip < n - 1; ip++)
                {
                    for (int iq = ip + 1; iq < n; iq++)
                    {
                        double g = 100.0 * Math.Abs(a[ip, iq]);
                        if (i > 4 && g <= EPS * Math.Abs(d[ip]) && g <= EPS * Math.Abs(d[iq]))
                        {
                            a[ip, iq] = 0.0;
                        }
                        else if (Math.Abs(a[ip, iq]) > tresh)
                        {
                            double h = d[iq] - d[ip];
                            double t;
                            if (g <= EPS * Math.Abs(h))
                            {
                                t = (a[ip, iq]) / h;
                            }
                            else
                            {
                                double theta = 0.5 * h / (a[ip, iq]);
                                t = 1.0 / (Math.Abs(theta) + Math.Sqrt(1.0 + theta * theta));
                                if (theta < 0.0)
                                {
                                    t = -t;
                                }
                            }
                            double c = 1.0 / Math.Sqrt(1 + t * t);
                            double s = t * c;
                            double tau = s / (1.0 + c);
                            h = t * a[ip, iq];
                            z[ip] -= h;
                            z[iq] += h;
                            d[ip] -= h;
                            d[iq] += h;
                            a[ip, iq] = 0.0;
                            for (int j = 0; j < ip; j++)
                            {
                                rot( a, s, tau, j, ip, j, iq);
                            }
                            for (int j = ip + 1; j < iq; j++)
                            {
                                rot( a, s, tau, ip, j, j, iq);
                            }
                            for (int j = iq + 1; j < n; j++)
                            {
                                rot( a, s, tau, ip, j, iq, j);
                            }
                            for (int j = 0; j < n; j++)
                            {
                                rot( v, s, tau, j, ip, j, iq);
                            }
                            ++nrot;
                        }
                    }
                }
                for (int ip = 0; ip < n; ip++)
                {
                    b[ip] += z[ip];
                    d[ip] = b[ip];
                    z[ip] = 0.0;
                }
            }
            throw new Exception("Too many iterations in routine jacobi");
        }

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

        /// <summary>
        /// Given the eigenvalues d[0..n - 1] and(optionally) the eigenvectors
        /// v[0..n - 1][0..n - 1] as determined by Jacobi or tqli, this routine sorts the
        /// eigenvalues into descending order and rearranges the columns of v
        /// correspondingly.The method is straight insertion.
        /// </summary>
        /// <param name="d"></param>
        /// <param name="v"></param>
        public static void eigsrt(double[] d, double[,] v)
        {
            int k;
            int n = d.Length;
            for (int i = 0; i < n - 1; i++)
            {
                double p = d[k = i];
                for (int j = i; j < n; j++)
                {
                    if (d[j] >= p)
                    {
                        p = d[k = j];
                    }
                }
                if (k != i)
                {
                    d[k] = d[i];
                    d[i] = p;
                    if (v != null)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            p = v[j, i];
                            v[j, i] = v[j, k];
                            v[j, k] = p;
                        }
                    }
                }
            }
        }
    }
}
 

2 代码格式

using System;namespace Legalsoft.Truffer
{/// <summary>/// Computes all eigenvalues and eigenvectors of/// a real symmetric matrix by Jacobi's method./// </summary>public class Jacobi{private int n { get; set; }private double[,] a;private double[,] v;private double[] d;private int nrot { get; set; }private double EPS { get; set; }/// <summary>/// Computes all eigenvalues and eigenvectors of a real symmetric matrix/// a[0..n - 1][0..n - 1]. On output, d[0..n - 1] contains the eigenvalues of a/// sorted into descending order, while v[0..n - 1][0..n - 1] is a matrix whose/// columns contain the corresponding normalized eigenvectors.nrot contains/// the number of Jacobi rotations that were required.Only the upper triangle/// of a is accessed./// </summary>/// <param name="aa"></param>/// <exception cref="Exception"></exception>public Jacobi(double[,] aa){this.n = aa.GetLength(0);this.a = aa;this.v = new double[n, n];this.d = new double[n];this.nrot = 0;this.EPS = float.Epsilon;double[] b = new double[n];double[] z = new double[n];for (int ip = 0; ip < n; ip++){for (int iq = 0; iq < n; iq++){v[ip, iq] = 0.0;}v[ip, ip] = 1.0;}for (int ip = 0; ip < n; ip++){b[ip] = d[ip] = a[ip, ip];z[ip] = 0.0;}for (int i = 1; i <= 50; i++){double sm = 0.0;for (int ip = 0; ip < n - 1; ip++){for (int iq = ip + 1; iq < n; iq++){sm += Math.Abs(a[ip, iq]);}}//if (sm == 0.0)if (Math.Abs(sm) <= float.Epsilon){eigsrt( d,  v);return;}double tresh;if (i < 4){tresh = 0.2 * sm / (n * n);}else{tresh = 0.0;}for (int ip = 0; ip < n - 1; ip++){for (int iq = ip + 1; iq < n; iq++){double g = 100.0 * Math.Abs(a[ip, iq]);if (i > 4 && g <= EPS * Math.Abs(d[ip]) && g <= EPS * Math.Abs(d[iq])){a[ip, iq] = 0.0;}else if (Math.Abs(a[ip, iq]) > tresh){double h = d[iq] - d[ip];double t;if (g <= EPS * Math.Abs(h)){t = (a[ip, iq]) / h;}else{double theta = 0.5 * h / (a[ip, iq]);t = 1.0 / (Math.Abs(theta) + Math.Sqrt(1.0 + theta * theta));if (theta < 0.0){t = -t;}}double c = 1.0 / Math.Sqrt(1 + t * t);double s = t * c;double tau = s / (1.0 + c);h = t * a[ip, iq];z[ip] -= h;z[iq] += h;d[ip] -= h;d[iq] += h;a[ip, iq] = 0.0;for (int j = 0; j < ip; j++){rot( a, s, tau, j, ip, j, iq);}for (int j = ip + 1; j < iq; j++){rot( a, s, tau, ip, j, j, iq);}for (int j = iq + 1; j < n; j++){rot( a, s, tau, ip, j, iq, j);}for (int j = 0; j < n; j++){rot( v, s, tau, j, ip, j, iq);}++nrot;}}}for (int ip = 0; ip < n; ip++){b[ip] += z[ip];d[ip] = b[ip];z[ip] = 0.0;}}throw new Exception("Too many iterations in routine jacobi");}public void rot(double[,] a, double s, double tau, int i, int j, int k, int l){double g = a[i, j];double h = a[k, l];a[i, j] = g - s * (h + g * tau);a[k, l] = h + s * (g - h * tau);}/// <summary>/// Given the eigenvalues d[0..n - 1] and(optionally) the eigenvectors/// v[0..n - 1][0..n - 1] as determined by Jacobi or tqli, this routine sorts the/// eigenvalues into descending order and rearranges the columns of v/// correspondingly.The method is straight insertion./// </summary>/// <param name="d"></param>/// <param name="v"></param>public static void eigsrt(double[] d, double[,] v){int k;int n = d.Length;for (int i = 0; i < n - 1; i++){double p = d[k = i];for (int j = i; j < n; j++){if (d[j] >= p){p = d[k = j];}}if (k != i){d[k] = d[i];d[i] = p;if (v != null){for (int j = 0; j < n; j++){p = v[j, i];v[j, i] = v[j, k];v[j, k] = p;}}}}}}
}

这篇关于C#,数值计算——计算实对称矩阵所有特征值和特征向量的雅可比(Jacobi)方法与源程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/457011

相关文章

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

Python中的魔术方法__new__详解

《Python中的魔术方法__new__详解》:本文主要介绍Python中的魔术方法__new__的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、核心意义与机制1.1 构造过程原理1.2 与 __init__ 对比二、核心功能解析2.1 核心能力2.2

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt