C# 矩阵运算

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 矩阵运算
{
    class Program
    {
        static void Main(string[] args)
        {
            double[][] a = 
            {
                new double[]{1.0,2.0,3.0},
                new double[]{4.0,5.0,6.0},
                new double[]{7.0,8.0,9.0}
            };

            double[][] b = 
            {
                new double[]{2.0,-2.0,1.0},
                new double[]{1.0,3.0,9.0},
                new double[]{17.0,-3.0,7.0}
            };

            double[][] c =
            {
                new double[3],
                new double[3],
                new double[3]
            };

            Console.WriteLine("相乘结果:");
            MatrixMul(a, b, c);
            MatrixLog(c);
            Console.WriteLine("相加结果:");
            MatrixPlus(a, b, c);
            MatrixLog(c);
            Console.WriteLine("相减结果:");
            MatrixSub(a, b, c);
            MatrixLog(c);
            Console.ReadLine();
        }



        //矩阵相加
        public static void MatrixPlus(double[][] a, double[][] b, double[][] c) 
        {
            if (a == null || b == null || (a.Length != b.Length) || (a[0].Length != b[0].Length))
                return;

            for (int i = 0; i < a.Length; i++)
            {
                for (int k = 0; k < a[0].Length; k++) 
                {
                    c[i][k] = a[i][k] + b[i][k];
                }
            }
        }

        //矩阵相减
        public static void MatrixSub(double[][] a, double[][] b, double[][] c)
        {
            if (a == null || b == null || (a.Length != b.Length) || (a[0].Length != b[0].Length))
                return;

            for (int i = 0; i < a.Length; i++)
            {
                for (int k = 0; k < a[0].Length; k++)
                {
                    c[i][k] = a[i][k] - b[i][k];
                }
            }
        }

        //矩阵乘法
        public static void MatrixMul(double[][] a, double[][] b,double[][] c) 
        {
            if (a == null || b == null || a[0].Length != b.Length)
                return;

            for (int i = 0; i < a.Length; i++)
            {
                for (int k = 0; k < b[0].Length; k++)
                {
                    c[i][k] = 0;
                    for (int l = 0; l < a[0].Length; l++)
                    {
                        c[i][k] += (a[i][l] * b[l][k]);             //a矩阵i行所有元素 *  b矩阵j行所有元素之和 = 新的矩阵一个元素
                    }
                }   
            }
        }


        public static void MatrixLog(double[][] c) 
        {
            for (int i = 0; i < c.Length; i++)
            {
                for (int k = 0; k < c[i].Length; k++)
                {
                    Console.Write(c[i][k] + " ");
                }

                Console.WriteLine();
            }
        }

    }
}

image

转载于:https://www.cnblogs.com/plateFace/p/5198042.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中可以使用第三方库MathNet.Numerics来进行矩阵运算。这个库提供了丰富的线性代数算法,包括叉乘、除法和转置等。 以下是使用MathNet.Numerics进行矩阵运算的示例代码: ```csharp using MathNet.Numerics.LinearAlgebra; // 创建矩阵 Matrix<double> matrixA = Matrix<double>.Build.DenseOfArray(new double[,] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }); Matrix<double> matrixB = Matrix<double>.Build.DenseOfArray(new double[,] { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} }); // 矩阵乘法 Matrix<double> matrixC = matrixA * matrixB; // 矩阵除法 Matrix<double> matrixD = matrixA / matrixB; // 矩阵转置 Matrix<double> matrixE = matrixA.Transpose(); // 矩阵叉乘 Vector<double> vectorA = Vector<double>.Build.Dense(new double[] {1, 2, 3}); Vector<double> vectorB = Vector<double>.Build.Dense(new double[] {4, 5, 6}); Vector<double> vectorC = vectorA.CrossProduct(vectorB); ``` 以上代码中,我们使用了Matrix和Vector类来表示矩阵和向量,这些类提供了丰富的线性代数算法。在创建矩阵时,我们使用Build类提供的DenseOfArray方法来从数组中创建矩阵。在进行矩阵乘法、除法和转置等操作时,我们直接使用矩阵的运算符重载。在进行矩阵叉乘时,我们使用向量的CrossProduct方法来计算叉乘结果。 需要注意的是,MathNet.Numerics库中提供了多种矩阵和向量类型,如DenseMatrix、SparseMatrix、DenseVector、SparseVector等,不同类型的矩阵和向量适用于不同的场景。在实际使用中,需要根据具体情况选择合适的类型来进行矩阵运算

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值