C#:测绘程序设计的间接平差模型

以此题为例

 先写出误差方程,确定系数矩阵B[i,j],l[i,j],P[i,j],带入程序运算

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

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

namespace 间接平差
{
    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();
            }
        }
      
        //矩阵转置函数
        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_Puls(ref double[,] b1, ref double[,] b2, ref double[,] puls)
        {
            int m1 = b1.GetLength(0);
            int n1 = b1.GetLength(1);
            int m2 = b2.GetLength(0);
            int n2 = b2.GetLength(1);
            puls  = new double[m1, n1];
            if (m1 == m2 & n1 == n2)
            {
                for (int i = 0; i < m1; i++)
                {
                    for (int j = 0; j < n1; j++)
                        puls [i, j] = b1[i, j] + b2[i, j];
                }
            }
            else
                Console.WriteLine("两矩阵不能相加");
        }
        //矩阵减法
        static void Matrix_Subtraction(ref double[,] a1, ref double[,] a2, ref double[,] sub)
        {
            int m1 = a1.GetLength(0);
            int n1 = a1.GetLength(1);
            int m2 = a2.GetLength(0);
            int n2 = a2.GetLength(1);
            sub = new double[m1, n1];
            if (m1 == m2 & n1 == n2)
            {
                for (int i = 0; i < m1; i++)
                {
                    for (int j = 0; j < n1; j++)
                        sub [i, j] = a1[i, j] - a2[i, j];
                }
            }
            else
                Console.WriteLine("两矩阵不能相减");
        }
        static void Main(string[] args)
        {
            //函数变量的初始化
            int m = 0,n=0;
            double [,] A = { };
            double [,] M = { };
            double [,] ab = { };
            double [,] ba = { };
            double[,] a = { };
            double[,] b = { };
            double[,] ab2 = { };
            double[,] Arry = { };
            double[,] NI = { };
            double[,] a1 = { };
            double[,] a2 = { };
            double[,] sub = { };
            double[,] b1 = { };
            double[,] b2 = { };
            double[,] plus = { };



            Console.WriteLine("---------------输入B[i,j]矩阵---------------");
            Console.WriteLine();
            InPut(ref m, ref n, ref A);
            double[,] B = { };
            B = A; //B[i,j]矩阵
            Console.WriteLine("---------------B[i,j]--------------");
            Console.WriteLine();
            M = B;
            OutPut(ref M);
            Console.WriteLine("---------------输入l[i,j]矩阵---------------");
            Console.WriteLine();
            InPut(ref m, ref n, ref A);
            double[,] l = { };
            l = A;//l[i,j]矩阵
            Console.WriteLine("---------------l[i,j]---------------");
            Console.WriteLine();
            M = l;
            OutPut(ref M);
            Console.WriteLine("---------------输入P[i,j]矩阵---------------");
            Console.WriteLine();
            InPut(ref m, ref n, ref A);
            double[,] P = { };
            P = A;//P[i,j]矩阵
            Console.WriteLine("---------------P[i,j]----------------");
            Console.WriteLine();
            M = P;
            OutPut(ref M);
            //计算NBB
            double[,] NBB = { };
            ab=B;
            Transpose_Matrix(ref ab, ref ba);
            double[,] BT = { };
            BT = ba;
            a = ba;
            b = P;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            a = ab2;
            b = B;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            NBB = ab2;//NBB[,]矩阵
            Console.WriteLine("---------------NBB[i,j]---------------");
            Console.WriteLine();
            M = NBB;
            OutPut(ref M);
            //计算W[i,j] 矩阵
            a = BT;
            b = P;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            a = ab2;
            b = l;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            double[,] W = { };
            W = ab2;//W[i,j]矩阵
            Console.WriteLine("---------------W[i,j]---------------");
            Console.WriteLine();
            M = W;
            OutPut(ref M);
            //解方程,计算x[i,j]矩阵
            Arry = NBB;
            Inverse(ref Arry, ref NI);
            a = NI;
            b = W;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            double[,] x = { };
            x = ab2;//x[i,j]矩阵
            Console.WriteLine("---------------x[i,j]---------------");
            Console.WriteLine();
            for (int i = 0; i < x.GetLength(0); i++)
            {
                for (int j = 0; j < x.GetLength(1); j++)
                {
                    Console.Write("xˇ{0}={1,15:f5}", i + 1, x[i, j]);
                }
                Console.WriteLine();
            }
            
            //计算改正数
            a = B;
            b = x;
            Multiplication_Matrix(ref a, ref b, ref ab2);
            a1 = ab2;
            a2 = l;
            Matrix_Subtraction(ref a1, ref a2, ref sub);
            double[,] V = { };
            V = sub;//改正数矩阵
            Console.WriteLine("---------------V[i,j]---------------");
            Console.WriteLine();
            for (int i = 0; i < V.GetLength(0); i++)
            {
                for (int j = 0; j < V.GetLength(1); j++)
                {
                    Console.Write("V{0}={1,15:f5}", i + 1, V [i, j]);
                }
                Console.WriteLine();
            }
           
            //计算观测值的平差值矩阵
            Console .WriteLine("---------------输入观测值的矩阵L[i,j]---------------");
            Console .WriteLine();
            InPut(ref m,ref n,ref A);
            double [,] L={};
            L=A;//观测值L[i,j]矩阵
            b1 = L;
            b2 = V;
            Matrix_Puls(ref b1, ref b2, ref plus);
            double[,] LL = { };
            LL = plus;
            Console.WriteLine("---------------Lˇ[i,j]---------------");
            Console.WriteLine();
            for (int i=0;i<LL.GetLength (0);i++)
            {   for (int j=0;j<LL.GetLength (1);j++)
                {
                    Console.Write("Lˇ{0}={1,15:f5}", i + 1, LL[i, j]);
                }
            Console.WriteLine();
            }
        }
    }
}

运行结果如下

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值