using System; using System.Collections.Generic; using System.Linq; using System.Text; using Azimuth; using Azimuth.DenseLinearAlgebra; using Azimuth.Approximation; namespace Azimuth.UnitTests.Approximation { public class ChebyshevTests : UnitTestSharp.TestFixture { private static Func cheby_4 = x => ((8 * x * x) - 8) * x * x + 1; private static Func cheby_3 = x => (4 * x * x - 3) * x; private static Func cheby_2 = x => 2 * x * x - 1; private static Func cheby_1 = x => x; private static Func cheby_0 = x => 1; public class CtorTests : UnitTestSharp.TestFixture { public void Chebyshev_0() { var window = new Interval(-1, 1); var cheby = new Chebyshev(cheby_0, window); CheckEqual(new DenseVector(new Scalar[] { 2, }), cheby.Coefficients); } public void Chebyshev_1() { var window = new Interval(-1, 1); var cheby = new Chebyshev(cheby_1, window); CheckEqual(new DenseVector(new Scalar[] { 0, 1, }), cheby.Coefficients); } public void Chebyshev_2() { var window = new Interval(-1, 1); var cheby = new Chebyshev(cheby_2, window); CheckEqual(new DenseVector(new Scalar[] { 0, 0, 1, }), cheby.Coefficients); } public void Chebyshev_3() { var window = new Interval(-1, 1); var cheby = new Chebyshev(cheby_3, window); CheckEqual(new DenseVector(new Scalar[] { 0, 0, 0, 1, }), cheby.Coefficients); } public void Chebyshev_4() { var window = new Interval(-1, 1); var cheby = new Chebyshev(cheby_4, window); CheckEqual(new DenseVector(new Scalar[] { 0, 0, 0, 0, 1, }), cheby.Coefficients); } public void x54321() { var window = new Interval(-1, 1); var cheby = new Chebyshev(x => cheby_4(x) + 2 * cheby_3(x) + 3 * cheby_2(x) + 4 * cheby_1(x) + 2.5 * cheby_0(x), window); CheckEqual(new DenseVector(new Scalar[] { 5, 4, 3, 2, 1, }), cheby.Coefficients); } public void Chebyshev_0_Window() { var window = new Interval(3, 7); var cheby = new Chebyshev(x => cheby_0(x*2+5), window); CheckEqual(new DenseVector(new Scalar[] { 2, }), cheby.Coefficients); } public void Chebyshev_1_Window() { var window = new Interval(-1, 4); var cheby = new Chebyshev(x => cheby_1((x-1.5)/2.5), window); CheckEqual(new DenseVector(new Scalar[] { 0, 1, }), cheby.Coefficients); } public void Chebyshev_2_Window() { var window = new Interval(3, 6); var cheby = new Chebyshev(x => cheby_2((x-4.5)/1.5), window); CheckEqual(new DenseVector(new Scalar[] { 0, 0, 1, }), cheby.Coefficients); } public void Chebyshev_3_Window() { var window = new Interval(-2, 11); var cheby = new Chebyshev(x => cheby_3((x-4.5)/6.5), window); CheckEqual(new DenseVector(new Scalar[] { 0, 0, 0, 1, }), cheby.Coefficients); } public void Chebyshev_4_Window() { var window = new Interval(2.5, 3.5); var cheby = new Chebyshev(x => cheby_4((x-3)/0.5), window); CheckEqual(new DenseVector(new Scalar[] { 0, 0, 0, 0, 1, }), cheby.Coefficients); } public void x54321_Window() { var window = new Interval(-1, 0); var cheby = new Chebyshev(x => { x = ((x + 0.5) * 2); return cheby_4(x) + 2 * cheby_3(x) + 3 * cheby_2(x) + 4 * cheby_1(x) + 2.5 * cheby_0(x); }, window); CheckEqual(new DenseVector(new Scalar[] { 5, 4, 3, 2, 1, }), cheby.Coefficients); } } public class EvaluateTests : UnitTestSharp.TestFixture { public void Basic() { Func func = x => Math.Cos(10 * Math.PI * x) + 2 * Math.Sin(3 * Math.PI * x + 1) * x + 1; var window = new Interval(0, 2); var cheby = new Chebyshev(func, window); Scalar t = 0.25; var expected = func(t); CheckEqual(expected, cheby.Evaluate(t)); } public void Chebyshev_0() { var window = new Interval(-1, 1); var func = cheby_0; var cheby = new Chebyshev(func, window); int max = 13; for(int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_1() { var window = new Interval(-1, 1); var func = cheby_1; var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_2() { var window = new Interval(-1, 1); var func = cheby_2; var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_3() { var window = new Interval(-1, 1); var func = cheby_3; var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_4() { var window = new Interval(-1, 1); var func = cheby_4; var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void x54321() { var window = new Interval(-1, 1); Func func = x => cheby_4(x) + 2 * cheby_3(x) + 3 * cheby_2(x) + 4 * cheby_1(x) + 2.5 * cheby_0(x); var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_0_Window() { var window = new Interval(3, 7); Func func = x => cheby_0(x * 2 + 5); var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_1_Window() { var window = new Interval(-1, 4); Func func = x => cheby_1((x - 1.5) / 2.5); var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_2_Window() { var window = new Interval(3, 6); Func func = x => cheby_2((x - 4.5) / 1.5); var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_3_Window() { var window = new Interval(-2, 11); Func func = x => cheby_3((x - 4.5) / 6.5); var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void Chebyshev_4_Window() { var window = new Interval(2.5, 3.5); Func func = x => cheby_4((x - 3) / 0.5); var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } public void x54321_Window() { var window = new Interval(-1, 0); Func func = x => { x = ((x + 0.5) * 2); return cheby_4(x) + 2 * cheby_3(x) + 3 * cheby_2(x) + 4 * cheby_1(x) + 2.5 * cheby_0(x); }; var cheby = new Chebyshev(func, window); int max = 13; for (int i = 0; i < max; ++i) { Scalar t = (Scalar)i / (Scalar)max * (window.Max - window.Min) + window.Min; CheckEqual(func(t), cheby.Evaluate(t)); } } } } }