Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TensorExtensions.StdDev #110392

Merged
merged 6 commits into from
Dec 11, 2024
Merged

Conversation

lilinus
Copy link
Contributor

@lilinus lilinus commented Dec 4, 2024

Fixes #110381

I use IPowerFunctions<T>.Pow(x, 0.5) to replicate sqrt, but special case double and float to use Math.Sqrt and MathF.Sqrt respectively.

Would prefer to add IRootFunctions to method constraints to use T.Sqrt but I don't know if it is possible to add new constraints.

N.b. TensorExtensions is still marked as experimental

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Dec 4, 2024
@michaelgsharp michaelgsharp merged commit 9bededd into dotnet:main Dec 11, 2024
78 of 83 checks passed
{
return T.CreateChecked(Math.Sqrt(double.CreateChecked(variance)));
}
return T.Pow(variance, T.CreateChecked(0.5));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should indeed be updated to use IRootFunctions instead; not Pow which isn't guaranteed to behave the same in many important edge cases

@hez2010
Copy link
Contributor

hez2010 commented Dec 11, 2024

For floating point numbers Pow(0.5) is not the equivalent to Sqrt, so this could result in unexpected behavior for some custom types.
Considering TensorExtensions is still an experimental API, we can take the minor breaking change here to add an additional generic constraint IRootFunctions<T> and use T.Sqrt instead.
@lilinus Would you like to make a follow-up PR for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Numerics.Tensors community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TensorTests.cs and Tensor.cs StdDev calculates Variance rather than Standard deviations
4 participants