矩阵的基本变换

矩阵的基本变换

矩阵的行列基本移动

行移动

将整行向前或者向后移动(示例中是向前移动),超过的部分桉顺序补齐空缺。
比如【0 , 1 , 2, 3】向前移动后就变成【1 , 2, 3, 0】

列移动

比如这样的一列
0
1
2
3
想上移动就变成
1
2
3
0

转置矩阵

行列互换,即为该矩阵的转置矩阵。
如以下矩阵
【1 , 2, 3】
【4 , 5, 6】
【7 , 8, 9】
它的转置矩阵为:
【1 , 4, 7】
【2 , 5, 8】
【3 , 6, 9】

其他常见移动

比如常用的第一行,行移动移位,第二行行向前移动两位。。。 以此类推的移动。
列也可以,第一列向上移动一位,第二列向上移动两列。。 以此类推。

在这里我就不用二维矩阵实现了,而是使用HashMap实现,不过思路其实都差不多。

package Test;

import java.util.HashMap;

/**
 * @author 三文鱼
 * @title
 * @description
 * @date 2022/3/18
 **/
public class SBoxTest {
    public static void main(String[] args) {
        HashMap<Integer , Integer> hashMap = new HashMap<Integer, Integer>();
        //i表示行 j表示列
        initMatrix(hashMap);

        display(hashMap);
        //转置矩阵
        transposeMatrix(hashMap);
        //固定位移 第一行移动向前移动一位 第二行移动两位 以此类推
        //rowMove(hashMap);
        //cloMove(hashMap);
        //指定行移动 第一行向前移动五位 -- 从第二个参数从0开始
        //specRowMove(hashMap,1,5);
        //指定列移动 第三列向上移动3位 -- 从第二个参数从0开始
        //specCloMove(hashMap,3,3);
        System.out.println();
        System.out.println("=============变换后为==============");
        display(hashMap);
    }

    //行向前移动 第一行向前移动一位 第二行移动两位 以此类推
    public static void rowMove(HashMap<Integer ,Integer> hashMap) {
        for (int i = 0; i < 16; i++) {
            //逐行移动更改参数
            int[] needCopy = new int[i];//[moveStep]
            int now = 0;
            for(int n = 0; n < i; n++) {// < moveStep
                now = hashMap.get(i*16 + n);
                needCopy[n] = now;
            }

            //行前移
            for(int j = 0; j < 16; j++) {        // 0 +    0 + 1  put(0,1)
                hashMap.put(i*16 + j , hashMap.get(i*16 + (j + i)%16));//j + moveStep
            }

            for(int n = 0; n < i; n++) {//moveStep
                hashMap.put(i*16 + (16 - i + n) , needCopy[n]);//16 - moveStep
            }
        }
    }
    //列往上移动固定位置 第一列向上移动一位 第二列移动两位 以此类推
    public static void cloMove(HashMap<Integer ,Integer> hashMap) {
        for (int j = 0; j < 16; j++) {
            //拿出被覆盖的前moveStep个元素
            int[] needCopy = new int[j];// [moveStep]
            int now = 0;
            for(int n = 0; n < j; n++) {// < moveStep
                now = hashMap.get(n*16 + j);
                needCopy[n] = now;
            }

            //列上移
            for(int i = 0; i < 16; i++) {        // 0 +    0 + 1  put(0,1)
                hashMap.put(i*16 + j , hashMap.get((j + i)%16*16 + j));//moveStep + i
            }

            for(int n = 0; n < j; n++) {//n < moveStep
                hashMap.put((16 - j + n)%16*16 + j , needCopy[n]);//16 - moveStep
            }
        }
    }
    //指定行向前移动step位 
    public static void specRowMove(HashMap<Integer ,Integer> hashMap , int row , int step) {
        //逐行移动更改参数
        int[] needCopy = new int[step];//[moveStep]
        int now = 0;
        for(int n = 0; n < step; n++) {// < moveStep
            now = hashMap.get(row*16 + n);
            needCopy[n] = now;
        }

        //行前移
        for(int j = 0; j < 16; j++) {
            hashMap.put(row*16 + j , hashMap.get(row*16 + (j + step)%16));//j + moveStep
        }

        for(int n = 0; n < step; n++) {//moveStep
            hashMap.put(row*16 + (16 - step + n) , needCopy[n]);//16 - moveStep
        }
    }
    //指定列向上移动step位
    public static void specCloMove(HashMap<Integer ,Integer> hashMap , int clo , int step) {
            //拿出被覆盖的前moveStep个元素
            int[] needCopy = new int[step];// [moveStep]
            int now = 0;
            for(int n = 0; n < step; n++) {// < moveStep
                now = hashMap.get(n*16 + clo);
                needCopy[n] = now;
            }

            //列上移
            for(int i = 0; i < 16; i++) {        // 0 +    0 + 1  put(0,1)
                hashMap.put(i*16 + clo , hashMap.get((step + i)%16*16 + clo));//moveStep + i
            }

            for(int n = 0; n < step; n++) {//n < moveStep
                hashMap.put((16 - step + n)%16*16 + clo , needCopy[n]);//16 - moveStep
            }

    }
    //遍历输出矩阵
    public static void display(HashMap<Integer , Integer> hashMap) {
        for(int i = 0; i < 256; i++) {
            if(i%16 == 0 && i > 0) {
                System.out.println(" ");//换行
            }
            if(hashMap.get(i) <= 0xf){
                //小于16的值 加0 输出
                System.out.print("0" + String.valueOf(Integer.toHexString(hashMap.get(i))) + " ");
            }else {
                System.out.print(Integer.toHexString(hashMap.get(i)) + " ");
            }
        }
    }
    //转置矩阵
    public static void transposeMatrix(HashMap<Integer , Integer> hashMap) {
        //转置矩阵
        //对角线变换前八行 行列互换 第i行和第j列互换
        for(int i = 0; i < 16; i++) {
            for (int j = i; j < 16; j++) {
                int positon = i*16 + j;
                int nextPosition = j*16 + i;

                int now = hashMap.get(positon);// 1 -- 11
                int next = hashMap.get(nextPosition); //16 -- 21

                hashMap.put(positon , next);// 1 -- 21
                hashMap.put(nextPosition , now); //16 -- 11
            }
        }
    }
    //初始化矩阵 生成一个矩阵
    public static void initMatrix(HashMap<Integer , Integer> hashMap) {
        for (int i = 0; i < 16; i++) {
            for(int j = 0; j < 16; j++) {
                //构建s盒
                //键值 00 - 256
                int sItem = i*16 + j;
                //可以在这里简单变换矩阵
                int realPositiion = sItem;
                Integer.toHexString(sItem);
                hashMap.put(sItem, realPositiion);
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值