refactor: remove unnecessary class static access
This commit is contained in:
@@ -489,7 +489,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
/// <returns></returns>
|
||||
public virtual TScalar Trace()
|
||||
{
|
||||
TScalar res = StaticAccess<TScalar>.AdditionUnit;
|
||||
TScalar res = Structure.AdditionUnit;
|
||||
for (int i = 1; i <= Dim; i++)
|
||||
res = Structure.Addition(res, this[i, i]);
|
||||
return res;
|
||||
@@ -932,9 +932,10 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
for (int r = 1; r <= Dim; r++)
|
||||
{
|
||||
int p = 1;
|
||||
while (p < Dim && Structure.IsEqualApprox(res[r, p], StaticAccess<TScalar>.AdditionUnit))
|
||||
|
||||
while (p < Dim && Structure.IsEqualApprox(res[r, p], Structure.AdditionUnit))
|
||||
p += 1;
|
||||
if (!Structure.IsEqualApprox(res[r, p], StaticAccess<TScalar>.AdditionUnit))
|
||||
if (!Structure.IsEqualApprox(res[r, p], Structure.AdditionUnit))
|
||||
{
|
||||
pivotCol.Add(p);
|
||||
conj[r] = conj[r] / res[r, p];
|
||||
@@ -1090,7 +1091,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
void ScaleToUnit(int i, int j)
|
||||
{
|
||||
TScalar scale = u[i, j];
|
||||
if (Structure.IsEqualApprox(scale, StaticAccess<TScalar>.AdditionUnit))
|
||||
if (Structure.IsEqualApprox(scale, Structure.AdditionUnit))
|
||||
return;
|
||||
u[i] /= scale;
|
||||
l[i, false] *= scale;
|
||||
@@ -1101,7 +1102,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
{
|
||||
bool pivotNotFound = Structure.IsEqualApprox(
|
||||
u[i, i],
|
||||
StaticAccess<TScalar>.AdditionUnit
|
||||
Structure.AdditionUnit
|
||||
);
|
||||
for (int j = i; j <= Dim; j++)
|
||||
{
|
||||
@@ -1110,7 +1111,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
pivotNotFound &&
|
||||
!Structure.IsEqualApprox(
|
||||
u[j, i],
|
||||
StaticAccess<TScalar>.AdditionUnit
|
||||
Structure.AdditionUnit
|
||||
)
|
||||
)
|
||||
{
|
||||
@@ -1120,10 +1121,10 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
}
|
||||
|
||||
if (pivotNotFound)
|
||||
det = StaticAccess<TScalar>.AdditionUnit;
|
||||
det = Structure.AdditionUnit;
|
||||
for (int j = i + 1; j <= Dim; j++)
|
||||
{
|
||||
if (Structure.IsEqualApprox(u[j, i], StaticAccess<TScalar>.AdditionUnit))
|
||||
if (Structure.IsEqualApprox(u[j, i], Structure.AdditionUnit))
|
||||
continue;
|
||||
u[j] -= u[i];
|
||||
l[i, false] += l[j, false];
|
||||
@@ -1149,7 +1150,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
TVector ei = id[i];
|
||||
TVector x = qia[i, false].UpTruncateBy(i - 1);
|
||||
TScalar alpha =
|
||||
Structure.IsEqualApprox(Structure.Abs(x[i]), StaticAccess<TScalar>.AdditionUnit)
|
||||
Structure.IsEqualApprox(Structure.Abs(x[i]), Structure.AdditionUnit)
|
||||
? Structure.MultiplicationUnit
|
||||
: Structure.Division(x[i], Structure.Abs(x[i]));
|
||||
alpha = Structure.RealMul(alpha, x.Magnitude);
|
||||
@@ -1157,7 +1158,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
TVector vi = ui.Normalize;
|
||||
TScalar dm = vi.Conj() * x;
|
||||
TScalar omg = Structure.Division(x.Conj() * vi, dm);
|
||||
if (Structure.IsEqualApprox(dm, StaticAccess<TScalar>.AdditionUnit))
|
||||
if (Structure.IsEqualApprox(dm, Structure.AdditionUnit))
|
||||
omg = Structure.MultiplicationUnit;
|
||||
TMatrix qi =
|
||||
id - Structure.Addition(
|
||||
@@ -1207,7 +1208,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
TMatrix res = Copy();
|
||||
for (int i = 1; i <= d; i++)
|
||||
for (int j = 1; j <= d; j++)
|
||||
res[i, j] = StaticAccess<TScalar>.AdditionUnit;
|
||||
res[i, j] = Structure.AdditionUnit;
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -1223,7 +1224,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
int dim = Dim;
|
||||
for (int i = d; i <= dim; i++)
|
||||
for (int j = d; j <= dim; j++)
|
||||
res[i, j] = StaticAccess<TScalar>.AdditionUnit;
|
||||
res[i, j] = Structure.AdditionUnit;
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -1238,7 +1239,7 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
|
||||
for (int r = i; r >= 1; r--)
|
||||
{
|
||||
if (Structure.IsEqualApprox(this[i, i], this[r, r]))
|
||||
col[r] = StaticAccess<TScalar>.MultiplicationUnit;
|
||||
col[r] = Structure.MultiplicationUnit;
|
||||
else
|
||||
col[r] = Structure.Division(
|
||||
Structure.AdditionInverse(col * this[r]),
|
||||
|
||||
@@ -22,13 +22,13 @@ public class MatrixProperty<TScalar, TVector, TMatrix>
|
||||
/// M[i, j] = 0 if i!= j
|
||||
/// </summary>
|
||||
public bool IsDiagonal => Mat.OffDiagonals
|
||||
.All(x => Structure.IsEqualApprox(x, StaticAccess<TScalar>.AdditionUnit));
|
||||
.All(x => Structure.IsEqualApprox(x, Structure.AdditionUnit));
|
||||
|
||||
/// <summary>
|
||||
/// exist of inv(M)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool IsInvertible => !Structure.IsEqualApprox(Mat.Det(), StaticAccess<TScalar>.AdditionUnit);
|
||||
public bool IsInvertible => !Structure.IsEqualApprox(Mat.Det(), Structure.AdditionUnit);
|
||||
|
||||
/// <summary>
|
||||
/// M M* = M* M
|
||||
@@ -49,7 +49,7 @@ public class MatrixProperty<TScalar, TVector, TMatrix>
|
||||
{
|
||||
for (int i = 2; i <= Mat.Dim; i++)
|
||||
for (int j = 1; j < i; j++)
|
||||
if (!Structure.IsEqualApprox(Mat[i, j], StaticAccess<TScalar>.AdditionUnit))
|
||||
if (!Structure.IsEqualApprox(Mat[i, j], Structure.AdditionUnit))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public class MatrixProperty<TScalar, TVector, TMatrix>
|
||||
{
|
||||
for (int i = 1; i <= Mat.Dim; i++)
|
||||
for (int j = i + 1; j <= Mat.Dim; j++)
|
||||
if (!Structure.IsEqualApprox(Mat[i, j], StaticAccess<TScalar>.AdditionUnit))
|
||||
if (!Structure.IsEqualApprox(Mat[i, j], Structure.AdditionUnit))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ public class MatrixProperty<TScalar, TVector, TMatrix>
|
||||
/// <summary>
|
||||
/// tr(M) = 0
|
||||
/// </summary>
|
||||
public bool IsTraceless => Structure.IsEqualApprox(Mat.Trace(), StaticAccess<TScalar>.AdditionUnit);
|
||||
public bool IsTraceless => Structure.IsEqualApprox(Mat.Trace(), Structure.AdditionUnit);
|
||||
|
||||
/// <summary>
|
||||
/// M M^T = I
|
||||
|
||||
@@ -41,8 +41,8 @@ public partial class CategoryOf<TDimension>
|
||||
{
|
||||
for (int j = i + 1; j <= Dim; j++)
|
||||
{
|
||||
this[i, j] = StaticAccess<TScalar>.AdditionUnit;
|
||||
this[j, i] = StaticAccess<TScalar>.AdditionUnit;
|
||||
this[i, j] = Structure.AdditionUnit;
|
||||
this[j, i] = Structure.AdditionUnit;
|
||||
}
|
||||
}
|
||||
base.Fix();
|
||||
|
||||
@@ -80,8 +80,8 @@ public partial class CategoryOf<TDimension>
|
||||
{
|
||||
for (int j = 1 + 1; j <= FDim; j++)
|
||||
{
|
||||
this[i, j] = StaticAccess<TScalar>.AdditionUnit;
|
||||
this[j, i] = StaticAccess<TScalar>.AdditionUnit;
|
||||
this[i, j] = Structure.AdditionUnit;
|
||||
this[j, i] = Structure.AdditionUnit;
|
||||
}
|
||||
}
|
||||
InternalFix();
|
||||
|
||||
@@ -26,16 +26,9 @@ public abstract class FieldStructure<TScalar> : FieldStructure
|
||||
return (RealFieldStructure.Structure as FieldStructure<TScalar>)!;
|
||||
if (typeof(TScalar) == typeof(Complex))
|
||||
return (ComplexFieldStructure.Structure as FieldStructure<TScalar>)!;
|
||||
return ScalarFieldStructureProvider.GetOperation<TScalar>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public FieldStructure()
|
||||
{
|
||||
StaticAccess<TScalar>.AdditionUnit = AdditionUnit;
|
||||
StaticAccess<TScalar>.MultiplicationUnit = MultiplicationUnit;
|
||||
StaticAccess<TScalar>.Structure = this;
|
||||
return FieldStructureRegistry.GetOperation<TScalar>();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// addition in field
|
||||
|
||||
@@ -3,17 +3,24 @@ namespace Skeleton.Algebra.ScalarFieldStructure;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class ScalarFieldStructureProvider
|
||||
public static class FieldStructureRegistry
|
||||
{
|
||||
/// <summary>
|
||||
/// Register customer field structure
|
||||
/// </summary>
|
||||
/// <param name="structure"></param>
|
||||
/// <typeparam name="TScalar"></typeparam>
|
||||
public static void Register<TScalar>(FieldStructure<TScalar> structure)
|
||||
=> Cache[typeof(TScalar)] = structure;
|
||||
public static void Register<TScalar>(FieldStructure<TScalar> structure)
|
||||
=> FieldStructureRegistry<TScalar>.Structure = structure;
|
||||
|
||||
internal static FieldStructure<TScalar> GetOperation<TScalar>()
|
||||
=> FieldStructureRegistry<TScalar>.Structure;
|
||||
|
||||
}
|
||||
|
||||
internal static class FieldStructureRegistry<TScalar>
|
||||
{
|
||||
internal static FieldStructure<TScalar> Structure { get; set; }
|
||||
}
|
||||
|
||||
|
||||
internal static FieldStructure<TScalar> GetOperation<TScalar>()
|
||||
=> (Cache[typeof(TScalar)] as FieldStructure<TScalar>)!;
|
||||
private static readonly Dictionary<Type, FieldStructure> Cache = new ();
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
namespace Skeleton.Algebra.ScalarFieldStructure;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static class StaticAccess<T>
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static T AdditionUnit { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static T MultiplicationUnit { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static FieldStructure<T> Structure { get; set; }
|
||||
|
||||
}
|
||||
@@ -115,7 +115,7 @@ public static partial class CategoryOf<TDimension>
|
||||
|
||||
protected override TScalar Dot(FVector other)
|
||||
{
|
||||
TScalar res = StaticAccess<TScalar>.AdditionUnit;
|
||||
TScalar res = Structure.AdditionUnit;
|
||||
for (int i = 1; i <= FDim; i++)
|
||||
res = FieldStructure.Addition(res, FieldStructure.Multiplication(this[i], other[i]));
|
||||
return res;
|
||||
|
||||
@@ -197,7 +197,7 @@ public abstract class Vector<TScalar, TVector> : Vector<TVector>, IEnumerable<TS
|
||||
{
|
||||
Elements = new TScalar[Dim];
|
||||
for (int i = 1; i <= Dim; i++)
|
||||
Elements[i] = StaticAccess<TScalar>.AdditionUnit;
|
||||
Elements[i] = Structure.AdditionUnit;
|
||||
}
|
||||
|
||||
|
||||
@@ -272,15 +272,15 @@ public abstract class Vector<TScalar, TVector> : Vector<TVector>, IEnumerable<TS
|
||||
public bool IsLinearDependantWith(TVector v)
|
||||
{
|
||||
bool started = false;
|
||||
TScalar comDiv = StaticAccess<TScalar>.AdditionUnit;
|
||||
TScalar comDiv = Structure.AdditionUnit;
|
||||
for (int i = 1; i <= v.Dim; i++)
|
||||
{
|
||||
if (
|
||||
Structure.IsEqualApprox(v[i], StaticAccess<TScalar>.AdditionUnit) &&
|
||||
Structure.IsEqualApprox(this[i], StaticAccess<TScalar>.AdditionUnit)
|
||||
Structure.IsEqualApprox(v[i], Structure.AdditionUnit) &&
|
||||
Structure.IsEqualApprox(this[i], Structure.AdditionUnit)
|
||||
)
|
||||
continue;
|
||||
if (Structure.IsEqualApprox(v[i], StaticAccess<TScalar>.AdditionUnit))
|
||||
if (Structure.IsEqualApprox(v[i], Structure.AdditionUnit))
|
||||
return false;
|
||||
TScalar ratio = Structure.Division(this[i], v[i]);
|
||||
if (!started)
|
||||
@@ -345,7 +345,7 @@ public abstract class Vector<TScalar, TVector> : Vector<TVector>, IEnumerable<TS
|
||||
get
|
||||
{
|
||||
for (int i = 0; i <= Dim; i++)
|
||||
if (!Structure.IsEqualApprox(Elements[^(i + 1)], StaticAccess<TScalar>.AdditionUnit))
|
||||
if (!Structure.IsEqualApprox(Elements[^(i + 1)], Structure.AdditionUnit))
|
||||
return i;
|
||||
return Dim;
|
||||
}
|
||||
@@ -356,7 +356,7 @@ public abstract class Vector<TScalar, TVector> : Vector<TVector>, IEnumerable<TS
|
||||
get
|
||||
{
|
||||
for(int i = 0;i<Dim; i++)
|
||||
if (!Structure.IsEqualApprox(Elements[i], StaticAccess<TScalar>.AdditionUnit))
|
||||
if (!Structure.IsEqualApprox(Elements[i], Structure.AdditionUnit))
|
||||
return i;
|
||||
return Dim;
|
||||
}
|
||||
@@ -462,7 +462,7 @@ public abstract class Vector<TScalar, TVector> : Vector<TVector>, IEnumerable<TS
|
||||
get
|
||||
{
|
||||
TVector res = new();
|
||||
res.Elements = Enumerable.Range(0, Dim).Select(_ => StaticAccess<TScalar>.AdditionUnit).ToArray();
|
||||
res.Elements = Enumerable.Range(0, Dim).Select(_ => Structure.AdditionUnit).ToArray();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -478,7 +478,7 @@ public abstract class Vector<TScalar, TVector> : Vector<TVector>, IEnumerable<TS
|
||||
{
|
||||
TVector res = Copy();
|
||||
for (int i = 1; i <= d; i++)
|
||||
res[i] = StaticAccess<TScalar>.AdditionUnit;
|
||||
res[i] = Structure.AdditionUnit;
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -492,7 +492,7 @@ public abstract class Vector<TScalar, TVector> : Vector<TVector>, IEnumerable<TS
|
||||
{
|
||||
TVector res = Copy();
|
||||
for (int i = d; i < Dim; i++)
|
||||
res[i] = StaticAccess<TScalar>.AdditionUnit;
|
||||
res[i] = Structure.AdditionUnit;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user