[racket] Numbers with dimensions

From: Richard Cleis (rcleis at me.com)
Date: Sat Oct 26 12:14:28 EDT 2013

A few years ago, my approach (which didn't get very far) was to internally maintain values in some chosen internal units; this input conversion occurred once, when variables were defined or otherwise introduced into the program. The conversion to the user-desired units then took place once via functions of the value and only the user units. The goal was to have no computational overhead during the computations, and to only need conversion between the user and internal units when data is introduced or extracted.

I stopped when I had the feeling that this subject has been studied much over the decades, so I needed to engage in the research before writing a few dozen pages (at least) of software.

rac


On Oct 26, 2013, at 9:48 AM, Matthias Felleisen wrote:

> 
> The last line applies to my thoughts not yours. 
> 
> As someone else indicated, I think we should experiment with 'dimension' (distance, time) vs 'units' (meters vs yards, seconds vs hours). This separation injects a hierarchy that could be useful. 
> 
> I'd really like to see some experimentation here because people haven't explored the space much and Racket could help explore a #lang metric/racket and even #lang typed/metric/racket. 
> 
> -- Matthias
> 
> 
> 
> 
> 
> On Oct 26, 2013, at 11:36 AM, Matthias Felleisen wrote:
> 
>> 
>> 1. I would hope that some generics might help here. 
>> 2. I see a need for struct mixins here because meter isn't a refinement per se but some 'attribute'. Then you could mixin several different units and I may have both m and m-1. 
>> 
>> Data representation not fully thought thru. -- Matthias
>> 
>> 
>> 
>> 
>> 
>> On Oct 26, 2013, at 11:26 AM, Laurent wrote:
>> 
>>> How would you represents quantities like 2 kg.m^2/s^-2 with that?
>>> And how would you convert from mi/h to m/s?
>>> 
>>> Anyway, I've started adding in some converters:
>>> https://github.com/Metaxal/measures/blob/master/converters.rkt
>>> 
>>> Some more to come, but I may not be able to work on it for very long for now.
>>> 
>>> Laurent
>>> 
>>> 
>>> On Sat, Oct 26, 2013 at 5:05 PM, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>>> 
>>> 
>>> Wouldn't we want something like this: 
>>> 
>>> #lang racket
>>> 
>>> (module+ test (require rackunit))
>>> 
>>> (struct distance (value) #:transparent) ;; this should be abstract 
>>> (struct yard distance () #:transparent)
>>> (struct meter distance () #:transparent)
>>> 
>>> ;; distance distance -> distance 
>>> 
>>> (module+ test
>>>   (check-equal? (d+ (yard 1) (yard 2)) (yard 3))
>>>   (check-equal? (d+ (yard 1) (meter 2)) (meter (+ .9 2)))
>>>   (check-equal? (d+ (meter 1.8) (yard 1)) (yard (+ 2.0 1)))
>>>   (check-equal? (d+ (meter 2) (meter 1)) (meter 3)))
>>> 
>>> (define (d+ d1 d2) 
>>>   (match (list d1 d2)
>>>     [(list (struct yard (value)) (struct yard (value2))) (yard (+ value value2))]
>>>     [(list (struct yard (value)) (struct meter (value2))) (meter (+ (yard->meter value) value2))]
>>>     [(list (struct meter (value2)) (struct yard (value))) (yard (+ (meter->yard value2) value))]
>>>     [(list (struct meter (value)) (struct meter (value2))) (meter (+ value value2))]))
>>> 
>>> ;; distance distance-constructor -> distance 
>>> 
>>> (module+ test 
>>>   (check-equal? (conversion (yard 3) meter) (meter (* 3 .9))))
>>> 
>>> (define (conversion x d)
>>>   (match x 
>>>     [(struct yard (y)) (if (eq? yard d) x (meter (yard->meter y)))]
>>>     [(struct meter (y)) (if (eq? yard d) (yard (meter->yard y)) x)]))
>>> 
>>> 
>>> ;; auxiliaries 
>>> (define (yard->meter v) (* .9 v))
>>> (define (meter->yard v) (/ v .9))
>>> 
>>> -- Matthias
>>> 
>>> 
>>> 
>>> On Oct 26, 2013, at 7:20 AM, Laurent wrote:
>>> 
>>>> Ok, so I just hacked together a small lib for handling numbers with unit symbols and exponents:
>>>> 
>>>> Quick example:
>>>> > (measure->value
>>>>    (m* '(18 s) 
>>>>        '(1600 km (h -1))
>>>>        '(1000 m (km -1))
>>>>        '(1/3600 h (s -1))))
>>>> '(8000 m)
>>>> 
>>>> You can get it with:
>>>> $ raco pkg install measures
>>>> or from the File menu in DrRacket, if you have a recent version of Racket (strictly > 5.3.6).
>>>> 
>>>> Examples and details here:
>>>> https://github.com/Metaxal/measures
>>>> 
>>>> HTH,
>>>> Laurent
>>>> 
>>>> 
>>>> On Fri, Oct 25, 2013 at 12:51 AM, Alvin Schatte <AlvinSchatte at skytex.net> wrote:
>>>> Is there a library or package that combines numbers and their operations with dimensions that may be associated with them?
>>>> Alvin Schatte
>>>> 
>>>> ____________________
>>>>   Racket Users list:
>>>>   http://lists.racket-lang.org/users
>>>> 
>>>> 
>>>> ____________________
>>>>  Racket Users list:
>>>>  http://lists.racket-lang.org/users
>>> 
>>> 
>> 
>> ____________________
>>  Racket Users list:
>>  http://lists.racket-lang.org/users
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131026/bbaba59f/attachment.html>

Posted on the users mailing list.