00001
00002
00003
00004
00005 import OpenList;
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 public class Quantity
00017 {
00018
00019
00020
00021
00022 static final String multiplyOp = "*";
00023
00024
00025
00026
00027
00028
00029 static final String divideOp = "/";
00030
00031
00032
00033
00034
00035
00036 static final String powerOp = "^";
00037
00038
00039
00040
00041
00042
00043 static OpenList nil = OpenList.nil;
00044
00045
00046
00047
00048
00049
00050 final static Quantity one = new Quantity(1.0f, nil, nil);
00051
00052
00053
00054
00055 private float Factor;
00056
00057
00058
00059
00060 private OpenList Num;
00061
00062
00063
00064
00065 private OpenList Denom;
00066
00067
00068
00069
00070
00071
00072 Quantity(float Factor, OpenList Num, OpenList Denom)
00073 {
00074 this.Factor = Factor;
00075 this.Num = Num;
00076 this.Denom = Denom;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 float getFactor() { return Factor; }
00086
00087
00088
00089
00090
00091
00092
00093 OpenList getNum() { return Num; }
00094
00095
00096
00097
00098
00099
00100
00101 OpenList getDenom() { return Denom; }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 public static Quantity makeQuantity(Object tree)
00112 {
00113
00114
00115
00116 if( tree instanceof Number )
00117 {
00118 return new Quantity(((Number)tree).floatValue(), nil, nil);
00119 }
00120
00121
00122
00123
00124
00125 if( tree instanceof String )
00126 {
00127 return new Quantity(1.0f, list(tree), nil);
00128 }
00129
00130
00131
00132
00133
00134 if( tree instanceof OpenList )
00135 {
00136 OpenList treeAsList = (OpenList)tree;
00137
00138 String operator = (String)treeAsList.first();
00139 Object leftSubtree = treeAsList.second();
00140 Object rightSubtree = treeAsList.third();
00141
00142 if( operator.equals(multiplyOp) )
00143 {
00144 return multiply(makeQuantity(leftSubtree), makeQuantity(rightSubtree));
00145 }
00146
00147 if( operator.equals(divideOp) )
00148 {
00149 return divide(makeQuantity(leftSubtree), makeQuantity(rightSubtree));
00150 }
00151
00152 if( operator.equals(powerOp) )
00153 {
00154 return raise(makeQuantity(leftSubtree),
00155 ((Integer)rightSubtree).intValue());
00156 }
00157 }
00158
00159 return null;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 static Quantity raise(Quantity quantity, int power)
00172 {
00173 if( power == 0 )
00174 return one;
00175
00176 if( power > 0 )
00177 return multiply(quantity, raise(quantity, power-1));
00178 else
00179 return divide(quantity, raise(quantity, power+1));
00180 }
00181
00182
00183
00184
00185
00186
00187 static OpenList cons(Object F, OpenList R) { return OpenList.cons(F, R); }
00188
00189
00190
00191
00192
00193
00194 static OpenList list(Object X1) { return OpenList.list(X1); }
00195
00196
00197
00198
00199
00200
00201 static OpenList list(Object X1, Object X2) { return OpenList.list(X1, X2); }
00202
00203
00204
00205
00206
00207
00208 static OpenList list(Object X1, Object X2, Object X3)
00209 { return OpenList.list(X1, X2, X3); }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 static Quantity simplify(Quantity Q)
00221 {
00222 return new Quantity(Q.getFactor(),
00223 cancelFrom(Q.getNum(), Q.getDenom()),
00224 cancelFrom(Q.getDenom(), Q.getNum()));
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 static OpenList cancelFrom(OpenList L, OpenList M)
00250 {
00251 if( L.isEmpty() ) return nil;
00252 if( M.isEmpty() ) return L;
00253
00254 String A = (String)L.first();
00255 String B = (String)M.first();
00256
00257 if( A.equals(B) ) return cancelFrom(L.rest(), M.rest());
00258
00259 if( A.compareTo(B) < 0 ) return cons(A, cancelFrom(L.rest(), M));
00260 else return cancelFrom(L, M.rest());
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 static Quantity multiply(Quantity Q1, Quantity Q2)
00278 {
00279 return simplify(new Quantity(Q1.getFactor() * Q2.getFactor(),
00280 OpenList.merge(Q1.getNum(), Q2.getNum()),
00281 OpenList.merge(Q1.getDenom(), Q2.getDenom())));
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 static Quantity divide(Quantity Q1, Quantity Q2)
00299 {
00300 return simplify(new Quantity(Q1.getFactor() / Q2.getFactor(),
00301 OpenList.merge(Q1.getNum(), Q2.getDenom()),
00302 OpenList.merge(Q1.getDenom(), Q2.getNum())));
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 static Quantity invert(Quantity Q)
00314 {
00315 return divide(one, Q);
00316 }
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 static Quantity norm(Quantity Q, OpenList DB)
00334 {
00335 return multiply(new Quantity(Q.getFactor(), nil, nil),
00336 divide(normalizeAll(Q.getNum(), DB),
00337 normalizeAll(Q.getDenom(), DB)));
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 static Quantity normalizeAll(OpenList L, OpenList DB)
00353 {
00354 return multiplyAll(L.map(new NormalizeUnit(DB)));
00355 }
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 static Quantity normalizeUnit(String Unit, OpenList DB)
00375 {
00376 OpenList Found = OpenList.assoc(Unit, DB);
00377
00378 if( Found.isEmpty() )
00379 {
00380 return new Quantity(1.0f, OpenList.list(Unit), nil);
00381 }
00382 else
00383 {
00384 return norm((Quantity)(Found.second()), DB);
00385 }
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 static Quantity multiplyAll(OpenList L)
00401 {
00402 return (Quantity)L.reduce(new Multiply(), one);
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 static Quantity convert(Quantity A, Quantity B, OpenList DB)
00424 {
00425 return norm(divide(A, B), DB);
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435 public String toString()
00436 {
00437 if( Denom.isEmpty() )
00438 {
00439 if( Num.isEmpty() )
00440 {
00441 return "" + Factor;
00442 }
00443 else
00444 {
00445 return Factor + " " + Num.toString("", " ", "");
00446 }
00447 }
00448 else
00449 {
00450 if( Num.isEmpty() )
00451 {
00452 return Factor + "/" + Denom.toString("", " ", "");;
00453 }
00454 else
00455 {
00456 return Factor + " "
00457 + Num.toString("", " ", "")
00458 + "/" + Denom.toString("", " ", "");
00459 }
00460 }
00461 }
00462
00463 }