【測繪程式設計試題集】 試題02 矩陣卷積計算

staHuri發表於2018-10-18

資料

N 矩陣

10.00	13.50	14.00	13.80	13.90	15.60	13.30	14.50	13.70	14.40
13.50	13.30	15.10	16.40	15.40	14.90	11.30	13.50	17.70	13.30
15.70	14.00	16.30	18.60	16.80	16.60	12.50	15.50	16.70	14.80
16.50	15.90	15.20	17.40	17.60	17.70	14.30	14.50	18.50	15.60
12.60	13.30	14.40	16.50	18.40	18.40	17.30	16.50	19.70	17.40
14.10	17.70	16.00	15.40	14.50	19.60	15.20	18.50	14.70	18.30
18.50	14.50	14.70	13.10	15.40	14.30	12.30	17.50	12.40	13.20
22.30	15.20	15.80	18.00	17.20	13.50	13.70	16.50	14.70	15.30
17.50	16.30	16.30	13.60	18.40	15.70	16.30	15.50	15.70	16.40
13.20	17.30	15.00	12.80	19.10	16.60	17.60	16.50	13.30	17.30

M 矩陣

0.20	0.30	0.20
0.25	0.50	0.35
0.10	0.30	0.20

問題

問題2-1
問題2-2

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File    : ju.py
# @Author  : huifer
# @Time    : 2018/10/18 08:29

def read_m(path):
    m = []

    with open(path, 'r') as f:
        for i in f.readlines():
            aa = i.replace('\n', '').split("\t")
            bb = [eval(a) for a in aa]
            m.append(bb)

    return m


def MIJ(I, J, i, j):
    n = 10
    res = 0;
    if (I - i - 1 < 0 or J - j - 1 < 0 or I - i - 1 > n - 1 or J - j - 1 > 9):
        res = 0
    else:
        res = read_m('M矩陣.txt')[i][j]

    return res


def suanfa1(I, J):
    m = 3
    upper = 0
    under = 0
    for i in range(m):
        for j in range(m):
            mij = MIJ(I, J, i, j)
            eps = 1e-10
            if abs(mij) > eps:
                upper += mij * read_m("N矩陣.txt")[I - i - 1][J - j - 1]
                under += mij

    if under == 0:
        return 0
    else:
        return upper / under


def suanfa2(I, J):
    m = 3
    upper = 0
    under = 0
    for i in range(m):
        for j in range(m):
            mij = MIJ(I, J, i, j)
            eps = 1e-10
            if abs(mij) > eps:
                upper += mij * read_m("N矩陣.txt")[9 - (I - i - 1)][9 - (J - j - 1)]
                under += mij
    if under == 0:
        return 0
    else:
        return upper / under


def main1():
    n = 10
    v = [
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
    ]

    for i in range(n):
        for j in range(n):
            v[i][j] = suanfa1(i, j)

    return v


def main2():
    n = 10
    v = [
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
        list(range(10)),
    ]
    for i in range(n):
        for j in range(n):
            v[i][j] = suanfa2(i, j)

    return v

if __name__ == '__main__':
    m = read_m("M矩陣.txt")
    n = read_m("N矩陣.txt")

    print("===演算法1====")
    va1 = main1()
    for i in range(0, va1.__len__()):
        # print(i)
        for j in range(0, va1.__len__()):
            print(format(float(va1[i][j]), "0.2f") + "   ", end='')
        # print(" ",end='')
        print('\n')

    print("===演算法2====")
    va2 = main2()
    for i in range(0, va2.__len__()):
        # print(i)
        for j in range(0, va2.__len__()):
            print(format(float(va2[i][j]), "0.2f") + "   ", end='')
        # print(" ",end='')
        print('\n')

結果

===演算法1====
0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   

0.00   10.00   11.40   12.64   13.80   13.89   14.36   14.46   14.30   13.93   

0.00   11.56   12.07   13.03   14.25   14.61   14.76   14.33   13.78   13.99   

0.00   13.66   13.29   13.83   15.00   15.78   15.72   14.71   13.74   14.17   

0.00   15.59   15.07   14.95   15.73   16.86   16.98   15.70   14.33   14.70   

0.00   14.94   15.02   14.95   15.56   16.85   17.60   17.01   15.73   15.85   

0.00   13.85   14.51   14.82   15.37   16.10   17.27   17.52   16.95   16.59   

0.00   15.43   15.17   15.30   15.25   15.23   15.96   16.43   16.52   16.17   

0.00   19.08   17.34   16.38   15.40   15.34   15.30   15.06   14.99   15.35   

0.00   19.86   18.46   16.89   15.50   15.94   15.97   15.19   14.64   15.30   

===演算法2====
0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   

0.00   17.30   15.70   15.36   15.90   17.00   17.60   16.59   15.23   15.03   

0.00   16.90   16.03   15.51   15.75   16.59   17.16   16.63   15.48   15.10   

0.00   16.16   15.86   15.56   15.55   15.77   16.16   16.62   16.03   15.48   

0.00   14.74   14.70   15.10   15.18   14.84   14.79   15.81   15.96   15.49   

0.00   15.44   14.66   15.04   15.40   15.33   14.85   15.41   15.31   15.35   

0.00   17.05   16.48   16.32   16.46   16.57   16.54   16.35   15.32   15.24   

0.00   16.91   17.55   17.31   16.83   16.66   17.46   17.45   16.33   15.49   

0.00   15.64   16.60   16.85   16.00   15.48   16.63   17.61   17.05   15.84   

0.00   14.40   15.54   15.91   15.02   14.11   15.26   16.79   16.89   15.94   

相關文章