快速矩阵库的java,矩阵数据库

发布时间:2022-11-19

本文目录一览:

  1. 有什么能做高精确矩阵运算的java库
  2. 用JAVA写一个矩阵类
  3. java构造一个矩阵
  4. 用JAVA编写矩阵
  5. 【Java】【矩阵】相关

有什么能做高精确矩阵运算的java库

Colt简介 Colt是一个高性能的数学库,由以下几个子库构成:

  • Colt库:基本的动态数组、稀疏矩阵、线性代数。
  • Jet库:数理统计、直方图。
  • CoreJava库:类printf的打印函数,并行计算。

用JAVA写一个矩阵类

昨天刚帮一个网友改编的,输出矩阵并且在矩阵求幂后输出矩阵的一个类,直接可以运行。 注释都有的。希望你用的得到。

import java.util.Scanner;
public class JuZhen {
    //定义计算方法
    public static int calc(int x, int y, int score) {
        if (x == 0 && y == 0) {
            score = 0;
        } else {
            score = 1;
        }
        return score;
    }
    //输入矩阵
    public static void shuru() {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入矩阵的阶数:");
        int n = input.nextInt();
        int M[][] = new int[n][n];
        System.out.print("请输入矩阵的的值(0-1):");
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[i].length; j++) {
                M[i][j] = input.nextInt();
            }
        }
        System.out.println("你输入的矩阵为:");
        for (int i = 0; i < M.length; i++) {
            System.out.print("\n");
            for (int j = 0; j < M[i].length; j++) {
                System.out.print(M[i][j] + "\t");
            }
        }
    }
    //仅仅是一个求幂的递归。
    int myPow(int x, int y) {
        int pow = 0;
        if (y > 0) {
            pow = x * myPow(x, y - 1);
        }
        if (y < 0) {
            pow = 1 / x * myPow(x, y + 1);
        }
        if (y == 0) {
            pow = 1;
        }
        return pow;
    }
    //程序入口
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入矩阵的阶数:");
        int n = input.nextInt();
        int M[][] = new int[n][n];
        System.out.print("请输入矩阵的的值(0-1):");
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[i].length; j++) {
                M[i][j] = input.nextInt();
            }
        }
        int temp[][] = new int[n][n];
        int m[][] = new int[n][n];
        System.out.println("你输入的矩阵为:");
        for (int i = 0; i < M.length; i++) {
            System.out.print("\n");
            for (int j = 0; j < M[i].length; j++) {
                temp[i][j] = M[i][j];
                System.out.print(M[i][j] + "\t");
            }
        }
        System.out.print("\n\n你想求几次方:");
        int c = input.nextInt();
        for (int k = 0; k < c; k++) {
            for (int i = 0; i < M.length; i++) {
                for (int j = 0; j < M[i].length; j++) {
                    m[i][j] = new JuZhen().myPow(temp[i][j], c);
                }
            }
        }
        for (int i = 0; i < m.length; i++) {
            System.out.print("\n");
            for (int j = 0; j < m[i].length; j++) {
                System.out.print(m[i][j] + "\t");
            }
        }
    }
}

java构造一个矩阵

  1. java构造函数--数组 在构造函数中初始化数组,如
public class array {
    private int[][] matrix;
    public array(int r, int c) {
        matrix = new matrix[r][c];
    }
}

matrix = new matrix[r][c]; 2. 这里是new int[][]; java中8个基本数据类型都是有默认值的,int默认值为0, 3. 所以数组中默认都为0. 4. 但是切记有默认值的变量必须是类的属性,方法中局部变量必须赋值才可以使用。

用JAVA编写矩阵

public static void main(String[] args) throws Exception {
    print(create(getNum()));
}
private static int getNum() {
    Scanner scanner = new Scanner(System.in);
    int n = 0;
    do {
        System.out.println("请输入要生成的阶数:");
        String input = scanner.next();
        if (isDigital(input)) {
            n = Integer.parseInt(input);
            if (n <= 0) {
                System.out.println("阶数必须大于0");
            }
        }
    } while (n == 0);
    return n;
}
private static int[][] create(int n) {
    int[][] num = new int[n][n];
    int ax = 1, ay = 0, x = 0, y = 0;
    for (int m = 1; m <= n * n; m++) {
        num[x][y] = m;
        int tmpx = x + ax;
        int tmpy = y + ay;
        if (tmpx >= n || tmpx < 0 || tmpy >= n || tmpy < 0 || num[tmpx][tmpy] != 0) {
            if (ax == 0) {
                ax = -ay;
                ay = 0;
            } else if (ay == 0) {
                ay = ax;
                ax = 0;
            }
        }
        x += ax;
        y += ay;
    }
    return num;
}
private static void print(int[][] num) {
    int length = String.valueOf(num.length * num.length).length();
    for (int i = 0; i < num.length; i++) {
        for (int j = 0; j < num[i].length; j++) {
            String tmp = String.valueOf(num[i][j]);
            while (tmp.length() < length) {
                tmp = " " + tmp;
            }
            System.out.print(tmp + " ");
        }
        System.out.println();
    }
}
private static boolean isDigital(String input) {
    if (input == null || input.length() == 0) return false;
    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        if (!Character.isDigit(ch)) {
            System.out.println("输入的阶数必须为数字");
            return false;
        }
    }
    return true;
}

运行时输入要生成的阶数就可以了,比如生成问题上的矩阵输入4就可以了。

【Java】【矩阵】相关

  1. 方法有很多这里简单交流一下我的思路,可以定义一个算法将3*3的矩阵执行某个计算或者代入到某个代数让其运算得出一个数字,对函数或者代数的要求是可逆,即通过一个数字能反推出原矩阵值。
  2. 可以定义查看顺序按照顺序来将矩阵内的数字全部展开。比如我们按照从上到下从左到右的顺义依次记录(1,3)(2,3)(3,3)(1,2)(2,2)......然后使用0来链接两个不同的数这样就可以将矩阵压缩为一个数字并快速还原为矩阵。