-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_matrix.zig
More file actions
117 lines (94 loc) · 5.57 KB
/
basic_matrix.zig
File metadata and controls
117 lines (94 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! Zigen Example: Matrix Fundamentals
//! Demonstrates core matrix and vector creation, element access, operations,
//! and reductions — the building blocks for all linear algebra work.
const std = @import("std");
const Zigen = @import("zigen");
pub fn main() !void {
std.debug.print(
\\
\\ ╔═══════════════════════════════════════════════╗
\\ ║ Zigen — Matrix Fundamentals Example ║
\\ ╚═══════════════════════════════════════════════╝
\\
\\
, .{});
// ── 1. Creating matrices ──────────────────────────────────────────
std.debug.print("━━ 1. Matrix Creation ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n", .{});
const I = Zigen.Matrix3f.identity();
std.debug.print("Identity 3x3:\n", .{});
printMat3(I);
const A = Zigen.Matrix3f.fromArray(.{
.{ 1.0, 2.0, 3.0 },
.{ 4.0, 5.0, 6.0 },
.{ 7.0, 8.0, 9.0 },
});
std.debug.print("Custom 3x3 matrix A:\n", .{});
printMat3(A);
// ── 2. Element access ─────────────────────────────────────────────
std.debug.print("━━ 2. Element Access ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n", .{});
std.debug.print(" A(0,0) = {d:.1} A(1,2) = {d:.1} A(2,1) = {d:.1}\n\n", .{
A.at(0, 0), A.at(1, 2), A.at(2, 1),
});
// ── 3. Arithmetic operations ──────────────────────────────────────
std.debug.print("━━ 3. Arithmetic ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n", .{});
const B = Zigen.Matrix3f.fromArray(.{
.{ 9.0, 8.0, 7.0 },
.{ 6.0, 5.0, 4.0 },
.{ 3.0, 2.0, 1.0 },
});
const sum = A.add(B);
std.debug.print("A + B:\n", .{});
printMat3(sum);
const diff = A.sub(B);
std.debug.print("A - B:\n", .{});
printMat3(diff);
const scaled = A.scale(2.0);
std.debug.print("2*A:\n", .{});
printMat3(scaled);
const prod = A.mul(3, B);
std.debug.print("A x B:\n", .{});
printMat3(prod);
// ── 4. Transpose ──────────────────────────────────────────────────
std.debug.print("━━ 4. Transpose ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n", .{});
const At = A.transpose();
std.debug.print("A^T:\n", .{});
printMat3(At);
// ── 5. Reductions ─────────────────────────────────────────────────
std.debug.print("━━ 5. Reductions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n", .{});
std.debug.print(" trace(A) = {d:.1}\n", .{A.trace()});
std.debug.print(" determinant(A) = {d:.1}\n", .{A.determinant()});
std.debug.print(" norm(A) = {d:.4}\n\n", .{A.norm()});
// ── 6. Vectors ────────────────────────────────────────────────────
std.debug.print("━━ 6. Vector Operations ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n", .{});
const v1 = Zigen.Vector3f.fromArray(.{ 1.0, 2.0, 3.0 });
const v2 = Zigen.Vector3f.fromArray(.{ 4.0, 5.0, 6.0 });
std.debug.print(" v1 = [{d:.1}, {d:.1}, {d:.1}]\n", .{ v1.at(0), v1.at(1), v1.at(2) });
std.debug.print(" v2 = [{d:.1}, {d:.1}, {d:.1}]\n", .{ v2.at(0), v2.at(1), v2.at(2) });
std.debug.print(" v1 . v2 = {d:.1}\n", .{v1.dot(v2)});
const cross = v1.cross(v2);
std.debug.print(" v1 x v2 = [{d:.1}, {d:.1}, {d:.1}]\n", .{ cross.at(0), cross.at(1), cross.at(2) });
std.debug.print(" ||v1|| = {d:.4}\n", .{v1.norm()});
const n = v1.normalized();
std.debug.print(" v1/||v1|| = [{d:.4}, {d:.4}, {d:.4}]\n", .{ n.at(0), n.at(1), n.at(2) });
std.debug.print(" ||normed|| = {d:.4} (should be 1.0)\n\n", .{n.norm()});
// ── 7. Solve linear system to demonstrate matrix-vector ───────────
std.debug.print("━━ 7. Linear Solve (Matrix-Vector via LU) ━━━━━━━━━━━━\n\n", .{});
const M = Zigen.Matrix3f.fromArray(.{
.{ 2.0, 0.0, 0.0 },
.{ 0.0, 3.0, 0.0 },
.{ 0.0, 0.0, 4.0 },
});
const b = Zigen.Vector3f.fromArray(.{ 2.0, 6.0, 12.0 });
const lu = try Zigen.LU(f32, 3).compute(M);
const sol = lu.solve(b);
std.debug.print(" diag(2,3,4) * x = [2,6,12]\n", .{});
std.debug.print(" x = [{d:.1}, {d:.1}, {d:.1}] (expected [1,2,3])\n\n", .{ sol.at(0), sol.at(1), sol.at(2) });
std.debug.print("Done!\n", .{});
}
fn printMat3(m: Zigen.Matrix3f) void {
var i: usize = 0;
while (i < 3) : (i += 1) {
std.debug.print(" | {d:>8.4} {d:>8.4} {d:>8.4} |\n", .{ m.at(i, 0), m.at(i, 1), m.at(i, 2) });
}
std.debug.print("\n", .{});
}