矩阵的各种函数库——两个版本,Java与C#都有

开发了一个比较通用的矩阵计算静态方法库。

用以计算矩阵的点乘与叉乘,矩阵与列向量的乘积,列向量与矩阵的乘积,矩阵的转置。。。

分为Java与.NET C# 两个版本。

以下为各部分源码(各自包含测试的主函数);

Java:
在这里插入图片描述

package MatrixPackage;

public class Matrix {
    
    
    public static double dot(double[] x, double[] y) {
    
    
        if (x.length == y.length) {
    
    
            double result = 0d;
            for (int i = 0; i < x.length; i++) {
    
    
                result += x[i] * y[i];
            }
            return result;
        } else {
    
    
            return Double.MAX_VALUE;
        }
    }

    public static double[][] mult(double[][] x, double[][] y) {
    
    
        if (x[0].length == y.length) {
    
    
            double[][] result = new double[x.length][y[0].length];
            for (int i = 0; i < x.length; i++) {
    
    
                for (int k = 0; k < y[0].length; k++) {
    
    
                    double sum = 0;
                    for (int j = 0; j < y.length; j++) {
    
    
                        sum += x[i][j] * y[j][k];
                    }
                    result[i][k] = sum;
                }
            }
            return result;
        } else {
    
    
            return null;
        }
        // x[0].length 列数
    }

    public static double[][] transpose(double[][] x) {
    
    
        int row = x.length;
        int col = x[0].length;
        double[][] res = new double[col][row];
        for (int i = 0; i < row; i++) {
    
    
            for (int j = 0; j < col; j++) {
    
    
                res[j][i] = x[i][j];
            }
        }
        return res;
    }

    public static double[][] mult(double[][] a, double[] x) {
    
    

        if (a[0].length == x.length) {
    
    
            double[][] res = new double[a.length][1];
            for (int i = 0; i < a.length; i++) {
    
    
                for (int j = 0; j < x.length; j++) {
    
    
                    res[i][0] += a[i][j] * x[j];
                }
            }
            return res;
        } else {
    
    
            return null;
        }
    }

    public static double[] mult(double[] y, double[][] a) {
    
    
        if (y.length == a.length) {
    
    
            double[] res = new double[a[0].length];
            for (int i = 0; i < a[0].length; i++) {
    
    
                double sun = 0;
                for (int j = 0; j < a.length; j++) {
    
    
                    sun += a[j][i] * y[j];
                }
                res[i] = sun;
            }
            return res;
        } else {
    
    
            return null;
        }
    }
}

测试主函数:

package Test;
import MatrixPackage.Matrix;
public class TestDemo {
    
    

    public static void main(String[] args) {
    
    
        try {
    
    
            double[][] a = {
    
     {
    
     1, 2, 3, 4 }, {
    
     4, 5, 6, 8 }, {
    
     7, 8, 9, 20 } };
            double[][] b = {
    
     {
    
     4, 5, 6, 8, 9 }, {
    
     4, 5, 6, 8, 9 }, {
    
     4, 5, 6, 8, 6 }, {
    
     4, 5, 6, 8, 3 } };
            double[] d = {
    
     1, 2, 3 };
            double[] c = Matrix.mult(d, a);
//            for (int i = 0; i < c.length; i++) {
    
    
//                for (int j = 0; j < c[0].length; j++) {
    
    
//                    System.out.print(c[i][j] + " ");
//                }
//                System.out.println();
//            }
            for (int i = 0; i < c.length; i++) {
    
    
                System.out.print(c[i] + " ");
            }
            //System.out.println(c.length);
        } catch (Exception e) {
    
    
            System.out.println(e.getMessage());
        }

    }
}

.NET C#:
在这里插入图片描述

using System;

namespace Matrix
{
    
    
    public class MatrixLib
    {
    
    
        public static double dot(double[] x, double[] y)
        {
    
    
            if (x.Length == y.Length)
            {
    
    
                double result = 0d;
                for (int i = 0; i < x.Length; i++)
                {
    
    
                    result += x[i] * y[i];
                }
                return result;
            }
            else
            {
    
    
                return Double.NaN;
            }
        }

        public static double[,] mult(double[,] x, double[,] y)
        {
    
    
            if (x.GetLength(1)== y.GetLength(0))
            {
    
    
                double[, ] result = new double[x.GetLength(0), y.GetLength(1)];
                for (int i = 0; i < x.GetLength(0); i++)
                {
    
    
                    for (int k = 0; k < y.GetLength(1); k++)
                    {
    
    
                        double sum = 0;
                        for (int j = 0; j < y.GetLength(0); j++)
                        {
    
    
                            sum += x[i,j] * y[j,k];
                        }
                        result[i,k] = sum;
                    }
                }
                return result;
            }
            else
            {
    
    
                return null;
            }
        }

        public static double[,] transpose(double[,] x)
        {
    
    
            int row = x.GetLength(0);
            int col = x.GetLength(1);
            double[,] res = new double[col,row];
            for (int i = 0; i < row; i++)
            {
    
    
                for (int j = 0; j < col; j++)
                {
    
    
                    res[j,i] = x[i,j];
                }
            }
            return res;
        }

        public static double[,] mult(double[,] a, double[] x)
        {
    
    

            if (a.GetLength(1) == x.Length)
            {
    
    
                double[,] res = new double[a.GetLength(0),1];
                for (int i = 0; i < a.GetLength(0); i++)
                {
    
    
                    for (int j = 0; j < x.Length; j++)
                    {
    
    
                        res[i,0] += a[i,j] * x[j];
                    }
                }
                return res;
            }
            else
            {
    
    
                return null;
            }
        }

        public static double[] mult(double[] y, double[,] a)
        {
    
    
            if (y.Length == a.GetLength(0))
            {
    
    
                double[] res = new double[a.GetLength(1)];
                for (int i = 0; i < a.GetLength(1); i++)
                {
    
    
                    double sun = 0;
                    for (int j = 0; j < a.GetLength(0); j++)
                    {
    
    
                        sun += a[j,i] * y[j];
                    }
                    res[i] = sun;
                }
                return res;
            }
            else
            {
    
    
                return null;
            }
        }
    }
}

测试主函数:

using System;
using static Matrix.MatrixLib;
namespace MatrixTest
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            try
            {
    
    
                double[,] a = {
    
     {
    
     1, 2, 3, 4 }, {
    
     4, 5, 6, 8 }, {
    
     7, 8, 9, 20 } };
                double[,] b = {
    
     {
    
     4, 5, 6, 8, 9 }, {
    
     4, 5, 6, 8, 9 }, {
    
     4, 5, 6, 8, 6 }, {
    
     4, 5, 6, 8, 3 } };
                double[] d = {
    
     1, 2, 3 ,4};
                double[] e = {
    
     1, 2, 3 };
                double[] c = Matrix.MatrixLib.mult(d,b);
                //            for (int i = 0; i < c.length; i++) {
    
    
                //                for (int j = 0; j < c[0].length; j++) {
    
    
                //                    System.out.print(c[i][j] + " ");
                //                }
                //                System.out.println();
                //            }
                for (int i = 0; i < c.GetLength(0); i++)
                {
    
    
                    //for (int j = 0;j < c.GetLength(1); j++)
     //               {
    
    
                    //    Console.Write(c[i,j] + " ");
                    //}
                    Console.Write(c[i] + " ");
                }
                Console.WriteLine();
            }
            catch (Exception e)
            {
    
    
                Console.WriteLine(e.Message);
            }
        }
    }
}

不定时更新中,若有更多计算函数,会及时更新。

Guess you like

Origin blog.csdn.net/m0_47472749/article/details/118076010