double[,], int[], bool[] ... C# 中的矩阵、向量操作扩展

发布于 2024-08-04 14:37:19 字数 215 浏览 2 评论 0原文

我需要(用于快速原型设计和库集成)类似的东西(普通数组的扩展)


double[] d;
d.SetRow(1,{ 1.1 , 2.0 ,3.3});
var r = d.GetRow(1);
d = d.AppendRight(new int[]{1,2,3});
...

任何地方都存在这样的东西吗? 也许有人实现了它,所以我不需要我自己做?

I need(for rapid prototyping and libraries integration) something like this(extensions for usual arrays)


double[] d;
d.SetRow(1,{ 1.1 , 2.0 ,3.3});
var r = d.GetRow(1);
d = d.AppendRight(new int[]{1,2,3});
...

Does exist such thing anywhere?
On may be anybody implemented it so I do not need do i for me myself?

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

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

发布评论

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

评论(4

仅此而已 2024-08-11 14:37:19

查看 Math.NET。它是一个开源数学库。您可能会找到您需要的东西。

他们在此页面末尾提供了一个使用矩阵的示例。

Have a look at Math.NET. It is an open-source math library. You will probably find what you need.

They have an example using a matrix at the end of this page.

守望孤独 2024-08-11 14:37:19

自己写这个应该不会太难。

需要非常小心的是如何将数组作为属性进行编辑。

像这样的东西(非常粗糙的未经测试的代码,但应该给你一个想法):

public class ArrayRow<T> {
    //add your own ..ctor etc

    T[,] matrix; //don't make this public see http://msdn.microsoft.com/en-us/library/k2604h5s.aspx
    public int Index { get; private set; }

    //note that this will be a copy
    public T[] GetValues() {
        T[] retval = new T[matrix.GetLength(1)];
        for ( int i = 0; i < retval.Length; i++ )
           retval[i] = matrix[Index, i];

        return retval;
    }

    public void SetValues(T[] values)
    //..and so on, you get the idea
}

然后你扩展数组:

public static ArrayExtensions {

    public void SetRow<T> ( this T[,] matrix, int rowIndex, T[] values ) {
        //check rowIndex in range and array lengths match
    }

    public ArrayRow<T> GetRow<T> ( this T[,] matrix, int rowIndex ) {
        //check rowIndex in range
        return new ArrayRow<T> ( matrix, rowIndex );
    }
}

然后你可以依赖被推断的类型参数。

This shouldn't be too difficult to write yourself.

The thing to be very careful of is how arrays can be edited as properties.

Something like (very rough untested code, but should give you an idea):

public class ArrayRow<T> {
    //add your own ..ctor etc

    T[,] matrix; //don't make this public see http://msdn.microsoft.com/en-us/library/k2604h5s.aspx
    public int Index { get; private set; }

    //note that this will be a copy
    public T[] GetValues() {
        T[] retval = new T[matrix.GetLength(1)];
        for ( int i = 0; i < retval.Length; i++ )
           retval[i] = matrix[Index, i];

        return retval;
    }

    public void SetValues(T[] values)
    //..and so on, you get the idea
}

Then you extend the array:

public static ArrayExtensions {

    public void SetRow<T> ( this T[,] matrix, int rowIndex, T[] values ) {
        //check rowIndex in range and array lengths match
    }

    public ArrayRow<T> GetRow<T> ( this T[,] matrix, int rowIndex ) {
        //check rowIndex in range
        return new ArrayRow<T> ( matrix, rowIndex );
    }
}

Then you can rely on the type parameter being inferred.

阳光①夏 2024-08-11 14:37:19

Python 等语言支持混合类型的列表。您可以创建 IronPython 脚本,然后从 C# 应用程序调用它。关注 此链接 查看如何从您的应用程序调用 IronPython 脚本。

Languages like Python support lists with mixed types. You can create an IronPython script and then call it from your C# application. Follow this link to see how you can call IronPython script from your app.

随波逐流 2024-08-11 14:37:19

我编写了 Matrix Extensions C# 库来测试基于扩展的代码生成设计。

I wrote Matrix Extensions C# library to test extensions based code generation design.

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