Skip to content

Commit e529239

Browse files
committed
Added more time series models and started creating activation function implementations instead of having them all found in neural network helper class
1 parent c01f57d commit e529239

File tree

87 files changed

+2204
-257
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2204
-257
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace AiDotNet.ActivationFunctions;
2+
3+
public abstract class ActivationFunctionBase<T> : IActivationFunction<T>, IVectorActivationFunction<T>
4+
{
5+
protected static readonly INumericOperations<T> NumOps = MathHelper.GetNumericOperations<T>();
6+
7+
public virtual T Activate(T input)
8+
{
9+
return input; // Default to identity function
10+
}
11+
12+
public virtual T Derivative(T input)
13+
{
14+
return NumOps.One; // Default to constant derivative of 1
15+
}
16+
17+
public virtual Vector<T> Activate(Vector<T> input)
18+
{
19+
return input.Transform(Activate);
20+
}
21+
22+
public virtual Matrix<T> Derivative(Vector<T> input)
23+
{
24+
return Matrix<T>.CreateDiagonal(input.Transform(Derivative));
25+
}
26+
27+
protected abstract bool SupportsScalarOperations();
28+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace AiDotNet.ActivationFunctions;
2+
3+
public class ReLUActivation<T> : ActivationFunctionBase<T>
4+
{
5+
protected override bool SupportsScalarOperations() => true;
6+
7+
public override T Activate(T input)
8+
{
9+
return NumOps.GreaterThan(input, NumOps.Zero) ? input : NumOps.Zero;
10+
}
11+
12+
public override T Derivative(T input)
13+
{
14+
return NumOps.GreaterThan(input, NumOps.Zero) ? NumOps.One : NumOps.Zero;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace AiDotNet.ActivationFunctions;
2+
3+
public class SoftmaxActivation<T> : ActivationFunctionBase<T>
4+
{
5+
public override Vector<T> Activate(Vector<T> input)
6+
{
7+
Vector<T> expValues = input.Transform(NumOps.Exp);
8+
T sum = expValues.Sum();
9+
10+
return expValues.Transform(x => NumOps.Divide(x, sum));
11+
}
12+
13+
public override Matrix<T> Derivative(Vector<T> input)
14+
{
15+
Vector<T> softmaxOutput = Activate(input);
16+
int size = softmaxOutput.Length;
17+
Matrix<T> jacobian = new Matrix<T>(size, size);
18+
19+
for (int i = 0; i < size; i++)
20+
{
21+
for (int j = 0; j < size; j++)
22+
{
23+
if (i == j)
24+
{
25+
jacobian[i, j] = NumOps.Multiply(softmaxOutput[i], NumOps.Subtract(NumOps.One, softmaxOutput[i]));
26+
}
27+
else
28+
{
29+
jacobian[i, j] = NumOps.Multiply(NumOps.Negate(softmaxOutput[i]), softmaxOutput[j]);
30+
}
31+
}
32+
}
33+
34+
return jacobian;
35+
}
36+
37+
protected override bool SupportsScalarOperations() => false;
38+
}

src/DecompositionMethods/MatrixDecomposition/BidiagonalDecomposition.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using AiDotNet.Enums.AlgorithmTypes;
2+
13
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
24

35
public class BidiagonalDecomposition<T> : IMatrixDecomposition<T>
@@ -9,7 +11,7 @@ public class BidiagonalDecomposition<T> : IMatrixDecomposition<T>
911
public Matrix<T> B { get; private set; }
1012
public Matrix<T> V { get; private set; }
1113

12-
public BidiagonalDecomposition(Matrix<T> matrix, BidiagonalAlgorithm algorithm = BidiagonalAlgorithm.Householder)
14+
public BidiagonalDecomposition(Matrix<T> matrix, BidiagonalAlgorithmType algorithm = BidiagonalAlgorithmType.Householder)
1315
{
1416
A = matrix;
1517
NumOps = MathHelper.GetNumericOperations<T>();
@@ -19,17 +21,17 @@ public BidiagonalDecomposition(Matrix<T> matrix, BidiagonalAlgorithm algorithm =
1921
Decompose(algorithm);
2022
}
2123

22-
public void Decompose(BidiagonalAlgorithm algorithm = BidiagonalAlgorithm.Householder)
24+
public void Decompose(BidiagonalAlgorithmType algorithm = BidiagonalAlgorithmType.Householder)
2325
{
2426
switch (algorithm)
2527
{
26-
case BidiagonalAlgorithm.Householder:
28+
case BidiagonalAlgorithmType.Householder:
2729
DecomposeHouseholder();
2830
break;
29-
case BidiagonalAlgorithm.Givens:
31+
case BidiagonalAlgorithmType.Givens:
3032
DecomposeGivens();
3133
break;
32-
case BidiagonalAlgorithm.Lanczos:
34+
case BidiagonalAlgorithmType.Lanczos:
3335
DecomposeLanczos();
3436
break;
3537
default:

src/DecompositionMethods/MatrixDecomposition/CholeskyDecomposition.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
1+
using AiDotNet.Enums.AlgorithmTypes;
2+
3+
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
24

35
public class CholeskyDecomposition<T> : IMatrixDecomposition<T>
46
{
@@ -7,7 +9,7 @@ public class CholeskyDecomposition<T> : IMatrixDecomposition<T>
79
public Matrix<T> L { get; private set; }
810
public Matrix<T> A { get; private set; }
911

10-
public CholeskyDecomposition(Matrix<T> matrix, CholeskyAlgorithm algorithm = CholeskyAlgorithm.Crout)
12+
public CholeskyDecomposition(Matrix<T> matrix, CholeskyAlgorithmType algorithm = CholeskyAlgorithmType.Crout)
1113
{
1214
A = matrix;
1315
NumOps = MathHelper.GetNumericOperations<T>();
@@ -20,14 +22,14 @@ public Vector<T> Solve(Vector<T> b)
2022
return BackSubstitution(L.Transpose(), y);
2123
}
2224

23-
private Matrix<T> Decompose(Matrix<T> matrix, CholeskyAlgorithm algorithm)
25+
private Matrix<T> Decompose(Matrix<T> matrix, CholeskyAlgorithmType algorithm)
2426
{
2527
return algorithm switch
2628
{
27-
CholeskyAlgorithm.Crout => ComputeCholeskyCrout(matrix),
28-
CholeskyAlgorithm.Banachiewicz => ComputeCholeskyBanachiewicz(matrix),
29-
CholeskyAlgorithm.LDL => ComputeCholeskyLDL(matrix),
30-
CholeskyAlgorithm.BlockCholesky => ComputeBlockCholesky(matrix),
29+
CholeskyAlgorithmType.Crout => ComputeCholeskyCrout(matrix),
30+
CholeskyAlgorithmType.Banachiewicz => ComputeCholeskyBanachiewicz(matrix),
31+
CholeskyAlgorithmType.LDL => ComputeCholeskyLDL(matrix),
32+
CholeskyAlgorithmType.BlockCholesky => ComputeBlockCholesky(matrix),
3133
_ => throw new ArgumentException("Unsupported Cholesky decomposition algorithm.")
3234
};
3335
}

src/DecompositionMethods/MatrixDecomposition/EigenDecomposition.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
1+
using AiDotNet.Enums.AlgorithmTypes;
2+
3+
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
24

35
public class EigenDecomposition<T> : IMatrixDecomposition<T>
46
{
@@ -9,20 +11,20 @@ public class EigenDecomposition<T> : IMatrixDecomposition<T>
911

1012
public Matrix<T> A { get; private set; }
1113

12-
public EigenDecomposition(Matrix<T> matrix, EigenAlgorithm algorithm = EigenAlgorithm.QR)
14+
public EigenDecomposition(Matrix<T> matrix, EigenAlgorithmType algorithm = EigenAlgorithmType.QR)
1315
{
1416
NumOps = MathHelper.GetNumericOperations<T>();
1517
A = matrix;
1618
(EigenValues, EigenVectors) = Decompose(matrix, algorithm);
1719
}
1820

19-
private (Vector<T> eigenValues, Matrix<T> eigenVectors) Decompose(Matrix<T> matrix, EigenAlgorithm algorithm)
21+
private (Vector<T> eigenValues, Matrix<T> eigenVectors) Decompose(Matrix<T> matrix, EigenAlgorithmType algorithm)
2022
{
2123
return algorithm switch
2224
{
23-
EigenAlgorithm.QR => ComputeEigenQR(matrix),
24-
EigenAlgorithm.PowerIteration => ComputeEigenPowerIteration(matrix),
25-
EigenAlgorithm.Jacobi => ComputeEigenJacobi(matrix),
25+
EigenAlgorithmType.QR => ComputeEigenQR(matrix),
26+
EigenAlgorithmType.PowerIteration => ComputeEigenPowerIteration(matrix),
27+
EigenAlgorithmType.Jacobi => ComputeEigenJacobi(matrix),
2628
_ => throw new ArgumentException("Unsupported eigenvalue decomposition algorithm.")
2729
};
2830
}

src/DecompositionMethods/MatrixDecomposition/GramSchmidtDecomposition.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ public class GramSchmidtDecomposition<T> : IMatrixDecomposition<T>
88
public Matrix<T> R { get; private set; }
99
public Matrix<T> A { get; private set; }
1010

11-
public GramSchmidtDecomposition(Matrix<T> matrix, GramSchmidtAlgorithm algorithm = GramSchmidtAlgorithm.Classical)
11+
public GramSchmidtDecomposition(Matrix<T> matrix, GramSchmidtAlgorithmType algorithm = GramSchmidtAlgorithmType.Classical)
1212
{
1313
NumOps = MathHelper.GetNumericOperations<T>();
1414
A = matrix;
1515
(Q, R) = Decompose(matrix, algorithm);
1616
}
1717

18-
private (Matrix<T> Q, Matrix<T> R) Decompose(Matrix<T> matrix, GramSchmidtAlgorithm algorithm)
18+
private (Matrix<T> Q, Matrix<T> R) Decompose(Matrix<T> matrix, GramSchmidtAlgorithmType algorithm)
1919
{
2020
return algorithm switch
2121
{
22-
GramSchmidtAlgorithm.Classical => ComputeClassicalGramSchmidt(matrix),
23-
GramSchmidtAlgorithm.Modified => ComputeModifiedGramSchmidt(matrix),
22+
GramSchmidtAlgorithmType.Classical => ComputeClassicalGramSchmidt(matrix),
23+
GramSchmidtAlgorithmType.Modified => ComputeModifiedGramSchmidt(matrix),
2424
_ => throw new ArgumentException("Unsupported Gram-Schmidt algorithm.")
2525
};
2626
}

src/DecompositionMethods/MatrixDecomposition/HessenbergDecomposition.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ public class HessenbergDecomposition<T> : IMatrixDecomposition<T>
77
public Matrix<T> HessenbergMatrix { get; private set; }
88
public Matrix<T> A { get; private set; }
99

10-
public HessenbergDecomposition(Matrix<T> matrix, HessenbergAlgorithm algorithm = HessenbergAlgorithm.Householder)
10+
public HessenbergDecomposition(Matrix<T> matrix, HessenbergAlgorithmType algorithm = HessenbergAlgorithmType.Householder)
1111
{
1212
NumOps = MathHelper.GetNumericOperations<T>();
1313
A = matrix;
1414
HessenbergMatrix = Decompose(matrix, algorithm);
1515
}
1616

17-
private Matrix<T> Decompose(Matrix<T> matrix, HessenbergAlgorithm algorithm)
17+
private Matrix<T> Decompose(Matrix<T> matrix, HessenbergAlgorithmType algorithm)
1818
{
1919
return algorithm switch
2020
{
21-
HessenbergAlgorithm.Householder => ComputeHessenbergHouseholder(matrix),
22-
HessenbergAlgorithm.Givens => ComputeHessenbergGivens(matrix),
23-
HessenbergAlgorithm.ElementaryTransformations => ComputeHessenbergElementaryTransformations(matrix),
24-
HessenbergAlgorithm.ImplicitQR => ComputeHessenbergImplicitQR(matrix),
25-
HessenbergAlgorithm.Lanczos => ComputeHessenbergLanczos(matrix),
21+
HessenbergAlgorithmType.Householder => ComputeHessenbergHouseholder(matrix),
22+
HessenbergAlgorithmType.Givens => ComputeHessenbergGivens(matrix),
23+
HessenbergAlgorithmType.ElementaryTransformations => ComputeHessenbergElementaryTransformations(matrix),
24+
HessenbergAlgorithmType.ImplicitQR => ComputeHessenbergImplicitQR(matrix),
25+
HessenbergAlgorithmType.Lanczos => ComputeHessenbergLanczos(matrix),
2626
_ => throw new ArgumentException("Unsupported Hessenberg decomposition algorithm.")
2727
};
2828
}

src/DecompositionMethods/MatrixDecomposition/LdlDecomposition.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using AiDotNet.Enums.AlgorithmTypes;
2+
13
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
24

35
public class LdlDecomposition<T> : IMatrixDecomposition<T>
@@ -8,7 +10,7 @@ public class LdlDecomposition<T> : IMatrixDecomposition<T>
810
public Matrix<T> L { get; private set; }
911
public Vector<T> D { get; private set; }
1012

11-
public LdlDecomposition(Matrix<T> matrix, LdlAlgorithm algorithm = LdlAlgorithm.Cholesky)
13+
public LdlDecomposition(Matrix<T> matrix, LdlAlgorithmType algorithm = LdlAlgorithmType.Cholesky)
1214
{
1315
if (!matrix.IsSquareMatrix())
1416
throw new ArgumentException("Matrix must be square for LDL decomposition.");
@@ -21,14 +23,14 @@ public LdlDecomposition(Matrix<T> matrix, LdlAlgorithm algorithm = LdlAlgorithm.
2123
Decompose(algorithm);
2224
}
2325

24-
public void Decompose(LdlAlgorithm algorithm = LdlAlgorithm.Cholesky)
26+
public void Decompose(LdlAlgorithmType algorithm = LdlAlgorithmType.Cholesky)
2527
{
2628
switch (algorithm)
2729
{
28-
case LdlAlgorithm.Cholesky:
30+
case LdlAlgorithmType.Cholesky:
2931
DecomposeCholesky();
3032
break;
31-
case LdlAlgorithm.Crout:
33+
case LdlAlgorithmType.Crout:
3234
DecomposeCrout();
3335
break;
3436
default:

src/DecompositionMethods/MatrixDecomposition/LqDecomposition.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
1+
using AiDotNet.Enums.AlgorithmTypes;
2+
3+
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
24

35
public class LqDecomposition<T> : IMatrixDecomposition<T>
46
{
@@ -8,7 +10,7 @@ public class LqDecomposition<T> : IMatrixDecomposition<T>
810
public Matrix<T> Q { get; private set; }
911
public Matrix<T> A { get; private set; }
1012

11-
public LqDecomposition(Matrix<T> matrix, LqAlgorithm algorithm = LqAlgorithm.Householder)
13+
public LqDecomposition(Matrix<T> matrix, LqAlgorithmType algorithm = LqAlgorithmType.Householder)
1214
{
1315
NumOps = MathHelper.GetNumericOperations<T>();
1416
A = matrix;
@@ -21,13 +23,13 @@ public Vector<T> Solve(Vector<T> b)
2123
return Q.Transpose().Multiply(y);
2224
}
2325

24-
private (Matrix<T> L, Matrix<T> Q) Decompose(Matrix<T> matrix, LqAlgorithm algorithm)
26+
private (Matrix<T> L, Matrix<T> Q) Decompose(Matrix<T> matrix, LqAlgorithmType algorithm)
2527
{
2628
return algorithm switch
2729
{
28-
LqAlgorithm.Householder => ComputeLqHouseholder(matrix),
29-
LqAlgorithm.GramSchmidt => ComputeLqGramSchmidt(matrix),
30-
LqAlgorithm.Givens => ComputeLqGivens(matrix),
30+
LqAlgorithmType.Householder => ComputeLqHouseholder(matrix),
31+
LqAlgorithmType.GramSchmidt => ComputeLqGramSchmidt(matrix),
32+
LqAlgorithmType.Givens => ComputeLqGivens(matrix),
3133
_ => throw new ArgumentException("Unsupported LQ decomposition algorithm."),
3234
};
3335
}

src/DecompositionMethods/MatrixDecomposition/LuDecomposition.cs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
1+
using AiDotNet.Enums.AlgorithmTypes;
2+
3+
namespace AiDotNet.DecompositionMethods.MatrixDecomposition;
24

35
public class LuDecomposition<T> : IMatrixDecomposition<T>
46
{
@@ -9,7 +11,7 @@ public class LuDecomposition<T> : IMatrixDecomposition<T>
911

1012
private readonly INumericOperations<T> NumOps;
1113

12-
public LuDecomposition(Matrix<T> matrix, LuAlgorithm luAlgorithm = LuAlgorithm.PartialPivoting)
14+
public LuDecomposition(Matrix<T> matrix, LuAlgorithmType luAlgorithm = LuAlgorithmType.PartialPivoting)
1315
{
1416
A = matrix;
1517
NumOps = MathHelper.GetNumericOperations<T>();
@@ -24,15 +26,15 @@ public Vector<T> Solve(Vector<T> b)
2426
return BackSubstitution(U, y);
2527
}
2628

27-
private (Matrix<T> L, Matrix<T> U, Vector<int> P) Decompose(Matrix<T> matrix, LuAlgorithm algorithm)
29+
private (Matrix<T> L, Matrix<T> U, Vector<int> P) Decompose(Matrix<T> matrix, LuAlgorithmType algorithm)
2830
{
2931
return algorithm switch
3032
{
31-
LuAlgorithm.Doolittle => ComputeLuDoolittle(matrix),
32-
LuAlgorithm.Crout => ComputeLuCrout(matrix),
33-
LuAlgorithm.PartialPivoting => ComputeLuPartialPivoting(matrix),
34-
LuAlgorithm.CompletePivoting => ComputeLuCompletePivoting(matrix),
35-
LuAlgorithm.Cholesky => ComputeCholesky(matrix),
33+
LuAlgorithmType.Doolittle => ComputeLuDoolittle(matrix),
34+
LuAlgorithmType.Crout => ComputeLuCrout(matrix),
35+
LuAlgorithmType.PartialPivoting => ComputeLuPartialPivoting(matrix),
36+
LuAlgorithmType.CompletePivoting => ComputeLuCompletePivoting(matrix),
37+
LuAlgorithmType.Cholesky => ComputeCholesky(matrix),
3638
_ => throw new ArgumentException("Unsupported LU decomposition algorithm."),
3739
};
3840
}

0 commit comments

Comments
 (0)