const std = @import("std");
const math = std.math;
const cos = math.cos;
const sin = math.sin;
const MatrixError = error{IncompatibleDimensions};
const Point3 = struct {
data: [3]f64,
fn x(self: *Point3) f64 {
return self.data[0];
}
fn y(self: *Point3) f64 {
return self.data[1];
}
fn z(self: *Point3) f64 {
return self.data[2];
}
};
const Matrix = struct {
rows: usize,
cols: usize,
data: [][]f64,
fn mul(self: *Matrix, other: *Matrix) *Matrix {
if (self.cols != other.rows) {
return MatrixError.IncompatibleDimensions;
}
const rows = self.rows;
const columns = other.cols;
const data = std.heap.page_allocator.Allocator(f64).malloc(rows * columns);
var sum: f64 = undefined;
for (0..rows) |i| {
for (0..columns) |j| {
sum = 0;
for (0..self.cols) |k| {
sum += self.data[i][k] * other.data[k][j];
}
data[i * columns + j] = sum;
}
}
return Matrix{ .rows = rows, .cols = columns, .data = data };
}
};
pub fn main() !void {
const dX: f64 = 5;
const dY: f64 = 3;
const angle: f64 = 45.0;
var translation1: Matrix = Matrix{
.rows = 3,
.cols = 3,
.data = [3][3]f64{
[_]f64{ 1.0, 0.0, 0.0 },
[_]f64{ 0.0, 1.0, 0.0 },
[_]f64{ -dX, -dY, 1.0 },
},
};
var rotation: Matrix = Matrix{
.rows = 3,
.cols = 3,
.data = [3][3]f64{
[_]f64{ cos(angle), sin(angle), 0.0 },
[_]f64{ -sin(angle), cos(angle), 0.0 },
[_]f64{ 0.0, 0.0, 1.0 },
},
};
const data = translation1.mul(&rotation);
std.debug.print("{} ", .{data});
}
#matrix initialization
1 messages · Page 1 of 1 (latest)
you defined the matrix's .data field to be of type [][]f64, but you're trying to initialise it with [3][3]f64.
arrays and slices are not the same thing!
also, the return type of Matrix.mul is declared to be *Matrix, but you're trying to return a Matrix