142 lines
4.2 KiB
C#
142 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
using Skeleton.Algebra.Extensions;
|
|
using Skeleton.Algebra.ScalarFieldStructure;
|
|
using Skeleton.DataStructure.Packs.MatrixDecompositions;
|
|
using Skeleton.Samples;
|
|
using Skeleton.Utils;
|
|
using Skeleton.Utils.Helpers;
|
|
|
|
namespace Skeleton.Test;
|
|
[TestFixture]
|
|
public class DecompositionTest : BaseTest
|
|
{
|
|
[DatapointSource]
|
|
public static readonly IEnumerable<C44> TestMatrices = 100
|
|
.WithSeed(Seed)
|
|
.ScaleSamples(-1, 1)
|
|
.C44InvertibleSamples();
|
|
|
|
private static readonly Action<C44> OnFail = x =>
|
|
{
|
|
|
|
PLUPack<Complex, C44> res = x.PLUDecomposition();
|
|
Console.WriteLine("======================================");
|
|
Console.WriteLine(x.Representation);
|
|
Console.WriteLine("-------");
|
|
Console.WriteLine();
|
|
Console.WriteLine(x.Det());
|
|
Console.WriteLine();
|
|
Console.WriteLine(res.Det);
|
|
|
|
Console.WriteLine("P");
|
|
Console.WriteLine(res.P.Representation);
|
|
Console.WriteLine("L");
|
|
Console.WriteLine(res.L.Representation);
|
|
Console.WriteLine("U");
|
|
Console.WriteLine(res.U.Representation);
|
|
Console.WriteLine("+++++++++");
|
|
Console.WriteLine((res.P * res.L * res.U).Representation);
|
|
Console.WriteLine("======================================");
|
|
};
|
|
|
|
[Theory]
|
|
public static void C44PLUTest(C44 data)
|
|
{
|
|
Action<C44> rawTest = x =>
|
|
{
|
|
PLUPack<Complex, C44> res = x.PLUDecomposition();
|
|
Assert.IsTrue((res.P * res.L * res.U).IsEqualApprox(x));
|
|
Assert.IsTrue(res.Det.IsEqualApprox(x.Det()));
|
|
};
|
|
rawTest.OnFail(OnFail)(data);
|
|
}
|
|
|
|
[Test]
|
|
public static void R44PLUTest()
|
|
{
|
|
R44 t = new R44(
|
|
1, 2, 4, 8,
|
|
2, 8, 16, 64,
|
|
2, 8, 32, 128,
|
|
2, 8, 32, 256
|
|
);
|
|
PLUPack<double, R44> x = t.PLUDecomposition();
|
|
Assert.IsTrue(RealFieldStructure.Structure.IsEqualApprox(x.Det, t.Det()));
|
|
Assert.IsTrue((x.P * x.L * x.U).IsEqualApprox(t));
|
|
|
|
R44 tt = new R44(
|
|
0, 1, 2, 1,
|
|
1, 0, 0, 0,
|
|
2, 1, 2, 1,
|
|
1, 2, 4, 3
|
|
);
|
|
PLUPack<double, R44> xy = tt.PLUDecomposition();
|
|
Console.WriteLine($"{tt.Det()} x {xy.Det}");
|
|
Assert.IsTrue(RealFieldStructure.Structure.IsEqualApprox(tt.Det(), xy.Det));
|
|
|
|
Console.WriteLine(xy.L.Representation);
|
|
Console.WriteLine();
|
|
Console.WriteLine((xy.P * xy.L * xy.U).Representation);
|
|
Assert.IsTrue((xy.P * xy.L * xy.U).IsEqualApprox(tt));
|
|
}
|
|
|
|
[Test]
|
|
public static void R22PLUTest()
|
|
{
|
|
R22 a = new R22(3, 6, 2, 1);
|
|
var s = a.PLUDecomposition();
|
|
Assert.IsTrue(s.Det.IsEqualApprox(-9));
|
|
Assert.IsTrue((s.P * s.L * s.U).IsEqualApprox(a));
|
|
}
|
|
|
|
[Test]
|
|
public static void R33PLUTest()
|
|
{
|
|
R33 a = new R33(
|
|
1, 2, 3,
|
|
3, 5, 7,
|
|
2, 4, 1
|
|
);
|
|
var s = a.PLUDecomposition();
|
|
Console.WriteLine("P");
|
|
Console.WriteLine(s.P.Representation);
|
|
Console.WriteLine("L");
|
|
Console.WriteLine(s.L.Representation);
|
|
Console.WriteLine("U");
|
|
Console.WriteLine(s.U.Representation);
|
|
Console.WriteLine($"{s.Det} x {a.Det()}");
|
|
Assert.IsTrue(s.Det.IsEqualApprox(a.Det()));
|
|
Assert.IsTrue((s.P * s.L * s.U).IsEqualApprox(a));
|
|
|
|
}
|
|
[Test]
|
|
public static void C33QRDcompositionTest()
|
|
{
|
|
C33 a = new (
|
|
0, 3, 1,
|
|
0, 4, -2,
|
|
2, 1, 1
|
|
);
|
|
var w = a.QRDecomposition();
|
|
Assert.IsTrue(w.Q.Property.IsOrthogonal);
|
|
Assert.IsTrue(w.R.Property.IsUpperTriangle);
|
|
Assert.IsTrue((w.Q * w.R).IsEqualApprox(a));
|
|
}
|
|
|
|
[Theory]
|
|
public static void C44QRDecompositionTest(C44 data)
|
|
{
|
|
Action<C44> rawTest = x =>
|
|
{
|
|
QRPair<C44> qr = x.QRDecomposition();
|
|
Assert.IsTrue((qr.Q * qr.R).IsEqualApprox(x));
|
|
Assert.IsTrue(qr.Q.Property.IsUnitary);
|
|
Assert.IsTrue(qr.R.Property.IsUpperTriangle);
|
|
};
|
|
rawTest.OnFail(OnFail)(data);
|
|
}
|
|
|
|
}
|