Recursive AI

Row-major vs. row-vector matrices in C#

Scale and translate the same point in OneMath and System.Numerics, then see what to transpose when moving a matrix between them.

OneMath is a free, open-source .NET math library from Recursive AI, licensed MIT or Apache-2.0. NuGet · GitHub

dotnet add package RecursiveAI.OneMath --version 1.0.0

Row-major is about storage; row-vector is about multiplication. In a .NET 10 console project, scale (1, 2, 3) by two, then move it 10 units on X:

Program.cs.NET 10
using RecursiveAI.OneMath;
using Numerics = System.Numerics;
 
var point = new Vector3F(1, 2, 3);
var scale = Matrix4x4F.CreateScale(new Vector3F(2, 2, 2));
var move = Matrix4x4F.CreateTranslation(new Vector3F(10, 0, 0));
var transform = move * scale;
var a = transform.TransformPoint(point);
var b = (scale * move).TransformPoint(point);
 
var sn = Numerics.Matrix4x4.CreateScale(2)
       * Numerics.Matrix4x4.CreateTranslation(10, 0, 0);
var c = Numerics.Vector3.Transform(point.ToSystemNumerics(), sn);
Console.WriteLine($"OneMath move * scale: ({a.X}, {a.Y}, {a.Z})");
Console.WriteLine($"OneMath scale * move: ({b.X}, {b.Y}, {b.Z})");
Console.WriteLine($"System.Numerics: ({c.X}, {c.Y}, {c.Z})");
Console.WriteLine($"Converted matrix matches: {transform.ToSystemNumerics() == sn}");
OutputOneMath 1.0.0
OneMath move * scale: (12, 4, 6)
OneMath scale * move: (22, 4, 6)
System.Numerics: (12, 4, 6)
Converted matrix matches: True

Row-major is about storage, not multiplication

For a tightly packed 2×3 matrix with rows [a, b, c] and [d, e, f], row-major storage flattens it to [a, b, c, d, e, f]. Column-major storage flattens the same matrix to [a, d, b, e, c, f]. The elements haven’t changed their mathematical positions. Only their order in the buffer has changed.

By contrast, a row-vector transform is v * M, and a column-vector transform is M * v. Either can use either storage order. The row and column indices in a name like M14 describe a mathematical position, not a byte offset or a multiplication rule.

System.Numerics.Matrix4x4 stores its fields in row-major order in memory (checked on .NET 10 with Marshal.OffsetOf) and transforms row vectors. OneMath’s Matrix4x4F also stores its fields in row-major order in memory but transforms column vectors.

How to read the composition in C#

OneMath’s column-vector convention gives move * scale * point, so the rightmost matrix acts first. Start at X = 1, scale to X = 2, then move to X = 12. Swap the matrices and you get X = 22, because you now scale the translation too.

In System.Numerics the algebra is point * scale * move. The Vector3.Transform(point, matrix) call applies that row-vector convention even though you don’t type a multiplication sign between point and matrix.

Convert the transform and pack the buffer separately

In OneMath an affine 4×4 puts translation in M14, M24, and M34; in System.Numerics it’s in M41, M42, and M43. OneMath’s matrix conversions transpose the values so they represent the same transform in the other convention. Use ToSystemNumerics() or Matrix4x4F.FromSystemNumerics(...) instead of copying fields by name. When converting a composed matrix, transpose the whole matrix once; don’t reverse its factors again.

For a shader or binary format, check two things independently: which side the vector multiplies on, and which order the consumer expects in the buffer. Convert the transform if the first differs, and repack if the second differs. A test point away from the origin and a scale-plus-translation pair can catch errors that an identity matrix won’t.