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,95 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
with Ada.Numerics.Generic_Elementary_Functions;
procedure QR is
procedure Show (mat : Real_Matrix) is
package FIO is new Ada.Text_IO.Float_IO (Float);
begin
for row in mat'Range (1) loop
for col in mat'Range (2) loop
FIO.Put (mat (row, col), Exp => 0, Aft => 4, Fore => 5);
end loop;
New_Line;
end loop;
end Show;
function GetCol (mat : Real_Matrix; n : Integer) return Real_Matrix is
column : Real_Matrix (mat'Range (1), 1 .. 1);
begin
for row in mat'Range (1) loop
column (row, 1) := mat (row, n);
end loop;
return column;
end GetCol;
function Mag (mat : Real_Matrix) return Float is
sum : Real_Matrix := Transpose (mat) * mat;
package Math is new Ada.Numerics.Generic_Elementary_Functions
(Float);
begin
return Math.Sqrt (sum (1, 1));
end Mag;
function eVect (col : Real_Matrix; n : Integer) return Real_Matrix is
vect : Real_Matrix (col'Range (1), 1 .. 1);
begin
for row in col'Range (1) loop
if row /= n then vect (row, 1) := 0.0;
else vect (row, 1) := 1.0; end if;
end loop;
return vect;
end eVect;
function Identity (n : Integer) return Real_Matrix is
mat : Real_Matrix (1 .. n, 1 .. n) := (1 .. n => (others => 0.0));
begin
for i in Integer range 1 .. n loop mat (i, i) := 1.0; end loop;
return mat;
end Identity;
function Chop (mat : Real_Matrix; n : Integer) return Real_Matrix is
small : Real_Matrix (n .. mat'Length (1), n .. mat'Length (2));
begin
for row in small'Range (1) loop
for col in small'Range (2) loop
small (row, col) := mat (row, col);
end loop;
end loop;
return small;
end Chop;
function H_n (inmat : Real_Matrix; n : Integer)
return Real_Matrix is
mat : Real_Matrix := Chop (inmat, n);
col : Real_Matrix := GetCol (mat, n);
colT : Real_Matrix (1 .. 1, mat'Range (1));
H : Real_Matrix := Identity (mat'Length (1));
Hall : Real_Matrix := Identity (inmat'Length (1));
begin
col := col - Mag (col) * eVect (col, n);
col := col / Mag (col);
colT := Transpose (col);
H := H - 2.0 * (col * colT);
for row in H'Range (1) loop
for col in H'Range (2) loop
Hall (n - 1 + row, n - 1 + col) := H (row, col);
end loop;
end loop;
return Hall;
end H_n;
A : constant Real_Matrix (1 .. 3, 1 .. 3) := (
(12.0, -51.0, 4.0),
(6.0, 167.0, -68.0),
(-4.0, 24.0, -41.0));
Q1, Q2, Q3, Q, R: Real_Matrix (1 .. 3, 1 .. 3);
begin
Q1 := H_n (A, 1);
Q2 := H_n (Q1 * A, 2);
Q3 := H_n (Q2 * Q1* A, 3);
Q := Transpose (Q1) * Transpose (Q2) * TransPose(Q3);
R := Q3 * Q2 * Q1 * A;
Put_Line ("Q:"); Show (Q);
Put_Line ("R:"); Show (R);
end QR;

View file

@ -0,0 +1,268 @@
class Matrix {
constructor(data) {
if (data instanceof Matrix) {
this.data = data.data.map(row => [...row]);
this.rowCount = data.rowCount;
this.columnCount = data.columnCount;
} else if (typeof data === 'number') {
const rows = data;
const cols = arguments[1];
this.rowCount = rows;
this.columnCount = cols;
this.data = Array(rows).fill(0).map(() => Array(cols).fill(0));
} else {
this.rowCount = data.length;
this.columnCount = data[0].length;
this.data = data.map(row => [...row]);
}
}
add(other) {
if (other.rowCount !== this.rowCount || other.columnCount !== this.columnCount) {
throw new Error('Incompatible matrix dimensions.');
}
const result = new Matrix(this);
for (let i = 0; i < this.rowCount; i++) {
for (let j = 0; j < this.columnCount; j++) {
result.data[i][j] = this.data[i][j] + other.data[i][j];
}
}
return result;
}
multiply(other) {
if (this.columnCount !== other.rowCount) {
throw new Error('Incompatible matrix dimensions.');
}
const result = new Matrix(this.rowCount, other.columnCount);
for (let i = 0; i < this.rowCount; i++) {
for (let j = 0; j < other.columnCount; j++) {
for (let k = 0; k < this.rowCount; k++) {
result.data[i][j] += this.data[i][k] * other.data[k][j];
}
}
}
return result;
}
transpose() {
const result = new Matrix(this.columnCount, this.rowCount);
for (let i = 0; i < this.rowCount; i++) {
for (let j = 0; j < this.columnCount; j++) {
result.data[j][i] = this.data[i][j];
}
}
return result;
}
minor(index) {
const result = new Matrix(this.rowCount, this.columnCount);
for (let i = 0; i < index; i++) {
result.setEntry(i, i, 1.0);
}
for (let i = index; i < this.rowCount; i++) {
for (let j = index; j < this.columnCount; j++) {
result.setEntry(i, j, this.data[i][j]);
}
}
return result;
}
column(index) {
const result = new Matrix(this.rowCount, 1);
for (let i = 0; i < this.rowCount; i++) {
result.setEntry(i, 0, this.data[i][index]);
}
return result;
}
scalarMultiply(value) {
if (this.columnCount !== 1) {
throw new Error('Incompatible matrix dimension.');
}
const result = new Matrix(this.rowCount, this.columnCount);
for (let i = 0; i < this.rowCount; i++) {
result.data[i][0] = this.data[i][0] * value;
}
return result;
}
unit() {
if (this.columnCount !== 1) {
throw new Error('Incompatible matrix dimensions.');
}
const magnitude = this.magnitude();
const result = new Matrix(this.rowCount, this.columnCount);
for (let i = 0; i < this.rowCount; i++) {
result.data[i][0] = this.data[i][0] / magnitude;
}
return result;
}
magnitude() {
if (this.columnCount !== 1) {
throw new Error('Incompatible matrix dimensions.');
}
let norm = 0.0;
for (let i = 0; i < this.data.length; i++) {
norm += this.data[i][0] * this.data[i][0];
}
return Math.sqrt(norm);
}
size() {
if (this.columnCount !== 1) {
throw new Error('Incompatible matrix dimensions.');
}
return this.rowCount;
}
display(title) {
console.log(title);
for (let i = 0; i < this.rowCount; i++) {
let row = '';
for (let j = 0; j < this.columnCount; j++) {
row += this.data[i][j].toFixed(4).padStart(9);
}
console.log(row);
}
console.log();
}
getEntry(row, column) {
return this.data[row][column];
}
setEntry(row, column, value) {
this.data[row][column] = value;
}
getRowCount() {
return this.rowCount;
}
getColumnCount() {
return this.columnCount;
}
}
function householder(matrix) {
const rowCount = matrix.getRowCount();
const columnCount = matrix.getColumnCount();
const versionsOfQ = [];
let z = new Matrix(matrix);
let z1 = new Matrix(rowCount, columnCount);
for (let k = 0; k < columnCount && k < rowCount - 1; k++) {
const vectorE = new Matrix(rowCount, 1);
z1 = z.minor(k);
const vectorX = z1.column(k);
let magnitudeX = vectorX.magnitude();
if (matrix.getEntry(k, k) > 0) {
magnitudeX = -magnitudeX;
}
for (let i = 0; i < vectorE.size(); i++) {
vectorE.setEntry(i, 0, i === k ? 1 : 0);
}
const e = vectorE.scalarMultiply(magnitudeX).add(vectorX).unit();
versionsOfQ.push(householderFactor(e));
z = versionsOfQ[k].multiply(z1);
}
let Q = versionsOfQ[0];
for (let i = 1; i < columnCount && i < rowCount - 1; i++) {
Q = versionsOfQ[i].multiply(Q);
}
const R = Q.multiply(matrix);
Q = Q.transpose();
return { r: R, q: Q };
}
function householderFactor(vector) {
if (vector.getColumnCount() !== 1) {
throw new Error('Incompatible matrix dimensions.');
}
const size = vector.size();
const result = new Matrix(size, size);
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
result.setEntry(i, j, -2 * vector.getEntry(i, 0) * vector.getEntry(j, 0));
}
}
for (let i = 0; i < size; i++) {
result.setEntry(i, i, result.getEntry(i, i) + 1.0);
}
return result;
}
function fitPolynomial(x, y, polynomialDegree) {
const vandermonde = new Matrix(x.getColumnCount(), polynomialDegree + 1);
for (let i = 0; i < x.getColumnCount(); i++) {
for (let j = 0; j < polynomialDegree + 1; j++) {
vandermonde.setEntry(i, j, Math.pow(x.getEntry(0, i), j));
}
}
return leastSquares(vandermonde, y.transpose());
}
function leastSquares(vandermonde, b) {
const pair = householder(vandermonde);
return solveUpperTriangular(pair.r, pair.q.transpose().multiply(b));
}
function solveUpperTriangular(r, b) {
const columnCount = r.getColumnCount();
const result = new Matrix(columnCount, 1);
for (let k = columnCount - 1; k >= 0; k--) {
let total = 0.0;
for (let j = k + 1; j < columnCount; j++) {
total += r.getEntry(k, j) * result.getEntry(j, 0);
}
result.setEntry(k, 0, (b.getEntry(k, 0) - total) / r.getEntry(k, k));
}
return result;
}
// Main execution
const data = [
[12.0, -51.0, 4.0],
[6.0, 167.0, -68.0],
[-4.0, 24.0, -41.0],
[-1.0, 1.0, 0.0],
[2.0, 0.0, 3.0]
];
// Task 1
const A = new Matrix(data);
A.display('Initial matrix A:');
const pair = householder(A);
const Q = pair.q;
const R = pair.r;
Q.display('Matrix Q:');
R.display('Matrix R:');
const result = Q.multiply(R);
result.display('Matrix Q * R:');
// Task 2
const x = new Matrix([[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]]);
const y = new Matrix([[1.0, 6.0, 17.0, 34.0, 57.0, 86.0, 121.0, 162.0, 209.0, 262.0, 321.0]]);
const polyResult = fitPolynomial(x, y, 2);
polyResult.display('Result of fitting polynomial:');

View file

@ -0,0 +1,20 @@
require "matrix"
local inp = {
{12, -51, 4},
{ 6, 167, -68},
{-4, 24, -41},
{-1, 1, 0},
{ 2, 0, 3}
}
local x = matrix.from(inp)
local [Q, R] = x:qr()
local m = Q:matmul(R)
local f = "%8.3f"
print("Q:")
print(Q:format(f))
print("\nR:")
print(R:format(f))
print("\nQ x R:")
print(m:format(f))

View file

@ -0,0 +1,86 @@
function qr([double[][]]$A) {
$m,$n = $A.count, $A[0].count
$pm,$pn = ($m-1), ($n-1)
[double[][]]$Q = 0..($m-1) | foreach{$row = @(0) * $m; $row[$_] = 1; ,$row}
[double[][]]$R = $A | foreach{$row = $_; ,@(0..$pn | foreach{$row[$_]})}
foreach ($h in 0..$pn) {
[double[]]$u = $R[$h..$pm] | foreach{$_[$h]}
[double]$nu = $u | foreach {[double]$sq = 0} {$sq += $_*$_} {[Math]::Sqrt($sq)}
$u[0] -= if ($u[0] -lt 0) {$nu} else {-$nu}
[double]$nu = $u | foreach {$sq = 0} {$sq += $_*$_} {[Math]::Sqrt($sq)}
[double[]]$u = $u | foreach { $_/$nu}
[double[][]]$v = 0..($u.Count - 1) | foreach{$i = $_; ,($u | foreach{2*$u[$i]*$_})}
[double[][]]$CR = $R | foreach{$row = $_; ,@(0..$pn | foreach{$row[$_]})}
[double[][]]$CQ = $Q | foreach{$row = $_; ,@(0..$pm | foreach{$row[$_]})}
foreach ($i in $h..$pm) {
foreach ($j in $h..$pn) {
$R[$i][$j] -= $h..$pm | foreach {[double]$sum = 0} {$sum += $v[$i-$h][$_-$h]*$CR[$_][$j]} {$sum}
}
}
if (0 -eq $h) {
foreach ($i in $h..$pm) {
foreach ($j in $h..$pm) {
$Q[$i][$j] -= $h..$pm | foreach {$sum = 0} {$sum += $v[$i][$_]*$CQ[$_][$j]} {$sum}
}
}
} else {
$p = $h-1
foreach ($i in $h..$pm) {
foreach ($j in 0..$p) {
$Q[$i][$j] -= $h..$pm | foreach {$sum = 0} {$sum += $v[$i-$h][$_-$h]*$CQ[$_][$j]} {$sum}
}
foreach ($j in $h..$pm) {
$Q[$i][$j] -= $h..$pm | foreach {$sum = 0} {$sum += $v[$i-$h][$_-$h]*$CQ[$_][$j]} {$sum}
}
}
}
}
foreach ($i in 0..$pm) {
foreach ($j in $i..$pm) {$Q[$i][$j],$Q[$j][$i] = $Q[$j][$i],$Q[$i][$j]}
}
[PSCustomObject]@{"Q" = $Q; "R" = $R}
}
function leastsquares([Double[][]]$A,[Double[]]$y) {
$QR = qr $A
[Double[][]]$Q = $QR.Q
[Double[][]]$R = $QR.R
$m,$n = $A.count, $A[0].count
[Double[]]$z = foreach ($j in 0..($m-1)) {
0..($m-1) | foreach {$sum = 0} {$sum += $Q[$_][$j]*$y[$_]} {$sum}
}
[Double[]]$x = @(0)*$n
for ($i = $n-1; $i -ge 0; $i--) {
for ($j = $i+1; $j -lt $n; $j++) {
$z[$i] -= $x[$j]*$R[$i][$j]
}
$x[$i] = $z[$i]/$R[$i][$i]
}
$x
}
function polyfit([Double[]]$x,[Double[]]$y,$n) {
$m = $x.Count
[Double[][]]$A = 0..($m-1) | foreach{$row = @(1) * ($n+1); ,$row}
for ($i = 0; $i -lt $m; $i++) {
for ($j = $n-1; 0 -le $j; $j--) {
$A[$i][$j] = $A[$i][$j+1]*$x[$i]
}
}
leastsquares $A $y
}
function show($m) {$m | foreach {write-host "$_"}}
$A = @(@(12,-51,4), @(6,167,-68), @(-4,24,-41))
$x = @(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
$y = @(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321)
$QR = qr $A
$ps = (polyfit $x $y 2)
"Q = "
show $QR.Q
"R = "
show $QR.R
"polyfit "
"X^2 X constant"
"$(polyfit $x $y 2)"