Skip to content

More complete matrix implementation. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 105 additions & 7 deletions sources/Maths/Maths/Matrix2x2F.gen.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,119 @@
namespace Silk.NET.Maths
{
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

partial struct Matrix2x2F<T> : IEquatable<Matrix2x2F<T>> where T : IFloatingPointIeee754<T>
{
/// <summary>The multiplicative identity matrix of size 2x2.</summary>
public static readonly Matrix2x2F<T> Identity = new(
new(T.MultiplicativeIdentity, T.Zero),
new(T.Zero, T.MultiplicativeIdentity));

/// <summary>The 1st row of the matrix represented as a vector.</summary>
public Vector2F<T> Row1;
public T M11 => Row1.X;
public T M12 => Row1.Y;

/// <summary>The 2nd row of the matrix represented as a vector.</summary>
public Vector2F<T> Row2;
public T M21 => Row2.X;
public T M22 => Row2.Y;
public static bool operator ==(Matrix2x2F<T> left, Matrix2x2F<T> right) => left.Row1 == right.Row1 && left.Row2 == right.Row2;
public static bool operator !=(Matrix2x2F<T> left, Matrix2x2F<T> right) => !(left == right);

/// <summary>
/// Constructs a <see cref="Matrix2x2F{T}"/> from the given rows.
/// </summary>
public Matrix2x2F(Vector2F<T> row1, Vector2F<T> row2) => (Row1, Row2) = (row1, row2);

[UnscopedRef]
public ref Vector2F<T> this[int row]
{
get
{
switch (row)
{
case 0:
return ref Row1;
case 1:
return ref Row2;
}

throw new ArgumentOutOfRangeException(nameof(row));
}
}

[UnscopedRef]
public ref Vector2F<T> this[int row, int column] => ref this[row][column];

/// <summary>Gets the element in the 1st row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M11 => ref Row1.X;

/// <summary>Gets the element in the 1st row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M12 => ref Row1.Y;

/// <summary>Gets the element in the 2nd row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M21 => ref Row2.X;

/// <summary>Gets the element in the 2nd row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M22 => ref Row2.Y;

/// <inheridoc/>
public override bool Equals(object? obj) => obj is Matrix2x2F<T> other && Equals(other);

/// <inheridoc/>
public bool Equals(Matrix2x2F<T> other) => this == other;

/// <inheridoc/>
public override int GetHashCode() => HashCode.Combine(Row1, Row2);

/// <summary>Computes the transpose of the matrix.</summary>
public Matrix2x2F<T> Transpose() =>
new(new(M11, M21),
new(M12, M22))

/// <summary>Returns a boolean indicating whether the given two matrices are equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Matrix2x2F<T> left, Matrix2x2F<T> right) =>
left.Row1 == right.Row1 &&
left.Row2 == right.Row2;

/// <summary>Returns a boolean indicating whether the given two matrices are not equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Matrix2x2F<T> left, Matrix2x2F<T> right) => !(left == right);

/// <summary>Adds two matrices together.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the addition.</returns>
public static Matrix2x2F<T> operator +(Matrix2x2F<T> left, Matrix2x2F<T> right) =>
new(left.Row1 + right.Row1,
left.Row2 + right.Row2);

/// <summary>Subtracts the second matrix from the first.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the subtraction.</returns>
public static Matrix2x2F<T> operator -(Matrix2x2F<T> left, Matrix2x2F<T> right) =>
new(left.Row1 - right.Row1,
left.Row2 - right.Row2);

/// <summary>Returns a new matrix with the negated elements of the given matrix.</summary>
/// <param name="value">The source matrix.</param>
/// <returns>The negated matrix.</returns>
public static Matrix2x2F<T> operator -(Matrix2x2F<T> value) =>
new(-value.Row1,
-value.Row2);

/// <summary>Multiplies a matrix by another matrix.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the multiplication.</returns>
public static Matrix2x2F<T> operator *(Matrix2x2F<T> left, Matrix2x2F<T> right) =>
new(left.M11 * right.Row1 + left.M12 * right.Row2,
left.M21 * right.Row1 + left.M22 * right.Row2);
}
}
}
112 changes: 105 additions & 7 deletions sources/Maths/Maths/Matrix2x2I.gen.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,119 @@
namespace Silk.NET.Maths
{
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

partial struct Matrix2x2I<T> : IEquatable<Matrix2x2I<T>> where T : IBinaryInteger<T>
{
/// <summary>The multiplicative identity matrix of size 2x2.</summary>
public static readonly Matrix2x2I<T> Identity = new(
new(T.MultiplicativeIdentity, T.Zero),
new(T.Zero, T.MultiplicativeIdentity));

/// <summary>The 1st row of the matrix represented as a vector.</summary>
public Vector2I<T> Row1;
public T M11 => Row1.X;
public T M12 => Row1.Y;

/// <summary>The 2nd row of the matrix represented as a vector.</summary>
public Vector2I<T> Row2;
public T M21 => Row2.X;
public T M22 => Row2.Y;
public static bool operator ==(Matrix2x2I<T> left, Matrix2x2I<T> right) => left.Row1 == right.Row1 && left.Row2 == right.Row2;
public static bool operator !=(Matrix2x2I<T> left, Matrix2x2I<T> right) => !(left == right);

/// <summary>
/// Constructs a <see cref="Matrix2x2I{T}"/> from the given rows.
/// </summary>
public Matrix2x2I(Vector2I<T> row1, Vector2I<T> row2) => (Row1, Row2) = (row1, row2);

[UnscopedRef]
public ref Vector2I<T> this[int row]
{
get
{
switch (row)
{
case 0:
return ref Row1;
case 1:
return ref Row2;
}

throw new ArgumentOutOfRangeException(nameof(row));
}
}

[UnscopedRef]
public ref Vector2I<T> this[int row, int column] => ref this[row][column];

/// <summary>Gets the element in the 1st row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M11 => ref Row1.X;

/// <summary>Gets the element in the 1st row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M12 => ref Row1.Y;

/// <summary>Gets the element in the 2nd row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M21 => ref Row2.X;

/// <summary>Gets the element in the 2nd row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M22 => ref Row2.Y;

/// <inheridoc/>
public override bool Equals(object? obj) => obj is Matrix2x2I<T> other && Equals(other);

/// <inheridoc/>
public bool Equals(Matrix2x2I<T> other) => this == other;

/// <inheridoc/>
public override int GetHashCode() => HashCode.Combine(Row1, Row2);

/// <summary>Computes the transpose of the matrix.</summary>
public Matrix2x2I<T> Transpose() =>
new(new(M11, M21),
new(M12, M22))

/// <summary>Returns a boolean indicating whether the given two matrices are equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Matrix2x2I<T> left, Matrix2x2I<T> right) =>
left.Row1 == right.Row1 &&
left.Row2 == right.Row2;

/// <summary>Returns a boolean indicating whether the given two matrices are not equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Matrix2x2I<T> left, Matrix2x2I<T> right) => !(left == right);

/// <summary>Adds two matrices together.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the addition.</returns>
public static Matrix2x2I<T> operator +(Matrix2x2I<T> left, Matrix2x2I<T> right) =>
new(left.Row1 + right.Row1,
left.Row2 + right.Row2);

/// <summary>Subtracts the second matrix from the first.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the subtraction.</returns>
public static Matrix2x2I<T> operator -(Matrix2x2I<T> left, Matrix2x2I<T> right) =>
new(left.Row1 - right.Row1,
left.Row2 - right.Row2);

/// <summary>Returns a new matrix with the negated elements of the given matrix.</summary>
/// <param name="value">The source matrix.</param>
/// <returns>The negated matrix.</returns>
public static Matrix2x2I<T> operator -(Matrix2x2I<T> value) =>
new(-value.Row1,
-value.Row2);

/// <summary>Multiplies a matrix by another matrix.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the multiplication.</returns>
public static Matrix2x2I<T> operator *(Matrix2x2I<T> left, Matrix2x2I<T> right) =>
new(left.M11 * right.Row1 + left.M12 * right.Row2,
left.M21 * right.Row1 + left.M22 * right.Row2);
}
}
}
126 changes: 117 additions & 9 deletions sources/Maths/Maths/Matrix2x3F.gen.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,131 @@
namespace Silk.NET.Maths
{
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

partial struct Matrix2x3F<T> : IEquatable<Matrix2x3F<T>> where T : IFloatingPointIeee754<T>
{
/// <summary>The 1st row of the matrix represented as a vector.</summary>
public Vector3F<T> Row1;
public T M11 => Row1.X;
public T M12 => Row1.Y;
public T M13 => Row1.Z;

/// <summary>The 2nd row of the matrix represented as a vector.</summary>
public Vector3F<T> Row2;
public T M21 => Row2.X;
public T M22 => Row2.Y;
public T M23 => Row2.Z;
public static bool operator ==(Matrix2x3F<T> left, Matrix2x3F<T> right) => left.Row1 == right.Row1 && left.Row2 == right.Row2;
public static bool operator !=(Matrix2x3F<T> left, Matrix2x3F<T> right) => !(left == right);

/// <summary>
/// Constructs a <see cref="Matrix2x3F{T}"/> from the given rows.
/// </summary>
public Matrix2x3F(Vector3F<T> row1, Vector3F<T> row2) => (Row1, Row2) = (row1, row2);

[UnscopedRef]
public ref Vector3F<T> this[int row]
{
get
{
switch (row)
{
case 0:
return ref Row1;
case 1:
return ref Row2;
}

throw new ArgumentOutOfRangeException(nameof(row));
}
}

[UnscopedRef]
public ref Vector3F<T> this[int row, int column] => ref this[row][column];

/// <summary>Gets the element in the 1st row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M11 => ref Row1.X;

/// <summary>Gets the element in the 1st row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M12 => ref Row1.Y;

/// <summary>Gets the element in the 1st row and 3rd column of the matrix.</summary>
[UnscopedRef]
public ref T M13 => ref Row1.Z;

/// <summary>Gets the element in the 2nd row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M21 => ref Row2.X;

/// <summary>Gets the element in the 2nd row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M22 => ref Row2.Y;

/// <summary>Gets the element in the 2nd row and 3rd column of the matrix.</summary>
[UnscopedRef]
public ref T M23 => ref Row2.Z;

/// <inheridoc/>
public override bool Equals(object? obj) => obj is Matrix2x3F<T> other && Equals(other);

/// <inheridoc/>
public bool Equals(Matrix2x3F<T> other) => this == other;

/// <inheridoc/>
public override int GetHashCode() => HashCode.Combine(Row1, Row2);

/// <summary>Computes the transpose of the matrix.</summary>
public Matrix3x2F<T> Transpose() =>
new(new(M11, M21),
new(M12, M22),
new(M13, M23))

/// <summary>Returns a boolean indicating whether the given two matrices are equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Matrix2x3F<T> left, Matrix2x3F<T> right) =>
left.Row1 == right.Row1 &&
left.Row2 == right.Row2;

/// <summary>Returns a boolean indicating whether the given two matrices are not equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Matrix2x3F<T> left, Matrix2x3F<T> right) => !(left == right);

/// <summary>Adds two matrices together.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the addition.</returns>
public static Matrix2x3F<T> operator +(Matrix2x3F<T> left, Matrix2x3F<T> right) =>
new(left.Row1 + right.Row1,
left.Row2 + right.Row2);

/// <summary>Subtracts the second matrix from the first.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the subtraction.</returns>
public static Matrix2x3F<T> operator -(Matrix2x3F<T> left, Matrix2x3F<T> right) =>
new(left.Row1 - right.Row1,
left.Row2 - right.Row2);

/// <summary>Returns a new matrix with the negated elements of the given matrix.</summary>
/// <param name="value">The source matrix.</param>
/// <returns>The negated matrix.</returns>
public static Matrix2x3F<T> operator -(Matrix2x3F<T> value) =>
new(-value.Row1,
-value.Row2);

/// <summary>Multiplies a matrix by another matrix.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the multiplication.</returns>
public static Matrix2x3F<T> operator *(Matrix2x2F<T> left, Matrix2x3F<T> right) =>
new(left.M11 * right.Row1 + left.M12 * right.Row2,
left.M21 * right.Row1 + left.M22 * right.Row2);

/// <summary>Multiplies a matrix by another matrix.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the multiplication.</returns>
public static Matrix2x2F<T> operator *(Matrix2x3F<T> left, Matrix3x2F<T> right) =>
new(left.M11 * right.Row1 + left.M12 * right.Row2 + left.M13 * right.Row3,
left.M21 * right.Row1 + left.M22 * right.Row2 + left.M23 * right.Row3);
}
}
}
Loading