aboutsummaryrefslogtreecommitdiffstats
path: root/src/matrix.zig
blob: e1070690c5e5d83efb5370217e1379aaf3a36a88 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const std = @import("std");
const math = std.math;
const testing = std.testing;

pub fn Matrix(comptime N: usize) type {
  return packed struct {
    const Self = @This();
    pub const Scalar = f32;

    values: [N][N]Scalar,

    /// Initialies a matrix.
    pub fn init(values: [N][N]Scalar) Self {
      return .{ .values = values };
    }

    /// Creates a matrix filled with the given value.
    pub fn filled(n: Scalar) Self {
      var result: Self = undefined;

      comptime var i = 0;
      inline while (i < N) : (i += 1) {
        comptime var j = 0;
        inline while (j < N) : (j += 1) {
          result.values[i][j] = n;
        }
      }

      return result;
    }

    /// A zero initialized matrix.
    pub const ZERO: Self = comptime Self.filled(0);

    /// An identity initialized matrix.
    pub const IDENTITY: Self = comptime brk: {
      var result: Self = undefined;

      comptime var i = 0;
      while (i < N) : (i += 1) {
        comptime var j = 0;
        while (j < N) : (j += 1) {
          result.values[i][j] = brk: {
            if (i == j) {
              break :brk 1;
            } else {
              break :brk 0;
            }
          };
        }
      }

      break :brk result;
    };

    /// Transposes this matrix
    pub fn transpose(self: Self) Self {
      var result: Self = undefined;

      comptime var i = 0;
      inline while (i < N) : (i += 1) {
        comptime var j = 0;
        inline while (j < N) : (j += 1) {
          result.values[j][i] = self.values[i][j];
        }
      }

      return result;
    }

    /// Multiplies 2 matrices together.
    pub fn mul(self: Self, other: Self) Self {
      var result: Self = undefined;

      const a = self.transpose();
      const b = other;

      comptime var i = 0;
      inline while (i < N) : (i += 1) {
        comptime var j = 0;
        inline while (j < N) : (j += 1) {
          const row: @Vector(N, Scalar) = a.values[j];
          const column: @Vector(N, Scalar) = b.values[i];
          const products: [N]Scalar = row * column;

          var sum = @floatCast(f32, 0);
          comptime var k = 0;
          inline while (k < N) : (k += 1) {
            sum += products[k];
          }

          result.values[i][j] = sum;
        }
      }

      return result;
    }
  };
}

fn expectMatrixEqual(comptime N: usize, expected: [N][N]f32, actual: Matrix(N)) void {
  comptime var i = 0;
  inline while (i < N) : (i += 1) {
    comptime var j = 0;
    inline while (j < N) : (j += 1) {
      testing.expectEqual(expected[i][j], actual.values[i][j]);
    }
  }
}

test "matrix initialization" {
  const A = Matrix(2).init(.{ .{ 1, 2 }, .{ 3, 4 } });
  expectMatrixEqual(2, [2][2]f32{ .{ 1, 2, }, .{ 3, 4 } }, A);
}

test "matrix zero" {
  expectMatrixEqual(3, Matrix(3).filled(0).values, Matrix(3).ZERO);
}

test "matrix identity" {
  expectMatrixEqual(3, [3][3]f32{ .{ 1, 0, 0 }, .{ 0, 1 , 0 }, .{ 0, 0, 1 } }, Matrix(3).IDENTITY);
}

test "matrix transpose" {
  const actual = Matrix(3).init(.{ .{ 1, 2, 3 }, .{ 4, 5, 6 }, .{ 7, 8, 9 } });
  const expected = [3][3]f32{ .{ 1, 4, 7 }, .{  2, 5, 8 }, .{ 3, 6, 9 } };
  expectMatrixEqual(3, expected, actual.transpose());
}

test "matrix multiplication" {
  const A = Matrix(2).init(.{ .{ 1, 2 }, .{ 3, 4 } });
  const B = Matrix(2).init(.{ .{ 6, 7 }, .{ 8, 9 } });
  const expected = [2][2]f32{ .{ 27, 40 }, .{ 35, 52 } };
  expectMatrixEqual(2, expected, A.mul(B));
}