Find the general formula of a sequence generate by the order by the distance of each element to the matrix diagonal.

53 Views Asked by At

Some examples:
Case 1: n*(n-1)
$\mathbb {R}^{5\times4}$ $$\begin{bmatrix} {a_{00}}&{a_{01}}&{a_{02}}&{a_{03}}\\ {a_{10}}&{a_{11}}&{a_{12}}&{a_{13}}\\ {a_{20}}&{a_{21}}&{a_{22}}&{a_{23}}\\ {a_{30}}&{a_{31}}&{a_{32}}&{a_{33}}\\ {a_{40}}&{a_{41}}&{a_{42}}&{a_{43}}\\ \end{bmatrix}$$ The main order is along the diagonal(not the main diagonal) from top left to bottom right; among the elements of each secondary diagonal, if the distance of each element to the matrix diagonal is shorter, the priority is higher. So we got a sequence:
00
10 01
11 20 02
21 12 30 03
22 31 13 40
32 23 41
33 42
43
The general formula of n*(n-1) is quite easy to implement with a computer, but the general formula of n*(n-2) is much more confused.

Case 2: n*(n-2)
$\mathbb {R}^{5\times3}$ $$\begin{bmatrix} {a_{00}}&{a_{01}}&{a_{02}}\\ {a_{10}}&{a_{11}}&{a_{12}}\\ {a_{20}}&{a_{21}}&{a_{22}}\\ {a_{30}}&{a_{31}}&{a_{32}}\\ {a_{40}}&{a_{41}}&{a_{42}}\\ \end{bmatrix}$$ As the same, we got:
00
10 01
11 20 02
21 12 30 (draw a vertical line form the element to the diagonal, so not 21 30 12)
31 22 40
32 41
42

$\mathbb {R}^{6\times4}$ $$\begin{bmatrix} {a_{00}}&{a_{01}}&{a_{02}}&{a_{03}}\\ {a_{10}}&{a_{11}}&{a_{12}}&{a_{13}}\\ {a_{20}}&{a_{21}}&{a_{22}}&{a_{23}}\\ {a_{30}}&{a_{31}}&{a_{32}}&{a_{33}}\\ {a_{40}}&{a_{41}}&{a_{42}}&{a_{43}}\\ {a_{50}}&{a_{51}}&{a_{52}}&{a_{53}}\\ \end{bmatrix}$$ As the same, we got:
00
10 01
11 20 02
21 12 30 03
22 21 13 40
32 41 23 50
42 33 51
43 52
53

Case 3: m*n
$\mathbb {R}^{m\times{n}}$ $$\begin{bmatrix} {a_{11}}&{a_{12}}&{\cdots}&{a_{1n}}\\ {a_{21}}&{a_{22}}&{\cdots}&{a_{2n}}\\ {\vdots}&{\vdots}&{\ddots}&{\vdots}\\ {a_{m1}}&{a_{m2}}&{\cdots}&{a_{mn}}\\ \end{bmatrix}$$

At last, how to find the general formula or use computer program to generate the sequence of $\mathbb {R}^{m\times{n}}$.

1

There are 1 best solutions below

0
On
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
import numpy as np

def order_asym_new(m, n):
    length = int(m * n )
    order = np.empty([length, 2], dtype=int)
    def distance(x, y):
        d = abs(n * x - m * y)/math.sqrt(m ** 2 + n ** 2)
        return d

    X = []
    for x in range(m):
        x = float(x) + 0.5
        X.append(x)

    lst1 = []
    for b in range(1, m + n):
        lst2 = []
        for x in X:
            y = - x + b
            d = distance(x,y)
            if y > 0 and y < n:
                lst3 = []
                x = x - 0.5
                y = y - 0.5
                lst3.append(x)
                lst3.append(y)
                lst3.append(d)
            if not lst3 in lst2:
                lst2.append(lst3)
                lst3 = []
        lst2.remove([])
        #bubble_sort
        count = len(lst2)
        for i in range(0, count):
            for j in range(i + 1, count):
                if lst2[i][2] > lst2[j][2]:
                    lst2[i], lst2[j] = lst2[j], lst2[i]
        lst1.append(lst2)

    count = 0
    for item1 in lst1:
        for item2 in item1:
            order[count][0] = item2[0]
            order[count][1] = item2[1]
            count = count + 1
    return order