Skip to content

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?

hez2010 pushed a commit to hez2010/runtime that referenced this pull request Dec 14, 2024
* Fix TensorExtensions.StdDev

* Add constraint to ref

* Revert "Add constraint to ref"

This reverts commit f740f50.

* Revert "Fix TensorExtensions.StdDev"

This reverts commit c212984.

* Use pow method

* Use existing variable
@github-actions github-actions bot locked and limited conversation to collaborators Jan 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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