C#,数值计算——Globals的计算方法与源程序

news2025/4/8 2:50:20

 

1 文本格式

using System;
using System.Text;

namespace Legalsoft.Truffer
{
    public static partial class Globals
    {
        //const int FLT_RADIX = 2;
        //const int DBL_MANT_DIG = 53;
        //const int INT_DIGITS = 32;
        //const float FLT_EPSILON = 1.19209290E-07F;
        //const double DBL_EPSILON = 2.2204460492503131E-16;

        public static double SQR(double a)
        {
            return a * a;
        }

        /// <summary>
        /// 浮点数取余数的函数
        /// https://blog.csdn.net/Hanford/article/details/53633937
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static double fmod(double x, double y)
        {
            y = Math.Abs(y);
            if (x >= 0.0)
            {
                y = x - y * Math.Floor(x / y);
            }
            else
            {
                y = x - y * Math.Ceiling(x / y);
            }
            return y;
        }

        public static double SIGN(double a, double b)
        {
            return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);
        }

        public static void SWAP(ref int a, ref int b)
        {
            (a, b) = (b, a);
        }

        public static void SWAP(ref double a, ref double b)
        {
            (a, b) = (b, a);
        }

        /// <summary>
        /// 数组复制(int)
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int[] CopyFrom(int[] b)
        {
            int[] v = new int[b.Length];
            for (int i = 0; i < v.Length; i++)
            {
                v[i] = b[i];
            }
            return v;
        }

        /// <summary>
        /// 数组复制(double)
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double[] CopyFrom(double[] b)
        {
            double[] v = new double[b.Length];
            for (int i = 0; i < v.Length; i++)
            {
                v[i] = b[i];
            }
            return v;
        }

        /// <summary>
        /// 数组转(方)矩阵(int)
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double[,] CopyFrom(int row, int col, double[] b)
        {
            double[,] v = new double[row, col];
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    v[i, j] = b[i * col + j];
                }
            }
            return v;
        }

        /// <summary>
        /// 矩阵复制(double)
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double[,] CopyFrom(double[,] b)
        {
            int nn = b.GetLength(0);
            int mm = b.GetLength(1);
            double[,] v = new double[nn, mm];
            for (int i = 0; i < nn; i++)
            {
                for (int j = 0; j < mm; j++)
                {
                    v[i, j] = b[i, j];
                }
            }
            return v;
        }

        /// <summary>
        /// 复制矩阵b的第k行
        /// </summary>
        /// <param name="k"></param>
        /// <param name="b"></param>
        /// <returns>数组</returns>
        public static double[] CopyFrom(int k, double[,] b)
        {
            int mm = b.GetLength(1);
            double[] v = new double[mm];
            for (int j = 0; j < mm; j++)
            {
                v[j] = b[k, j];
            }
            return v;
        }

        /// <summary>
        /// 提取三元矩阵的第k层的矩阵
        /// </summary>
        /// <param name="k"></param>
        /// <param name="b"></param>
        /// <returns>矩阵</returns>
        public static double[,] CopyFrom(int k, double[,,] b)
        {
            int nn = b.GetLength(1);
            int mm = b.GetLength(2);
            double[,] v = new double[nn, mm];
            for (int i = 0; i < nn; i++)
            {
                for (int j = 0; j < mm; j++)
                {
                    v[i, j] = b[k, i, j];
                }
            }
            return v;
        }

        public static double dist(double[] p1, double[] p2)
        {
            double sum = 0.0;
            for (int i = 0; i < p1.Length; i++)
            {
                sum += Globals.SQR(p1[i] - p2[i]);
            }
            if (sum <= float.Epsilon)
            {
                return 0.0;
            }
            return Math.Sqrt(sum);
        }

        public static double ldexp(double v, int exp)
        {
            return v * Math.Pow(2.0, exp);
        }

        public static double frexp(double x, out int exp)
        {
            if (Math.Abs(x) <= float.Epsilon)
            {
                exp = 0;
                return 0.0;
            }
            long bits = BitConverter.DoubleToInt64Bits(x);
            ulong u1 = 0x800fffffffffffffL;
            ulong u2 = (ulong)bits;
            long r = (long)(u1 & u2);
            double d = BitConverter.Int64BitsToDouble(r | 0x3fe0000000000000L);
            exp = (int)((0x7ff0000000000000L & bits) >> 52) - 1022;
            return d;
        }

        public static double gammln(double xx)
        {
            double[] cof = { 57.1562356658629235, -59.5979603554754912, 14.1360979747417471, -0.491913816097620199, .339946499848118887e-4, .465236289270485756e-4, -.983744753048795646e-4, .158088703224912494e-3, -.210264441724104883e-3, .217439618115212643e-3, -.164318106536763890e-3, .844182239838527433e-4, -.261908384015814087e-4, .368991826595316234e-5 };
            if (xx <= 0)
            {
                throw new Exception("bad arg in gammln");
            }
            double x = xx;
            double y = xx;
            double tmp = x + 5.24218750000000000;
            tmp = (x + 0.5) * Math.Log(tmp) - tmp;
            double ser = 0.999999999999997092;
            for (int j = 0; j < 14; j++)
            {
                ser += cof[j] / ++y;
            }
            return tmp + Math.Log(2.5066282746310005 * ser / x);
        }

        private static double[] factrl_a;
        private static bool factrl_init = true;

        public static double factrl(int n)
        {
            if (factrl_init)
            {
                factrl_init = false;
                factrl_a = new double[171];
                factrl_a[0] = 1.0;
                for (int i = 1; i < 171; i++)
                {
                    factrl_a[i] = i * factrl_a[i - 1];
                }
            }
            if (n < 0 || n > 170)
            {
                throw new Exception("factrl out of range");
            }
            return factrl_a[n];
        }

        private static double[] factln_a = new double[2000];
        private static bool factln_init = true;

        public static double factln(int n)
        {
            const int NTOP = 2000;
            if (factln_init)
            {
                factln_init = false;
                for (int i = 0; i < NTOP; i++)
                {
                    factln_a[i] = gammln(i + 1.0);
                }
            }
            if (n < 0)
            {
                throw new Exception("negative arg in factln");
            }
            if (n < NTOP)
            {
                return factln_a[n];
            }
            return gammln(n + 1.0);
        }

        public static double bico(int n, int k)
        {
            if (n < 0 || k < 0 || k > n)
            {
                throw new Exception("bad args in bico");
            }
            if (n < 171)
            {
                return Math.Floor(0.5 + factrl(n) / (factrl(k) * factrl(n - k)));
            }
            return Math.Floor(0.5 + Math.Exp(factln(n) - factln(k) - factln(n - k)));
        }

        public static double beta(double z, double w)
        {
            return Math.Exp(gammln(z) + gammln(w) - gammln(z + w));
        }

        public static string ToString(double[] x)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < x.Length; i++)
            {
                sb.AppendFormat("{0:F12},", x[i]);
            }
            sb.AppendLine("<br>");
            return sb.ToString();
        }

        public static string ToString(double[,] x)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<style>td { padding:5px; } </style>");            
            sb.AppendLine("<table border=1 bordercolor='#888888' style='border-collapse:collapse;'>");
            for (int i = 0; i < x.GetLength(0); i++)
            {
                sb.AppendLine("<tr>");
                for (int j = 0; j < x.GetLength(1); j++)
                {
                    sb.AppendFormat("<td>{0:F12}</td>", x[i, j]);
                }
                sb.AppendLine("</tr>");
            }
            sb.AppendLine("</table>");
            sb.AppendLine("<br>");
            return sb.ToString();
        }
    }
}
 

2 代码格式

using System;
using System.Text;

namespace Legalsoft.Truffer
{
    public static partial class Globals
    {
        //const int FLT_RADIX = 2;
        //const int DBL_MANT_DIG = 53;
        //const int INT_DIGITS = 32;
        //const float FLT_EPSILON = 1.19209290E-07F;
        //const double DBL_EPSILON = 2.2204460492503131E-16;

        public static double SQR(double a)
        {
            return a * a;
        }

        /// <summary>
        /// 浮点数取余数的函数
        /// https://blog.csdn.net/Hanford/article/details/53633937
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static double fmod(double x, double y)
        {
            y = Math.Abs(y);
            if (x >= 0.0)
            {
                y = x - y * Math.Floor(x / y);
            }
            else
            {
                y = x - y * Math.Ceiling(x / y);
            }
            return y;
        }

        public static double SIGN(double a, double b)
        {
            return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);
        }

        public static void SWAP(ref int a, ref int b)
        {
            (a, b) = (b, a);
        }

        public static void SWAP(ref double a, ref double b)
        {
            (a, b) = (b, a);
        }

        /// <summary>
        /// 数组复制(int)
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int[] CopyFrom(int[] b)
        {
            int[] v = new int[b.Length];
            for (int i = 0; i < v.Length; i++)
            {
                v[i] = b[i];
            }
            return v;
        }

        /// <summary>
        /// 数组复制(double)
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double[] CopyFrom(double[] b)
        {
            double[] v = new double[b.Length];
            for (int i = 0; i < v.Length; i++)
            {
                v[i] = b[i];
            }
            return v;
        }

        /// <summary>
        /// 数组转(方)矩阵(int)
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double[,] CopyFrom(int row, int col, double[] b)
        {
            double[,] v = new double[row, col];
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    v[i, j] = b[i * col + j];
                }
            }
            return v;
        }

        /// <summary>
        /// 矩阵复制(double)
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double[,] CopyFrom(double[,] b)
        {
            int nn = b.GetLength(0);
            int mm = b.GetLength(1);
            double[,] v = new double[nn, mm];
            for (int i = 0; i < nn; i++)
            {
                for (int j = 0; j < mm; j++)
                {
                    v[i, j] = b[i, j];
                }
            }
            return v;
        }

        /// <summary>
        /// 复制矩阵b的第k行
        /// </summary>
        /// <param name="k"></param>
        /// <param name="b"></param>
        /// <returns>数组</returns>
        public static double[] CopyFrom(int k, double[,] b)
        {
            int mm = b.GetLength(1);
            double[] v = new double[mm];
            for (int j = 0; j < mm; j++)
            {
                v[j] = b[k, j];
            }
            return v;
        }

        /// <summary>
        /// 提取三元矩阵的第k层的矩阵
        /// </summary>
        /// <param name="k"></param>
        /// <param name="b"></param>
        /// <returns>矩阵</returns>
        public static double[,] CopyFrom(int k, double[,,] b)
        {
            int nn = b.GetLength(1);
            int mm = b.GetLength(2);
            double[,] v = new double[nn, mm];
            for (int i = 0; i < nn; i++)
            {
                for (int j = 0; j < mm; j++)
                {
                    v[i, j] = b[k, i, j];
                }
            }
            return v;
        }

        public static double dist(double[] p1, double[] p2)
        {
            double sum = 0.0;
            for (int i = 0; i < p1.Length; i++)
            {
                sum += Globals.SQR(p1[i] - p2[i]);
            }
            if (sum <= float.Epsilon)
            {
                return 0.0;
            }
            return Math.Sqrt(sum);
        }

        public static double ldexp(double v, int exp)
        {
            return v * Math.Pow(2.0, exp);
        }

        public static double frexp(double x, out int exp)
        {
            if (Math.Abs(x) <= float.Epsilon)
            {
                exp = 0;
                return 0.0;
            }
            long bits = BitConverter.DoubleToInt64Bits(x);
            ulong u1 = 0x800fffffffffffffL;
            ulong u2 = (ulong)bits;
            long r = (long)(u1 & u2);
            double d = BitConverter.Int64BitsToDouble(r | 0x3fe0000000000000L);
            exp = (int)((0x7ff0000000000000L & bits) >> 52) - 1022;
            return d;
        }

        public static double gammln(double xx)
        {
            double[] cof = { 57.1562356658629235, -59.5979603554754912, 14.1360979747417471, -0.491913816097620199, .339946499848118887e-4, .465236289270485756e-4, -.983744753048795646e-4, .158088703224912494e-3, -.210264441724104883e-3, .217439618115212643e-3, -.164318106536763890e-3, .844182239838527433e-4, -.261908384015814087e-4, .368991826595316234e-5 };
            if (xx <= 0)
            {
                throw new Exception("bad arg in gammln");
            }
            double x = xx;
            double y = xx;
            double tmp = x + 5.24218750000000000;
            tmp = (x + 0.5) * Math.Log(tmp) - tmp;
            double ser = 0.999999999999997092;
            for (int j = 0; j < 14; j++)
            {
                ser += cof[j] / ++y;
            }
            return tmp + Math.Log(2.5066282746310005 * ser / x);
        }

        private static double[] factrl_a;
        private static bool factrl_init = true;

        public static double factrl(int n)
        {
            if (factrl_init)
            {
                factrl_init = false;
                factrl_a = new double[171];
                factrl_a[0] = 1.0;
                for (int i = 1; i < 171; i++)
                {
                    factrl_a[i] = i * factrl_a[i - 1];
                }
            }
            if (n < 0 || n > 170)
            {
                throw new Exception("factrl out of range");
            }
            return factrl_a[n];
        }

        private static double[] factln_a = new double[2000];
        private static bool factln_init = true;

        public static double factln(int n)
        {
            const int NTOP = 2000;
            if (factln_init)
            {
                factln_init = false;
                for (int i = 0; i < NTOP; i++)
                {
                    factln_a[i] = gammln(i + 1.0);
                }
            }
            if (n < 0)
            {
                throw new Exception("negative arg in factln");
            }
            if (n < NTOP)
            {
                return factln_a[n];
            }
            return gammln(n + 1.0);
        }

        public static double bico(int n, int k)
        {
            if (n < 0 || k < 0 || k > n)
            {
                throw new Exception("bad args in bico");
            }
            if (n < 171)
            {
                return Math.Floor(0.5 + factrl(n) / (factrl(k) * factrl(n - k)));
            }
            return Math.Floor(0.5 + Math.Exp(factln(n) - factln(k) - factln(n - k)));
        }

        public static double beta(double z, double w)
        {
            return Math.Exp(gammln(z) + gammln(w) - gammln(z + w));
        }

        public static string ToString(double[] x)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < x.Length; i++)
            {
                sb.AppendFormat("{0:F12},", x[i]);
            }
            sb.AppendLine("<br>");
            return sb.ToString();
        }

        public static string ToString(double[,] x)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<style>td { padding:5px; } </style>");            
            sb.AppendLine("<table border=1 bordercolor='#888888' style='border-collapse:collapse;'>");
            for (int i = 0; i < x.GetLength(0); i++)
            {
                sb.AppendLine("<tr>");
                for (int j = 0; j < x.GetLength(1); j++)
                {
                    sb.AppendFormat("<td>{0:F12}</td>", x[i, j]);
                }
                sb.AppendLine("</tr>");
            }
            sb.AppendLine("</table>");
            sb.AppendLine("<br>");
            return sb.ToString();
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1149261.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

精密空调监控方法,让你一次全学会!

随着科技的迅猛发展&#xff0c;越来越多的领域和行业对温度和湿度等环境参数的高度控制需求日益增长。无论在哪里&#xff0c;对精密空调的依赖都在不断增加。 然而&#xff0c;要确保这些系统的高效运行和监测已经变得愈发复杂。为了解决这些挑战&#xff0c;精密空调监控系统…

【java学习—十】TreeSet集合(5)

文章目录 1. TreeSet1.1. 自然排序1.2. 定制排序 1. TreeSet TreeSet 是 SortedSet 接口的实现类&#xff0c; TreeSet 可以确保集合元素处于排序状态。     TreeSet 支持两种排序方法&#xff1a;自然排序和定制排序。默认情况下&#xff0c; TreeSet 采用自然排序。 1.1.…

java项目之学校招生信息网(ssm)

项目简介 学校招生信息网实现了以下功能&#xff1a; 管理员&#xff1a;个人中心、学生管理&#xff0c;党员风采管理&#xff0c;校园之家管理&#xff0c;师资队伍管理&#xff0c;院系简介管理&#xff0c;专业简介管理&#xff0c;录取信息管理、学生录取管理、系统管理…

【ChatGPT瀑布到水母】AI 在驱动软件研发的革新与实践

这里写目录标题 前言内容简介作者简介专家推荐读者对象目录直播预告 前言 计算机技术的发展和互联网的普及&#xff0c;使信息处理和传输变得更加高效&#xff0c;极大地改变了金融、商业、教育、娱乐等领域的运作方式。数据分析、人工智能和云计算等新兴技术&#xff0c;也在不…

Egg.js 中 Controller 的使用

如果需要了解Controller &#xff0c;就得知道什么是mvc。 MVC概述 什么是mvc&#xff0c;这个概念&#xff0c;我相信绝大部分人肯定是了解的。MVC是模型&#xff08;model&#xff09;- 视图&#xff08;view&#xff09;- 控制器&#xff08;controller&#xff09;的缩写…

git跳过用户名密码验证,以及配置credential-helper

平时我们在使用git命令时&#xff0c;如果使用http方式拉取代码每次都需要使用填写用户名和密码&#xff0c;非常的麻烦。 如何才能绕过每次繁琐的填充? 如果想要绕过git的交互方式&#xff0c;首先需要了解git的密码存储机制。 git使用的使用是一种名叫**[credential helpe…

ssm搭建后404的错误

spring、mybatis 的配置文件一般不会出错&#xff08;注解扫描路径要涵盖 controller层&#xff09;&#xff0c;controller 层 url 一般也不会写错&#xff0c;出现404&#xff0c;更容易忽略的是 文件路径位置。 首先是 idea 提示 servlet-mapping 找不到 , 我这里显然是有的…

Java-PriorityQueue 优先队列(结构与用法)

1. 数据结构 1.1 2.Java使用 2.1 核心要点 PriorityQueue是一个无限制的队列&#xff0c;并且动态增长。默认情况下&#xff0c;优先级队列的对象按自然顺序排序。PriorityQueue 不是线程安全的。多线程情况下可以使用PriorityBlockingQueue。 2.2 构造函数 PriorityQueu…

如何去掉Word文档中蓝色和红色波浪线

在Word2013文档中有些文字或者字母的下面会出现一些红色或蓝色的波浪线&#xff0c;这是因为Word会自动检查文档中拼写和语法错误所造成的&#xff0c;那么如何去掉这些蓝色红色的波浪线呢&#xff1f;下面就跟大家分享一下方法。 工具/原料 Word2013 方法/步骤 1 首先打开一个…

Widget必须在GUI线程中创建

背景&#xff1a;miniblink的vip版本&#xff0c;下载功能是独立线程&#xff0c;我希望在下载后弹出窗口&#xff0c;就在其中创建了QWidget子类对象。然后出现了上面的错误。 解决方法&#xff1a; 使用信号和槽来处理。 具体来讲&#xff0c;在独立线程中创建QObject子类…

【波形图】LabVIEW中的波形图和波形图表有什么区别?

波形图和波形图表在显示和更新数据的方式上有所不同。 波形图可接受各种类型的数据阵列&#xff0c;例如数组&#xff0c;波形或动态数据。波形图在接收到数据后将立即绘制所有接收到的数据点 。波形图不接受单点值。当您将包含数据点的数组连接到波形图时&#xff0c;波形图会…

深度学习中的FPN详解

深度学习入门小菜鸟&#xff0c;希望像做笔记记录自己学的东西&#xff0c;也希望能帮助到同样入门的人&#xff0c;更希望大佬们帮忙纠错啦~侵权立删。 目录 一、FPN提出原因 二、FPN的参考思想 三、特征金字塔 四、FPN具体思路 一、FPN提出原因 卷积网络中&#xff0c;深层网…

IDEA 使用技巧

文章目录 语言支持简化编写 有问题&#xff0c;可暂时跳过 个人常用快捷键插件主题插件功能插件 碰到过的问题 除了一些在Linux上用vim开发的大佬&#xff0c;idea算是很友好的集成开发工具了&#xff0c;功能全面&#xff0c;使用也很广泛。 记录一下我的 IDEA 使用技巧&#…

省钱兄短剧短视频视频滑动播放模块源码支持微信小程序h5安卓IOS

# 开源说明 开源省钱兄短剧系统的播放视频模块&#xff08;写了测试弄了好久才弄出来、最核心的模块、已经实战了&#xff09;&#xff0c;使用uniapp技术&#xff0c;提供学习使用&#xff0c;支持IOSAndroidH5微信小程序&#xff0c;使用Hbuilder导入即可运行 #注意&#xff…

1.2 OSI安全架构

思维导图&#xff1a; 1.2 OSI安全架构 为了有效评估组织的安全需求并选择合适的安全产品和政策&#xff0c;安全管理员需要系统化地定义安全需求并描述满足这些需求的措施。这在集中式数据处理环境下就已经很困难&#xff0c;而在今天使用的局域网和广域网中更是如此。 OSI安…

Undefined reference错误处理及Linux设置动态链接库so的默认搜索路径

文章目录 1 问题的提出2 问题的分析3 问题的解决3.1 Windows的VS修改配置属性3.2 Linux系统里添加搜索路径json在/usr/llib目录中libcryto.so在/usr/lib64文件夹中 Linux设置动态链接库so的默认搜索路径方法一&#xff1a;修改 ld.so.conf 文件方法二&#xff1a;修改环境变量方…

3分钟搞定腾讯云轻量应用服务器和云服务器的区别

腾讯云轻量服务器和云服务器有什么区别&#xff1f;为什么轻量应用服务器价格便宜&#xff1f;是因为轻量服务器CPU内存性能比云服务器CVM性能差吗&#xff1f;轻量应用服务器适合中小企业或个人开发者搭建企业官网、博客论坛、微信小程序或开发测试环境&#xff0c;云服务器CV…

CentOS停更沉寂,RHEL巨变限制源代:Docker容器化技术的兴起助力操作系统新格局

一、概述 操作系统是计算机系统的核心软件&#xff0c;它管理和控制着计算机的硬件和软件资源&#xff0c;为用户和应用程序提供了一个统一、高效、安全的运行环境。操作系统的发展历史也是计算机技术的发展历史的重要组成部分&#xff0c;它见证了计算机从单机到网络&#xf…

GaussDB SQL基础语法示例-常见的条件表达式

目录 一、前言 二、条件表达式的概念及GaussDB中的常见条件表达式 三、GaussDB中常用的条件表达式&#xff08;语法 示例&#xff09; 1、CASE表达式 2、DECODE表达式 3、COALESCE表达式 4、NULLIF表达式 5、GREATEST/ LEAST表达式 6、NVL表达式 四、小结 一、前言 …

聚观早报 |蔚来推出婚车服务;长城汽车第三季度财报

【聚观365】10月30日消息 蔚来推出婚车服务 长城汽车第三季度财报 AI汽车机器人极越01上市 谷歌投资初创公司Anthropic 东方财富第三季度营收 蔚来推出婚车服务 据蔚来汽车官方消息&#xff0c;蔚来宣布推出“蔚来用户专享”的婚庆用车定制服务。 据悉&#xff0c;该服务…