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,31 @@
with Ada.Text_IO, Matrix_Scalar;
procedure Scalar_Ops is
subtype T is Integer range 1 .. 3;
package M is new Matrix_Scalar(T, T, Integer);
-- the functions to solve the task
function "+" is new M.Func("+");
function "-" is new M.Func("-");
function "*" is new M.Func("*");
function "/" is new M.Func("/");
function "**" is new M.Func("**");
function "mod" is new M.Func("mod");
-- for output purposes, we need a Matrix->String conversion
function Image is new M.Image(Integer'Image);
A: M.Matrix := ((1,2,3),(4,5,6),(7,8,9)); -- something to begin with
begin
Ada.Text_IO.Put_Line(" Initial M=" & Image(A));
Ada.Text_IO.Put_Line(" M+2=" & Image(A+2));
Ada.Text_IO.Put_Line(" M-2=" & Image(A-2));
Ada.Text_IO.Put_Line(" M*2=" & Image(A*2));
Ada.Text_IO.Put_Line(" M/2=" & Image(A/2));
Ada.Text_IO.Put_Line(" square(M)=" & Image(A ** 2));
Ada.Text_IO.Put_Line(" M mod 2=" & Image(A mod 2));
Ada.Text_IO.Put_Line("(M*2) mod 3=" & Image((A*2) mod 3));
end Scalar_Ops;

View file

@ -0,0 +1,16 @@
generic
type Rows is (<>);
type Cols is (<>);
type Num is private;
package Matrix_Scalar is
type Matrix is array(Rows, Cols) of Num;
generic
with function F(L, R: Num) return Num;
function Func(Left: Matrix; Right: Num) return Matrix;
generic
with function Image(N: Num) return String;
function Image(M: Matrix) return String;
end Matrix_Scalar;

View file

@ -0,0 +1,45 @@
package body Matrix_Scalar is
function Func(Left: Matrix; Right: Num) return Matrix is
Result: Matrix;
begin
for R in Rows loop
for C in Cols loop
Result(R,C) := F(Left(R,C), Right);
end loop;
end loop;
return Result;
end Func;
function Image(M: Matrix) return String is
function Img(R: Rows) return String is
function I(C: Cols) return String is
S: String := Image(M(R,C));
L: Positive := S'First;
begin
while S(L) = ' ' loop
L := L + 1;
end loop;
if C=Cols'Last then
return S(L .. S'Last);
else
return S(L .. S'Last) & "," & I(Cols'Succ(C));
end if;
end I;
Column: String := I(Cols'First);
begin
if R=Rows'Last then
return "(" & Column & ")";
else
return "(" & Column & ")," & Img(Rows'Succ(R));
end if;
end Img;
begin
return("(" & Img(Rows'First) & ")");
end Image;
end Matrix_Scalar;

View file

@ -0,0 +1,44 @@
const OPERATIONS = {
add: (a, b) => a + b,
sub: (a, b) => a - b,
mul: (a, b) => a * b,
div: (a, b) => a / b,
pow: (a, b) => Math.pow(a, b),
mod: (a, b) => a % b,
};
function scalarOp(op, matr, scalar) {
const operation = OPERATIONS[op] || ((a, b) => a);
const result = matr.map(row => row.map(val => operation(val, scalar)));
return result;
}
function matrOp(op, matr, scalar) {
const operation = OPERATIONS[op] || ((a, b) => a);
const result = matr.map((row, i) =>
row.map((val, j) =>
operation(val, scalar[i % scalar.length][j % scalar[i % scalar.length].length])
)
);
return result;
}
function printMatrix(matr) {
matr.forEach(row => console.log(row.join(', ')));
}
// Example usage:
printMatrix(scalarOp("mul", [
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]
], 3.0));
printMatrix(matrOp("div", [
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]
], [
[1.0, 2.0],
[3.0, 4.0]
]));

View file

@ -0,0 +1,33 @@
require "matrix"
local a = { {3, 5, 7}, {1, 2, 3}, {2, 4, 6} }
local m = matrix.from(a)
local s = 2
print("Matrix/matrix operations:")
print("m:")
print(m)
print("\nm + m:")
print(m + m)
print("\nm - m:")
print(m - m)
print("\nm * m:")
print(m * m)
print("\nm / m:")
print((m / m):format("%d"))
print("\nm ^ m:")
print((m ^ m):format("%6d"))
print($"\nMatrix/scalar operations for s = {s}: ")
print("m:")
print(m)
print("\nm + s:")
print(m + s)
print("\nm - s:")
print(m - s)
print("\nm * s:")
print(m * s)
print("\nm / s:")
print(m / s)
print("\nm ^ s:")
print((m ^ s):format("%2d"))