-
Notifications
You must be signed in to change notification settings - Fork 383
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
Recommended way to create 3 dimensional magnetic field? #780
Comments
Do I understand correctly that you would like to have a Consider this code:
Unfortunatelly the above doesn't work, because the generic type "T" (aka any type) does not have a defined + operator. There's no way to add a type constraint on "something that is a number". And declaring interfaces ( This works:
But of course, this comes with a performance penalty (I don't know how big it is) and the downside that problems will pop up only at runtime and not at compile time. |
Hi, UnitsNet does not currently support XYZ-dimensional quantities in a generic way. You can, however, easily add your own wrapper types for specific quantities. public struct MagneticField3
{
public MagneticField X { get; }
public MagneticField Y { get; }
public MagneticField Z { get; }
public MagneticField3(MagneticField x, MagneticField y, MagneticField z) { ... }
// Operator overloads for arithmetic
public static MagneticField3 operator +(MagneticField3 left, MagneticField3 right) { ... }
public static MagneticField3 operator -(MagneticField3 left, MagneticField3 right) { ... }
public static MagneticField3 operator *(double left, MagneticField3 right) { ... }
public static MagneticField3 operator /(MagneticField3 left, double right) { ... } Did that answer your question? |
@pgrawehr not necessarily, you could alternatively have MagneticField3 struct. Generally speaking the magnetic field definition talks about vectors and not scalars. I agree it's not ideal that above cannot be expressed too well in C# and generic vector would be a perfect solution. |
We could use the code waiting in #698 to do this in a generic way, and without a large performance hit. |
Sounds good, but would apparently also need the definition of the Vector and Matrix classes to be useful, right? A matrix multiplication would be a good test case for the performance impact. |
Honestly for units I only care about vector. Matrices are probably only used in more advanced physics (i.e. jacobian) which usually need super high performance in which case you'd likely write your own version anyway. Good idea for completeness but I'd personally skip it for now. @angularsen that should be fine for now although this is something I'd really love to see here in the future (although understand the limitations and why not yet) |
I created #801 as a POC. I assume you would expect to write something as follows: var length1X = Length.FromMeters(1.0);
var length1Y = Length.FromMeters(2.0);
var length1Z = Length.FromMeters(3.0);
var length2X = Length.FromMeters(4.0);
var length2Y = Length.FromMeters(5.0);
var length2Z = Length.FromMeters(6.0);
var vector1 = new Vector3<Length>(length1X, length1Y, length1Z);
var vector2 = new Vector3<Length>(length2X, length2Y, length2Z);
var result = vector1 + vector2;
var expectedX = Length.FromMeters(5.0);
var expectedY = Length.FromMeters(7.0);
var expectedZ = Length.FromMeters(9.0);
Assert.Equal( new Vector3<Length>( expectedX, expectedY, expectedZ ), result ); |
Nice start, and nice that this works with base types, too:
|
This doesn't make a lot of sense, but I just tried to do:
Not that I would have a meaningful usage for this, but this failed with
String does have a binary + operator, so this message confuses me a bit and I'm worried we're overlooking some meaningful use case here? |
String does not have a + operator 😃 It is syntactic sugar for the compiler to generate string.Concat. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@tmilnthorp yes, something similar would be perfect. At minimum I think:
Note that things like Normalize and similar could not have units anymore so they would have to return regular Vector3 |
Yea, that sounds good. The Vector is not limited to 3 dimensions though, right? |
@pgrawehr Vector in .NET is more about SIMD instructions. I currently don't see any larger short term benefits of having Matrix in IoT repo but Vector3 is being used quite a bit |
Right, IOT will not necessarily benefit, but having a generic matrix class could come in handy in other places. Due to the limits in the .NET generics system (for which we seem to have a good workaround now), this probably hasn't been done yet. |
This sparked my interest, since we're also using 3D vectors in our code (geometry related) What I don't quite understand, is the added benefit of adding a vector object to UnitsNet? That is what we're doing now too and on top, we are wrapping MathNet.Spatial Vector3D along with it:
|
@dschuermans Your implementation is not generic (but only for Lenght in this case). We want to be able to define |
@pgrawehr So my point remains: what's the benefit of adding a vector object to UnitsNet? As far as I understand Vectors, it is their values that matter and the result of mathematic operations on them.
So wouldn't a solution for this issue be that you have a unitless vector, yet with some functionality to convert it to a set of X, Y and Z values in a specific unit?
Output:
With a unitless vector, you can fallback onto libraries that specialize in this stuff (e.g. MathNet), instead of reinventing the wheel and creating your own Vector implementation again. |
The suggestion above could solve the original issue, that's true. I'll take a look at that MathNet library (I don't know that one), whether it otherwise supports what we need. However, I disagree that it's always length. I've used vectors at least for length, speed, orientation (angles), and force. Having a generic
|
Earlier in this issue there are several links to proof of concepts and proposals for a generic way to do arithmetic of quantities, which would be transferable to vectors of quantities. It seems doable, but someone needs to champion it and start a PR to drive that forward. Without this, there are today several feasible alternatives:
|
@angularsen Now that .net 6 supports generic math, maybe it is easier to do this kind of stuff? |
Well hello there Mr Krog :D Yes I think so. We touched on it in #977, but it's always hard to see the possibilities before trying a bit. I threw up #984 just to play with it.
Fortunately they have split that up into fine grained interfaces like I'm pretty sure we will make use of this when it leaves preview. |
cc: @tannergooding |
We're currently considering to use this library in https://github.com/dotnet/iot/ . We have initially started developing very similar APIs and then noticed this library so we decided to give it a try since it already solves many problems we were about to hit.
We have converted our Temperature/Pressure structs to this library without any issues (see dotnet/iot#1052) but now we're trying to use 3-dimensional magnetic field which is the output of IMU (i.e. BNO055 sensor). We haven't ourselves developed such unit yet and used Vector3.
What's the recommended way to achieve 3-dimensional magnetic field?
The text was updated successfully, but these errors were encountered: