AlgorithmStar(AS机器学习与科学计算库) 实现 矩阵数据类型的计算函数汇总

news2024/10/5 19:14:04

AlgorithmStar 实现 矩阵 计算

AlgorithmStar
本文中将会演示通过 AS 机器学习库 实现 矩阵计算

目录

文章目录

  • AlgorithmStar 实现 矩阵 计算
    • 目录
    • 矩阵创建
      • 通过数组创建
      • 通过稀疏矩阵创建
      • 通过填充创建矩阵
      • 通过随机的方式创建矩阵
    • 矩阵计算
      • 矩阵的基本运算
        • 矩阵的加法计算
        • 矩阵的减法计算
        • 矩阵的乘法计算
        • 矩阵的内积计算
      • 矩阵的转化计算
        • 矩阵的维度重设
        • 矩阵的子矩阵提取
        • 矩阵的源子矩阵提取
        • 矩阵的元素提取
        • 矩阵到数组的转化
          • 全矩阵源数组提取
          • 全矩阵非源数组提取
          • 矩阵的行或列提取
        • 相关性维度的删除
        • 冗余维度行去除
        • 矩阵扁平化
        • 矩阵元素镜像反转
        • 矩阵元素位移
        • 洗牌打乱矩阵
        • 矩阵的行迭代

在这里插入图片描述

矩阵创建

通过数组创建

package com.zhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 创建矩阵对象
        final MatixFactory matixFactory = AlgorithmStar.matixFactory();
        final DoubleMatrix matrix1 = matixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        final DoubleMatrix matrix2 = matixFactory.parseMatrix(
                new double[]{1, 20, 3},
                new double[]{4, 50, 6},
                new double[]{7, 80, 9}
        );

        System.out.println(matrix1);
        System.out.println(matrix2);
    }

}

下面就是打印结果

------------MatrixStart-----------
[1.0, 2.0, 3.0]
[4.0, 5.0, 6.0]
[7.0, 8.0, 9.0]
------------MatrixEnd------------

------------MatrixStart-----------
[1.0, 20.0, 3.0]
[4.0, 50.0, 6.0]
[7.0, 80.0, 9.0]
------------MatrixEnd------------


进程已结束,退出代码0

通过稀疏矩阵创建

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 以及稀疏矩阵的方式创建两个矩阵
        final IntegerMatrix integerMatrix = matrixFactory.sparseMatrix(
                // 在坐标 (2,3) 的位置创建一个元素 1
                new int[]{1, 2, 3},
                // 在坐标 (1,2) 的位置创建一个元素 2
                new int[]{2, 1, 2}
        );
        final DoubleMatrix doubleMatrix = matrixFactory.sparseMatrix(
                // 在坐标 (2,3) 的位置创建一个元素 1
                new double[]{1, 2, 3},
                // 在坐标 (1,2) 的位置创建一个元素 2
                new double[]{2, 1, 2}
        );
        System.out.println(integerMatrix);
        System.out.println(doubleMatrix);
    }
}


下面就是代码的计算结果

------------MatrixStart-----------
[0, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 1]
------------MatrixEnd------------

------------MatrixStart-----------
[0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 2.0, 0.0]
[0.0, 0.0, 0.0, 1.0]
------------MatrixEnd------------


进程已结束,退出代码0

通过填充创建矩阵

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1
    public static void main(String[] args) {
       // 获取到矩阵工厂
        MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 填充一个 3 行 4 列的 矩阵 其中的元素为 1024
        IntegerMatrix fill1 = matrixFactory.fill(1024, 3, 4);
        // 如果数值是 double 类型,则矩阵也是 double 类型
        DoubleMatrix fill2 = matrixFactory.fill(1024.5, 3, 4);
        // 打印矩阵
        System.out.println(fill1);
        System.out.println(fill2);
    }
}

下面就是代码的执行结果

------------MatrixStart-----------
[1024, 1024, 1024, 1024]
[1024, 1024, 1024, 1024]
[1024, 1024, 1024, 1024]
------------MatrixEnd------------

------------MatrixStart-----------
[1024.5, 1024.5, 1024.5, 1024.5]
[1024.5, 1024.5, 1024.5, 1024.5]
[1024.5, 1024.5, 1024.5, 1024.5]
------------MatrixEnd------------


进程已结束,退出代码0

通过随机的方式创建矩阵

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
       // 获取到矩阵工厂
        MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 创建 3 行 4 列的 矩阵 其中的元素 随机创建 在这里设置的随机种子是 22
        IntegerMatrix integerMatrix = matrixFactory.randomGetInt(3, 4, 22);
        DoubleMatrix doubleMatrix = matrixFactory.randomGetDouble(3, 4, 22);
        // 打印矩阵
        System.out.println(integerMatrix);
        System.out.println(doubleMatrix);
    }
}


下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271]
[1679422763, -1489264737, 551745089]
[-1724358601, -2014521070, 1780678643]
[-976397893, -375228091, -1488911514]
------------MatrixEnd------------

------------MatrixStart-----------
[0.7322219172863654, 0.8669148741814655, 0.6532535274097795]
[0.5985164721452856, 0.4145965542296267, 0.9126354106267657]
[0.2859704488730964, 0.6145075557422693, 0.9270089804071319]
[0.999587711996269, 0.563868878760328, 0.06820469035033427]
------------MatrixEnd------------

矩阵计算

矩阵数据类型在 AS 机器学习库中的使用率最高,且其提供的计算函数也是最多的。

矩阵的基本运算

AS 库中所有的操作数对象都支持的基本运算,矩阵也是支持的哦!

矩阵的加法计算
package top.lingyuzhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 准备两个矩阵
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final DoubleMatrix doubles = matrixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        // 复制出新矩阵
        final DoubleMatrix doubles2 = matrixFactory.parseMatrix(doubles.copyToNewArrays());
        // 矩阵与整数计算
        System.out.println(doubles.add(2));
        // 矩阵与矩阵计算
        System.out.println(doubles.add(doubles2));
    }
}

下面就是计算结果

------------MatrixStart-----------
[3.0, 4.0, 5.0]
[6.0, 7.0, 8.0]
[9.0, 10.0, 11.0]
------------MatrixEnd------------

------------MatrixStart-----------
[2.0, 4.0, 6.0]
[8.0, 10.0, 12.0]
[14.0, 16.0, 18.0]
------------MatrixEnd------------
矩阵的减法计算
package top.lingyuzhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 准备两个矩阵
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final DoubleMatrix doubles = matrixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        // 复制出新矩阵
        final DoubleMatrix doubles2 = matrixFactory.parseMatrix(doubles.copyToNewArrays());
        // 矩阵与整数计算
        System.out.println(doubles.diff(2));
        // 矩阵与矩阵计算
        System.out.println(doubles.diff(doubles2));
    }
}

下面就是计算结果

------------MatrixStart-----------
[-1.0, 0.0, 1.0]
[2.0, 3.0, 4.0]
[5.0, 6.0, 7.0]
------------MatrixEnd------------

------------MatrixStart-----------
[0.0, 0.0, 0.0]
[0.0, 0.0, 0.0]
[0.0, 0.0, 0.0]
------------MatrixEnd------------
矩阵的乘法计算
package top.lingyuzhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 准备两个矩阵
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final DoubleMatrix doubles = matrixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        // 复制出新矩阵
        final DoubleMatrix doubles2 = matrixFactory.parseMatrix(doubles.copyToNewArrays());
        // 矩阵与矩阵计算
        System.out.println(doubles.multiply(doubles2));
    }
}

下面就是计算结果

------------MatrixStart-----------
[2.0, 3.0, 2.0, 6.0, 3.0, 6.0]
[20.0, 24.0, 20.0, 30.0, 24.0, 30.0]
[56.0, 63.0, 56.0, 72.0, 63.0, 72.0]
------------MatrixEnd------------
矩阵的内积计算
package top.lingyuzhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 准备两个矩阵
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final DoubleMatrix doubles = matrixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        // 复制出新矩阵
        final DoubleMatrix doubles2 = matrixFactory.parseMatrix(doubles.copyToNewArrays());
        // 矩阵与矩阵计算
        System.out.println(doubles.innerProduct(doubles2));
    }
}

下面就是集散结果

285.0

矩阵的转化计算

矩阵对象,在AS库中数据非常重要的一部分,所以矩阵操作数对象的转换计算操作是非常丰富的,在这里您将可以学习到诸多的知识!

矩阵的维度重设

针对一个矩阵对象的维度,我们可以直接通过 reShape 函数将其中的矩阵维度进行重新设置,实现有效的矩阵变换操作,例如将一个 2行8列的矩阵 变成 4行4列 的矩阵,下面是一个示例。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 2x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(2, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里维度重设为了 4 行 4 列
        System.out.println(integerMatrix.reShape(4, 4));
    }
}


下面就是计算结果

------------MatrixStart-----------
[-1150098092, 279129721]
[-571596271, 1679422763]
[-1489264737, 551745089]
[-1724358601, -2014521070]
[1780678643, -976397893]
[-375228091, -1488911514]
[1228233692, -165598556]
[-1655677467, -63220304]
------------MatrixEnd------------

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
------------MatrixEnd------------


进程已结束,退出代码0
矩阵的子矩阵提取

在一个矩阵中,如果我们需要提取出其中的某些元素,并将这些元素勾成一个新的矩阵,就需要使用到这里的子矩阵提取操作,这样的提取效果会很棒,接下来我们就要开始进行一个演示。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了(2,4) 到 (4, 8) 坐标之间的子矩阵
        // 需要注意的是,这里的坐标从 0 开始,所以每个轴都需要减一
        System.out.println(integerMatrix.extractMat(1, 3, 3, 7));
    }
}

下面就是计算结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

------------MatrixStart-----------
[-165598556, -1655677467, -63220304]
[-1748391512, -1770763, -771252503]
[-435684294, 292936915, 1240741745]
[1767746472, 1107247833, -350820282]
[-1766358679, -1375246549, 1190957425]
------------MatrixEnd------------


进程已结束,退出代码0
矩阵的源子矩阵提取

此操作也是可以实现矩阵中某些元素提取的操作,值得注意的是,这样提取出来的矩阵中的修改操作将会直接作用到源矩阵中,例如从 A 中提取的 B子矩阵,B被反转之后,A也会变化,,而且此操作只可以进行提取的高度的设置,宽度无法设置,您可以参考下面的表格来查询其不同。

对照项目非源子矩阵源子矩阵
提取速度较慢较块
支持的坐标宽,高
修改会修改父矩阵是,修改操作会作用到源矩阵对象中
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了(最大宽度,4) 到 (最大宽度, 8) 坐标之间的子矩阵
        IntegerMatrix integerMatrix1 = integerMatrix.extractSrcMat(4, 8);
        System.out.println(integerMatrix1);
        // 对子矩阵进行修改 在这里是 使用不拷贝的方式 左右反转 相当于直接修改 子矩阵
        integerMatrix1.reverseLR(false);
        // 查询被修改之后的父矩阵是否有了变化
        System.out.println(integerMatrix);
    }
}

下面就是操作结果,可以看到,父矩阵虽然没有调用修改的函数,但是由于源子矩阵修改了,所以父矩阵爷就被修改了。

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

------------MatrixStart-----------
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-771252503, -1770763, -1748391512, -313494065]
[1240741745, 292936915, -435684294, -1873168937]
[-350820282, 1107247833, 1767746472, 353159284]
[1190957425, -1375246549, -1766358679, -1814378561]
------------MatrixEnd------------


进程已结束,退出代码0

矩阵的元素提取
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了 第1行 第3个元素
        System.out.println(integerMatrix.get(0, 2));
    }
}

下面就是打印之后的结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

-571596271

进程已结束,退出代码0
矩阵到数组的转化
全矩阵源数组提取

此操作会将矩阵对象中所引用的二维数组原样获取到,这样的操作获取是非常快速的一种方式,但是这样获取的矩阵不建议修改,因为修改之后会影响源矩阵对象中的数据,因为两者指向同一个内存地址。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了 矩阵对应的数组
        System.out.println(Arrays.deepToString(integerMatrix.toArrays()));
    }
}

下面就是运行的代码

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

[[-1150098092, 279129721, -571596271, 1679422763], [-1489264737, 551745089, -1724358601, -2014521070], [1780678643, -976397893, -375228091, -1488911514], [1228233692, -165598556, -1655677467, -63220304], [-313494065, -1748391512, -1770763, -771252503], [-1873168937, -435684294, 292936915, 1240741745], [353159284, 1767746472, 1107247833, -350820282], [-1814378561, -1766358679, -1375246549, 1190957425]]

进程已结束,退出代码0
全矩阵非源数组提取

这样的提取结果与 源数组 提取是一样的,唯一不同的就是这样提取的数组,与源矩阵之间毫无关系,因此在这个函数获取到的数组是可以进行直接修改的,下面就是一个基本的示例。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了 矩阵对应的数组
        System.out.println(Arrays.deepToString(integerMatrix.copyToNewArrays()));
    }
}

下面就是计算结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

[[-1150098092, 279129721, -571596271, 1679422763], [-1489264737, 551745089, -1724358601, -2014521070], [1780678643, -976397893, -375228091, -1488911514], [1228233692, -165598556, -1655677467, -63220304], [-313494065, -1748391512, -1770763, -771252503], [-1873168937, -435684294, 292936915, 1240741745], [353159284, 1767746472, 1107247833, -350820282], [-1814378561, -1766358679, -1375246549, 1190957425]]

进程已结束,退出代码0
矩阵的行或列提取
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了 列索引为 0 的列,并且转换为了数组
        System.out.println(Arrays.toString(integerMatrix.getArrayByColIndex(0)));
        // 当然 也可以直接提取出 行 在这里我们还提取了行索引为 0 的行 并转换为了数组
        System.out.println(Arrays.toString(integerMatrix.getArrayByRowIndex(0)));
    }
}

下面就是代码计算之后的结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

[-1150098092, -1489264737, 1780678643, 1228233692, -313494065, -1873168937, 353159284, -1814378561]
[-1150098092, 279129721, -571596271, 1679422763]

进程已结束,退出代码0
相关性维度的删除

在一些案例中,我们希望去除一些维度(例如机器学习的分类之前,需要进行数据清洗,有些不需要的维度需要被删除掉),我们通过这里的相关性维度的删除可以实现将不需要的维度,以及所有可能与其相关的维度删除。

import zhao.algorithmMagic.operands.matrix.ColumnIntegerMatrix;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;


public class MAIN1 {
    public static void main(String[] args) {
        int[][] ints = {
                new int[]{1, 2, 1, 1, 1, 1, 1, 1, 1},
                new int[]{1, 2, 1, 40, 1, 1, 60, 1, 1},
                new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9},
                new int[]{10, 20, 30, 40, 50, 60, 70, 80, 90}
        };
        // 准备一个矩阵 其中存储的是鸟的数据样本
        IntegerMatrix parse1 = ColumnIntegerMatrix.parse(
                new String[]{"1d", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d"}, // 样本来源地区编号
                new String[]{"羽毛", "字段占位", "羽毛的颜色", "种族"}, // 样本统计的三种维度
                ints
        );
        System.out.println(parse1);
        // 开始进行特征清洗 去除掉与其中第4行 正相关系数区间达到 [0.8, 1] 的维度
        parse1 = parse1.deleteRelatedDimensions(3, 0.8, 1);
        System.out.println(parse1);
    }
}

下面就是一个计算结果

------------IntegerMatrixStart-----------
1d	2d	3d	4d	5d	6d	7d	8d	9d	rowColName
[1, 2, 1, 1, 1, 1, 1, 1, 1]	羽毛
[1, 2, 1, 40, 1, 1, 60, 1, 1]	字段占位
[1, 2, 3, 4, 5, 6, 7, 8, 9]	羽毛的颜色
[10, 20, 30, 40, 50, 60, 70, 80, 90]	种族
------------IntegerMatrixEnd------------

------------IntegerMatrixStart-----------
1d	2d	3d	4d	5d	6d	7d	8d	9d	rowColName
[1, 2, 1, 1, 1, 1, 1, 1, 1]	羽毛
[1, 2, 1, 40, 1, 1, 60, 1, 1]	字段占位
------------IntegerMatrixEnd------------


进程已结束,退出代码0
冗余维度行去除
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.parseMatrix(
                new int[]{1, 2, 3, 4},
                new int[]{5, 6, 7, 8},
                new int[]{10, 9, 8, 7},
                new int[]{10, 9, 8, 70}
        );
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们清理了其中的所有 与第一行(索引为0的行) 相关的其他行
        System.out.println(integerMatrix.featureSelection(
                // 这里设置了需要去除的比例 我们要去除掉其中的 50% 会按照离散值算法
                // 将最没有特点的行去除,最后返回的一定是原本的 50% 因为我们去除掉了 百分之50
                0.5
        ));
    }
}

下面就是计算出来的结果

------------MatrixStart-----------
[1, 2, 3, 4]
[5, 6, 7, 8]
[10, 9, 8, 7]
[10, 9, 8, 70]
------------MatrixEnd------------

------------MatrixStart-----------
[10, 9, 8, 70]
[10, 9, 8, 7]
------------MatrixEnd------------


进程已结束,退出代码0
矩阵扁平化

此操作能够将一个矩阵压扁成只有一行的数组,在下面就是一个具体的示例。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.parseMatrix(
                new int[]{1, 2, 3, 4},
                new int[]{5, 6, 7, 8},
                new int[]{10, 9, 8, 7},
                new int[]{10, 9, 8, 70}
        );
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们将矩阵扁平化为了一个数组
        System.out.println(Arrays.toString(integerMatrix.flatten()));
    }
}

下面就是扁平化之后的结果

------------MatrixStart-----------
[1, 2, 3, 4]
[5, 6, 7, 8]
[10, 9, 8, 7]
[10, 9, 8, 70]
------------MatrixEnd------------

[1, 2, 3, 4, 5, 6, 7, 8, 10, 9, 8, 7, 10, 9, 8, 70]

进程已结束,退出代码0
矩阵元素镜像反转
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN {

    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(2, 3, 22);
        // 先打印一下矩阵
        System.out.println(integerMatrix);
        // 打印一下矩阵反转之后的结果 
        // 这个反转函数具有一个拷贝参数,您如果想要在源矩阵对象进行修改的话,需要传入 false 代表不需要进行拷贝
        // 左右反转
        System.out.println(integerMatrix.reverseLR(true));
        // 上下反转
        System.out.println(integerMatrix.reverseBT(true));
    }

}

下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721]
[-571596271, 1679422763]
[-1489264737, 551745089]
------------MatrixEnd------------

------------MatrixStart-----------
[279129721, -1150098092]
[1679422763, -571596271]
[551745089, -1489264737]
------------MatrixEnd------------

------------MatrixStart-----------
[-1150098092, 279129721]
[-571596271, 1679422763]
[-1489264737, 551745089]
------------MatrixEnd------------


进程已结束,退出代码0

矩阵元素位移

矩阵中包含很多的元素,我们可以让每行的元素统一的向左或向右移动,下面是一个实际演示示例。

package com.zhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN {

    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 6, 22);
        // 先打印一下矩阵
        System.out.println(integerMatrix);
        // 打印一下矩阵左移2位的结果 
        // 第二个参数代表的就是 是否需要拷贝 如果不需要 则转换操作将会直接作用在原矩阵
        System.out.println(integerMatrix.leftShift(2, true));
        // 再打印一下矩阵右移2位的结果
        System.out.println(integerMatrix.rightShift(2, true));
    }

}

下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
------------MatrixEnd------------

------------MatrixStart-----------
[-571596271, 1679422763, 0, 0]
[-1724358601, -2014521070, 0, 0]
[-375228091, -1488911514, 0, 0]
[-1655677467, -63220304, 0, 0]
[-1770763, -771252503, 0, 0]
[292936915, 1240741745, 0, 0]
------------MatrixEnd------------

------------MatrixStart-----------
[0, 0, -1150098092, 279129721]
[0, 0, -1489264737, 551745089]
[0, 0, 1780678643, -976397893]
[0, 0, 1228233692, -165598556]
[0, 0, -313494065, -1748391512]
[0, 0, -1873168937, -435684294]
------------MatrixEnd------------


进程已结束,退出代码0

洗牌打乱矩阵
package com.zhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN {

    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 6, 22);
        // 先打印一下矩阵
        System.out.println(integerMatrix);
        // 打印一下洗牌之后的矩阵 在这里设置打乱算法随机种子为 22
        System.out.println(integerMatrix.shuffle(22));
    }

}

下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
------------MatrixEnd------------

------------MatrixStart-----------
[-1489264737, 551745089, -1724358601, -2014521070]
[1228233692, -165598556, -1655677467, -63220304]
[-1150098092, 279129721, -571596271, 1679422763]
[1780678643, -976397893, -375228091, -1488911514]
[-1873168937, -435684294, 292936915, 1240741745]
[-313494065, -1748391512, -1770763, -771252503]
------------MatrixEnd------------


进程已结束,退出代码0

矩阵的行迭代

矩阵实现了 Java 中的迭代器接口,可以直接使用Java中的增强 for 来进行迭代,增强for的语法为for (类型 参数名字 : 被迭代的对象) 这个样子,接下来就是实际演示。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;

public class MAIN {

    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 6, 22);
        // 先打印一下矩阵
        System.out.println(integerMatrix);
        // 开始迭代矩阵
        for (int[] matrix : integerMatrix) {
            System.out.println(Arrays.toString(matrix));
        }
    }

}

下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
------------MatrixEnd------------

[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]

进程已结束,退出代码0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1566736.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Cloud flare反向代理流量实验

前言 本实验将会为大家解析cloud flare的反向解析代理服务如何搭建,works如何创建等等。本文中教学创建的实例已在文章编写结束后释放,该项技术不可用于违法用途!违者自行承担后果!! 原理拓扑图 一、知识链条 1、Clou…

JavaScript(六)---【回调、异步、promise、Async】

零.前言 JavaScript(一)---【js的两种导入方式、全局作用域、函数作用域、块作用域】-CSDN博客 JavaScript(二)---【js数组、js对象、this指针】-CSDN博客 JavaScript(三)---【this指针,函数定义、Call、Apply、函数绑定、闭包】-CSDN博客 JavaScript(四)---【执…

STM32的I2C补充说明

1.前言 前面不是开发了F407的i2c嘛,最近做项目有三四个i2c器件,项目要求用f103,于是看了一下f103,发现并没有多大区别,下面我会说一下异同。还有关于接收的过程也有要补充的。 2.F103 VS F407 两者之间几乎没有区别&a…

Maven 项目之快速选择环境配置文件

Maven项目中,多环境之间如何进行配置文件的切换。在我们开发的过程中,经常会出现开发环境、测试环境、生产环境等之间的切换,如果我们每次都去替换配置文件,就会跟繁琐,这个时候就可以创建多个环境,同时在对…

下载页面上的视频

引言:有些页面上的视频可以直接右键另存为或者F12检索元素找到视频地址打开后保存,但有些视频页面是转码后的视频,不能直接另存为视频格式,可以参考下本方法 以该页面视频为例:加载中...点击查看详情https://wx.vzan.c…

zookeeper监听集群节点的实现zkclient组件实现方案(Java版)

ZooKeeper Watcher 机制 client 向zookeeper 注册监听client注册的同时会存储一个WatchManager对象向zookeeper发生改变则notification client 并发送一个WatchManager对象,然后client再更新该对象 package com.jacky.zk.demo;import org.I0Itec.zkclient.IZkChildListener;…

LeetCode575——分糖果

题目链接:. - 力扣(LeetCode) 这道题比较简单,但我还是花费了将近四个小时的时间去解答,AC的那一刻,终于全身舒畅,这道题的思路就是先求出糖果的种数,然后我们从题中可以得出&#x…

如何配置vite的proxy

1.前言 vite项目,本地开发环境可以通过配置proxy代理实现跨域请求。但是生产环境,该配置不生效,一般使用 nginx 转发,或者后端配置cors 2.解释 server: {port: 9000,proxy: { // 本地开发环境通过代理实现跨域,生产…

如何处理Flutter应用在iOS平台上的兼容性问题

大家好,我是咕噜铁蛋!今天我想和大家聊聊一个常见但令人头疼的问题——Flutter应用在iOS平台上的兼容性问题。在开发跨平台应用时,尤其是在Android和iOS两大平台上,兼容性问题总是难以避免。而Flutter,作为一个旨在解决…

使用OMP复原一维信号(MATLAB)

参考文献 https://github.com/aresmiki/CS-Recovery-Algorithms/tree/master MATLAB代码 %% 含有噪声 % minimize ||x||_1 % subject to: (||Ax-y||_2)^2<eps; % minimize : (||Ax-y||_2)^2lambda*||x||_1 % y传输中可能含噪 yyw % %% clc;clearvars; close all; %% 1.构…

IE浏览器兼容性问题——el-button点击失效

el-table 中经常有这种场景&#xff1a;最后一列是操作&#xff0c;只有一个图标 之前的实现是直接讲点击事件绑定到了 icon 图标上&#xff0c;这样在谷歌、edge、火狐等是没问题的&#xff0c;但是在ie浏览器下&#xff0c;就会出现点击事件无效的情况&#xff0c;点击后不会…

使用minikube安装使用单机版K8S(docker)

前置&#xff1a;作为一个开发&#xff0c;工作之余想玩一下k8s&#xff0c;但是搭建成本太高&#xff0c;所以就找到了minikube这个工具&#xff0c;快速搭建单机版k8s&#xff0c;下面是个人搭建流程&#xff0c;基于centos7&#xff0c;仅供参考。 1.下载kubectl&#xff0…

Idea2023创建Servlet项目

① Java EE 只是一个抽象的规范&#xff0c;具体实现称为应用服务器。 ② Java EE 只需要两个包 jsp-api.jar 和 servlet-api.jar&#xff0c;而这两个包是没有官方版本的。也就是说&#xff0c;Java 没有提供这两个包&#xff0c;只提供了一个规范。那么这两个包是谁提供的…

【Linux多线程】信号量以及线程池

【Linux多线程】信号量以及线程池 目录 【Linux多线程】信号量以及线程池POSIX信号量基于环形队列的生产者消费者模型 线程池 作者&#xff1a;爱写代码的刚子 时间&#xff1a;2024.4.2 前言&#xff1a;本篇博客将会介绍Linux线程中的信号量以及线程池&#xff0c;完善生产者…

elementui 实现一个固定位置的Pagination(分页)组件

系列文章目录 一、elementui 导航菜单栏和Breadcrumb 面包屑关联 二、elementui 左侧导航菜单栏与main区域联动 三、elementui 中设置图片的高度并支持PC和手机自适应 四、 elementui 实现一个固定位置的Pagination&#xff08;分页&#xff09;组件 文章目录 系列文章目录…

Python基于深度学习的人脸识别项目源码+演示视频,利用OpenCV进行人脸检测与识别 preview

​ 一、原理介绍 该人脸识别实例是一个基于深度学习和计算机视觉技术的应用&#xff0c;主要利用OpenCV和Python作为开发工具。系统采用了一系列算法和技术&#xff0c;其中包括以下几个关键步骤&#xff1a; 图像预处理&#xff1a;首先&#xff0c;对输入图像进行预处理&am…

深入剖析主机安全中的零信任机制及其实施原理

引言 在数字化转型加速与云端服务普及的大背景下&#xff0c;传统依赖边界的网络安全模式逐渐显露出其局限性。面对愈发复杂多变的威胁环境&#xff0c;零信任安全架构作为新一代的安全范式应运而生&#xff0c;尤其是在主机层面的安全实践中&#xff0c;零信任机制正扮演着至…

RabbitMQ3.x之七_RabbitMQ消息队列模型

RabbitMQ3.x之七_RabbitMQ消息队列模型 文章目录 RabbitMQ3.x之七_RabbitMQ消息队列模型1. RabbitMQ消息队列模型1. 简单队列2. Work Queues(工作队列)3. Publish/Subscribe(发布/订阅)4. Routing(路由)5. Topics(主题)6. RPC(远程过程调用)7. Publisher Confirms(发布者确认) …

防止推特Twitter账号被冻结,应该选什么代理类型IP?

在处理多个 Twitter 帐号时&#xff0c;选择合适的代理IP对于避免大规模帐户暂停至关重要。现在&#xff0c;问题出现了&#xff1a;哪种类型的代理是满足您需求的最佳选择&#xff1f;下面文章将为你具体讲解推特账号冻结原因以及重点介绍如何选择代理IP。 一、推特账号被冻结…

C# WPF编程-命令

C# WPF编程-命令 概述WPF命令模型ICommand接口RoutedCommand类RoutedUICommand类命令库 概述 使用路由事件可以响应广泛的鼠标和键盘事件&#xff0c;这些事件是低级的元素。在实际应用程序中&#xff0c;功能被划分成一些高级的任务。这些任务可通过各种不同的动作和用户界面…