C#,数值计算——Globals的计算方法与源程序
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();}}
}
相关文章:

C#,数值计算——Globals的计算方法与源程序
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_…...

腾讯云香港服务器轻量24元一个月性能测试
腾讯云香港轻量应用服务器优惠价格24元一个月,一年288元,以前是30M峰值带宽,现在是20M峰值带宽,阿腾云atengyun.com分享腾讯云香港轻量应用服务器性能测评,包括香港轻量服务器配置价格表、CPU性能和CN2网络延迟测试&am…...

深度学习之基于YoloV8的行人跌倒目标检测系统
欢迎大家点赞、收藏、关注、评论啦 ,由于篇幅有限,只展示了部分核心代码。 文章目录 一项目简介 二、功能三、行人跌倒目标检测系统四. 总结 一项目简介 世界老龄化趋势日益严重,现代化的生活习惯又使得大多数老人独居,统计数据表…...

Seata入门系列【16】XA模式入门案例
1 前言 在之前,我们试过了AT、TCC 模式,Seata 还支持XA 模式。 2 XA 协议 XA协议由Tuxedo首先提出的,并交给X/Open组织,作为资源管理器(数据库)与事务管理器的接口标准。Oracle、Informix、DB2和Sybase等…...

高级深入--day44
Scrapy 和 scrapy-redis的区别 Scrapy 是一个通用的爬虫框架,但是不支持分布式,Scrapy-redis是为了更方便地实现Scrapy分布式爬取,而提供了一些以redis为基础的组件(仅有组件)。 pip install scrapy-redis Scrapy-redis提供了下面四种组件&a…...

Apache Doris (四十八): Doris表结构变更-替换表
🏡 个人主页:IT贫道_大数据OLAP体系技术栈,Apache Doris,Clickhouse 技术-CSDN博客 🚩 私聊博主:加入大数据技术讨论群聊,获取更多大数据资料。 🔔 博主个人B栈地址:豹哥教你大数据的个人空间-豹哥教你大数据个人主页-哔哩哔哩视频 目录...

消息认证码--数字签名--证书
6. 消息认证码—>保证数据的完整性 "消息认证码 --- 消息被正确传送了吗?"6.1 什么是消息认证码 Alice 和 Bob 的故事 像以前一样,我们还是从一个Alice和Bob的故事开始讲起。不过,这一次Alice和Bob分别是两家银行,Alice银行通…...

四个制作PPT的小技巧
制作PPT已经很麻烦了,学习一些小技巧可以帮助我们省时省力吧! 技巧一:自动更新日期和时间 当我们给幻灯片添加了页脚并且是时间日期,可以通过设置达到自动更新,这样我们就不需要每次修改的时候都要手动更新日期和时间…...
Echarts饼状图grid设置
饼状图不能设置grid,而是center {type: "pie",radius: ["30%", "70%"],center: ["50%", "25%"], }center 圆心:控制圆的位置 radius 饼图的半径 控制显示尺寸 参考文章 Echarts饼状图设置...
系列三、Spring IOC
一、概述 IOC的中文意思是控制反转,通俗地讲就是把创建对象的控制权交给了Spring去管理,以前是由程序员自己去创建控制对象,现在交由Spring去创建控制。 二、优点 集中管理对象,方便维护,降低耦合度。 三、IOC的底层…...
electron汇总
python3自带了pip pip search已经被禁用,安装pip—— pip install pip-searchpython3.x移除了distutils 管理员权限下运行cmd,运行以下命令 // 修改pip镜像地址 pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ // 安装 Set…...

电脑怎么共享屏幕?电脑屏幕共享软件分享!
如何控制某人的电脑屏幕? 有时我们可能需要远程控制某人的计算机屏幕,例如,为我们的客户提供远程支持,远程帮助朋友或家人解决计算机问题,或在家中与同事完成团队合作。那么,电脑怎么共享屏幕ÿ…...
全新一代数字内容体验云平台
随着AIGC能力的提升,企业接入AIGC后将实现数字内容生产的无限供给,如何管理AIGC数字内容将成为话题。 “Baklib是新⼀代企业数字内容体验云平台,包括数字资产及知识库管理、数字应用构建和客户体验,助力企业数字化体验从 IA 扩展…...

目标检测理论知识
目标检测 1.基本概念 目标检测(Object Detection)的任务是找出图像中所有感兴趣的目标(物体),确定它们的类别和位置,是计算机视觉领域的核心问题之一。由于各类物体有不同的外观、形状和姿态,…...

聚观早报 |蔚来推出婚车服务;长城汽车第三季度财报
【聚观365】10月30日消息 蔚来推出婚车服务 长城汽车第三季度财报 AI汽车机器人极越01上市 谷歌投资初创公司Anthropic 东方财富第三季度营收 蔚来推出婚车服务 据蔚来汽车官方消息,蔚来宣布推出“蔚来用户专享”的婚庆用车定制服务。 据悉,该服务…...
垃圾收费站
使用form-data传递数组和x-www-form-urlencoded传递的区别 项目场景: 我将后端接口的一个接收参数设计成了数组,然后前端使用form-data去传递 问题描述 访问的时候出现了问题,后端接收到的数组多出了一层中括号,也就是被两层中括号…...
ElasticSearch 统计搜索热词
实际开发中,我们会统计某个模块下的搜索热词,这个在elasticsearch中特别好用,也比较简单, 使用可以使用 "terms aggregation" 来统计热词 terms 是代表的elasticSerach中的Term Query,统计的就是Term Query, Term Query是一种最基本的查询方式,它用于在Ela…...

el-table(vue2中)滚动条被固定列盖住
一、项目场景: vue2 el-table 二、问题描述 1、现场图片: 2、全局css环境配置了滚动条高度为6px /* 全局滚动条配置 */ ::-webkit-scrollbar {width: 6px;height: 6px; }::-webkit-scrollbar-track {background-color: #f1f1f1; }::-webkit-scrollbar-…...

两数之和(C++解法)
题目 给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。 请你将两个数相加,并以相同形式返回一个表示和的链表。 你可以假设除了数字 0 之外,这两个数都不会…...
SCNet:自校正卷积网络(附代码)
论文地址:https://mftp.mmcheng.net/Papers/20cvprSCNet.pdf 代码地址:https://github.com/MCG-NKU/SCNet 1.是什么? SCNet是一种卷积神经网络,它使用自校准卷积(Self-Calibrated Convolutions)来增强子…...

23-Oracle 23 ai 区块链表(Blockchain Table)
小伙伴有没有在金融强合规的领域中遇见,必须要保持数据不可变,管理员都无法修改和留痕的要求。比如医疗的电子病历中,影像检查检验结果不可篡改行的,药品追溯过程中数据只可插入无法删除的特性需求;登录日志、修改日志…...
多场景 OkHttpClient 管理器 - Android 网络通信解决方案
下面是一个完整的 Android 实现,展示如何创建和管理多个 OkHttpClient 实例,分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...

python/java环境配置
环境变量放一起 python: 1.首先下载Python Python下载地址:Download Python | Python.org downloads ---windows -- 64 2.安装Python 下面两个,然后自定义,全选 可以把前4个选上 3.环境配置 1)搜高级系统设置 2…...
【位运算】消失的两个数字(hard)
消失的两个数字(hard) 题⽬描述:解法(位运算):Java 算法代码:更简便代码 题⽬链接:⾯试题 17.19. 消失的两个数字 题⽬描述: 给定⼀个数组,包含从 1 到 N 所有…...
Qt Widget类解析与代码注释
#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码,写上注释 当然可以!这段代码是 Qt …...

学校招生小程序源码介绍
基于ThinkPHPFastAdminUniApp开发的学校招生小程序源码,专为学校招生场景量身打造,功能实用且操作便捷。 从技术架构来看,ThinkPHP提供稳定可靠的后台服务,FastAdmin加速开发流程,UniApp则保障小程序在多端有良好的兼…...
python如何将word的doc另存为docx
将 DOCX 文件另存为 DOCX 格式(Python 实现) 在 Python 中,你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是,.doc 是旧的 Word 格式,而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...

基于Docker Compose部署Java微服务项目
一. 创建根项目 根项目(父项目)主要用于依赖管理 一些需要注意的点: 打包方式需要为 pom<modules>里需要注册子模块不要引入maven的打包插件,否则打包时会出问题 <?xml version"1.0" encoding"UTF-8…...

Ascend NPU上适配Step-Audio模型
1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统,支持多语言对话(如 中文,英文,日语),语音情感(如 开心,悲伤)&#x…...

c#开发AI模型对话
AI模型 前面已经介绍了一般AI模型本地部署,直接调用现成的模型数据。这里主要讲述讲接口集成到我们自己的程序中使用方式。 微软提供了ML.NET来开发和使用AI模型,但是目前国内可能使用不多,至少实践例子很少看见。开发训练模型就不介绍了&am…...