title: How can I add coordinate system / reference frame information to my data to avoid errors?
body: <p>Often times when dealing with vectors, reference frames are implicitly enforced through documentation, comments, or worse, (human) memory. For example, I want to compute the torque acting on a body moving with a given velocity from a plane due to drag (using a simple drag model):</p>
  
  <pre><code>torque = velocity.dot(normal) * position.cross(normal)
  </code></pre>
  
  <p>Here, a plane that is <code>position</code> away from the center of the body has normal <code>normal</code>. The body is moving at velocity <code>velocity</code>. The torque calculated will only be correct if all three quantities are w.r.t the same reference frame or coordinate system. If the quantities are obtained from different frames, then they will have to be converted before computing the torque:</p>
  
  <pre><code>velocity_A = B_to_A * velocity_source    # velocity comes in frame B
  position_A = C_to_A * position_source    # position comes in frame C
  torque_A = velocity_A.dot(normal_source) + ...  
  </code></pre>
  
  <p>This is tedious and error prone. I would like this information to explicitly tracked so that errors cannot occur:</p>
  
  <pre><code>A, B, C = Frame()
  B.conversion_to(B_to_A)                  # etc. do this ONCE
  
  velocity = Quantity(velocity_source, B)
  position = Quantity(position_source, C)
  normal = Quantity(normal_source, A)
  
  torque = velocity.dot(normal) * position.cross(normal)
  total_torque = torque + some_other_torque
  # do other computations similarly
  
  external_thing.send_data(total_torque.to(D)) # This expects torque in the D reference frame
  </code></pre>
  
  <p>Essentially, all of the conversions are gone and all the programmer needs to do is implement the math and proper computations. Internally, the framework will have freedom to choose how to compute most efficiently (using the least number of transforms). It can even avoid any computation unless values are needed outside of the framework to find optimizations, but the internals are not important.</p>
  
  <p>How can one achieve such an interface? I am familiar with Python's pint (<a href="https://pint.readthedocs.org/en/0.6/" rel="nofollow">https://pint.readthedocs.org/en/0.6/</a>) but it doesn't seem general enough to handle coordinate frames. C++ has Boost::units, but that also does not seem general enough. Ideally, the system would work with numpy arrays. I would like to avoid rewriting a vector library.</p>
  
  <p>I have attempted to implement something like this in Python, but it looks like this:</p>
  
  <pre><code>vel = Quantity(velocity_source, B)
  pos = Quantity(position_source, C)
  normal = Quantity(normal_source, A)
  computation = lambda vel, pos, normal: vel.dot(normal) * pos.cross(normal)
  torque = compute(computation, vel=vel, pos=pos, normal=normal)
  </code></pre>
  
  <p>This is not ideal because everything needs to be done using functions or lambdas. Ideally, the system would get out of your way, e.g. you can add two <code>Quantity</code>s together without knowing they are <code>Quantity</code>s.</p>
  
  <p>How can one best achieve such a framework? If the motivation isn't clear please let me know and I will clarify. This seems like something that would be very useful in any graphics or simulation engine, yet hours of searching have turned up nothing. Language doesn't particularly matter, I'm mostly looking for general ideas.</p>