Tech.Numerics 2026.4.6

dotnet add package Tech.Numerics --version 2026.4.6
                    
NuGet\Install-Package Tech.Numerics -Version 2026.4.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tech.Numerics" Version="2026.4.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tech.Numerics" Version="2026.4.6" />
                    
Directory.Packages.props
<PackageReference Include="Tech.Numerics" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tech.Numerics --version 2026.4.6
                    
#r "nuget: Tech.Numerics, 2026.4.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tech.Numerics@2026.4.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tech.Numerics&version=2026.4.6
                    
Install as a Cake Addin
#tool nuget:?package=Tech.Numerics&version=2026.4.6
                    
Install as a Cake Tool

Tech.Numerics API 文档

Tech.Numerics 是高性能矩阵运算库,基于 Tech.Core 的 MemoryArray<double> 实现行优先存储,提供矩阵创建、算术运算、转置乘法、相等性比较等核心线性代数能力。

Tech.Numerics 依赖 Tech.Core,安装时会自动引入。

Matrix

Tech.Numerics 命名空间提供密封的 Matrix 类,继承 MemoryArray<double> 并实现 IEquatable<Matrix>

构造函数

using Tech.Numerics;

// 零矩阵(内部使用)
// var m = new Matrix(rows, columns);

// 填充矩阵
var m1 = new Matrix(3, 3, 1.0);              // 3x3 全 1 矩阵

// 从一维数组创建
var m2 = new Matrix(2, 3, new[] { 1, 2, 3, 4, 5, 6 });

// 从二维数组创建
var m3 = new Matrix(new double[,] { { 1, 2 }, { 3, 4 } });

// 拷贝构造
var m4 = new Matrix(m3);

// 零拷贝包装现有数组
var m5 = Matrix.Wrap(new[] { 1.0, 2.0, 3.0, 4.0 }, 2, 2);

属性

属性 类型 说明
Rows int 行数
Columns int 列数
IsSquare bool 是否为方阵
IsRowVector bool 是否为行向量(1 行)
IsColumnVector bool 是否为列向量(1 列)
IsVector bool 是否为向量(行或列)
Length int 元素总数(继承自 MemoryArray<double>

元素访问

var m = new Matrix(2, 2, new double[,] { { 1, 2 }, { 3, 4 } });

// 读写元素
double value = m[0, 1];   // 2
m[1, 0] = 5.0;

// 获取行视图
Span<double> row = m.GetRow(1);  // [5.0, 4.0]

// 内部快速访问(无边界检查)
double v = m.GetAt(0, 0);
m.SetAt(0, 0, 10.0);

矩阵操作

var m = new Matrix(3, 3, 1.0);

// 清除(全部置零)
m.Clear();

// 填充
m.Fill(5.0);

// 复制
m.CopyTo(destinationSpan);
m.CopyTo(otherMatrix);

// 克隆
var clone = m.Clone();

// 转换为数组
double[] flat = m.ToArray();          // 一维
double[,] arr2D = m.ToArray2D();      // 二维

字符串表示

var m = new Matrix(2, 2, new double[,] { { 1, 2 }, { 3, 4 } });
Console.WriteLine(m);
// [[1.0000, 2.0000]
//  [3.0000, 4.0000]]

算术运算

运算符重载

var a = new Matrix(2, 2, new double[,] { { 1, 2 }, { 3, 4 } });
var b = new Matrix(2, 2, new double[,] { { 5, 6 }, { 7, 8 } });

// 一元
var p = +a;   // 浅拷贝
var n = -a;   // 取反

// 矩阵 ± 矩阵
var sum  = a + b;
var diff = a - b;

// 矩阵 * 矩阵
var prod = a * b;

// 矩阵 ± 标量
var s1 = a + 2.0;
var s2 = a - 1.0;

// 标量 ± 矩阵
var s3 = 2.0 + a;
var s4 = 5.0 - a;

// 矩阵 * / 标量
var s5 = a * 3.0;
var s6 = a / 2.0;

// 标量 * / 矩阵
var s7 = 3.0 * a;
var s8 = 6.0 / a;

方法调用

所有算术方法均提供两种形式:返回新矩阵,或写入已有 result 矩阵。

加法
// 矩阵 + 标量
var r1 = a.Add(2.0);
a.Add(2.0, result);

// 矩阵 + 矩阵
var r2 = a.Add(b);
a.Add(b, result);
减法
// 矩阵 - 标量
var r1 = a.Subtract(1.0);
a.Subtract(1.0, result);

// 标量 - 矩阵
var r2 = a.SubtractFrom(5.0);
a.SubtractFrom(5.0, result);

// 矩阵 - 矩阵
var r3 = a.Subtract(b);
a.Subtract(b, result);
乘法
// 矩阵 * 标量
var r1 = a.Multiply(3.0);
a.Multiply(3.0, result);

// 矩阵 * 矩阵(要求 a.Columns == b.Rows)
var r2 = a.Multiply(b);
a.Multiply(b, result);
除法
// 矩阵 / 标量
var r1 = a.Divide(2.0);
a.Divide(2.0, result);

// 标量 / 矩阵
var r2 = a.DivideByThis(6.0);
a.DivideByThis(6.0, result);
取反
var r = a.Negate();
a.Negate(result);

转置乘法

避免显式转置的中间矩阵分配,适用于高维矩阵运算。

var a = new Matrix(3, 4, 1.0);  // 3x4
var b = new Matrix(5, 4, 2.0);  // 5x4

// this * otherᵀ(要求 Columns == other.Columns)
var r1 = a.TransposeAndMultiply(b);          // 3x5
a.TransposeAndMultiply(b, result);

// thisᵀ * other(要求 Rows == other.Rows)
var r2 = a.TransposeThisAndMultiply(b);      // 仅当 b 为 4xN 时可用
a.TransposeThisAndMultiply(b, result);

相等性比较

var a = new Matrix(2, 2, new double[,] { { 1, 2 }, { 3, 4 } });
var b = new Matrix(a);

// 精确相等(逐元素比较)
bool equal = a.Equals(b);              // true
bool objEq = a.Equals((object)b);      // true
bool opEq  = a == null;                // false

// 近似相等(浮点容差)
bool approx = a.ApproxEquals(b, tolerance: 1e-10);

// 哈希码
int hash = a.GetHashCode();

待实现功能

以下 API 已声明但尚未实现,调用将抛出 NotImplementedException

API 说明
DoAdd 加法核心计算
DoSubtract 减法核心计算
DoMultiply 乘法核心计算
DoDivide 除法核心计算
DoNegate 取反核心计算
DoTransposeAndMultiply 转置乘法核心计算
DoTransposeThisAndMultiply 自身转置乘法核心计算
Inverse() 矩阵求逆
Trace() 矩阵迹
/ 矩阵÷矩阵 矩阵除法运算符
% 取模运算符 矩阵取模

MatrixDimensions

Tech.Numerics.Dimensions 命名空间提供矩阵维度的记录结构。

var dim = new MatrixDimensions(3, 4);

// 属性
int count = dim.ElementCount;          // 12
bool square = dim.IsSquare;            // false
bool vector = dim.IsVector;            // false
bool rowVec = dim.IsRowVector;         // false
bool colVec = dim.IsColumnVector;      // false
long bytes = dim.SizeInBytes;          // 96

// 工厂方法
var sq = MatrixDimensions.Square(3);              // 3x3
var dim2 = MatrixDimensions.Create(3, 4);         // 3x4

// 转置
var transposed = dim.Transposed();                // 4x3

// 维度验证
bool canMul = dim.CanMultiplyWith(other);         // Columns == other.Rows
bool canEW  = dim.CanElementWiseOpWith(other);    // 同维度
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.4.6 126 4/6/2026