Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,90 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Test_Matrix is
|
||||
generic
|
||||
type Element is private;
|
||||
Zero : Element;
|
||||
One : Element;
|
||||
with function "+" (A, B : Element) return Element is <>;
|
||||
with function "*" (A, B : Element) return Element is <>;
|
||||
with function Image (X : Element) return String is <>;
|
||||
package Matrices is
|
||||
type Matrix is array (Integer range <>, Integer range <>) of Element;
|
||||
function "*" (A, B : Matrix) return Matrix;
|
||||
function "**" (A : Matrix; Power : Natural) return Matrix;
|
||||
procedure Put (A : Matrix);
|
||||
end Matrices;
|
||||
|
||||
package body Matrices is
|
||||
function "*" (A, B : Matrix) return Matrix is
|
||||
R : Matrix (A'Range (1), B'Range (2));
|
||||
Sum : Element := Zero;
|
||||
begin
|
||||
for I in R'Range (1) loop
|
||||
for J in R'Range (2) loop
|
||||
Sum := Zero;
|
||||
for K in A'Range (2) loop
|
||||
Sum := Sum + A (I, K) * B (K, J);
|
||||
end loop;
|
||||
R (I, J) := Sum;
|
||||
end loop;
|
||||
end loop;
|
||||
return R;
|
||||
end "*";
|
||||
|
||||
function "**" (A : Matrix; Power : Natural) return Matrix is
|
||||
begin
|
||||
if Power = 1 then
|
||||
return A;
|
||||
end if;
|
||||
declare
|
||||
R : Matrix (A'Range (1), A'Range (2)) := (others => (others => Zero));
|
||||
P : Matrix := A;
|
||||
E : Natural := Power;
|
||||
begin
|
||||
for I in P'Range (1) loop -- R is identity matrix
|
||||
R (I, I) := One;
|
||||
end loop;
|
||||
if E = 0 then
|
||||
return R;
|
||||
end if;
|
||||
loop
|
||||
if E mod 2 /= 0 then
|
||||
R := R * P;
|
||||
end if;
|
||||
E := E / 2;
|
||||
exit when E = 0;
|
||||
P := P * P;
|
||||
end loop;
|
||||
return R;
|
||||
end;
|
||||
end "**";
|
||||
|
||||
procedure Put (A : Matrix) is
|
||||
begin
|
||||
for I in A'Range (1) loop
|
||||
for J in A'Range (1) loop
|
||||
Put (Image (A (I, J)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Put;
|
||||
end Matrices;
|
||||
|
||||
package Integer_Matrices is new Matrices (Integer, 0, 1, Image => Integer'Image);
|
||||
use Integer_Matrices;
|
||||
|
||||
M : Matrix (1..2, 1..2) := ((3,2),(2,1));
|
||||
begin
|
||||
Put_Line ("M ="); Put (M);
|
||||
Put_Line ("M**0 ="); Put (M**0);
|
||||
Put_Line ("M**1 ="); Put (M**1);
|
||||
Put_Line ("M**2 ="); Put (M**2);
|
||||
Put_Line ("M*M ="); Put (M*M);
|
||||
Put_Line ("M**3 ="); Put (M**3);
|
||||
Put_Line ("M*M*M ="); Put (M*M*M);
|
||||
Put_Line ("M**4 ="); Put (M**4);
|
||||
Put_Line ("M*M*M*M ="); Put (M*M*M*M);
|
||||
Put_Line ("M**10 ="); Put (M**10);
|
||||
Put_Line ("M*M*M*M*M*M*M*M*M*M ="); Put (M*M*M*M*M*M*M*M*M*M);
|
||||
end Test_Matrix;
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Complex_Text_IO; use Ada.Complex_Text_IO;
|
||||
with Ada.Numerics.Complex_Types; use Ada.Numerics.Complex_Types;
|
||||
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
|
||||
with Ada.Numerics.Complex_Arrays; use Ada.Numerics.Complex_Arrays;
|
||||
with Ada.Numerics.Complex_Elementary_Functions; use Ada.Numerics.Complex_Elementary_Functions;
|
||||
|
||||
procedure Test_Matrix is
|
||||
function "**" (A : Complex_Matrix; Power : Complex) return Complex_Matrix is
|
||||
L : Real_Vector (A'Range (1));
|
||||
X : Complex_Matrix (A'Range (1), A'Range (2));
|
||||
R : Complex_Matrix (A'Range (1), A'Range (2));
|
||||
RL : Complex_Vector (A'Range (1));
|
||||
begin
|
||||
Eigensystem (A, L, X);
|
||||
for I in L'Range loop
|
||||
RL (I) := (L (I), 0.0) ** Power;
|
||||
end loop;
|
||||
for I in R'Range (1) loop
|
||||
for J in R'Range (2) loop
|
||||
declare
|
||||
Sum : Complex := (0.0, 0.0);
|
||||
begin
|
||||
for K in RL'Range (1) loop
|
||||
Sum := Sum + X (I, K) * RL (K) * X (J, K);
|
||||
end loop;
|
||||
R (I, J) := Sum;
|
||||
end;
|
||||
end loop;
|
||||
end loop;
|
||||
return R;
|
||||
end "**";
|
||||
procedure Put (A : Complex_Matrix) is
|
||||
begin
|
||||
for I in A'Range (1) loop
|
||||
for J in A'Range (2) loop
|
||||
Put (A (I, J));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Put;
|
||||
M : Complex_Matrix (1..2, 1..2) := (((3.0,0.0),(2.0,1.0)),((2.0,-1.0),(1.0,0.0)));
|
||||
begin
|
||||
Put_Line ("M ="); Put (M);
|
||||
Put_Line ("M**0 ="); Put (M**(0.0,0.0));
|
||||
Put_Line ("M**1 ="); Put (M**(1.0,0.0));
|
||||
Put_Line ("M**0.5 ="); Put (M**(0.5,0.0));
|
||||
end Test_Matrix;
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
|
||||
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
|
||||
|
||||
procedure Test_Matrix is
|
||||
procedure Put (A : Real_Matrix) is
|
||||
begin
|
||||
for I in A'Range (1) loop
|
||||
for J in A'Range (2) loop
|
||||
Put (" ");
|
||||
Put (A (I, J));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Put;
|
||||
function "**" (A : Real_Matrix; Power : Integer) return Real_Matrix is
|
||||
L : Real_Vector (A'Range (1));
|
||||
X : Real_Matrix (A'Range (1), A'Range (2));
|
||||
R : Real_Matrix (A'Range (1), A'Range (2));
|
||||
RL : Real_Vector (A'Range (1));
|
||||
begin
|
||||
Eigensystem (A, L, X);
|
||||
for I in L'Range loop
|
||||
RL (I) := L (I) ** Power;
|
||||
end loop;
|
||||
for I in R'Range (1) loop
|
||||
for J in R'Range (2) loop
|
||||
declare
|
||||
Sum : Float := 0.0;
|
||||
begin
|
||||
for K in RL'Range loop
|
||||
Sum := Sum + X (I, K) * RL (K) * X (J, K);
|
||||
end loop;
|
||||
R (I, J) := Sum;
|
||||
end;
|
||||
end loop;
|
||||
end loop;
|
||||
return R;
|
||||
end "**";
|
||||
M : Real_Matrix (1..2, 1..2) := ((3.0, 2.0), (2.0, 1.0));
|
||||
begin
|
||||
Put_Line ("M ="); Put (M);
|
||||
Put_Line ("M**0 ="); Put (M**0);
|
||||
Put_Line ("M**1 ="); Put (M**1);
|
||||
Put_Line ("M**2 ="); Put (M**2);
|
||||
Put_Line ("M**3 ="); Put (M**3);
|
||||
Put_Line ("M**50 ="); Put (M**50);
|
||||
end Test_Matrix;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
proc **(a, e) {
|
||||
// create result matrix of same dimensions
|
||||
var r:[a.domain] a.eltType;
|
||||
// and initialize to identity matrix
|
||||
forall ij in r.domain do
|
||||
r(ij) = if ij(1) == ij(2) then 1 else 0;
|
||||
|
||||
for 1..e do
|
||||
r *= a;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
var m:[1..3, 1..3] int;
|
||||
m(1,1) = 1; m(1,2) = 2; m(1,3) = 0;
|
||||
m(2,1) = 0; m(2,2) = 3; m(2,3) = 1;
|
||||
m(3,1) = 1; m(3,2) = 0; m(3,3) = 0;
|
||||
|
||||
config param n = 10;
|
||||
|
||||
for i in 0..n do {
|
||||
writeln("Order ", i);
|
||||
writeln(m ** i, "\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
import util
|
||||
|
||||
procedure main()
|
||||
M := Matrix([[3,2], [2,1]])
|
||||
every i := 0 to 5 do {
|
||||
write("M^",i,":")
|
||||
writeMatrix(M^i)
|
||||
}
|
||||
end
|
||||
|
||||
class Matrix(M) # Operator overloading needs a class
|
||||
|
||||
# Extend the "^" binary operator
|
||||
method __powr__(n)
|
||||
if not (integer(n) >= 0) then fail
|
||||
M1 := m_identity(*M,*M[1])
|
||||
# Brute-force approach:
|
||||
every 1 to n do M1 := m_multiply(M1,M)
|
||||
return M1
|
||||
end
|
||||
|
||||
initially (a)
|
||||
M := m_copy(a)
|
||||
end
|
||||
|
||||
procedure writeMatrix(M)
|
||||
every r := !M do {
|
||||
every writes(!r," ")
|
||||
write()
|
||||
}
|
||||
write()
|
||||
end
|
||||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
require "matrix"
|
||||
|
||||
local m = matrix.from({ {3, 2}, {2, 1} })
|
||||
print("Original:\n")
|
||||
print(m)
|
||||
print("\nRaised to power of 10 using element-wise exponentiation:\n")
|
||||
print((m ^ 10):format("%5d"))
|
||||
print("\nRaised to power of 10 using repeated matrix multiplication:\n")
|
||||
print(m :matpow(10))
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
(define (dec x)
|
||||
(- x 1))
|
||||
|
||||
(define (halve x)
|
||||
(/ x 2))
|
||||
|
||||
(define (row*col row col)
|
||||
(apply + (map * row col)))
|
||||
|
||||
(define (matrix-multiply m1 m2)
|
||||
(map
|
||||
(lambda (row)
|
||||
(apply map (lambda col (row*col row col))
|
||||
m2))
|
||||
m1))
|
||||
|
||||
(define (matrix-exp mat exp)
|
||||
(cond ((= exp 1) mat)
|
||||
((even? exp) (square-matrix (matrix-exp mat (halve exp))))
|
||||
(else (matrix-multiply mat (matrix-exp mat (dec exp))))))
|
||||
|
||||
(define (square-matrix mat)
|
||||
(matrix-multiply mat mat))
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
(import (srfi 231))
|
||||
|
||||
(define (matrix* A B)
|
||||
(array-copy! (array-inner-product A + * B)))
|
||||
|
||||
(define (matrix-square A)
|
||||
(matrix* A A))
|
||||
|
||||
(define (matrix-identity A)
|
||||
(array-copy! (make-array (array-domain A)
|
||||
(lambda (i j) (if (= i j) 1 0)))))
|
||||
|
||||
(define (matrix-expt A n)
|
||||
(cond ((zero? n) (matrix-identity A))
|
||||
((= 1 n) A)
|
||||
((even? n) (matrix-expt (matrix-square A)
|
||||
(quotient n 2)))
|
||||
(else (matrix* A (matrix-expt (matrix-square A)
|
||||
(quotient n 2))))))
|
||||
|
||||
(define a
|
||||
(list*->array 2 '((3 2)
|
||||
(2 1))))
|
||||
|
||||
(for-each (lambda (i)
|
||||
(for-each display
|
||||
(list "a^" i " = " (array->list* (matrix-expt a i)) #\newline)))
|
||||
(iota 11))
|
||||
|
|
@ -0,0 +1,182 @@
|
|||
const std = @import("std");
|
||||
const print = std.debug.print;
|
||||
const ArrayList = std.ArrayList;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const WIDTH: usize = 6;
|
||||
|
||||
const SqMat = struct {
|
||||
data: ArrayList(ArrayList(i64)),
|
||||
allocator: Allocator,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init(allocator: Allocator, mat_size: usize) !Self {
|
||||
var mat = Self{
|
||||
.data = ArrayList(ArrayList(i64)){},
|
||||
.allocator = allocator,
|
||||
};
|
||||
|
||||
for (0..mat_size) |_| {
|
||||
var row = ArrayList(i64){};
|
||||
for (0..mat_size) |_| {
|
||||
try row.append(allocator, 0);
|
||||
}
|
||||
try mat.data.append(allocator, row);
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
pub fn initWithData(allocator: Allocator, data: []const []const i64) !Self {
|
||||
var mat = Self{
|
||||
.data = ArrayList(ArrayList(i64)){},
|
||||
.allocator = allocator,
|
||||
};
|
||||
|
||||
for (data) |row_data| {
|
||||
var row = ArrayList(i64){};
|
||||
for (row_data) |val| {
|
||||
try row.append(allocator, val);
|
||||
}
|
||||
try mat.data.append(allocator, row);
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
// pub fn deinit(self: *Self) void {
|
||||
// for (self.data.items) |*row| {
|
||||
// row.deinit();
|
||||
// }
|
||||
// self.data.deinit();
|
||||
// }
|
||||
|
||||
pub fn clone(self: *const Self) !Self {
|
||||
var new_mat = Self{
|
||||
.data = ArrayList(ArrayList(i64)){},
|
||||
.allocator = self.allocator,
|
||||
};
|
||||
|
||||
for (self.data.items) |row| {
|
||||
var new_row = ArrayList(i64){};
|
||||
for (row.items) |val| {
|
||||
try new_row.append(self.allocator, val);
|
||||
}
|
||||
try new_mat.data.append(self.allocator, new_row);
|
||||
}
|
||||
|
||||
return new_mat;
|
||||
}
|
||||
|
||||
|
||||
pub fn printMatrix(self: *const Self) void {
|
||||
for (self.data.items) |row| {
|
||||
for (row.items) |val| {
|
||||
print("{d:>6} ", .{val});
|
||||
}
|
||||
print("\n" , .{});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size(self: *const Self) usize {
|
||||
return self.data.items.len;
|
||||
}
|
||||
|
||||
pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
|
||||
_ = fmt;
|
||||
_ = options;
|
||||
|
||||
for (self.data.items) |row| {
|
||||
for (row.items) |val| {
|
||||
try writer.print("{d:>6} ", .{val});
|
||||
}
|
||||
try writer.print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pow(self: *const Self, n: u32) !Self {
|
||||
const mat_size = self.size();
|
||||
var aux_data = try self.clone();
|
||||
//defer aux_data.deinit();
|
||||
|
||||
// Initialize identity matrix
|
||||
var ans = try Self.init(self.allocator, mat_size);
|
||||
for (0..mat_size) |i| {
|
||||
ans.data.items[i].items[i] = 1;
|
||||
}
|
||||
|
||||
var b = n;
|
||||
while (b > 0) {
|
||||
if (b & 1 > 0) {
|
||||
// ans = ans * aux
|
||||
var tmp = try Self.init(self.allocator, mat_size);
|
||||
// defer tmp.deinit();
|
||||
|
||||
for (0..mat_size) |i| {
|
||||
for (0..mat_size) |j| {
|
||||
tmp.data.items[i].items[j] = 0;
|
||||
for (0..mat_size) |k| {
|
||||
tmp.data.items[i].items[j] += ans.data.items[i].items[k] * aux_data.data.items[k].items[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Copy tmp to ans
|
||||
for (0..mat_size) |i| {
|
||||
for (0..mat_size) |j| {
|
||||
ans.data.items[i].items[j] = tmp.data.items[i].items[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b >>= 1;
|
||||
if (b > 0) {
|
||||
// aux = aux * aux
|
||||
var tmp = try Self.init(self.allocator, mat_size);
|
||||
// defer tmp.deinit();
|
||||
|
||||
for (0..mat_size) |i| {
|
||||
for (0..mat_size) |j| {
|
||||
tmp.data.items[i].items[j] = 0;
|
||||
for (0..mat_size) |k| {
|
||||
tmp.data.items[i].items[j] += aux_data.data.items[i].items[k] * aux_data.data.items[k].items[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Copy tmp to aux_data
|
||||
for (0..mat_size) |i| {
|
||||
for (0..mat_size) |j| {
|
||||
aux_data.data.items[i].items[j] = tmp.data.items[i].items[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const matrix_data = [_][]const i64{
|
||||
&[_]i64{ 1, 2, 0 },
|
||||
&[_]i64{ 0, 3, 1 },
|
||||
&[_]i64{ 1, 0, 0 },
|
||||
};
|
||||
|
||||
var sm = try SqMat.initWithData(allocator, &matrix_data);
|
||||
// defer sm.deinit();
|
||||
|
||||
for (0..11) |i| {
|
||||
var result = try sm.pow(@intCast(i));
|
||||
// defer result.deinit();
|
||||
print("Power of {d}:\n", .{i});
|
||||
result.printMatrix();
|
||||
print("\n" , .{});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue