diff --git a/src/Mathematics.NET/AutoDiff/Dual.cs b/src/Mathematics.NET/AutoDiff/Dual.cs index 7d532f74..c7595994 100644 --- a/src/Mathematics.NET/AutoDiff/Dual.cs +++ b/src/Mathematics.NET/AutoDiff/Dual.cs @@ -27,6 +27,9 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; +using Mathematics.NET.DifferentialGeometry; +using Mathematics.NET.DifferentialGeometry.Abstractions; +using Mathematics.NET.LinearAlgebra; namespace Mathematics.NET.AutoDiff; @@ -248,4 +251,32 @@ public static Dual Tan(Dual x) /// A dual number. public static Dual CustomOperation(Dual x, Dual y, Func f, Func dfx, Func dfy) => new(f(x._d0, y._d0), dfy(x._d0, y._d0) * x._d1 + dfx(x._d0, y._d1) * y._d1); + + // + // DifGeo + // + + public static AutoDiffTensor2, T, U> CreateAutoDiffTensor(in Vector2 x, in Vector2 seed) + where U : IIndex + => new(CreateVariable(x.X1, seed.X1), CreateVariable(x.X2, seed.X2)); + + public static AutoDiffTensor2, T, U> CreateAutoDiffTensor(in T x0, in T x1, in T seed0, in T seed1) + where U : IIndex + => new(CreateVariable(x0, seed0), CreateVariable(x1, seed1)); + + public static AutoDiffTensor3, T, U> CreateAutoDiffTensor(in Vector3 x, in Vector3 seed) + where U : IIndex + => new(CreateVariable(x.X1, seed.X1), CreateVariable(x.X2, seed.X2), CreateVariable(x.X3, seed.X3)); + + public static AutoDiffTensor3, T, U> CreateAutoDiffTensor(in T x0, in T x1, in T x2, in T seed0, in T seed1, in T seed2) + where U : IIndex + => new(CreateVariable(x0, seed0), CreateVariable(x1, seed1), CreateVariable(x2, seed2)); + + public static AutoDiffTensor4, T, U> CreateAutoDiffTensor(in Vector4 x, in Vector4 seed) + where U : IIndex + => new(CreateVariable(x.X1, seed.X1), CreateVariable(x.X2, seed.X2), CreateVariable(x.X3, seed.X3), CreateVariable(x.X4, seed.X4)); + + public static AutoDiffTensor4, T, U> CreateAutoDiffTensor(in T x0, in T x1, in T x2, in T x3, in T seed0, in T seed1, in T seed2, in T seed3) + where U : IIndex + => new(CreateVariable(x0, seed0), CreateVariable(x1, seed1), CreateVariable(x2, seed2), CreateVariable(x3, seed3)); } diff --git a/src/Mathematics.NET/AutoDiff/GradientTape.cs b/src/Mathematics.NET/AutoDiff/GradientTape.cs index 020a4286..99692d87 100644 --- a/src/Mathematics.NET/AutoDiff/GradientTape.cs +++ b/src/Mathematics.NET/AutoDiff/GradientTape.cs @@ -62,6 +62,9 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using Mathematics.NET.DifferentialGeometry; +using Mathematics.NET.DifferentialGeometry.Abstractions; +using Mathematics.NET.LinearAlgebra; using Microsoft.Extensions.Logging; namespace Mathematics.NET.AutoDiff; @@ -102,10 +105,10 @@ public GradientTape(int n, bool isTracking = true) // Methods // - public Variable CreateVariable(T seed) + public Variable CreateVariable(T value) { _nodes.Add(new(_variableCount)); - Variable variable = new(_variableCount++, seed); + Variable variable = new(_variableCount++, value); return variable; } @@ -665,4 +668,32 @@ public Variable CustomOperation(Variable x, Variable y, Func f } return new(_nodes.Count, f(x.Value, y.Value)); } + + // + // DifGeo + // + + public AutoDiffTensor2 CreateAutoDiffTensor(in Vector2 x) + where U : IIndex + => new(CreateVariable(x.X1), CreateVariable(x.X2)); + + public AutoDiffTensor2 CreateAutoDiffTensor(in T x0, in T x1) + where U : IIndex + => new(CreateVariable(x0), CreateVariable(x1)); + + public AutoDiffTensor3 CreateAutoDiffTensor(in Vector3 x) + where U : IIndex + => new(CreateVariable(x.X1), CreateVariable(x.X2), CreateVariable(x.X3)); + + public AutoDiffTensor3 CreateAutoDiffTensor(in T x0, in T x1, in T x2) + where U : IIndex + => new(CreateVariable(x0), CreateVariable(x1), CreateVariable(x2)); + + public AutoDiffTensor4 CreateAutoDiffTensor(in Vector4 x) + where U : IIndex + => new(CreateVariable(x.X1), CreateVariable(x.X2), CreateVariable(x.X3), CreateVariable(x.X4)); + + public AutoDiffTensor4 CreateAutoDiffTensor(in T x0, in T x1, in T x2, in T x3) + where U : IIndex + => new(CreateVariable(x0), CreateVariable(x1), CreateVariable(x2), CreateVariable(x3)); } diff --git a/src/Mathematics.NET/AutoDiff/HessianTape.cs b/src/Mathematics.NET/AutoDiff/HessianTape.cs index 9c3a6f09..478b7d67 100644 --- a/src/Mathematics.NET/AutoDiff/HessianTape.cs +++ b/src/Mathematics.NET/AutoDiff/HessianTape.cs @@ -29,6 +29,9 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using Mathematics.NET.DifferentialGeometry; +using Mathematics.NET.DifferentialGeometry.Abstractions; +using Mathematics.NET.LinearAlgebra; using Microsoft.Extensions.Logging; namespace Mathematics.NET.AutoDiff; @@ -69,10 +72,10 @@ public HessianTape(int n, bool isTracking = true) // Methods // - public Variable CreateVariable(T seed) + public Variable CreateVariable(T value) { _nodes.Add(new(_variableCount)); - Variable variable = new(_variableCount++, seed); + Variable variable = new(_variableCount++, value); return variable; } @@ -846,4 +849,32 @@ public Variable CustomOperation( } return new(_nodes.Count, f(x.Value, y.Value)); } + + // + // DifGeo + // + + public AutoDiffTensor2 CreateAutoDiffTensor(in Vector2 x) + where U : IIndex + => new(CreateVariable(x.X1), CreateVariable(x.X2)); + + public AutoDiffTensor2 CreateAutoDiffTensor(in T x0, in T x1) + where U : IIndex + => new(CreateVariable(x0), CreateVariable(x1)); + + public AutoDiffTensor3 CreateAutoDiffTensor(in Vector3 x) + where U : IIndex + => new(CreateVariable(x.X1), CreateVariable(x.X2), CreateVariable(x.X3)); + + public AutoDiffTensor3 CreateAutoDiffTensor(in T x0, in T x1, in T x2) + where U : IIndex + => new(CreateVariable(x0), CreateVariable(x1), CreateVariable(x2)); + + public AutoDiffTensor4 CreateAutoDiffTensor(in Vector4 x) + where U : IIndex + => new(CreateVariable(x.X1), CreateVariable(x.X2), CreateVariable(x.X3), CreateVariable(x.X4)); + + public AutoDiffTensor4 CreateAutoDiffTensor(in T x0, in T x1, in T x2, in T x3) + where U : IIndex + => new(CreateVariable(x0), CreateVariable(x1), CreateVariable(x2), CreateVariable(x3)); } diff --git a/src/Mathematics.NET/AutoDiff/HyperDual.cs b/src/Mathematics.NET/AutoDiff/HyperDual.cs index 6530a6ef..b0b887a5 100644 --- a/src/Mathematics.NET/AutoDiff/HyperDual.cs +++ b/src/Mathematics.NET/AutoDiff/HyperDual.cs @@ -27,6 +27,9 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; +using Mathematics.NET.DifferentialGeometry; +using Mathematics.NET.DifferentialGeometry.Abstractions; +using Mathematics.NET.LinearAlgebra; namespace Mathematics.NET.AutoDiff; @@ -275,4 +278,32 @@ public static HyperDual CustomOperation( Func, Dual, Dual> dfx, Func, Dual, Dual> dfy) => new(f(x._d0, y._d0), dfy(x._d0, y._d0) * x._d1 + dfx(x._d0, y._d1) * y._d1); + + // + // DifGeo + // + + public static AutoDiffTensor2, T, U> CreateAutoDiffTensor(in Vector2 x, in Vector2 seed) + where U : IIndex + => new(CreateVariable(x.X1, seed.X1), CreateVariable(x.X2, seed.X2)); + + public static AutoDiffTensor2, T, U> CreateAutoDiffTensor(in T x0, in T x1, in T seed0, in T seed1) + where U : IIndex + => new(CreateVariable(x0, seed0), CreateVariable(x1, seed1)); + + public static AutoDiffTensor3, T, U> CreateAutoDiffTensor(in Vector3 x, in Vector3 seed) + where U : IIndex + => new(CreateVariable(x.X1, seed.X1), CreateVariable(x.X2, seed.X2), CreateVariable(x.X3, seed.X3)); + + public static AutoDiffTensor3, T, U> CreateAutoDiffTensor(in T x0, in T x1, in T x2, in T seed0, in T seed1, in T seed2) + where U : IIndex + => new(CreateVariable(x0, seed0), CreateVariable(x1, seed1), CreateVariable(x2, seed2)); + + public static AutoDiffTensor4, T, U> CreateAutoDiffTensor(in Vector4 x, in Vector4 seed) + where U : IIndex + => new(CreateVariable(x.X1, seed.X1), CreateVariable(x.X2, seed.X2), CreateVariable(x.X3, seed.X3), CreateVariable(x.X4, seed.X4)); + + public static AutoDiffTensor4, T, U> CreateAutoDiffTensor(in T x0, in T x1, in T x2, in T x3, in T seed0, in T seed1, in T seed2, in T seed3) + where U : IIndex + => new(CreateVariable(x0, seed0), CreateVariable(x1, seed1), CreateVariable(x2, seed2), CreateVariable(x3, seed3)); } diff --git a/src/Mathematics.NET/AutoDiff/IDual.cs b/src/Mathematics.NET/AutoDiff/IDual.cs index d93151cb..b6605f5a 100644 --- a/src/Mathematics.NET/AutoDiff/IDual.cs +++ b/src/Mathematics.NET/AutoDiff/IDual.cs @@ -27,51 +27,118 @@ using Mathematics.NET.Core.Operations; using Mathematics.NET.Core.Relations; +using Mathematics.NET.DifferentialGeometry; +using Mathematics.NET.DifferentialGeometry.Abstractions; +using Mathematics.NET.LinearAlgebra; namespace Mathematics.NET.AutoDiff; /// Defines support for dual numbers. -/// The type that implements the interface. -/// A type that implements and . -public interface IDual - : IAdditionOperation, - IDivisionOperation, - IMultiplicationOperation, - IUnaryMinusOperation, - IUnaryPlusOperation, - ISubtractionOperation, +/// The type that implements the interface. +/// A type that implements and . +public interface IDual + : IAdditionOperation, + IDivisionOperation, + IMultiplicationOperation, + IUnaryMinusOperation, + IUnaryPlusOperation, + ISubtractionOperation, IFormattable, - IEqualityRelation, - IEquatable, - IDifferentiableFunctions - where T : IDual - where U : IComplex, IDifferentiableFunctions + IEqualityRelation, + IEquatable, + IDifferentiableFunctions + where TDN : IDual + where TN : IComplex, IDifferentiableFunctions { /// Represents the primal part of the dual number. - U D0 { get; } + TN D0 { get; } /// Represents the tangent part of the dual number. - U D1 { get; } + TN D1 { get; } /// Create an instance of the type with a specified value. /// A value. /// An instance of the type. - static abstract T CreateVariable(U value); + static abstract TDN CreateVariable(TN value); /// Create an instance of the type with a specified value and seed. /// A value. /// A seed. /// An instance of the type. - static abstract T CreateVariable(U value, U seed); + static abstract TDN CreateVariable(TN value, TN seed); /// - static abstract T Pow(T x, U y); + static abstract TDN Pow(TDN x, TN y); /// - static abstract T Pow(U x, T y); + static abstract TDN Pow(TN x, TDN y); /// Create a seeded instance of this type. /// The seed value. /// A seeded value. - T WithSeed(U seed); + TDN WithSeed(TN seed); + + // + // DifGeo + // + + /// Create an autodiff, rank-one tensor from vectors of initial and seed values. + /// An index. + /// A vector of initial values. + /// A vector of seed values. + /// A rank-one tensor of two initial and seed values. + static abstract AutoDiffTensor2 CreateAutoDiffTensor(in Vector2 x, in Vector2 seed) + where TI : IIndex; + + /// Create an autodiff, rank-one tensor from intial and seed values. + /// An index. + /// The zeroth value. + /// The first value. + /// The zeroth seed value. + /// The first seed value. + /// A rank-one tensor of two initial and seed values. + static abstract AutoDiffTensor2 CreateAutoDiffTensor(in TN x0, in TN x1, in TN seed0, in TN seed1) + where TI : IIndex; + + /// Create an autodiff, rank-one tensor from vectors of initial and seed values. + /// An index. + /// A vector of initial values. + /// A vector of seed values. + /// A rank-one tensor of three initial and seed values. + static abstract AutoDiffTensor3 CreateAutoDiffTensor(in Vector3 x, in Vector3 seed) + where TI : IIndex; + + /// Create an autodiff, rank-one tensor from intial and seed values. + /// An index. + /// The zeroth value. + /// The first value. + /// The second value. + /// The zeroth seed value. + /// The first seed value. + /// The second seed value. + /// A rank-one tensor of three initial and seed values. + static abstract AutoDiffTensor3 CreateAutoDiffTensor(in TN x0, in TN x1, in TN x2, in TN seed0, in TN seed1, in TN seed2) + where TI : IIndex; + + /// Create an autodiff, rank-one tensor from vectors of initial and seed values. + /// An index. + /// A vector of initial values. + /// A vector of seed values. + /// A rank-one tensor of four initial and seed values. + static abstract AutoDiffTensor4 CreateAutoDiffTensor(in Vector4 x, in Vector4 seed) + where TI : IIndex; + + /// Create an autodiff, rank-one tensor from intial and seed values. + /// An index. + /// The zeroth value. + /// The first value. + /// The second value. + /// The third value. + /// The zeroth seed value. + /// The first seed value. + /// The second seed value. + /// The third seed value. + /// A rank-one tensor of four initial and seed values. + static abstract AutoDiffTensor4 CreateAutoDiffTensor(in TN x0, in TN x1, in TN x2, in TN x3, in TN seed0, in TN seed1, in TN seed2, in TN seed3) + where TI : IIndex; } diff --git a/src/Mathematics.NET/AutoDiff/ITape.cs b/src/Mathematics.NET/AutoDiff/ITape.cs index 995037f5..3330c1d1 100644 --- a/src/Mathematics.NET/AutoDiff/ITape.cs +++ b/src/Mathematics.NET/AutoDiff/ITape.cs @@ -25,6 +25,9 @@ // SOFTWARE. // +using Mathematics.NET.DifferentialGeometry; +using Mathematics.NET.DifferentialGeometry.Abstractions; +using Mathematics.NET.LinearAlgebra; using Microsoft.Extensions.Logging; namespace Mathematics.NET.AutoDiff; @@ -35,35 +38,35 @@ public interface ITape where T : IComplex, IDifferentiableFunctions { /// This property indicates whether or not the tape is currently tracking nodes. - public bool IsTracking { get; set; } + bool IsTracking { get; set; } /// Get the number of nodes on the gradient tape. - public int NodeCount { get; } + int NodeCount { get; } /// Get the number of variables that are being tracked. - public int VariableCount { get; } + int VariableCount { get; } /// Create a variable for the gradient tape to track. - /// A seed value. + /// An initial value. /// A variable. - public Variable CreateVariable(T seed); + Variable CreateVariable(T value); /// Log nodes tracked by the tape. /// A logger. /// A cancellation token. /// The total number of nodes to log. - public void LogNodes(ILogger> logger, CancellationToken cancellationToken, int limit = 100); + void LogNodes(ILogger> logger, CancellationToken cancellationToken, int limit = 100); /// Perform reverse accumulation on the gradient or Hessian tape and output the resulting gradient. /// The gradient. /// The gradient tape does not have any tracked variables. - public void ReverseAccumulate(out ReadOnlySpan gradient); + void ReverseAccumulate(out ReadOnlySpan gradient); /// Perform reverse accumulation on the gradient or Hessian tape and output the resulting gradient. /// The gradient. /// A seed value. /// The gradient tape does not have any tracked variables. - public void ReverseAccumulate(out ReadOnlySpan gradient, T seed); + void ReverseAccumulate(out ReadOnlySpan gradient, T seed); // // Basic operations @@ -73,91 +76,91 @@ public interface ITape /// The first variable. /// The second variable. /// A variable. - public Variable Add(Variable x, Variable y); + Variable Add(Variable x, Variable y); /// Add a constant value and a variable. /// A constant value. /// A variable. /// A variable. - public Variable Add(T c, Variable x); + Variable Add(T c, Variable x); /// Add a variable and a constant value. /// A variable. /// A constant value. /// A variable. - public Variable Add(Variable x, T c); + Variable Add(Variable x, T c); /// Divide two variables. /// A dividend. /// A divisor. /// A variable. - public Variable Divide(Variable x, Variable y); + Variable Divide(Variable x, Variable y); /// Divide a constant value by a variable. /// A constant dividend. /// A variable divisor. /// A variable. - public Variable Divide(T c, Variable x); + Variable Divide(T c, Variable x); /// Divide a variable by a constant value. /// A variable dividend. /// A constant divisor. /// A variabl. - public Variable Divide(Variable x, T c); + Variable Divide(Variable x, T c); /// Compute the modulo of a variable given a divisor. /// A dividend. /// A divisor. /// mod . - public Variable Modulo(in Variable x, in Variable y); + Variable Modulo(in Variable x, in Variable y); /// Compute the modulo of a real value given a divisor. /// A real dividend. /// A variable divisor. /// mod . - public Variable Modulo(Real c, in Variable x); + Variable Modulo(Real c, in Variable x); /// Compute the modulo of a variable given a divisor. /// A variable dividend. /// A real divisor. /// mod . - public Variable Modulo(in Variable x, Real c); + Variable Modulo(in Variable x, Real c); /// Multiply two variables. /// The first variable. /// The second variable. /// A variable. - public Variable Multiply(Variable x, Variable y); + Variable Multiply(Variable x, Variable y); /// Multiply a constant value by a variable. /// A constant value. /// A variable. /// A variable. - public Variable Multiply(T c, Variable x); + Variable Multiply(T c, Variable x); /// Multiply a variable by a constant value. /// A variable. /// A constant value. /// A variable. - public Variable Multiply(Variable x, T c); + Variable Multiply(Variable x, T c); /// Subract two variables. /// The first variable. /// The second variable. /// A variable. - public Variable Subtract(Variable x, Variable y); + Variable Subtract(Variable x, Variable y); /// Subtract a variable from a constant value. /// A constant value. /// A variable. /// A variable. - public Variable Subtract(T c, Variable x); + Variable Subtract(T c, Variable x); /// Subtract a constant value from a variable. /// A variable. /// A constant value. /// A variable. - public Variable Subtract(Variable x, T c); + Variable Subtract(Variable x, T c); // // Other operations @@ -166,95 +169,147 @@ public interface ITape /// Negate a variable. /// A variable. /// Minus one times the variable. - public Variable Negate(Variable x); + Variable Negate(Variable x); // Exponential functions /// - public Variable Exp(Variable x); + Variable Exp(Variable x); /// - public Variable Exp2(Variable x); + Variable Exp2(Variable x); /// - public Variable Exp10(Variable x); + Variable Exp10(Variable x); // Hyperbolic functions /// - public Variable Acosh(Variable x); + Variable Acosh(Variable x); /// - public Variable Asinh(Variable x); + Variable Asinh(Variable x); /// - public Variable Atanh(Variable x); + Variable Atanh(Variable x); /// - public Variable Cosh(Variable x); + Variable Cosh(Variable x); /// - public Variable Sinh(Variable x); + Variable Sinh(Variable x); /// - public Variable Tanh(Variable x); + Variable Tanh(Variable x); // Logarithmic functions /// - public Variable Ln(Variable x); + Variable Ln(Variable x); /// - public Variable Log(Variable x, Variable b); + Variable Log(Variable x, Variable b); /// - public Variable Log2(Variable x); + Variable Log2(Variable x); /// - public Variable Log10(Variable x); + Variable Log10(Variable x); // Power functions /// - public Variable Pow(Variable x, Variable y); + Variable Pow(Variable x, Variable y); /// - public Variable Pow(Variable x, T y); + Variable Pow(Variable x, T y); /// - public Variable Pow(T x, Variable y); + Variable Pow(T x, Variable y); // Root functions /// - public Variable Cbrt(Variable x); + Variable Cbrt(Variable x); /// - public Variable Root(Variable x, Variable n); + Variable Root(Variable x, Variable n); /// - public Variable Sqrt(Variable x); + Variable Sqrt(Variable x); // Trigonometric function /// - public Variable Acos(Variable x); + Variable Acos(Variable x); /// - public Variable Asin(Variable x); + Variable Asin(Variable x); /// - public Variable Atan(Variable x); + Variable Atan(Variable x); /// - public Variable Atan2(in Variable y, in Variable x); + Variable Atan2(in Variable y, in Variable x); /// - public Variable Cos(Variable x); + Variable Cos(Variable x); /// - public Variable Sin(Variable x); + Variable Sin(Variable x); /// - public Variable Tan(Variable x); + Variable Tan(Variable x); + + // + // DifGeo + // + + /// Create an autodiff, rank-one tensor from a vector of initial values. + /// An index. + /// A vector of initial values. + /// A rank-one tensor of two variables. + AutoDiffTensor2 CreateAutoDiffTensor(in Vector2 x) + where U : IIndex; + + /// Create an autodiff, rank-one tensor from initial values. + /// An index. + /// The zeroth value. + /// The first value. + /// A rank-one tensor of two variables. + AutoDiffTensor2 CreateAutoDiffTensor(in T x0, in T x1) + where U : IIndex; + + /// Create an autodiff, rank-one tensor from a vector of initial values. + /// An index. + /// A vector of initial values. + /// A rank-one tensor of three variables. + AutoDiffTensor3 CreateAutoDiffTensor(in Vector3 x) + where U : IIndex; + + /// Create an autodiff, rank-one tensor from initial values. + /// An index. + /// The zeroth value. + /// The first value. + /// The second value. + /// A rank-one tensor of three variables. + AutoDiffTensor3 CreateAutoDiffTensor(in T x0, in T x1, in T x2) + where U : IIndex; + + /// Create an autodiff, rank-one tensor from a vector of initial values. + /// An index. + /// A vector of initial values. + /// A rank-one tensor of four variables. + AutoDiffTensor4 CreateAutoDiffTensor(in Vector4 x) + where U : IIndex; + + /// Create an autodiff, rank-one tensor from initial values. + /// An index. + /// The zeroth value. + /// The first value. + /// The second value. + /// The third value. + /// A rank-one tensor of four variables. + AutoDiffTensor4 CreateAutoDiffTensor(in T x0, in T x1, in T x2, in T x3) + where U : IIndex; } diff --git a/src/Mathematics.NET/Core/Buffers/FMTensor4Buffer4.cs b/src/Mathematics.NET/Core/Buffers/FMTensor4Buffer4.cs new file mode 100644 index 00000000..ef453315 --- /dev/null +++ b/src/Mathematics.NET/Core/Buffers/FMTensor4Buffer4.cs @@ -0,0 +1,44 @@ +// +// Mathematics.NET +// https://github.com/HamletTanyavong/Mathematics.NET +// +// MIT License +// +// Copyright (c) 2023-present Hamlet Tanyavong +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +#pragma warning disable IDE0051 + +using System.Runtime.CompilerServices; +using Mathematics.NET.AutoDiff; +using Mathematics.NET.DifferentialGeometry; +using Mathematics.NET.DifferentialGeometry.Abstractions; + +namespace Mathematics.NET.Core.Buffers; + +[InlineArray(4)] +internal struct FMTensor4Buffer4 + where TDN : IDual + where TN : IComplex, IDifferentiableFunctions + where TPI : IIndex +{ + private Func, TDN> _element0; +} diff --git a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor2Buffer2.cs b/src/Mathematics.NET/Core/Buffers/RMTensor2Buffer2.cs similarity index 94% rename from src/Mathematics.NET/Core/Buffers/AutoDiffTensor2Buffer2.cs rename to src/Mathematics.NET/Core/Buffers/RMTensor2Buffer2.cs index fc723d85..a35aced6 100644 --- a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor2Buffer2.cs +++ b/src/Mathematics.NET/Core/Buffers/RMTensor2Buffer2.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -39,7 +39,7 @@ namespace Mathematics.NET.Core.Buffers; /// A type that implements and . /// The index of the point on the manifold. [InlineArray(2)] -internal struct AutoDiffTensor2Buffer2 +internal struct RMTensor2Buffer2 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex diff --git a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor2Buffer2x2.cs b/src/Mathematics.NET/Core/Buffers/RMTensor2Buffer2x2.cs similarity index 87% rename from src/Mathematics.NET/Core/Buffers/AutoDiffTensor2Buffer2x2.cs rename to src/Mathematics.NET/Core/Buffers/RMTensor2Buffer2x2.cs index c0992090..ac4b578e 100644 --- a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor2Buffer2x2.cs +++ b/src/Mathematics.NET/Core/Buffers/RMTensor2Buffer2x2.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -33,15 +33,15 @@ namespace Mathematics.NET.Core.Buffers; -/// Represents a buffer of 2 AutoDiffTensor2Buffer2 buffers. +/// Represents a buffer of 2 RMTensor2Buffer2 buffers. /// A type that implements . /// A type that implements and . /// The index of the point on the manifold. [InlineArray(2)] -internal struct AutoDiffTensor2Buffer2x2 +internal struct RMTensor2Buffer2x2 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex { - private AutoDiffTensor2Buffer2 _element0; + private RMTensor2Buffer2 _element0; } diff --git a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor3Buffer3.cs b/src/Mathematics.NET/Core/Buffers/RMTensor3Buffer3.cs similarity index 94% rename from src/Mathematics.NET/Core/Buffers/AutoDiffTensor3Buffer3.cs rename to src/Mathematics.NET/Core/Buffers/RMTensor3Buffer3.cs index ecad4276..7c044d13 100644 --- a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor3Buffer3.cs +++ b/src/Mathematics.NET/Core/Buffers/RMTensor3Buffer3.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -39,7 +39,7 @@ namespace Mathematics.NET.Core.Buffers; /// A type that implements and . /// The index of the point on the manifold. [InlineArray(3)] -internal struct AutoDiffTensor3Buffer3 +internal struct RMTensor3Buffer3 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex diff --git a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor3Buffer3x3.cs b/src/Mathematics.NET/Core/Buffers/RMTensor3Buffer3x3.cs similarity index 87% rename from src/Mathematics.NET/Core/Buffers/AutoDiffTensor3Buffer3x3.cs rename to src/Mathematics.NET/Core/Buffers/RMTensor3Buffer3x3.cs index 018032fa..05d05b60 100644 --- a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor3Buffer3x3.cs +++ b/src/Mathematics.NET/Core/Buffers/RMTensor3Buffer3x3.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -33,15 +33,15 @@ namespace Mathematics.NET.Core.Buffers; -/// Represents a buffer of 3 AutoDiffTensor3Buffer3 buffers. +/// Represents a buffer of 3 RMTensor3Buffer3 buffers. /// A type that implements . /// A type that implements and . /// The index of the point on the manifold. [InlineArray(3)] -internal struct AutoDiffTensor3Buffer3x3 +internal struct RMTensor3Buffer3x3 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex { - private AutoDiffTensor3Buffer3 _element0; + private RMTensor3Buffer3 _element0; } diff --git a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor4Buffer4.cs b/src/Mathematics.NET/Core/Buffers/RMTensor4Buffer4.cs similarity index 94% rename from src/Mathematics.NET/Core/Buffers/AutoDiffTensor4Buffer4.cs rename to src/Mathematics.NET/Core/Buffers/RMTensor4Buffer4.cs index c4145966..77218a6a 100644 --- a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor4Buffer4.cs +++ b/src/Mathematics.NET/Core/Buffers/RMTensor4Buffer4.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -39,7 +39,7 @@ namespace Mathematics.NET.Core.Buffers; /// A type that implements and . /// The index of the point on the manifold. [InlineArray(4)] -internal struct AutoDiffTensor4Buffer4 +internal struct RMTensor4Buffer4 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex diff --git a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor4Buffer4x4.cs b/src/Mathematics.NET/Core/Buffers/RMTensor4Buffer4x4.cs similarity index 87% rename from src/Mathematics.NET/Core/Buffers/AutoDiffTensor4Buffer4x4.cs rename to src/Mathematics.NET/Core/Buffers/RMTensor4Buffer4x4.cs index f37c4696..d059be09 100644 --- a/src/Mathematics.NET/Core/Buffers/AutoDiffTensor4Buffer4x4.cs +++ b/src/Mathematics.NET/Core/Buffers/RMTensor4Buffer4x4.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -33,15 +33,15 @@ namespace Mathematics.NET.Core.Buffers; -/// Represents a buffer of 4 AutoDiffTensor4Buffer4 buffers. +/// Represents a buffer of 4 RMTensor4Buffer4 buffers. /// A type that implements . /// A type that implements and . /// The index of the point on the manifold. [InlineArray(4)] -internal struct AutoDiffTensor4Buffer4x4 +internal struct RMTensor4Buffer4x4 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex { - private AutoDiffTensor4Buffer4 _element0; + private RMTensor4Buffer4 _element0; } diff --git a/src/Mathematics.NET/DifferentialGeometry/DifGeo.cs b/src/Mathematics.NET/DifferentialGeometry/DifGeo.cs index 495e1736..d35b4047 100644 --- a/src/Mathematics.NET/DifferentialGeometry/DifGeo.cs +++ b/src/Mathematics.NET/DifferentialGeometry/DifGeo.cs @@ -208,7 +208,7 @@ public static void Derivative( /// A rank-three tensor. public static void Derivative( TT tape, - TensorField2x2> tensor, + RMTensorField2x2> tensor, AutoDiffTensor2> point, out Tensor, TN, Index, Index, Index> dTensor) where TT : ITape @@ -230,17 +230,19 @@ public static void Derivative( _ = function(tape, point); tape.ReverseAccumulate(out var gradient); - dTensor[0, i, j] = gradient[0]; - dTensor[1, i, j] = gradient[1]; + for (int k = 0; k < 2; k++) + { + dTensor[k, i, j] = gradient[k]; + } } } } } - /// + /// public static void Derivative( TT tape, - TensorField3x3> tensor, + RMTensorField3x3> tensor, AutoDiffTensor3> point, out Tensor, TN, Index, Index, Index> dTensor) where TT : ITape @@ -262,18 +264,19 @@ public static void Derivative( _ = function(tape, point); tape.ReverseAccumulate(out var gradient); - dTensor[0, i, j] = gradient[0]; - dTensor[1, i, j] = gradient[1]; - dTensor[2, i, j] = gradient[2]; + for (int k = 0; k < 3; k++) + { + dTensor[k, i, j] = gradient[k]; + } } } } } - /// + /// public static void Derivative( TT tape, - TensorField4x4> tensor, + RMTensorField4x4> tensor, AutoDiffTensor4> point, out Tensor, TN, Index, Index, Index> dTensor) where TT : ITape @@ -295,19 +298,19 @@ public static void Derivative( _ = function(tape, point); tape.ReverseAccumulate(out var gradient); - dTensor[0, i, j] = gradient[0]; - dTensor[1, i, j] = gradient[1]; - dTensor[2, i, j] = gradient[2]; - dTensor[3, i, j] = gradient[3]; + for (int k = 0; k < 4; k++) + { + dTensor[k, i, j] = gradient[k]; + } } } } } - /// + /// public static void Derivative( TT tape, - TensorField2x2> tensor, + RMTensorField2x2> tensor, AutoDiffTensor2> point, out Tensor, TN, Index, Index, Index> dTensor) where TT : ITape @@ -329,17 +332,19 @@ public static void Derivative( _ = function(tape, point); tape.ReverseAccumulate(out var gradient); - dTensor[0, i, j] = gradient[0]; - dTensor[1, i, j] = gradient[1]; + for (int k = 0; k < 2; k++) + { + dTensor[k, i, j] = gradient[k]; + } } } } } - /// + /// public static void Derivative( TT tape, - TensorField3x3> tensor, + RMTensorField3x3> tensor, AutoDiffTensor3> point, out Tensor, TN, Index, Index, Index> dTensor) where TT : ITape @@ -361,18 +366,19 @@ public static void Derivative( _ = function(tape, point); tape.ReverseAccumulate(out var gradient); - dTensor[0, i, j] = gradient[0]; - dTensor[1, i, j] = gradient[1]; - dTensor[2, i, j] = gradient[2]; + for (int k = 0; k < 3; k++) + { + dTensor[k, i, j] = gradient[k]; + } } } } } - /// + /// public static void Derivative( TT tape, - TensorField4x4> tensor, + RMTensorField4x4> tensor, AutoDiffTensor4> point, out Tensor, TN, Index, Index, Index> dTensor) where TT : ITape @@ -394,10 +400,10 @@ public static void Derivative( _ = function(tape, point); tape.ReverseAccumulate(out var gradient); - dTensor[0, i, j] = gradient[0]; - dTensor[1, i, j] = gradient[1]; - dTensor[2, i, j] = gradient[2]; - dTensor[3, i, j] = gradient[3]; + for (int k = 0; k < 4; k++) + { + dTensor[k, i, j] = gradient[k]; + } } } } @@ -419,7 +425,7 @@ public static void Derivative( /// A rank-four tensor. public static void SecondDerivative( HessianTape tape, - TensorField2x2, TN, TI3P, TI4P, Index> tensor, + RMTensorField2x2, TN, TI3P, TI4P, Index> tensor, AutoDiffTensor2> point, out Tensor, TN, Index, Index, Index, Index> d2Tensor) where TN : IComplex, IDifferentiableFunctions @@ -441,20 +447,22 @@ public static void SecondDerivative hessian); - d2Tensor[0, 0, i, j] = hessian[0, 0]; - d2Tensor[0, 1, i, j] = hessian[0, 1]; - - d2Tensor[1, 0, i, j] = hessian[1, 0]; - d2Tensor[1, 1, i, j] = hessian[1, 1]; + for (int k = 0; k < 2; k++) + { + for (int l = 0; l < 2; l++) + { + d2Tensor[k, l, i, j] = hessian[k, l]; + } + } } } } } - /// + /// public static void SecondDerivative( HessianTape tape, - TensorField3x3, TN, TI3P, TI4P, Index> tensor, + RMTensorField3x3, TN, TI3P, TI4P, Index> tensor, AutoDiffTensor3> point, out Tensor, TN, Index, Index, Index, Index> d2Tensor) where TN : IComplex, IDifferentiableFunctions @@ -476,26 +484,22 @@ public static void SecondDerivative hessian); - d2Tensor[0, 0, i, j] = hessian[0, 0]; - d2Tensor[0, 1, i, j] = hessian[0, 1]; - d2Tensor[0, 2, i, j] = hessian[0, 2]; - - d2Tensor[1, 0, i, j] = hessian[1, 0]; - d2Tensor[1, 1, i, j] = hessian[1, 1]; - d2Tensor[1, 2, i, j] = hessian[1, 2]; - - d2Tensor[2, 0, i, j] = hessian[2, 0]; - d2Tensor[2, 1, i, j] = hessian[2, 1]; - d2Tensor[2, 2, i, j] = hessian[2, 2]; + for (int k = 0; k < 3; k++) + { + for (int l = 0; l < 3; l++) + { + d2Tensor[k, l, i, j] = hessian[k, l]; + } + } } } } } - /// + /// public static void SecondDerivative( HessianTape tape, - TensorField4x4, TN, TI3P, TI4P, Index> tensor, + RMTensorField4x4, TN, TI3P, TI4P, Index> tensor, AutoDiffTensor4> point, out Tensor, TN, Index, Index, Index, Index> d2Tensor) where TN : IComplex, IDifferentiableFunctions @@ -517,34 +521,22 @@ public static void SecondDerivative hessian); - d2Tensor[0, 0, i, j] = hessian[0, 0]; - d2Tensor[0, 1, i, j] = hessian[0, 1]; - d2Tensor[0, 2, i, j] = hessian[0, 2]; - d2Tensor[0, 3, i, j] = hessian[0, 3]; - - d2Tensor[1, 0, i, j] = hessian[1, 0]; - d2Tensor[1, 1, i, j] = hessian[1, 1]; - d2Tensor[1, 2, i, j] = hessian[1, 2]; - d2Tensor[1, 3, i, j] = hessian[1, 3]; - - d2Tensor[2, 0, i, j] = hessian[2, 0]; - d2Tensor[2, 1, i, j] = hessian[2, 1]; - d2Tensor[2, 2, i, j] = hessian[2, 2]; - d2Tensor[2, 3, i, j] = hessian[2, 3]; - - d2Tensor[3, 0, i, j] = hessian[3, 0]; - d2Tensor[3, 1, i, j] = hessian[3, 1]; - d2Tensor[3, 2, i, j] = hessian[3, 2]; - d2Tensor[3, 3, i, j] = hessian[3, 3]; + for (int k = 0; k < 4; k++) + { + for (int l = 0; l < 4; l++) + { + d2Tensor[k, l, i, j] = hessian[k, l]; + } + } } } } } - /// + /// public static void SecondDerivative( HessianTape tape, - TensorField2x2, TN, TI3P, TI4P, Index> tensor, + RMTensorField2x2, TN, TI3P, TI4P, Index> tensor, AutoDiffTensor2> point, out Tensor, TN, Index, Index, Index, Index> d2Tensor) where TN : IComplex, IDifferentiableFunctions @@ -566,20 +558,22 @@ public static void SecondDerivative hessian); - d2Tensor[0, 0, i, j] = hessian[0, 0]; - d2Tensor[0, 1, i, j] = hessian[0, 1]; - - d2Tensor[1, 0, i, j] = hessian[1, 0]; - d2Tensor[1, 1, i, j] = hessian[1, 1]; + for (int k = 0; k < 2; k++) + { + for (int l = 0; l < 2; l++) + { + d2Tensor[k, l, i, j] = hessian[k, l]; + } + } } } } } - /// + /// public static void SecondDerivative( HessianTape tape, - TensorField3x3, TN, TI3P, TI4P, Index> tensor, + RMTensorField3x3, TN, TI3P, TI4P, Index> tensor, AutoDiffTensor3> point, out Tensor, TN, Index, Index, Index, Index> d2Tensor) where TN : IComplex, IDifferentiableFunctions @@ -601,26 +595,22 @@ public static void SecondDerivative hessian); - d2Tensor[0, 0, i, j] = hessian[0, 0]; - d2Tensor[0, 1, i, j] = hessian[0, 1]; - d2Tensor[0, 2, i, j] = hessian[0, 2]; - - d2Tensor[1, 0, i, j] = hessian[1, 0]; - d2Tensor[1, 1, i, j] = hessian[1, 1]; - d2Tensor[1, 2, i, j] = hessian[1, 2]; - - d2Tensor[2, 0, i, j] = hessian[2, 0]; - d2Tensor[2, 1, i, j] = hessian[2, 1]; - d2Tensor[2, 2, i, j] = hessian[2, 2]; + for (int k = 0; k < 3; k++) + { + for (int l = 0; l < 3; l++) + { + d2Tensor[k, l, i, j] = hessian[k, l]; + } + } } } } } - /// + /// public static void SecondDerivative( HessianTape tape, - TensorField4x4, TN, TI3P, TI4P, Index> tensor, + RMTensorField4x4, TN, TI3P, TI4P, Index> tensor, AutoDiffTensor4> point, out Tensor, TN, Index, Index, Index, Index> d2Tensor) where TN : IComplex, IDifferentiableFunctions @@ -642,25 +632,13 @@ public static void SecondDerivative hessian); - d2Tensor[0, 0, i, j] = hessian[0, 0]; - d2Tensor[0, 1, i, j] = hessian[0, 1]; - d2Tensor[0, 2, i, j] = hessian[0, 2]; - d2Tensor[0, 3, i, j] = hessian[0, 3]; - - d2Tensor[1, 0, i, j] = hessian[1, 0]; - d2Tensor[1, 1, i, j] = hessian[1, 1]; - d2Tensor[1, 2, i, j] = hessian[1, 2]; - d2Tensor[1, 3, i, j] = hessian[1, 3]; - - d2Tensor[2, 0, i, j] = hessian[2, 0]; - d2Tensor[2, 1, i, j] = hessian[2, 1]; - d2Tensor[2, 2, i, j] = hessian[2, 2]; - d2Tensor[2, 3, i, j] = hessian[2, 3]; - - d2Tensor[3, 0, i, j] = hessian[3, 0]; - d2Tensor[3, 1, i, j] = hessian[3, 1]; - d2Tensor[3, 2, i, j] = hessian[3, 2]; - d2Tensor[3, 3, i, j] = hessian[3, 3]; + for (int k = 0; k < 4; k++) + { + for (int l = 0; l < 4; l++) + { + d2Tensor[k, l, i, j] = hessian[k, l]; + } + } } } } @@ -1113,8 +1091,7 @@ public static void Riemann( { for (int n = 0; n < 2; n++) { - riemann[r, s, m, n] = - dChristoffel[m, r, s, n] - dChristoffel[n, r, s, m] + cChristoffels[r, m, s, n] - cChristoffels[r, n, s, m]; + riemann[r, s, m, n] = dChristoffel[m, r, s, n] - dChristoffel[n, r, s, m] + cChristoffels[r, m, s, n] - cChristoffels[r, n, s, m]; } } } @@ -1147,8 +1124,7 @@ public static void Riemann( { for (int n = 0; n < 3; n++) { - riemann[r, s, m, n] = - dChristoffel[m, r, s, n] - dChristoffel[n, r, s, m] + cChristoffels[r, m, s, n] - cChristoffels[r, n, s, m]; + riemann[r, s, m, n] = dChristoffel[m, r, s, n] - dChristoffel[n, r, s, m] + cChristoffels[r, m, s, n] - cChristoffels[r, n, s, m]; } } } @@ -1181,8 +1157,7 @@ public static void Riemann( { for (int n = 0; n < 4; n++) { - riemann[r, s, m, n] = - dChristoffel[m, r, s, n] - dChristoffel[n, r, s, m] + cChristoffels[r, m, s, n] - cChristoffels[r, n, s, m]; + riemann[r, s, m, n] = dChristoffel[m, r, s, n] - dChristoffel[n, r, s, m] + cChristoffels[r, m, s, n] - cChristoffels[r, n, s, m]; } } } diff --git a/src/Mathematics.NET/DifferentialGeometry/DifGeoExtensions.cs b/src/Mathematics.NET/DifferentialGeometry/DifGeoExtensions.cs index 8e501aac..8357649c 100644 --- a/src/Mathematics.NET/DifferentialGeometry/DifGeoExtensions.cs +++ b/src/Mathematics.NET/DifferentialGeometry/DifGeoExtensions.cs @@ -26,9 +26,6 @@ // using System.Runtime.CompilerServices; -using Mathematics.NET.AutoDiff; -using Mathematics.NET.DifferentialGeometry.Abstractions; -using Mathematics.NET.LinearAlgebra; using Mathematics.NET.LinearAlgebra.Abstractions; namespace Mathematics.NET.DifferentialGeometry; @@ -36,130 +33,6 @@ namespace Mathematics.NET.DifferentialGeometry; /// A class containing difgeo extension methods. public static class DifGeoExtensions { - // - // AutoDiff - // - - /// Create an autodiff, rank-one tensor from a real vector. - /// An index. - /// A type that implements . - /// A vector of seed values. - /// A rank-one tensor of four variables. - public static AutoDiffTensor2 CreateAutoDiffTensor(this ITape tape, Vector2 x) - where T : IIndex - => new(tape.CreateVariable(x.X1), tape.CreateVariable(x.X2)); - - /// Create an autodiff, rank-one tensor from a complex vector. - /// An index. - /// A type that implements . - /// A vector of seed values. - /// A rank-one tensor of four variables. - public static AutoDiffTensor2 CreateAutoDiffTensor(this ITape tape, Vector2 x) - where T : IIndex - => new(tape.CreateVariable(x.X1), tape.CreateVariable(x.X2)); - - /// Create an autodiff, rank-one tensor from real seed values. - /// An index. - /// A type that implements . - /// The zeroth seed value. - /// The first seed value. - /// A rank-one tensor of four variables. - public static AutoDiffTensor2 CreateAutoDiffTensor(this ITape tape, Real x0Seed, Real x1Seed) - where T : IIndex - => new(tape.CreateVariable(x0Seed), tape.CreateVariable(x1Seed)); - - /// Create an autodiff, rank-one tensor from complex seed values. - /// An index. - /// A type that implements . - /// The zeroth seed value. - /// The first seed value. - /// A rank-one tensor of four variables. - public static AutoDiffTensor2 CreateAutoDiffTensor(this ITape tape, in Complex x0Seed, in Complex x1Seed) - where T : IIndex - => new(tape.CreateVariable(x0Seed), tape.CreateVariable(x1Seed)); - - /// Create an autodiff, rank-one tensor from a real vector. - /// An index. - /// A type that implements . - /// A vector of seed values. - /// A rank-one tensor of four variables. - public static AutoDiffTensor3 CreateAutoDiffTensor(this ITape tape, Vector3 x) - where T : IIndex - => new(tape.CreateVariable(x.X1), tape.CreateVariable(x.X2), tape.CreateVariable(x.X3)); - - /// Create an autodiff, rank-one tensor from a complex vector. - /// An index. - /// A type that implements . - /// A vector of seed values. - /// A rank-one tensor of four variables. - public static AutoDiffTensor3 CreateAutoDiffTensor(this ITape tape, Vector3 x) - where T : IIndex - => new(tape.CreateVariable(x.X1), tape.CreateVariable(x.X2), tape.CreateVariable(x.X3)); - - /// Create an autodiff, rank-one tensor from real seed values. - /// An index. - /// A type that implements . - /// The zeroth seed value. - /// The first seed value. - /// The second seed value. - /// A rank-one tensor of four variables. - public static AutoDiffTensor3 CreateAutoDiffTensor(this ITape tape, Real x0Seed, Real x1Seed, Real x2Seed) - where T : IIndex - => new(tape.CreateVariable(x0Seed), tape.CreateVariable(x1Seed), tape.CreateVariable(x2Seed)); - - /// Create an autodiff, rank-one tensor from complex seed values. - /// An index. - /// A type that implements . - /// The zeroth seed value. - /// The first seed value. - /// The second seed value. - /// A rank-one tensor of four variables. - public static AutoDiffTensor3 CreateAutoDiffTensor(this ITape tape, in Complex x0Seed, in Complex x1Seed, in Complex x2Seed) - where T : IIndex - => new(tape.CreateVariable(x0Seed), tape.CreateVariable(x1Seed), tape.CreateVariable(x2Seed)); - - /// Create an autodiff, rank-one tensor from a real vector. - /// An index. - /// A type that implements . - /// A vector of seed values. - /// A rank-one tensor of four variables. - public static AutoDiffTensor4 CreateAutoDiffTensor(this ITape tape, Vector4 x) - where T : IIndex - => new(tape.CreateVariable(x.X1), tape.CreateVariable(x.X2), tape.CreateVariable(x.X3), tape.CreateVariable(x.X4)); - - /// Create an autodiff, rank-one tensor from a complex vector. - /// An index. - /// A type that implements . - /// A vector of seed values. - /// A rank-one tensor of four variables. - public static AutoDiffTensor4 CreateAutoDiffTensor(this ITape tape, Vector4 x) - where T : IIndex - => new(tape.CreateVariable(x.X1), tape.CreateVariable(x.X2), tape.CreateVariable(x.X3), tape.CreateVariable(x.X4)); - - /// Create an autodiff, rank-one tensor from real seed values. - /// An index. - /// A type that implements . - /// The zeroth seed value. - /// The first seed value. - /// The second seed value. - /// The third seed value. - /// A rank-one tensor of four variables. - public static AutoDiffTensor4 CreateAutoDiffTensor(this ITape tape, Real x0Seed, Real x1Seed, Real x2Seed, Real x3Seed) - where T : IIndex - => new(tape.CreateVariable(x0Seed), tape.CreateVariable(x1Seed), tape.CreateVariable(x2Seed), tape.CreateVariable(x3Seed)); - - /// Create an autodiff, rank-one tensor from complex seed values. - /// An index. - /// A type that implements . - /// The zeroth seed value. - /// The first seed value. - /// The second seed value. - /// The third seed value. - /// A rank-one tensor of four variables. - public static AutoDiffTensor4 CreateAutoDiffTensor(this ITape tape, in Complex x0Seed, in Complex x1Seed, in Complex x2Seed, in Complex x3Seed) - where T : IIndex - => new(tape.CreateVariable(x0Seed), tape.CreateVariable(x1Seed), tape.CreateVariable(x2Seed), tape.CreateVariable(x3Seed)); - // // Calculus // diff --git a/src/Mathematics.NET/DifferentialGeometry/MetricTensorField2x2.cs b/src/Mathematics.NET/DifferentialGeometry/MetricTensorField2x2.cs index 5ebf1e51..be06bb0a 100644 --- a/src/Mathematics.NET/DifferentialGeometry/MetricTensorField2x2.cs +++ b/src/Mathematics.NET/DifferentialGeometry/MetricTensorField2x2.cs @@ -35,7 +35,7 @@ namespace Mathematics.NET.DifferentialGeometry; /// A type that implements . /// A type that implements and . /// The index of the point on the manifold. -public abstract class MetricTensorField2x2 : TensorField2x2 +public abstract class MetricTensorField2x2 : RMTensorField2x2 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex diff --git a/src/Mathematics.NET/DifferentialGeometry/MetricTensorField3x3.cs b/src/Mathematics.NET/DifferentialGeometry/MetricTensorField3x3.cs index 83a1d255..158e2e8a 100644 --- a/src/Mathematics.NET/DifferentialGeometry/MetricTensorField3x3.cs +++ b/src/Mathematics.NET/DifferentialGeometry/MetricTensorField3x3.cs @@ -35,7 +35,7 @@ namespace Mathematics.NET.DifferentialGeometry; /// A type that implements . /// A type that implements and . /// The index of the point on the manifold. -public abstract class MetricTensorField3x3 : TensorField3x3 +public abstract class MetricTensorField3x3 : RMTensorField3x3 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex diff --git a/src/Mathematics.NET/DifferentialGeometry/MetricTensorField4x4.cs b/src/Mathematics.NET/DifferentialGeometry/MetricTensorField4x4.cs index 09cbbdc0..e3679da3 100644 --- a/src/Mathematics.NET/DifferentialGeometry/MetricTensorField4x4.cs +++ b/src/Mathematics.NET/DifferentialGeometry/MetricTensorField4x4.cs @@ -35,7 +35,7 @@ namespace Mathematics.NET.DifferentialGeometry; /// A type that implements . /// A type that implements and . /// The index of the point on the manifold. -public abstract class MetricTensorField4x4 : TensorField4x4 +public abstract class MetricTensorField4x4 : RMTensorField4x4 where TT : ITape where TN : IComplex, IDifferentiableFunctions where TPI : IIndex diff --git a/src/Mathematics.NET/DifferentialGeometry/TensorField2.cs b/src/Mathematics.NET/DifferentialGeometry/RMTensorField2.cs similarity index 91% rename from src/Mathematics.NET/DifferentialGeometry/TensorField2.cs rename to src/Mathematics.NET/DifferentialGeometry/RMTensorField2.cs index e7d3c6af..dfb2e361 100644 --- a/src/Mathematics.NET/DifferentialGeometry/TensorField2.cs +++ b/src/Mathematics.NET/DifferentialGeometry/RMTensorField2.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -37,15 +37,15 @@ namespace Mathematics.NET.DifferentialGeometry; /// A type that implements and . /// An index position. /// An index. -public class TensorField2 : TensorField +public class RMTensorField2 : TensorField where TT : ITape where TN : IComplex, IDifferentiableFunctions where TIP : IIndexPosition where TPI : IIndex { - private AutoDiffTensor2Buffer2 _buffer; + private RMTensor2Buffer2 _buffer; - public TensorField2() { } + public RMTensorField2() { } public Func, Variable> this[int index] { diff --git a/src/Mathematics.NET/DifferentialGeometry/TensorField2x2.cs b/src/Mathematics.NET/DifferentialGeometry/RMTensorField2x2.cs similarity index 93% rename from src/Mathematics.NET/DifferentialGeometry/TensorField2x2.cs rename to src/Mathematics.NET/DifferentialGeometry/RMTensorField2x2.cs index 6c18f16f..f949e790 100644 --- a/src/Mathematics.NET/DifferentialGeometry/TensorField2x2.cs +++ b/src/Mathematics.NET/DifferentialGeometry/RMTensorField2x2.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -39,16 +39,16 @@ namespace Mathematics.NET.DifferentialGeometry; /// The position of the first index of the tensor. /// The position of the second index of the tensor. /// The index of the point on the manifold. -public class TensorField2x2 : TensorField +public class RMTensorField2x2 : TensorField where TT : ITape where TN : IComplex, IDifferentiableFunctions where TI1P : IIndexPosition where TI2P : IIndexPosition where TPI : IIndex { - private protected AutoDiffTensor2Buffer2x2 _buffer; + private protected RMTensor2Buffer2x2 _buffer; - public TensorField2x2() { } + public RMTensorField2x2() { } public Func, Variable> this[int row, int column] { diff --git a/src/Mathematics.NET/DifferentialGeometry/TensorField3.cs b/src/Mathematics.NET/DifferentialGeometry/RMTensorField3.cs similarity index 91% rename from src/Mathematics.NET/DifferentialGeometry/TensorField3.cs rename to src/Mathematics.NET/DifferentialGeometry/RMTensorField3.cs index 5e6fe0fe..995d5031 100644 --- a/src/Mathematics.NET/DifferentialGeometry/TensorField3.cs +++ b/src/Mathematics.NET/DifferentialGeometry/RMTensorField3.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -37,15 +37,15 @@ namespace Mathematics.NET.DifferentialGeometry; /// A type that implements and . /// An index position. /// An index. -public class TensorField3 : TensorField +public class RMTensorField3 : TensorField where TT : ITape where TN : IComplex, IDifferentiableFunctions where TIP : IIndexPosition where TPI : IIndex { - private AutoDiffTensor3Buffer3 _buffer; + private RMTensor3Buffer3 _buffer; - public TensorField3() { } + public RMTensorField3() { } public Func, Variable> this[int index] { diff --git a/src/Mathematics.NET/DifferentialGeometry/TensorField3x3.cs b/src/Mathematics.NET/DifferentialGeometry/RMTensorField3x3.cs similarity index 89% rename from src/Mathematics.NET/DifferentialGeometry/TensorField3x3.cs rename to src/Mathematics.NET/DifferentialGeometry/RMTensorField3x3.cs index d9130372..94b385d1 100644 --- a/src/Mathematics.NET/DifferentialGeometry/TensorField3x3.cs +++ b/src/Mathematics.NET/DifferentialGeometry/RMTensorField3x3.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -39,16 +39,16 @@ namespace Mathematics.NET.DifferentialGeometry; /// The position of the first index of the tensor. /// The position of the second index of the tensor. /// The index of the point on the manifold. -public class TensorField3x3 : TensorField +public class RMTensorField3x3 : TensorField where TT : ITape where TN : IComplex, IDifferentiableFunctions where TI1P : IIndexPosition where TI2P : IIndexPosition where TPI : IIndex { - private protected AutoDiffTensor3Buffer3x3 _buffer; + private protected RMTensor3Buffer3x3 _buffer; - public TensorField3x3() { } + public RMTensorField3x3() { } public Func, Variable> this[int row, int column] { @@ -59,7 +59,7 @@ public TensorField3x3() { } set => _buffer[row][column] = value; } - /// + /// public Tensor, TN, Index, Index> Compute(TT tape, AutoDiffTensor3 x) where TI1N : IIndexName where TI2N : IIndexName diff --git a/src/Mathematics.NET/DifferentialGeometry/TensorField4.cs b/src/Mathematics.NET/DifferentialGeometry/RMTensorField4.cs similarity index 91% rename from src/Mathematics.NET/DifferentialGeometry/TensorField4.cs rename to src/Mathematics.NET/DifferentialGeometry/RMTensorField4.cs index 9261cd12..be3d4e92 100644 --- a/src/Mathematics.NET/DifferentialGeometry/TensorField4.cs +++ b/src/Mathematics.NET/DifferentialGeometry/RMTensorField4.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -37,15 +37,15 @@ namespace Mathematics.NET.DifferentialGeometry; /// A type that implements and . /// An index position. /// An index. -public class TensorField4 : TensorField +public class RMTensorField4 : TensorField where TT : ITape where TN : IComplex, IDifferentiableFunctions where TIP : IIndexPosition where TPI : IIndex { - private AutoDiffTensor4Buffer4 _buffer; + private RMTensor4Buffer4 _buffer; - public TensorField4() { } + public RMTensorField4() { } public Func, Variable> this[int index] { diff --git a/src/Mathematics.NET/DifferentialGeometry/TensorField4x4.cs b/src/Mathematics.NET/DifferentialGeometry/RMTensorField4x4.cs similarity index 89% rename from src/Mathematics.NET/DifferentialGeometry/TensorField4x4.cs rename to src/Mathematics.NET/DifferentialGeometry/RMTensorField4x4.cs index 4bba0b8c..c184a3ea 100644 --- a/src/Mathematics.NET/DifferentialGeometry/TensorField4x4.cs +++ b/src/Mathematics.NET/DifferentialGeometry/RMTensorField4x4.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -39,16 +39,16 @@ namespace Mathematics.NET.DifferentialGeometry; /// The position of the first index of the tensor. /// The position of the second index of the tensor. /// The index of the point on the manifold. -public class TensorField4x4 : TensorField +public class RMTensorField4x4 : TensorField where TT : ITape where TN : IComplex, IDifferentiableFunctions where TI1P : IIndexPosition where TI2P : IIndexPosition where TPI : IIndex { - private protected AutoDiffTensor4Buffer4x4 _buffer; + private protected RMTensor4Buffer4x4 _buffer; - public TensorField4x4() { } + public RMTensorField4x4() { } public Func, Variable> this[int row, int column] { @@ -59,7 +59,7 @@ public TensorField4x4() { } set => _buffer[row][column] = value; } - /// + /// public Tensor, TN, Index, Index> Compute(TT tape, AutoDiffTensor4 point) where TI1N : IIndexName where TI2N : IIndexName diff --git a/tests/Mathematics.NET.Benchmarks.Implementations/AutoDiff/GradientTapeWithLinkedList.cs b/tests/Mathematics.NET.Benchmarks.Implementations/AutoDiff/GradientTapeWithLinkedList.cs index a2229f17..20c4f01b 100644 --- a/tests/Mathematics.NET.Benchmarks.Implementations/AutoDiff/GradientTapeWithLinkedList.cs +++ b/tests/Mathematics.NET.Benchmarks.Implementations/AutoDiff/GradientTapeWithLinkedList.cs @@ -27,6 +27,9 @@ using System.Runtime.CompilerServices; using Mathematics.NET.AutoDiff; +using Mathematics.NET.DifferentialGeometry; +using Mathematics.NET.DifferentialGeometry.Abstractions; +using Mathematics.NET.LinearAlgebra; using Microsoft.Extensions.Logging; namespace Mathematics.NET.Benchmarks.Implementations.AutoDiff; @@ -570,4 +573,15 @@ public Variable CustomOperation(Variable x, Variable y, Func f _nodes.AddLast(new GradientNode(dfx(x.Value, y.Value), dfy(x.Value, y.Value), x._index, y._index)); return new(_nodes.Count - 1, f(x.Value, y.Value)); } + + // + // DifGeo + // + + public AutoDiffTensor2 CreateAutoDiffTensor(in Vector2 x) where U : IIndex => throw new NotImplementedException(); + public AutoDiffTensor2 CreateAutoDiffTensor(in T x0, in T x1) where U : IIndex => throw new NotImplementedException(); + public AutoDiffTensor3 CreateAutoDiffTensor(in Vector3 x) where U : IIndex => throw new NotImplementedException(); + public AutoDiffTensor3 CreateAutoDiffTensor(in T x0, in T x1, in T x2) where U : IIndex => throw new NotImplementedException(); + public AutoDiffTensor4 CreateAutoDiffTensor(in Vector4 x) where U : IIndex => throw new NotImplementedException(); + public AutoDiffTensor4 CreateAutoDiffTensor(in T x0, in T x1, in T x2, in T x3) where U : IIndex => throw new NotImplementedException(); }