Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/SmartVectorDotNet.Test/ArrayNumericUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartVectorDotNet;

public class ArrayNumericUtilsTest
{
public static TheoryData<double, double, int, double[]> LinspaceTestCases()
=> new()
{
{ 0, 0, 1, [0] },
{ 0, 1, 1, [0] },
{ 0, 0, 2, [0, 0] },
{ 0, 1, 2, [0, 0.5] },
{ 0, 1, 4, [0, 0.25, 0.5, 0.75] },
{ 1, 0, 4, [1, 0.75, 0.5, 0.25] },
{ -5, 0, 5, [-5, -4, -3, -2, -1] },
{ 0, 11, 11, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] },
};

[Theory]
[MemberData(nameof(LinspaceTestCases))]
public void Linspace(double beginInclusive, double endInclusive, int count, double[] expected)
{
var result = ArrayNumericUtils.Linspace(beginInclusive, endInclusive, count);
Assert.Equal(expected.Length, result.Length);
for (int i = 0; i < expected.Length; i++)
{
Assert.Equal(expected[i], result[i], 6); // Using 6 decimal places for floating point comparison
}
}


public static TheoryData<double, double, int, double[]> LinspaceMaxInclusiveTestCases()
=> new()
{
{ 0, 1, 2, [0, 1] },
{ 0, 1, 3, [0, 0.5, 1] },
{ 0, 1, 5, [0, 0.25, 0.5, 0.75, 1] },
{ 1, 0, 2, [1, 0] },
{ 1, 0, 3, [1, 0.5, 0] },
{ -5, 0, 6, [-5, -4, -3, -2, -1, 0] },
{ 0, 10, 11, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] },
{ 2.5, 3.5, 3, [2.5, 3.0, 3.5] },
{ 10, 20, 6, [10, 12, 14, 16, 18, 20] },
{ -2, 2, 5, [-2, -1, 0, 1, 2] },
};

[Theory]
[MemberData(nameof(LinspaceMaxInclusiveTestCases))]
public void LinspaceMaxInclusive(double beginInclusive, double endInclusive, int count, double[] expected)
{
var result = ArrayNumericUtils.LinspaceMaxInclusive(beginInclusive, endInclusive, count);
Assert.Equal(expected.Length, result.Length);
for (int i = 0; i < expected.Length; i++)
{
Assert.Equal(expected[i], result[i], 6); // Using 6 decimal places for floating point comparison
}

}
}
32 changes: 14 additions & 18 deletions src/SmartVectorDotNet/ArrayNumericUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,47 @@ public static class ArrayNumericUtils
/// Creates a new array which contains evenly spaced numbers over a specified interval.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="minInclusive"></param>
/// <param name="maxExclusive"></param>
/// <param name="beginInclusive"></param>
/// <param name="endExclusive"></param>
/// <param name="pointNum"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static T[] Linspace<T>(T minInclusive, T maxExclusive, int pointNum)
public static T[] Linspace<T>(T beginInclusive, T endExclusive, int pointNum)
where T : unmanaged
{
Guard.ValidArgument(OP.LessThanOrEqual(minInclusive, maxExclusive), "`minInclusive` must be less than or equals `maxExclusive`.");
Guard.NotNegative(pointNum);
Guard.ValidRange(pointNum > 0, $"{nameof(pointNum)} must be greater than 0.");

var stepNumAsT = OP.Convert<int, T>(pointNum);
var retval = new T[pointNum];
var step = OP.Divide(OP.Subtract(maxExclusive, minInclusive), stepNumAsT);
var step = OP.Divide(OP.Subtract(endExclusive, beginInclusive), stepNumAsT);
for(var i = 0; i < pointNum; ++i)
{
retval[i] = OP.Add(minInclusive, OP.Multiply(step, OP.Convert<int, T>(i)));
retval[i] = OP.Add(beginInclusive, OP.Multiply(step, OP.Convert<int, T>(i)));
}
return retval;
}


/// <summary>
/// Similar with <see cref="Linspace{T}(T, T, int)"/>, but the return value of this method contains <paramref name="maxInclusive"/>.
/// Similar with <see cref="Linspace{T}(T, T, int)"/>, but the return value of this method contains <paramref name="endInclusive"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="minInclusive"></param>
/// <param name="maxInclusive"></param>
/// <param name="beginInclusive"></param>
/// <param name="endInclusive"></param>
/// <param name="pointNum"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static T[] LinspaceMaxInclusive<T>(T minInclusive, T maxInclusive, int pointNum)
public static T[] LinspaceMaxInclusive<T>(T beginInclusive, T endInclusive, int pointNum)
where T : unmanaged
{
Guard.ValidArgument(OP.LessThanOrEqual(minInclusive, maxInclusive), "`minInclusive` must be less than or equals `maxInclusive`.");
Guard.NotNegative(pointNum);
Guard.ValidRange(pointNum > 1, $"{nameof(pointNum)} must be greater than 1.");

var stepNumAsT = OP.Convert<int, T>(Math.Max(pointNum - 1, 0));
var stepNumAsT = OP.Convert<int, T>(pointNum - 1);
var retval = new T[pointNum];
var step = OP.Divide(OP.Subtract(maxInclusive, minInclusive), stepNumAsT);
var step = OP.Divide(OP.Subtract(endInclusive, beginInclusive), stepNumAsT);
for (var i = 0; i < pointNum; ++i)
{
retval[i] = OP.Add(minInclusive, OP.Multiply(step, OP.Convert<int, T>(i)));
retval[i] = OP.Add(beginInclusive, OP.Multiply(step, OP.Convert<int, T>(i)));
}
return retval;
}
Expand Down