Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,117 @@
import java.math.BigInteger;
import java.util.Arrays;
public class Matrix<T extends Number> {
private final T[][] matrix;
private final int rows;
private final int cols;
public Matrix(T[][] matrix) {
this.matrix = matrix;
this.rows = matrix.length;
this.cols = matrix[0].length;
}
public T[] row(int i) {
return matrix[i];
}
@SuppressWarnings("unchecked")
public T[] col(int i) {
T[] column = (T[]) new Number[rows];
for (int j = 0; j < rows; j++) {
column[j] = matrix[j][i];
}
return column;
}
@SuppressWarnings("unchecked")
public Matrix<T> multiply(Matrix<T> other) {
if (this.cols != other.rows) {
throw new IllegalArgumentException("Matrix dimensions do not match for multiplication.");
}
T[][] result = (T[][]) new Number[this.rows][other.cols];
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < other.cols; j++) {
result[i][j] = zero();
for (int k = 0; k < this.cols; k++) {
result[i][j] = add(result[i][j], multiply(this.matrix[i][k], other.matrix[k][j]));
}
}
}
return new Matrix<>(result);
}
@SuppressWarnings("unchecked")
public Matrix<T> power(int x) {
if (x < 0) {
throw new IllegalArgumentException("Power must be non-negative.");
}
if (x == 0) {
return createIdentityMatrix();
} else if (x == 1) {
return this;
} else if (x == 2) {
return this.multiply(this);
} else {
Matrix<T> result = this;
for (int i = 1; i < x; i++) {
result = result.multiply(this);
}
return result;
}
}
@SuppressWarnings("unchecked")
public Matrix<T> createIdentityMatrix() {
T[][] identity = (T[][]) new Number[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
identity[i][j] = (i == j) ? one() : zero();
}
}
return new Matrix<>(identity);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (T[] row : matrix) {
sb.append(Arrays.toString(row)).append("\n");
}
return sb.toString();
}
// Helper methods for numeric operations
private T zero() {
return (T) BigInteger.ZERO;
}
private T one() {
return (T) BigInteger.ONE;
}
private T add(T a, T b) {
return (T) ((BigInteger) a).add((BigInteger) b);
}
private T multiply(T a, T b) {
return (T) ((BigInteger) a).multiply((BigInteger) b);
}
public static void main(String[] args) {
BigInteger[][] data = {
{BigInteger.valueOf(3), BigInteger.valueOf(2)},
{BigInteger.valueOf(2), BigInteger.valueOf(1)}
};
Matrix<BigInteger> m = new Matrix<>(data);
System.out.println("-- m --\n" + m);
int[] powers = {0, 1, 2, 3, 4, 10, 20, 50};
for (int x : powers) {
System.out.println("-- m**" + x + " --");
System.out.println(m.power(x));
}
}
}

View file

@ -0,0 +1,93 @@
import java.util.Arrays;
public class MatrixDemo {
public static final class Matrix {
private final int rows;
private final int cols;
private final double[][] a;
public Matrix(double[][] data) {
if (data == null || data.length == 0 || data[0].length == 0)
throw new IllegalArgumentException("Matrix cannot have zero dimension");
this.rows = data.length;
this.cols = data[0].length;
this.a = new double[rows][cols];
for (int i = 0; i < rows; i++) {
if (data[i].length != cols)
throw new IllegalArgumentException("Jagged arrays are not allowed");
System.arraycopy(data[i], 0, this.a[i], 0, cols);
}
}
public int rows() { return rows; }
public int cols() { return cols; }
public static Matrix identity(int n) {
if (n < 1) throw new IllegalArgumentException("Size of identity matrix can't be less than 1");
double[][] id = new double[n][n];
for (int i = 0; i < n; i++) id[i][i] = 1.0;
return new Matrix(id);
}
public Matrix multiply(Matrix other) {
if (this.cols != other.rows)
throw new IllegalArgumentException("Matrices cannot be multiplied: " +
this.rows + "x" + this.cols + " * " + other.rows + "x" + other.cols);
double[][] r = new double[this.rows][other.cols];
for (int i = 0; i < this.rows; i++) {
for (int k = 0; k < this.cols; k++) {
double v = this.a[i][k];
if (v == 0) continue;
for (int j = 0; j < other.cols; j++) {
r[i][j] += v * other.a[k][j];
}
}
}
return new Matrix(r);
}
public Matrix pow(int n) {
if (rows != cols) throw new IllegalStateException("Not a square matrix");
if (n < 0) throw new IllegalArgumentException("Negative exponents not supported");
if (n == 0) return identity(rows);
if (n == 1) return this;
Matrix result = identity(rows);
Matrix base = this;
int e = n;
while (e > 0) {
if ((e & 1) == 1) result = result.multiply(base);
e >>= 1;
if (e > 0) base = base.multiply(base);
}
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; i < rows; i++) {
sb.append(Arrays.toString(a[i]));
if (i < rows - 1) sb.append('\n');
}
sb.append(']');
return sb.toString();
}
}
public static void main(String[] args) {
Matrix m = new Matrix(new double[][] {
{3, 2},
{2, 1}
});
for (int i = 0; i <= 10; i++) {
System.out.println("** Power of " + i + " **");
System.out.println(m.pow(i));
System.out.println();
}
}
}