C#矩阵运算类库 -- 一些补充

本文介绍了一段C#代码,展示了如何在Matrix类中实现矩阵的深度复制,并进行了矩阵元素的获取与修改,以及向量处理的示例。通过创建、复制、修改矩阵和向量,探讨了线性代数在编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 重点补充,发现原类中没有深度复制(也许是我没找到。。。)

于是修改如下

MatrixAlgebra --> Matrix.cs --> Initialize函数

        private void Initialize(double[,] Elements)
        {
            elements = Elements.CopyMatrix();
            SubMat = new MatrixSubset(elements);
        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinearAlgebra;
using LinearAlgebra.VectorAlgebra;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] d1 = new double[20];
            double[] d2 = new double[20];

            for (int i = 0; i < 20; i++)
            {
                d1[i] = i;
                d2[i] = i*i;
            }

            // 矩阵的创建
            Matrix A = Matrix.Create(4, 5, d1);
            Matrix B = Matrix.Create(4, 5, d2);
            Matrix Mat = Matrix.Create(2, 2, new double[] { 1, 2, 3, 4 });

            // 矩阵的深度复制
            Matrix C = new Matrix(A.Elements);

            // 矩阵元素获取修改            
            double[,] D = A.Elements;  
            double dData = C.ElementAt(2);
            A[1, 1] = 666;
            A.SetColumn(1, B.GetColumn(1));

            // 向量处理
            Vector v1 = Vector.Range(VectorType.Row, 2, 10, 0.1);
            Vector v2 = Vector.Range(VectorType.Row, 1, 10, 0.1);
            Vector v3 = v1 * v2;

            // 元素获取
            double[] dVec = v3.Elements;

            Console.WriteLine(A);
            Console.WriteLine(C);
            Console.WriteLine(v3);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值