cortex-js / compute-engine

An engine for symbolic manipulation and numeric evaluation of math formulas expressed with MathJSON
https://cortexjs.io
MIT License
375 stars 47 forks source link

[FEATURE] Support to Units #52

Closed victorvianaom closed 2 years ago

victorvianaom commented 2 years ago

I want extend the capabilities of Cortexjs Compute Engine to be able to properly handle measurement units, similar to how this project does: hurmet.app/examples.

That is, I want to evaluate expressions taking into account the units.

What is the most feasible way to achieve this?

stefnotch commented 2 years ago

Would standard dimensional analysis work?

As in: $\frac{1 m + 3cm}{3 hours}$

  1. extract all units from a formula: $\frac{m + m}{hours}$ (cm = 0.1 * m)
  2. do symbolic mathematics with just the units (treat them like variables): $\frac{m}{hours}$
  3. use the result $\frac{1 + 3 * 0.1}{3} \frac{m}{hours}$
michelmar commented 2 years ago

Really nice approch. We are going in this direction

victorvianaom commented 2 years ago

Very Good! Thank you for your reply. We'll try it! But how would you go about differentiating a symbol m (bound variables, free variables and constants) from the unit of length meter m. Because the calculations I have to perform with both are different. For exemple, I should convert all units to SI units and then perform calculation. Also I would like to be able to define symbol m and unit m (representing different entities) on the same expression. Is it possible, for instance, to define a symbol \operatorname{m}? to make that represent my unit of measure meter, and then define a symbol \operatorname{cm} giving it the value = $10^{-2} \cdot $\operatorname{m}. That is, can I in some way store all unit conversions in the Compute Engine context and at the same time differentiating variables from units?

stefnotch commented 2 years ago

I think such ambiguity would have to be handled when the user is inputting an equation, because I don't think think it can always be automatically resolved. As in, I don't think there is a clear cut way for mathlive to say "this m is definitely a variable, and that m is 100% a unit".

victorvianaom commented 2 years ago

It totally makes sense! Thank you so much @stefnotch for the help!