ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

矩阵的基本变换

2022-03-21 14:03:12  阅读:167  来源: 互联网

标签:基本 hashMap 16 变换 矩阵 ++ int moveStep 移动


矩阵的基本变换

矩阵的基本变换

矩阵的行列基本移动

行移动

将整行向前或者向后移动(示例中是向前移动),超过的部分桉顺序补齐空缺。
比如【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 lwl
 * @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);
            }
        }
    }

}

标签:基本,hashMap,16,变换,矩阵,++,int,moveStep,移动
来源: https://blog.csdn.net/qq_44717657/article/details/123631642

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有