C#:声明和使用 XNA 向量进行矩阵乘法等。阿尔

发布于 2024-11-18 11:32:05 字数 160 浏览 4 评论 0原文

我试图在 C# 中声明和使用 XNA 向量进行矩阵乘法、求和等。

这些将用于图像处理,使其比常规的 SetPixel 和 GetPixel 更快。然而,我总是找不到一个有效的例子,我在网上尝试了很多例子,但似乎我错过了一些东西。

有帮助和示例代码吗?

谢谢!

I am trying to declare and use XNA Vectors for Matrix Multiplication, Summation, etc. in C#.

Those will be used for Image processing to make it faster than regular SetPixel and GetPixel. However, I am always failing to find a working example and I tried many examples online but it seems I am missing something.

Any help and sample code?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫离伤花 2024-11-25 11:32:05

如果您担心性能,那么您可以恢复在不安全上下文中进行编码。

通过使用 unsafe 关键字标记类型、类型成员或语句块,您可以使用指针类型并在该范围内的内存上执行 C++ 样式的指针操作,并且能够在托管执行框架中执行此操作。不安全的代码可以比相应的安全实现运行得更快。

这是一个来自《C# 4.0 in a Nutshell》一书的简短示例:(

unsafe void BlueFilter (int[,] bitmap)
  {
    int length = bitmap.Length;
    fixed (int* b=bitmap)
    {
        int* p=b;
        for (int i=0, i<length; i++)
        *p++ &= 0xFF;
    }
   }

来源


除此之外,您还应该看看这个SO问题

为什么.NET中的矩阵乘法这么慢?

If you are worried about Performance then you can revert to coding in unsafe context.

By marking a type, type member, or statement block with the unsafe keyword, you're permitted to use pointer types and perform C++ style pointer operations on memory within that scope, and to be able to do this within the managed execution framework. Unsafe code can run faster than a corresponding safe implementation.

Here is a nice, short example that comes from the book C# 4.0 in a Nutshell:

unsafe void BlueFilter (int[,] bitmap)
  {
    int length = bitmap.Length;
    fixed (int* b=bitmap)
    {
        int* p=b;
        for (int i=0, i<length; i++)
        *p++ &= 0xFF;
    }
   }

(Source)


Apart from that you should also take a look at this SO Question

Why is matrix multiplication in .NET so slow?

深爱不及久伴 2024-11-25 11:32:05

Verctor 只是 1 xn 矩阵。创建一个 Matrix 类,其中包含求和和乘法方法。

public class Matrix
{
    private int[,] m_array;

    public Matrix(int m, int n)
    {
        m_array = new int[m, n];
    }

    public Matrix(int[,] other)
    {
        m_array = other;
    }

    public Matrix Mult(Matrix other)
    {
        if (this.m_array.GetLength(1) != other.m_array.GetLength(0))
            return null;

        int[,] res = new int[this.m_array.GetLength(0), other.m_array.GetLength(1)];

        for (int i = 0; i < this.m_array.GetLength(0); ++i)
            for (int j = 0; j < other.m_array.GetLength(1); ++j)
            {
                int s = 0;
                for (int k = 0; k < this.m_array.GetLength(1); ++k)
                    s += this.m_array[i, k] * other.m_array[k, j];
                res[i, j] = s;
            }

        return new Matrix(res);
    }
}

Verctors are just 1 x n matrices. Create a Matrix class, with methods for summation and multiplication.

public class Matrix
{
    private int[,] m_array;

    public Matrix(int m, int n)
    {
        m_array = new int[m, n];
    }

    public Matrix(int[,] other)
    {
        m_array = other;
    }

    public Matrix Mult(Matrix other)
    {
        if (this.m_array.GetLength(1) != other.m_array.GetLength(0))
            return null;

        int[,] res = new int[this.m_array.GetLength(0), other.m_array.GetLength(1)];

        for (int i = 0; i < this.m_array.GetLength(0); ++i)
            for (int j = 0; j < other.m_array.GetLength(1); ++j)
            {
                int s = 0;
                for (int k = 0; k < this.m_array.GetLength(1); ++k)
                    s += this.m_array[i, k] * other.m_array[k, j];
                res[i, j] = s;
            }

        return new Matrix(res);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文