15
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 => m
N m => J
J / W => s
A J / W => C
T m m => Wb
N / A m => T
V s / A => H
J S / F A => V
s / s s => Hz
Hz kg m Hz => N
Hz s / Ω => S
Wb / H => A
V Pa S s / C => Pa
N s / m Hz => kg
V A => W
s / Ω => F
J / A s A => Ω
Shortest code (in bytes) wins.
2
kg m / s s
would actually be kilogam-meter-seconds per second, or just kilogram-meters. Multiplication and division work LTR. What you are looking for iskg m / (s s)
. This also applies to the other examples. – LegionMammal978 – 2015-11-10T22:03:25.037@Legion: Not necessarily. Implicit multiplication and division with the slash are ambiguous; the ordr depends on convention. Here implicit multiplication is taken to have a higher precedence than division. – Deusovi – 2015-11-10T22:06:17.117
2...that breaks about all of mathematics. Implicit and explicit multiplication mean the exact same thing. – LegionMammal978 – 2015-11-10T22:09:01.077
@LegionMammal978 Not really. Does
1 / 2x
really meanx / 2
? – Ypnypn – 2015-11-10T22:12:20.960@Ypnypn No, it means
(1 / 2)x
orx / 2
. – LegionMammal978 – 2015-11-10T22:13:40.6535@LegionMammal978 - actually, 1/2x is common notation for 1/(2x). More generally, where it's not ambiguous, the slash would be interpreted as the dividing line between numerator and denominator. The convention being used here is fine - especially because this convention is standard in units. kg/ms means kg/(m*s) when written as a unit. Take it from a guy with a PhD in Maths. – Glen O – 2015-11-10T22:49:08.980
No Mathematica answer yet? Disappointed. – Kroltan – 2015-11-11T16:03:47.617
@Kroltan It can't understand the funny notation – LegionMammal978 – 2015-11-12T12:46:48.757
@LegionMammal978 Me neither, but it is hilarious at how many builtins for this sort of thing exist in the language :P – Kroltan – 2015-11-12T13:28:04.300