Dimensional Analysis Calculate a product or ratio of SI units.

For example, kg m / s s (kilogram-meter per second squared) should return N (newton).

The input will always be either:


A list of symbols for SI units, space-separated (representing a product) or
The above, /, and the above (representing a ratio).


The input will never contain any other characters (such as numeric literals or other punctuation).

You may assume that this will always equal a single SI unit.

Use the following symbols:

Base quantities:
s               # second
m               # meter
kg              # kilogram
A               # ampere

Derived quantities:
N  = kg m / s s # newton
J  = N m        # joule
W  = J / s      # watt
Hz = W / J      # hertz
Pa = N / m m    # pascal
C  = s A        # coulomb
V  = J / C      # volt
F  = C / V      # farad
Ω  = V / A      # ohm      (you may use the O symbol instead, for a penalty of +3 bytes)
S  = A / V      # siemens
Wb = J / A      # weber
T  = Wb / m m   # tesla
H  = Wb / A     # henry


Examples:

m            =&gt; m
N m          =&gt; J
J / W        =&gt; s
A J / W      =&gt; C
T m m        =&gt; Wb
N / A m      =&gt; T
V s / A      =&gt; H
J S / F A    =&gt; V
s / s s      =&gt; Hz
Hz kg m Hz   =&gt; N
Hz s / Ω     =&gt; S
Wb / H       =&gt; A
V Pa S s / C =&gt; Pa
N s / m Hz   =&gt; kg
V A          =&gt; W
s / Ω        =&gt; F
J / A s A    =&gt; Ω


Shortest code (in bytes) wins.
CJam, 184 105 98 96 bytes

00000000: 22 73 20 6d 20 6b 67 41 20 4e 20 4a 20 57 20 48  "s m kgA N J W H     
00000010: 7a 50 61 43 20 56 20 46 20 ce a9 53 20 57 62 54  zPaC V F ..S WbT     
00000020: 20 48 22 32 2f 53 66 2d 22 d6 9c 64 c6 a1 24 a4   H"2/Sf-"..d..$.     
00000030: 4b f9 1c 4a 57 f4 61 79 31 ed 82 34 22 33 31 38  K..JW.ay1..4"318     
00000040: 62 35 62 32 66 6d 34 2f 6c 53 25 32 24 32 24 65  b5b2fm4/lS%2$2$e
00000050: 72 22 2f 22 61 2f 3a 3a 2e 2b 3a 2e 2d 61 23 3d  r"/"a/::.+:.-a#=


The above is a hexdump; it can be reversed with xxd -r.

Verify all test cases at once in the CJam interpreter. 1

Test run

$ LANG=en_US
$ cjam si.cjam &lt;&lt;&lt; 'Hz s / Ω'; echo
S
$ cjam si.cjam &lt;&lt;&lt; 'J / A s A'; echo
Ω


Idea

We can encode each unit u = sa mb kgc Ad as the vector tu = (a + c - d, b, c, d). 2

This way, for the input u1 &hellip; un / v1 &hellip; vm, we only have to calculate (tu1 + &hellip; + tun) - (tv1 + &hellip; + tvm) and check which unit it corresponds to.

Code

"s m kgA N J W HzPaC V F Î©S WbT H"

2/     e# Split the string into chunks of length 2.
Sf-    e# Remove all spaces (if any) from each chunk.

"ÖdÆ¡$¤KùJWôay1í4"

318b   e# Convert from base 318 to integer.
5b     e# Convert from integer to base 5.
2fm    e# Subtract 2 from each base-5 digit.
4/     e# Split into chunks of length 4.
lS%    e# Read one line of input sand split it at spaces.
2$2$   e# Copy the unit names and the vector table.
er     e# Perform transliteration.
"/"a/  e# Split at "/".
::.+   e# Add the vectors of numerator and denominator (if any).
:.-    e# Subtract the resulting sums (or leave a single sum untouched).
a#     e# Find the index of the resulting vector in the vector table.
=      e# Retrieve the corresponding unit.




1 Note that, due to limitations of the online interpreter, I cannot use a different encoding for source code and I/O. Therefore, the symbol Ω shows up as its UTF-8 encoding (Î©). The official Java interpreter does not share this limitation.
2 This saves a few bytes over the straightforward mapping to (a, b, c, d), since it shortens the range of the first coordinate.