Changeset 697
- Timestamp:
- 01/29/10 02:19:16 (2 years ago)
- Files:
-
- trunk/units/si2.d (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/units/si2.d
r690 r697 14 14 import rational; 15 15 16 template T(Tp...){alias Tp T;} 17 16 18 version(FullSI) 17 19 { 18 template SI(int LengthN, int MassN, int TimeN, int TempN, int CurrentN, int MoleN, int LumiN, T = real) 19 { 20 alias SIB!(LengthN, 1, MassN, 1, TimeN, 1, TempN, 1, CurrentN, 1, MoleN, 1, LumiN, 1, T) SI; 20 21 private alias T!(0,0) extra; 22 private alias T!(0,1,0,1) extra2; 23 24 template Batch( 25 int LengthN, int LengthD, 26 int MassN, int MassD, 27 int TimeN, int TimeD, 28 int TempN, int TempD, 29 int CurrentN, int CurrentD, 30 int LumiN, int LumiD, 31 int MoleN, int MoleD 32 ) 33 { 34 // sanity check 35 static assert (Reduce!(LengthN, LengthD).Reduced, "Length must be in reduced form"); 36 static assert (Reduce!(MassN, MassD).Reduced, "Mass must be in reduced form"); 37 static assert (Reduce!(TimeN, TimeD).Reduced, "Time must be in reduced form"); 38 static assert (Reduce!(TempN, TempD).Reduced, "Temp must be in reduced form"); 39 static assert (Reduce!(CurrentN, CurrentD).Reduced,"Current must be in reduced form"); 40 static assert (Reduce!(LumiN, LumiD).Reduced, "Luminous intensity must be in reduced form"); 41 static assert (Reduce!(MoleN, MoleD).Reduced, "Quantity must be in reduced form"); 42 43 /// visable units 44 enum LenN = LengthN, LenD = LengthD; 45 enum MasN = MassN, MasD = MassD; /// 46 enum TimN = TimeN, TimD = TimeD; /// 47 enum TmpN = TempN, TmpD = TempD; /// 48 enum CurN = CurrentN, CurD = CurrentD; /// 49 enum LumN = LumiN, LumD = LumiD; /// 50 enum MolN = MoleN, MolD = MoleD; /// 51 } 52 53 template SI(int LengthN, int MassN, int TimeN, int TempN, int CurrentN, int LumiN, int MoleN, T = real) 54 { 55 alias SIB!(Batch!( 56 /// visable units 57 LengthN, 1, 58 MassN, 1, 59 TimeN, 1, 60 TempN, 1, 61 CurrentN, 1, 62 LumiN, 1, 63 MoleN, 1 64 ), T) SI; 21 65 } 22 66 template SI( … … 26 70 int TempN, int TempD, 27 71 int CurrentN, int CurrentD, 72 int LuminN, int LuminD, 28 73 int MoleN, int MoleD, 29 int LuminN, int LuminD,30 74 T = real 31 75 ) 32 76 { 33 alias SIB!( LengthN, LengthD, MassN, MassD, TimeN, TimeD, TempN, TempD, CurrentN, CurrentD, MoleN, MoleD, LuminN, LuminD, T ) SI; 77 alias SIB!(Batch!( 78 /// visable units 79 LengthN, LengthD, 80 MassN, MassD, 81 TimeN, TimeD, 82 TempN, TempD, 83 CurrentN, CurrentD, 84 LuminN, LuminD, 85 MoleN, MoleD 86 ), T ) SI; 34 87 } 35 88 } 36 89 else 37 90 { 91 private alias T!() extra; 92 private alias T!() extra2; 38 93 template Batch( 39 94 int LengthN, int LengthD, … … 103 158 static if(Exp.CurD == 1) private enum sCur = Exp.CurN.stringof; else private enum sCur = Exp.CurN.stringof~"/"~Exp.CurD.stringof; 104 159 /// The type name 105 public enum Stringof = "SI!("~sLen~","~sMas~","~sTim~","~sTmp~","~sCur~","~U.stringof~")"; 160 version(FullSI) 161 { 162 static if(Exp.LumD == 1) private enum sLum = Exp.LumN.stringof; else private enum sLum = Exp.LumN.stringof~"/"~Exp.LumD.stringof; 163 static if(Exp.MolD == 1) private enum sMol = Exp.MolN.stringof; else private enum sMol = Exp.MolN.stringof~"/"~Exp.MolD.stringof; 164 public enum Stringof = "SI!("~sLen~","~sMas~","~sTim~","~sTmp~","~sCur~","~sLum~","~sMol~","~U.stringof~")"; 165 } 166 else public enum Stringof = "SI!("~sLen~","~sMas~","~sTim~","~sTmp~","~sCur~","~U.stringof~")"; 106 167 107 168 /// the value … … 114 175 static assert(is(SIB!(Exp,U) == This)); 115 176 116 template Using(V) 117 { 118 alias SIB!(Exp,V) Using; 119 } 120 177 template Using(V) { alias SIB!(Exp,V) Using; } 178 179 template Alike2(V) 180 { 181 version(FullSI) enum bool Alike2 = 182 Exp.LumN == V.Exp.LumN && Exp.LumD == V.Exp.LumD && 183 Exp.MolN == V.Exp.MolN && Exp.MolD == V.Exp.MolD; 184 else enum bool Alike2 = true; 185 } 121 186 template Alike(V) 122 187 { 123 enum bool Alike = 188 enum bool Alike = Alike2!(V) && 124 189 Exp.LenN == V.Exp.LenN && Exp.LenD == V.Exp.LenD && 125 190 Exp.MasN == V.Exp.MasN && Exp.MasD == V.Exp.MasD && … … 131 196 132 197 /// find the type of "This * T" 198 private template Mul2(U) 199 { 200 version(FullSI) alias T!( 201 Reduce!(Exp.LumN*U.Exp.LumD + Exp.LumD*U.Exp.LumN, Exp.LumD*U.Exp.LumD).V, 202 Reduce!(Exp.MolN*U.Exp.MolD + Exp.MolD*U.Exp.MolN, Exp.MolD*U.Exp.MolD).V) Mul2; 203 else alias T!() Mul2; 204 } 133 205 private template Mul(T) 134 206 { … … 141 213 Reduce!(Exp.TimN*T.Exp.TimD + Exp.TimD*T.Exp.TimN, Exp.TimD*T.Exp.TimD).V, 142 214 Reduce!(Exp.TmpN*T.Exp.TmpD + Exp.TmpD*T.Exp.TmpN, Exp.TmpD*T.Exp.TmpD).V, 143 Reduce!(Exp.CurN*T.Exp.CurD + Exp.CurD*T.Exp.CurN, Exp.CurD*T.Exp.CurD).V), 215 Reduce!(Exp.CurN*T.Exp.CurD + Exp.CurD*T.Exp.CurN, Exp.CurD*T.Exp.CurD).V, 216 Mul2!(T)), 144 217 U 145 218 ) Mul; … … 147 220 148 221 /// find the type of "This / T" 149 public template Div(T) 222 private template Div2(U) 223 { 224 version(FullSI) alias T!( 225 Reduce!(Exp.LumN*U.Exp.LumD - Exp.LumD*U.Exp.LumN, Exp.LumD*U.Exp.LumD).V, 226 Reduce!(Exp.MolN*U.Exp.MolD - Exp.MolD*U.Exp.MolN, Exp.MolD*U.Exp.MolD).V) Div2; 227 else alias T!() Div2; 228 } 229 private template Div(T) 150 230 { 151 231 static if(is(T : U)) … … 157 237 Reduce!(Exp.TimN*T.Exp.TimD - Exp.TimD*T.Exp.TimN, Exp.TimD*T.Exp.TimD).V, 158 238 Reduce!(Exp.TmpN*T.Exp.TmpD - Exp.TmpD*T.Exp.TmpN, Exp.TmpD*T.Exp.TmpD).V, 159 Reduce!(Exp.CurN*T.Exp.CurD - Exp.CurD*T.Exp.CurN, Exp.CurD*T.Exp.CurD).V), 239 Reduce!(Exp.CurN*T.Exp.CurD - Exp.CurD*T.Exp.CurN, Exp.CurD*T.Exp.CurD).V, 240 Div2!(T)), 160 241 U 161 242 ) Div; … … 163 244 164 245 /// find the type of "This ^ 1/i" 165 static public template RootT(int i) 246 private template Root2(int i) 247 { 248 version(FullSI) alias T!( 249 Reduce!(Exp.LumN,Exp.LumD*i).V, 250 Reduce!(Exp.MolN,Exp.MolD*i).V) Root2; 251 else alias T!() Root2; 252 } 253 private static template RootT(int i) 166 254 { 167 255 alias SIB!(Batch!( … … 170 258 Reduce!(Exp.TimN, Exp.TimD*i).V, 171 259 Reduce!(Exp.TmpN, Exp.TmpD*i).V, 172 Reduce!(Exp.CurN, Exp.CurD*i).V 260 Reduce!(Exp.CurN, Exp.CurD*i).V, 261 Root2!(i) 173 262 ), 174 263 U) RootT; … … 176 265 177 266 /// find the type of "This ^ i" 178 public template Power(int i) 267 private template Power2(int i) 268 { 269 version(FullSI) alias T!( 270 Reduce!(Exp.LumN*i,Exp.LumD).V, 271 Reduce!(Exp.MolN*i,Exp.MolD).V) Power2; 272 else alias T!() Power2; 273 } 274 private template Power(int i) 179 275 { 180 276 alias SIB!(Batch!( … … 183 279 Reduce!(Exp.TimN*i, Exp.TimD).V, 184 280 Reduce!(Exp.TmpN*i, Exp.TmpD).V, 185 Reduce!(Exp.CurN*i, Exp.CurD).V 281 Reduce!(Exp.CurN*i, Exp.CurD).V, 282 Power2!(i) 186 283 ), 187 284 U) Power; 188 285 } 189 286 190 // I don ;t think I need this anymore287 // I don't think I need this anymore 191 288 //This opCall(This ret) { return ret; } 192 289 … … 225 322 auto opDiv_r(U that) { return Power!(-1)(that / this.value); } /// 226 323 227 static if(Exp.LenN == 0 && Exp.MasN == 0 && Exp.TimN == 0 && Exp.TmpN == 0 && Exp.CurN == 0) 324 version(FullSI) private enum unitless2 = Exp.LumN == 0 && Exp.MolN == 0; 325 else private enum unitless2 = true; 326 static if(Exp.LenN == 0 && Exp.MasN == 0 && Exp.TimN == 0 && Exp.TmpN == 0 && Exp.CurN == 0 && unitless2) 228 327 alias value this; 229 328 else … … 286 385 public struct Types 287 386 { 288 alias SI!( 0, 0, 0, 0, 0) Value; /// 289 alias SI!( 1, 0, 0, 0, 0) Distance; /// 290 alias SI!( 0, 1, 0, 0, 0) Mass; /// 291 alias SI!( 0, 0, 1, 0, 0) Time; /// 292 alias SI!( 0, 0, 0, 1, 0) Temp; /// 293 alias SI!( 0, 0, 0, 0, 1) Current; /// 294 295 alias SI!(-3, 1, 0, 0, 0) MassDensity; /// 296 297 alias SI!(-2,-1, 3, 0, 2) Resistance; /// 298 alias SI!(-2,-1, 4, 0, 2) Capacitance; /// 299 300 alias SI!(-1, 1,-2, 0, 0) Pressure; /// 301 alias SI!(-1, 1,-1, 0, 0) Viscosity; /// 302 303 alias SI!( 0, 0,-1, 0, 0) Frequency; /// 304 alias SI!( 0, 0, 1, 0, 1) Charge; /// 305 alias SI!( 0, 1,-1, 0, 0) MassFlow; /// 306 alias SI!( 0, 1,-2, 0,-1) MagneticFluxD;/// 307 308 alias SI!( 1, 1,-2, 0, 0) Force; /// 309 alias SI!( 1, 0,-2, 0, 0) Acceleration; /// 310 alias SI!( 1, 0,-1, 0, 0) Speed; /// 311 312 313 alias SI!( 2, 1,-3, 0,-1) Voltage; /// 314 alias SI!( 2, 1,-3, 0, 0) Power; /// 315 alias SI!( 2, 1,-2, 0,-2) Inductance; /// 316 alias SI!( 2, 1,-2, 0, 0) Energy; /// 317 alias SI!( 2, 0, 0, 0, 0) Area; /// 318 alias SI!( 2, 0,-1, 0, 0) KViscosity; /// 319 320 alias SI!( 3, 0,-1, 0, 0) VolumeFlow; /// 321 alias SI!( 3, 0, 0, 0, 0) Volume; /// 387 alias SI!( 0, 0, 0, 0, 0, extra) Value; /// 388 alias SI!( 1, 0, 0, 0, 0, extra) Distance; /// 389 alias SI!( 0, 1, 0, 0, 0, extra) Mass; /// 390 alias SI!( 0, 0, 1, 0, 0, extra) Time; /// 391 alias SI!( 0, 0, 0, 1, 0, extra) Temp; /// 392 alias SI!( 0, 0, 0, 0, 1, extra) Current; /// 393 version(FullSI) 394 { 395 alias SI!( 0, 0, 0, 0, 0, 1, 0) Luminosity; /// 396 alias SI!( 0, 0, 0, 0, 0, 0, 1) Quantity; /// 397 } 398 399 alias SI!(-3, 1, 0, 0, 0, extra) MassDensity; /// 400 401 alias SI!(-2,-1, 3, 0, 2, extra) Resistance; /// 402 alias SI!(-2,-1, 4, 0, 2, extra) Capacitance; /// 403 404 alias SI!(-1, 1,-2, 0, 0, extra) Pressure; /// 405 alias SI!(-1, 1,-1, 0, 0, extra) Viscosity; /// 406 407 alias SI!( 0, 0,-1, 0, 0, extra) Frequency; /// 408 alias SI!( 0, 0, 1, 0, 1, extra) Charge; /// 409 alias SI!( 0, 1,-1, 0, 0, extra) MassFlow; /// 410 alias SI!( 0, 1,-2, 0,-1, extra) MagneticFluxD; /// 411 412 alias SI!( 1, 1,-2, 0, 0, extra) Force; /// 413 alias SI!( 1, 0,-2, 0, 0, extra) Acceleration; /// 414 alias SI!( 1, 0,-1, 0, 0, extra) Speed; /// 415 416 417 alias SI!( 2, 1,-3, 0,-1, extra) Voltage; /// 418 alias SI!( 2, 1,-3, 0, 0, extra) Power; /// 419 alias SI!( 2, 1,-2, 0,-2, extra) Inductance; /// 420 alias SI!( 2, 1,-2, 0, 0, extra) Energy; /// 421 alias SI!( 2, 0, 0, 0, 0, extra) Area; /// 422 alias SI!( 2, 0,-1, 0, 0, extra) KViscosity; /// 423 424 alias SI!( 3, 0,-1, 0, 0, extra) VolumeFlow; /// 425 alias SI!( 3, 0, 0, 0, 0, extra) Volume; /// 322 426 } 323 427 … … 328 432 public struct Thermal 329 433 { 330 alias SI!( 2, 0,-2,-1, 0 ) ThermalCap; ///331 alias SI!( 1, 1,-3,-1, 0 ) ThermalCond; ///332 alias SI!( 0, 1,-3,-1, 0 ) ConvectionCoeff; ///434 alias SI!( 2, 0,-2,-1, 0, extra) ThermalCap; /// 435 alias SI!( 1, 1,-3,-1, 0, extra) ThermalCond; /// 436 alias SI!( 0, 1,-3,-1, 0, extra) ConvectionCoeff; /// 333 437 } 334 438 /// 335 439 public struct FracturMechanics 336 440 { 337 alias SI!(-1,2, 1,1,-2,1, 0,1, 0,1 ) StressIntensity;///441 alias SI!(-1,2, 1,1,-2,1, 0,1, 0,1, extra2) StressIntensity;/// 338 442 } 339 443 } … … 346 450 template Unit(string name : "kelvin") { enum mul = 1.0; alias Types.Temp type; } /// 347 451 template Unit(string name : "ampere") { enum mul = 1.0; alias Types.Current type; } /// 452 version(FullSI) 453 { 454 //template Unit(string name : "candela") { enum mul = 1.0; alias Types.Luminosity type; } /// This one is named wrong 455 template Unit(string name : "mole") { enum mul = 1.0; alias Types.Quantity type; } /// 456 } 348 457 349 458 /// Distance Unit Types … … 465 574 template Alt(string name : "Wb") { enum Alt = "weber"; } 466 575 template Alt(string name : "T") { enum Alt = "tesla"; } 576 version(FullSI) 577 { 578 template Alt(string name : "mol"){ enum Alt = "mole"; } 579 template Alt(string name : "cd") { enum Alt = "candela"; } 580 } 467 581 468 582 public: … … 475 589 writef("%s\n", OfType.foot(3.0).meter); 476 590 477 SI!(1,1, 0,1, 0,1, 0,1, 0,1)v; //pragma(msg,"v:\t"~typeof(v).stringof);591 Types.Distance v; //pragma(msg,"v:\t"~typeof(v).stringof); 478 592 479 593 auto v2 = v * v; //pragma(msg,"v2:\t"~typeof(v2).stringof);
