Comments (7)
I would recommend that you read through the hard sphere section of dimensioned-examples. It covers using dimensioned with vectors -- if you find anything confusing or lacking there, please open a ticket. I would be happy to try to make it more clear!
It is likely that you will have more luck by placing units on the outside of na::Vector3
, not the inside. (Which is discussed in the above mentioned examples).
That said, you can make that code compile by adding some trait bounds. The compiler tells you you need Debug
, Copy
, and PartialEq
for the output type (which together form na::Scalar
). Then, it will still give you some lifetime errors. These messages are less helpful, but can be resolved by bounding L
and R
by Copy
.
This code compiles:
fn dim_vector3<L, R>(unit_const: L, arr: [R; 3]) -> na::Vector3<dim::typenum::Prod<L, R>>
where
L: std::ops::Mul<R> + Copy,
R: Copy,
dim::typenum::Prod<L, R>: na::Scalar,
{
na::Vector3::new(
unit_const * arr[0],
unit_const * arr[1],
unit_const * arr[2],
)
}
from dimensioned.
And, for fun, you can clean it up a bit with the op!
macro:
#[macro_use] extern crate typenum;
fn dim_vector3<L, R>(unit_const: L, arr: [R; 3]) -> na::Vector3<op!(L*R)>
where
L: std::ops::Mul<R> + Copy,
R: Copy,
op!(L*R): na::Scalar,
{
na::Vector3::new(
unit_const * arr[0],
unit_const * arr[1],
unit_const * arr[2],
)
}
This reminds me that I need to make that macro 2018 edition compatible....
from dimensioned.
One of the things that makes combining dimensioned and nalgebra hard is that there are cases where the units of matrices are non-homogeneous. For example, if you have a 2D Point2<Meter<f64>>
and you wanted to translate and rotate it, you'd multiply an augmented vector by a 3x3 matrix. The units would look like this:
[ 1 1 m ] [ m ]
[ 1 1 m ] X [ m ]
[ 1/m 1/m 1 ] [ 1 ]
I've struggled in the past trying to put units inside nalgebra types, like na::Point2<si::Meter<f64>>
, so in most of my code I only use dimensioned for 1D problems. That said, I just tried putting units on the outside and the following worked. Maybe I'll revisit using nalgebra and dimensioned together in the future.
use dimensioned::si;
use nalgebra as na;
fn main() {
let position = si::Meter::new(na::Point2::new(2.0, 0.0));
let velocity = si::MeterPerSecond::new(na::Vector2::new(3.0, 1.0));
let time = 12.0 * si::S;
let rotation = si::Unitless::new(na::Rotation2::new(std::f64::consts::FRAC_PI_2));
println!("Position: {}", position);
println!("Velocity: {}", velocity);
println!("Time: {}", time);
println!("Rotation: {}", rotation);
println!("End position: {}", position + rotation * velocity * time);
}
from dimensioned.
Thank you both!
This was so helpful. I feel like I'm missing out on some of the implementation details surrounding dimensioned. I read through the examples, but found them a bit hard to follow. Perhaps once i understand this all better I can contribute some more docs for total newbies like me.
from dimensioned.
BTW if you just want a 3D vector type that works well with dimensioned, you could try
https://crates.io/crates/vector3d
Which I created for that purpose.
from dimensioned.
Perhaps once i understand this all better I can contribute some more docs for total newbies like me.
That would be great!
from dimensioned.
Related Issues (20)
- Add support for absolute values HOT 2
- Implement various functions on primitives.
- Use #[rustc_on_unimplemented] HOT 1
- travis fails to build HOT 2
- Re-add rustfmt and clippy to TravisCI build HOT 1
- Update `approx` dependency
- Validate and ideally repace uses of `mem::transmute`
- Typo in UCUM: MH20
- http://paholg.com/dimensioned/ is a 404 HOT 3
- Question: accepting generic dimension for calculation in specific units HOT 11
- Feature request: Celsius HOT 5
- support std::iter::Sum. HOT 1
- Any good idea how to integrate with `ordered_float`? HOT 3
- Use conventional letter case HOT 1
- Support f256 via the qd github repo HOT 15
- fmt String as Joules instead of MKS HOT 2
- has_units() -> bool HOT 2
- Implement `max` and `min` functions for primitive types that have them HOT 1
- `serde_test` should be dev-dependency
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dimensioned.