C#:测绘程序设计的条件平差模型

以此题为例:

 写出条件方程,确定系数矩阵A,矩阵W,矩阵Q,L矩阵。

说明:以下程序代码运行的数据,数据有单位的均按单位mm处理。

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

namespace _10月8号_条件平差
{
    class Program
    {
    
        //输入函数
         static void  InPut(ref int m,ref int n, ref double [,]A )
        {
            Console.WriteLine("输入矩阵的行数列数");
            string r = Console.ReadLine();
            string [] rs = r.Split(' ');
            m = int.Parse(rs[0]);
            n = int.Parse(rs[1]);
            A = new double[m, n];
            Console.WriteLine("矩阵按行输入");
            Console.WriteLine();
            for (int i=0;i<m;i++)
            {
                string str = Console.ReadLine();
                string[] str1 = str.Split(' ');
                for (int j=0;j<n;j++)
                {
                    A[i, j] = double.Parse(str1[j]);
                }
            }
        }

        //输出函数
        static void OutPut(ref double [,] M)
        {
            int m = M.GetLength(0);
            int n = M.GetLength(1);
            for (int i=0;i<m;i++)
            {
                for (int j=0;j<n;j++)
                {
                    Console.Write("{0,12:f4}", M[i, j]);
                }
                Console.WriteLine();
            }
        }
        //输出函数2
        static void OutPut1(ref double [,] M1)
        {
            int m = M1.GetLength(0);
            int n = M1.GetLength(1);
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write("h{0}={1,12:f4}      ",i+1, M1[i, j]);
                }
                Console.WriteLine();
            }
        }
        //矩阵转置函数
        static void Transpose_Matrix (ref double [,] ab,ref double [,]ba )
        {
            int p = ab.GetLength(0);
            int q = ab.GetLength(1);
            ba = new double [q,p];
            for (int i=0;i<q;i++)
            {
                for (int j = 0; j < p; j++)
                    ba[i, j] = ab[j, i];
            }
        }

        //乘法矩阵

        static void Multiplication_Matrix(ref double[,] a, ref double[,] b, ref double[,] ab2)
        {
            ab2 = new double[a.GetLength(0), b.GetLength(1)];
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < b.GetLength(1); j++)
                {
                    for (int k = 0; k < a.GetLength(1); k++)
                    {
                        ab2[i, j] += a[i, k] * b[k, j];
                    }
                }
            }
        }
 
       
        //逆矩阵函数
        static void Inverse(ref double[,] Array, ref double[,] NI)
        {
            int m = Array.GetLength(0);
            int n = Array.GetLength(1);
            double[,] array = new double[2 * m + 1, 2 * n + 1];
            for (int k = 0; k < 2 * m + 1; k++)  //初始化数组
            {
                for (int t = 0; t < 2 * n + 1; t++)
                {
                    array[k, t] = 0.00000000;
                }
            }
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    array[i, j] = Array[i, j];
                }
            }
            for (int k = 0; k < m; k++)
            {
                for (int t = n; t <= 2 * n; t++)
                {
                    if ((t - k) == m)
                    {
                        array[k, t] = 1.0;
                    }
                    else
                    {
                        array[k, t] = 0;
                    }
                }
            }
            //得到逆矩阵
            for (int k = 0; k < m; k++)
            {
                if (array[k, k] != 1)
                {
                    double bs = array[k, k];
                    array[k, k] = 1;
                    for (int p = k + 1; p < 2 * n; p++)
                    {
                        array[k, p] /= bs;
                    }
                }
                for (int q = 0; q < m; q++)
                {
                    if (q != k)
                    {
                        double bs = array[q, k];
                        for (int p = 0; p < 2 * n; p++)
                        {
                            array[q, p] -= bs * array[k, p];
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            NI = new double[m, n];
            for (int x = 0; x < m; x++)
            {
                for (int y = n; y < 2 * n; y++)
                {
                    NI[x, y - n] = array[x, y];
                }
            }
        }
        static void Matrix_plus(ref double [,] a1,ref double [,] a2,ref double [,] plus)
        {
            int m1 = a1.GetLength(0);
            int n1 = a1.GetLength(1);
            int m2 = a2.GetLength(0);
            int n2 = a2.GetLength(1);
            plus = new double[m1, n1];
            if (m1 == m2 & n1 == n2)
            {
                for (int i = 0; i < m1; i++)
                {
                    for (int j = 0; j < n1; j++)
                        plus[i, j] = a1[i, j] + a2[i, j];
                }
            }
            else
                Console.WriteLine("两矩阵不能相加");
        }
        static void Main(string[] args)
        {
            Console.WriteLine("------------输入系数矩阵A[i,j]------------");
            int m = 0, n = 0;
            double [,] A={};
            double[,] M = { };
            InPut(ref m,  ref n,  ref A);
            double[,] AT = { };
            double[,] ab = { };
            double[,] ba = { };
            double[,] A1 = { };
            A1 = A;//系数矩阵
            ab = A;
            Transpose_Matrix(ref ab, ref ba);
            AT = ba;//系数矩阵的转置
            Console.WriteLine("------------矩阵A的转置为------------");
            Console.WriteLine();
            M = AT;
            OutPut(ref M);
            Console.WriteLine("------------输入协因数矩阵Q[i,j]------------");
            Console.WriteLine();
            InPut(ref m,ref n,ref A);
            double[,] Q = { };
            Q = A;//协因数矩阵
            Console.WriteLine("------------Q[i,j]------------");
            Console.WriteLine();
            M=Q;
            OutPut(ref M);
            Console .WriteLine("------------输入W矩阵------------");
            Console .WriteLine();
            double [,] W={};
            InPut(ref m,ref n ,ref A);
            Console .WriteLine("------------W[i,j]------------");
            Console .WriteLine();
            W=A;
            M=W;
            OutPut(ref M);
            Console.WriteLine("------------按单位为毫米输入L[i,j]矩阵------------");
            Console .WriteLine();
            InPut (ref m, ref n, ref A);
            double[,] L = { };
            L = A;//l矩阵
            Console .WriteLine("------------L[i,j]------------");
            Console .WriteLine();
            M=L;
            OutPut(ref M);

            //计算A1Q
            double[,] a = { };
            double[,] b = { };
            double[,] ab2 = { };
            a = A1;
            b = Q;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            double[,] A1Q = { };
            A1Q = ab2;
            double[,] AQAT = { };
            a = A1Q;
            b = AT;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            AQAT = ab2;  //Naa矩阵
            Console.WriteLine("------------Naa[i,j]------------");
            Console.WriteLine();
            M = AQAT;
            OutPut(ref M);

            //计算-K矩阵.............................由于乘以-1过于繁琐,所以把直接计算-K简化过程
            double[,] Arry = { };
            double[,] NI = { };
            Arry = AQAT;
            Inverse(ref Arry,ref NI);
            a=NI;
            b=W;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            double[,] K = { };
            K = ab2;//-K矩阵
            Console.WriteLine("------------/-K[i,j]/------------");
            Console.WriteLine();
            M=K;
            OutPut(ref M);

            //计算-V矩阵
            double[,] QAT = { };
            a = Q;
            b = AT;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            QAT = ab2;
            double[,] v = { };
            a = QAT;
            b = K;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            v = ab2;//-V[i,j]矩阵
            Console.WriteLine("------------/-v[i,j]/------------");
            Console.WriteLine();
            M = v;
            OutPut(ref M);

            //计算最终改正后的高差
            double [,] a1 = { };
            double [,] a2 = { };
            double [,] plus = { };
            double[,] V = { };
            a1 = L;
            a2 = v;
            Matrix_plus(ref a1, ref a2, ref plus);
            V = plus;
            Console.WriteLine("------------高差的改正后矩阵------------");
            Console.WriteLine();
            double [,] M1={};
            M1=V;
            OutPut1(ref M1);
        }
    }
}

运行结果如下:

仅供参考,如有错误多多谅解,敬请斧正。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值