我想使用System.Numerics中的BigInteger类,但如果我想写
using System.Numerics;
Run Code Online (Sandbox Code Playgroud)
Numerics找不到.我在网上搜索,发现我必须添加一个引用System.Numerics.dll,但是我该怎么做呢?
尽管System.Numerics.Vectors可以通过 NuGet 获得的库有自己的视图和投影矩阵函数,但我想自己实现它并只使用向量和矩阵结构。
不幸的是,当我的目标向量与(正确的)视图矩阵相乘时,我已经得到了完全错误的结果。我正在使用右手坐标系和以下说明
var cameraVector4 = new Vector4(0, 4, 2, 1);
var focusVector4 = new Vector4(0, 0, 0, 1);
var vMatrix4 = LookAt(cameraVector4, focusVector4, new Vector4(0, 1.0f, 0, 0));
var targetVector4 = new Vector4(1, 0, -1, 1);
var targetViewVector4 = Vector4.Transform(targetVector4, vMatrix4);
Run Code Online (Sandbox Code Playgroud)
具有以下功能
private static Matrix4x4 LookAt(Vector4 cameraVector4, Vector4 focusVector4, Vector4 upVector4) {
if (cameraVector4 == focusVector4) return Matrix4x4.Identity;
var z = Vector4.Normalize(cameraVector4 - focusVector4);
var x = Vector4.Normalize(upVector4.Cross(z));
var y = Vector4.Normalize(z.Cross(x));
return new Matrix4x4(
x.X, …Run Code Online (Sandbox Code Playgroud) 我已经实现了一种使用 .NET 中可用的 SIMD 内在函数解析长度 <= 8 的无符号整数字符串的方法,如下所示:
public unsafe static uint ParseUint(string text)
{
fixed (char* c = text)
{
var parsed = Sse3.LoadDquVector128((byte*) c);
var shift = (8 - text.Length) * 2;
var shifted = Sse2.ShiftLeftLogical128BitLane(parsed,
(byte) (shift));
Vector128<byte> digit0 = Vector128.Create((byte) '0');
var reduced = Sse2.SubtractSaturate(shifted, digit0);
var shortMult = Vector128.Create(10, 1, 10, 1, 10, 1, 10, 1);
var collapsed2 = Sse2.MultiplyAddAdjacent(reduced.As<byte, short>(), shortMult);
var repack = Sse41.PackUnsignedSaturate(collapsed2, collapsed2);
var intMult = Vector128.Create((short)0, 0, 0, 0, 100, …Run Code Online (Sandbox Code Playgroud) 为什么System.Numerics 命名空间定义了 Matrix3x2 和 Matrix4x4 类型,但不提供 2x2 或 3x3 矩阵?这些至少同样有用。
我正在尝试使用System.Numerics.Vector<T>(文档).
我写了一个简单的单元测试:
var v = new System.Numerics.Vector<double>(new double[] { 12, 13, 14 });
Assert.AreEqual(3, v.Count);
Run Code Online (Sandbox Code Playgroud)
但它给了我一个构建错误:
无法使用实例引用访问成员'Vector.Count'; 用类型名称来限定它
令我惊讶的Vector<T>.Count是,是静态的.
所以我尝试过:
var v = new System.Numerics.Vector<double>(new double[] { 12, 13, 14 });
Assert.AreEqual(3, Vector<double>.Count);
Run Code Online (Sandbox Code Playgroud)
现在代码构建但单元测试失败:
Assert.AreEqual失败.预期:<3>.实际:<2>.
这是怎么回事?
调查我发现:
Assert.AreEqual(2, Vector<double>.Count);
Assert.AreEqual(4, Vector<float>.Count);
Assert.AreEqual(4, Vector<int>.Count);
Assert.AreEqual(2, Vector<long>.Count);
Run Code Online (Sandbox Code Playgroud) 由于SIMD,我正在尝试使用System.Numerics库制作带有4个双打的向量.所以我做了这个结构:
public struct Vector4D
{
System.Numerics.Vector<double> vecXY, vecZW;
...
}
Run Code Online (Sandbox Code Playgroud)
在这个阶段,我将其编码为128位SIMD寄存器.它工作正常,但当我想要这样的东西:
Vector4D* pntr = stackalloc Vector4D[8];
Run Code Online (Sandbox Code Playgroud)
我明白了:
不能取地址,获取大小,或声明指向托管类型的指针('Vector4D')
知道如何在System.Numerics.Vector中使用stackalloc吗?使用System.Numerics.Vector4(浮点精度)指针没有问题,但我需要双精度.
如果C#编译器/抖动在所使用的硬件上可用,是否使用融合的乘法加法运算?如果可以,我是否需要设置任何特定的编译器设置才能利用它?
我的意图是将补偿算法用于扩展精度算术,并且其中一些可以编写为使用FMA。
我想访问System.Numerics.Vector<T>C#中的元素.我正在关注官方文档:https://docs.microsoft.com/en-us/dotnet/api/system.numerics.vector-1?view = netcore-2.2
我能够创建具有不同数据类型的不同向量.例如:var test = new Vector<double>(new double[] { 1.0, 2.0, 1.0 });
但现在我遇到了问题,我无法调用test.Count; 无法在类型实例上调用Count System.Numerics.Vector<T>.
我可以访问单个元素access operator [],但我不知道向量中有多少元素.
根据文件,应该有公共财产:
public static int Count { get; }
Run Code Online (Sandbox Code Playgroud)
但我不能打电话给我的实例System.Numerics.Vector<T>.相反,我只能以静态方式调用它,如下所示:
Vector<double>.Count
Run Code Online (Sandbox Code Playgroud)
这相当于2.
我也可以打电话:
Vector<Int32>.Count
Run Code Online (Sandbox Code Playgroud)
回归:4和
Vector<Int16>.Count
Run Code Online (Sandbox Code Playgroud)
返回8.
现在我真的有点困惑,关于如何使用这个静态属性.起初,我想,这个属性将返回存储在向量中的元素数量(如文档中所述).其次,我想,这个属性返回内存中向量的大小,但是这个数字从double增加到Int32到Int16.
有趣的是,我无法从我创建的实例中调用此静态属性:
var test = new Vector<double>(new double[] { 1.0, 2.0, 1.0 });
Run Code Online (Sandbox Code Playgroud)
我不能打电话test.Count!
你知道如何访问元素System.Numerics.Vector<T>吗?
我试图通过利用System.Numerics在数组上执行 SIMD 操作来提高 .NET Core 库的性能float[]。System.Numerics现在有点时髦,我很难看出它有什么好处。我知道,为了看到 SIMD 的性能提升,必须通过大量计算进行摊销,但考虑到它目前的实现方式,我不知道如何实现这一点。
Vector<float>需要 8 个float值 - 不多也不少。如果我想对一组小于 8 的值执行 SIMD 运算,我必须将这些值复制到一个新数组,并用零填充余数。如果该组值大于 8,我需要复制这些值,用零填充以确保其长度与 8 的倍数对齐,然后循环它们。长度要求是有道理的,但适应这一点似乎是抵消任何性能增益的好方法。
我编写了一个测试包装类来处理填充和对齐:
public readonly struct VectorWrapper<T>
where T : unmanaged
{
#region Data Members
public readonly int Length;
private readonly T[] data_;
#endregion
#region Constructor
public VectorWrapper( T[] data )
{
Length = data.Length;
var stepSize = Vector<T>.Count;
var bufferedLength = data.Length - ( data.Length % stepSize ) + stepSize;
data_ = …Run Code Online (Sandbox Code Playgroud) 我无法找出一种方法来获取System.Numerics.Vector类型的向量中的元素总和。
double sum(System.Numerics.Vector<double> vect)
{
// Something like
// double sum = 0;
// foreach e in vect { sum += e; }
// return sum;
// Vector.method???
// For loop ???
}
Run Code Online (Sandbox Code Playgroud)
如果真的可能吗?我怎样才能做到这一点?
我收到此错误: System.OverflowException: '值不是数字。' 我的印象是大整数可以存储任何大小的值(在这种情况下为 500 ^ 500),所以我不明白为什么会发生这种情况。
public int decode(int code)
{
int totient = (p - 1) * (q - 1);
int d = modInverse(e, totient);
int pq = p * q;
BigInteger decodedMessage = new BigInteger(Math.Pow(code, d) % pq);
return (int)decodedMessage;
}
Run Code Online (Sandbox Code Playgroud)