Skip to content

Commit

Permalink
Fix a stupid mistake in my math, well math class was way to long ago
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWandererProductions committed Dec 12, 2024
1 parent 8a936c1 commit 630c1b4
Show file tree
Hide file tree
Showing 24 changed files with 342 additions and 544 deletions.
59 changes: 12 additions & 47 deletions Mathematics/BaseMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,9 @@ public sealed class BaseMatrix : IDisposable
/// <param name="dimY">The Height.</param>
public BaseMatrix(int dimX, int dimY)
{
if (dimX <= 0)
{
throw new ArgumentException(MathResources.MatrixErrorNegativeValue, nameof(dimX));
}
if (dimX <= 0) throw new ArgumentException(MathResources.MatrixErrorNegativeValue, nameof(dimX));

if (dimY <= 0)
{
throw new ArgumentException(MathResources.MatrixErrorNegativeValue, nameof(dimY));
}
if (dimY <= 0) throw new ArgumentException(MathResources.MatrixErrorNegativeValue, nameof(dimY));

Matrix = new double[dimX, dimY];
}
Expand All @@ -51,9 +45,7 @@ public BaseMatrix(int dimX, int dimY)
public BaseMatrix(double[,] matrix)
{
if (matrix.GetLength(0) <= 0 || matrix.GetLength(1) <= 0)
{
throw new ArgumentException(MathResources.MatrixErrorNegativeValue, nameof(matrix));
}

Matrix = matrix;
}
Expand Down Expand Up @@ -141,10 +133,7 @@ public void Dispose()
/// </param>
private void Dispose(bool disposing)
{
if (Disposed)
{
return;
}
if (Disposed) return;

if (disposing)
{
Expand Down Expand Up @@ -174,10 +163,7 @@ private void Dispose(bool disposing)
/// <returns>The Inverse Matrix</returns>
public BaseMatrix Inverse()
{
if (Height != Width)
{
throw new NotImplementedException(MathResources.MatrixErrorInverseNotCubic);
}
if (Height != Width) throw new NotImplementedException(MathResources.MatrixErrorInverseNotCubic);

var result = MatrixInverse.Inverse(Matrix);
return new BaseMatrix(result);
Expand All @@ -190,10 +176,7 @@ public BaseMatrix Inverse()
/// <exception cref="System.NotImplementedException"></exception>
public KeyValuePair<BaseMatrix, BaseMatrix> LuDecomposition()
{
if (Height != Width)
{
throw new NotImplementedException(MathResources.MatrixErrorInverseNotCubic);
}
if (Height != Width) throw new NotImplementedException(MathResources.MatrixErrorInverseNotCubic);

var (l, u) = MatrixInverse.LuDecomposition(Matrix);

Expand Down Expand Up @@ -223,10 +206,7 @@ public double Determinant()
/// </returns>
public static BaseMatrix operator *(BaseMatrix first, BaseMatrix second)
{
if (first.Width != second.Height)
{
throw new ArithmeticException(MathResources.MatrixErrorColumns);
}
if (first.Width != second.Height) throw new ArithmeticException(MathResources.MatrixErrorColumns);

return MatrixUtility.UnsafeMultiplication(first, second);
}
Expand Down Expand Up @@ -269,15 +249,9 @@ public double Determinant()
/// </returns>
public static BaseMatrix operator +(BaseMatrix first, BaseMatrix second)
{
if (first.Width != second.Width)
{
throw new ArithmeticException();
}
if (first.Width != second.Width) throw new ArithmeticException();

if (first.Height != second.Height)
{
throw new ArithmeticException();
}
if (first.Height != second.Height) throw new ArithmeticException();

return MatrixUtility.UnsafeAddition(first, second);
}
Expand All @@ -292,15 +266,9 @@ public double Determinant()
/// </returns>
public static BaseMatrix operator -(BaseMatrix first, BaseMatrix second)
{
if (first.Width != second.Width)
{
throw new ArithmeticException();
}
if (first.Width != second.Width) throw new ArithmeticException();

if (first.Height != second.Height)
{
throw new ArithmeticException();
}
if (first.Height != second.Height) throw new ArithmeticException();

return MatrixUtility.UnsafeSubtraction(first, second);
}
Expand Down Expand Up @@ -376,10 +344,7 @@ public override int GetHashCode()
/// </returns>
public static explicit operator Vector3D(BaseMatrix first)
{
if (first.Height != 4 && first.Width != 4)
{
return null;
}
if (first.Height != 4 && first.Width != 4) return null;

var v = new Vector3D(first[0, 0], first[0, 1], first[0, 2]);
v.SetW(first[0, 3]);
Expand Down Expand Up @@ -409,4 +374,4 @@ public override string ToString()
return Matrix.ToText();
}
}
}
}
4 changes: 2 additions & 2 deletions Mathematics/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static class Constants
{ 105, MinusSixPlusTwo },
{ 120, -0.5 },
{ 135, -1 / Two },
{ 150, -Three / 2 },
//{ 150, -Three / 2 }, //TODO error here
{ 165, MinusSixMinusTwo },
{ 180, -1 },
{ 195, MinusSixMinusTwo },
Expand All @@ -101,4 +101,4 @@ public static class Constants
{ 360, 1 }
};
}
}
}
9 changes: 3 additions & 6 deletions Mathematics/Coordinate2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public Coordinate2D()
/// <value>
/// The null point.
/// </value>
public static Coordinate2D NullPoint
{
get;
} = new(0, 0);
public static Coordinate2D NullPoint { get; } = new(0, 0);

/// <summary>
/// Gets the identifier of the Coordinate in the 2D System.
Expand Down Expand Up @@ -205,7 +202,7 @@ public int CalculateId(int width)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int CalculateId(int x, int y, int width)
{
return (y * width) + x;
return y * width + x;
}

/// <summary>
Expand Down Expand Up @@ -255,4 +252,4 @@ public override string ToString()
return string.Concat(MathResources.StrX, X, MathResources.StrY, Y, MathResources.StrId, Id);
}
}
}
}
9 changes: 3 additions & 6 deletions Mathematics/Dispersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public sealed class Dispersion
/// <param name="row">The row.</param>
public Dispersion(List<double> row)
{
if (row == null || row.Count == 0)
{
throw new ArgumentException(MathResources.StatisticsErrorInput);
}
if (row == null || row.Count == 0) throw new ArgumentException(MathResources.StatisticsErrorInput);

Row = row;
CalculateStatistics();
Expand Down Expand Up @@ -179,7 +176,7 @@ private void CalculateMedian()
{
var sortedList = Row.OrderBy(x => x).ToList();
var n = sortedList.Count;
Median = n % 2 == 0 ? (sortedList[(n / 2) - 1] + sortedList[n / 2]) / 2.0 : sortedList[n / 2];
Median = n % 2 == 0 ? (sortedList[n / 2 - 1] + sortedList[n / 2]) / 2.0 : sortedList[n / 2];
}

/// <summary>
Expand All @@ -192,4 +189,4 @@ private void CalculateMode()
Mode = groups.First(g => g.Count() == maxCount).Key;
}
}
}
}
32 changes: 11 additions & 21 deletions Mathematics/ExtendedMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,18 @@ public static class ExtendedMath
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double CalcCos(int degree)
{
double cos;
// Use the absolute value of the degree for cosine lookup.
var lookupDegree = Math.Abs(degree);

if (Constants.CoSinus.ContainsKey(Math.Abs(degree)))
// Look up the cosine value in the dictionary for the positive angle.
if (Constants.CoSinus.ContainsKey(lookupDegree))
{
cos = Constants.CoSinus[Math.Abs(degree)];

//catch negative degrees
if (degree < 0)
{
cos *= -1;
}
}
else
{
const double rad = Math.PI / 180.0;
cos = Math.Cos(degree * rad);
return Constants.CoSinus[lookupDegree];
}

return cos;
// If the value is not found, calculate it normally.
const double rad = Math.PI / 180.0;
return Math.Cos(degree * rad);
}

/// <summary>
Expand All @@ -60,15 +53,12 @@ public static double CalcSin(int degree)
{
double sin;

if (Constants.Sinus.ContainsKey(Math.Abs(degree)))
if (Constants.Sinus.ContainsKey(degree))
{
sin = Constants.Sinus[Math.Abs(degree)];

//catch negative degrees
if (degree < 0)
{
sin *= -1;
}
if (degree < 0) sin *= -1;
}
else
{
Expand All @@ -79,4 +69,4 @@ public static double CalcSin(int degree)
return sin;
}
}
}
}
Loading

0 comments on commit 630c1b4

Please sign in to comment.