当前位置: 首页 > news >正文

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;}}}}}}
}

相关文章:

  • 源码安装git
  • 【BUG】SpringBoot项目Long类型数据返回前端精度丢失问题
  • Android Studio Giraffe版本遇到的问题
  • Backend - Django makemigrations
  • es常用查询编辑
  • SpringBoot集成i18n(多语言)
  • 精密制造ERP系统包含哪些模块?精密制造ERP软件是做什么的
  • java使用xstream框架生成xml文件
  • 5.【自动驾驶与机器人中的SLAM技术】2D点云的scan matching算法 和 检测退化场景的思路
  • Unity中Shader编译目标渲染器
  • Java转Go学习之旅 | Go入门(2)
  • Java零基础-if条件语句
  • 雷军:我的程序人生路
  • JS的监听事件
  • 企业微信协议开发,API接口调用
  • [笔记] php常见简单功能及函数
  • ES10 特性的完整指南
  • Git的一些常用操作
  • linux安装openssl、swoole等扩展的具体步骤
  • Next.js之基础概念(二)
  • php的插入排序,通过双层for循环
  • springboot_database项目介绍
  • webgl (原生)基础入门指南【一】
  • 新书推荐|Windows黑客编程技术详解
  • 一些关于Rust在2019年的思考
  • 怎么将电脑中的声音录制成WAV格式
  • 湖北分布式智能数据采集方法有哪些?
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • #《AI中文版》V3 第 1 章 概述
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)...
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (简单) HDU 2612 Find a way,BFS。
  • (亲测有效)解决windows11无法使用1500000波特率的问题
  • (十一)图像的罗伯特梯度锐化
  • (实战篇)如何缓存数据
  • (万字长文)Spring的核心知识尽揽其中
  • (一一四)第九章编程练习
  • (转)Linq学习笔记
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • .Family_物联网
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .net core 控制台应用程序读取配置文件app.config
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .Net Core 中间件验签
  • .net Signalr 使用笔记
  • .NET 中使用 Mutex 进行跨越进程边界的同步
  • .netcore如何运行环境安装到Linux服务器
  • .Net程序帮助文档制作
  • .NET开发人员必知的八个网站
  • .vimrc php,修改home目录下的.vimrc文件,vim配置php高亮显示
  • /etc/sudoers (root权限管理)
  • @angular/cli项目构建--http(2)