473,767 Members | 8,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SIunits-style dimension checking and vectors

Hi,

I'm using a homebrew, lightweight version of SIunits, the physical
dimension checker. For example I define types such as m (meters), s
(seconds) and mps (meters per second) in namespace SI and I have all
the overloadings of operator* such as

SI::m operator* (SI::s, SI::mps)
{ ... }

saying that a velocity multiplied by a time yields a distance.

On the other hand, before I had implemented the dimension checking, I
was using a generic three dimension vector type with an operator*

vec<T1> operator* (T2 a, vec<T1> b) { return vec<T1> (b.x * a, b.y *
a, b.z * a) ; }

Now of course if I cannot multiply a time by a vector of speeds and
automatically get a vector
of distances, since the template tries to instanciate

vec<SI::mps> operator* (SI::s a, vec<SI::mps> b)

which is not dimensionally correct.

I'm not familiar with the full-blown implementation of SIunits, even
though I use the same basic idea of a template parameterized by ints
giving the exponent of the unit in each dimension.
I don't know if their implementation allows an elegant implementation
of a generic vector class that would work transparently for normal
scalar types and for dimension-checked units, e.g.
that would implement

vec<SI::m> operator* (SI::s, vec<SI::mps>)

But since my problem looks quite common I thought someone might have
an idea of the most elegant way to do that.
Jul 22 '05 #1
1 1628
Lo?c Henry-Gr?ard wrote:
Hi,

I'm using a homebrew, lightweight version of SIunits, the physical
dimension checker. For example I define types such as m (meters), s
(seconds) and mps (meters per second) in namespace SI and I have all
the overloadings of operator* such as

SI::m operator* (SI::s, SI::mps)
{ ... }

saying that a velocity multiplied by a time yields a distance.

On the other hand, before I had implemented the dimension checking, I
was using a generic three dimension vector type with an operator*

vec<T1> operator* (T2 a, vec<T1> b) { return vec<T1> (b.x * a, b.y *
a, b.z * a) ; }

Now of course if I cannot multiply a time by a vector of speeds and
automatically get a vector
of distances, since the template tries to instanciate

vec<SI::mps> operator* (SI::s a, vec<SI::mps> b)

which is not dimensionally correct.

I'm not familiar with the full-blown implementation of SIunits, even
though I use the same basic idea of a template parameterized by ints
giving the exponent of the unit in each dimension.
I don't know if their implementation allows an elegant implementation
of a generic vector class that would work transparently for normal
scalar types and for dimension-checked units, e.g.
that would implement

vec<SI::m> operator* (SI::s, vec<SI::mps>)

But since my problem looks quite common I thought someone might have
an idea of the most elegant way to do that.


Well, the most straight-forward way would be to specialize each operator
for all possible argument types. A more sophisticated approach would be
to define a traits template, specialized for each possible pair of
argument types. I've posted code below to show the idea; this is
absurdly simple, and I'm not familiar with the "SIunits" package to
begin with, so don't think I'm implying this code is production-worthy.
:) It should at least compile, though, and that ought to prove the
point.

namespace SI
{
struct s { int value; s( int v =0 ): value( v ) { } };
struct m { int value; m( int v =0 ): value( v ) { } };
struct mps { int value; mps( int v =0 ): value( v ) { } };

template< typename T > struct vec
{
T x, y, z;

vec( T const& ax, T const& ay, T const& az ):
x( ax ), y( ay ), z( az ) { }
};

template< typename T, typename U > struct Traits { };
template< > struct Traits< s, mps > { typedef m Product_Type; };

template< typename T, typename U >
typename Traits< T, U >::Product_Ty pe
operator * ( T const& a, U const& b )
{
typename Traits< T, U >::Product_Ty pe result;
result.value = a.value * b.value;
return result;
}

template< typename T, typename U >
vec< typename Traits< T, U >::Product_Ty pe >
operator * ( T const& a, vec< U > const& b )
{
return vec< typename Traits< T, U >::Product_Ty pe >(
a * b.x, a * b.y, a * b.z );
}
}

int main( )
{
SI::s s;
SI::mps mps;
SI::vec< SI::mps > mpsvec( 3, 4, 5 );
SI::m m = s * mps;
SI::vec< SI::m > mvec = s * mpsvec;
}

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1823
by: Dan Sommers | last post by:
Hi, I have a class whose objects represent physical quantities including uncertainties and units, and I would like more control over the way they print. I have a __str__ method which outputs the quantity and its uncertainty, properly rounded (i.e. the uncertainty to one digit and the quantity to the same precision as the uncertainty), followed by the units (mostly) as they accumulated during whatever calculations led to the quantity's
14
13031
by: David Fisher | last post by:
The most common sizes of integer types seem to be: 8 bits - signed char 16 bits - short 16 or 32 bits - int 32 or 64 bits - long Question #1: Does anyone know how common sizes other than these are ? I know that the
2
1828
by: kwikius | last post by:
On May 30, 10:18 am, James Kanze <james.ka...@gmail.comwrote: The duration type is pretty much of an exact replica of the quan quantity. But..... From the text the impression one gets is that the design is basically that of Jeff Garland boost date time lib, with some vague reference to Walter Brown, presumably SI units library and implementation by
0
9575
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9960
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7384
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.