On 10/24/07, Frank Hamilton <[EMAIL PROTECTED]> wrote:
> Has anyone considered an application to calculate with systems of
> units, such as SI?

Here's an attempt at tracking dimensions:

units=:3 :0"0&;:
  assert._1-:nc y ['new unit name must have no other meaning'
  L1=. 'assert.0=#$0+n [''unit exponent must be scalar'''
  L2=. '(;:''',(>y),''');m,&<n'
  (y)=: 2 :(L1;L2)
  i.0 0
)

uni=:1 :0
:
  'xu xv xx'=.x
  'yu yv yx'=.y
  zv=.xv u yv  NB. basic operation
  xb=.xu e.yu  NB. find unit overlap
  select. u`''
    case. +`'' do.
        assert. xu -: yu ['units must match'
        assert. xx -: yx ['unit exponents must match'
        zu=. xu
        zx=. xx
NB. repeat for -
    case. *`'' do.
        zu=. ~.xu,yu
        zx=. (xu,yu) +//. xx,yx
    case. %`'' do.
        zu=. ~.xu,yu
        zx=. (xu,yu) -//. xx,yx
    case. do. assert 0['unsuppported operation'
  end.
  (/:~zu);zv ,&< zx/:zu
)

raw=: 1 {:: ]

example use:
   units 'm g s' NB. meters grams seconds
   10 m 1 NB. 10 meters
   10 m 1 *uni 10 s _1 NB. meters/second
   10 m 1 *uni 10 s _1 * uni 10 s _1 NB. meters/second^2


Notes:
I have performed only very limited testing

I do not currently support subtraction -- see coment in uni for how
to fix that.  I think select should be modified to support multiple
case. entries with a single do. but I guess I should post that on
the wiki feature request page.

units must be declared exactly once.  In a script, I recommend
preceding units with erase.  For example:
   erase 'm g s'
   units 'm g s'

If you prefer, erase y could be incorporated in the body of units

All manipulations of numbers must either use uni operations or
raw values.

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to