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

相关文章

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

C#中的 Dictionary常用操作

《C#中的Dictionary常用操作》C#中的DictionaryTKey,TValue是用于存储键值对集合的泛型类,允许通过键快速检索值,并且具有唯一键、动态大小和无序集合的特性,常用操作包括添... 目录基本概念Dictionary的基本结构Dictionary的主要特性Dictionary的常用操作

C# winform操作CSV格式文件

《C#winform操作CSV格式文件》这篇文章主要为大家详细介绍了C#在winform中的表格操作CSV格式文件的相关实例,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录实例一实例效果实现代码效果展示实例二实例效果完整代码实例一实例效果当在winform界面中点击读取按钮时 将csv中

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J