Re: [O] Unit conversions and symbolic mathematics with Babel

2014-09-04 Thread H. Dieter Wilhelm
die...@duenenhof-wilhelm.de (H. Dieter Wilhelm) writes:

> Hello Org,
>
> for some engineering most often I need estimations with unit
> conversions, Emacs Calc is perfect for this.
>
> When it comes to reporting and documentation I'd like to do it with
> org-mode, of course.  For numerical stuff and visualisation purposes I'm
> using already R and TikZ. But for unit conversions and symbolic maths
> I'm in a bind when trying to do this with free programs.
>
> - R :: Do you have experience with a units packages under the R-project?
>   What I've seen so far is not so reassuring...
>
> - Maxima :: I think there is a nice and intuitive units package for
>   maxima.  But this would mean learning an additional tool and mentally
>   switching between both within one document.
>
> - Calc :: I'd prefer to do it with Calc but the Babel interface is
>   (yet?) restricted
>
>   For example, I can define functions and do some unit conversions:
>
> #+BEGIN_SRC emacs-lisp
> (defmath cvun(a b)
> (math-convert-units a b))
> #+END_SRC
>
> #+RESULTS:
> : calcFunc-cvun
>
> #+BEGIN_SRC calc :var L1 = "5 mm" 
> cvun( L1, m)
> #+END_SRC
>
> #+RESULTS:
> : 5e-3 m
>
>   But what is missing is to assign variables within a source block
>   
> #+BEGIN_SRC calc :var L1 = "5 mm" 
> L2 := cvun( L1, m)
> #+END_SRC
>   
>   Unfortunately this is not working.  Do you have an idea how to
>   implement this?



For your information: Jay Belanger found a workaround

#+BEGIN_SRC emacs-lisp
(defmath sett(a b) ; or any other name not identical to the applied lisp 
functions
  (set (nth 2 a) b))
#+END_SRC

#+RESULTS:
: calcFunc-sett

#+BEGIN_SRC calc :var L1 = 3
sett( L2, 3.14)
L2 - L1
#+END_SRC

#+RESULTS:
: 0.14

-- 
   Dieter   



> - Are you using (better) alternatives?
>
> Thanks for your insights
>
>Dieter

-- 
Best wishes
H. Dieter Wilhelm
Darmstadt, Germany




Re: [O] Unit conversions and symbolic mathematics with Babel

2014-09-03 Thread Brett Viren
Hi,

die...@duenenhof-wilhelm.de (H. Dieter Wilhelm) writes:

>   But what is missing is to assign variables within a source block
>   
> #+BEGIN_SRC calc :var L1 = "5 mm" 
> L2 := cvun( L1, m)
> #+END_SRC
>   
>   Unfortunately this is not working.  Do you have an idea how to
>   implement this?
>
> - Are you using (better) alternatives?

I like Python and in Python I like Pint for units.

  http://pint.readthedocs.org

Below is an example org document that should "run" and shows a couple
ways to use snippets of Pint code to do unit conversion.  Not shown but
Pint Quantity objects support arithmetic so are useful for carrying
units through some calculation.

For assigning to variables - presumably for use in later blocks - maybe
you can investigate using the :session header argument to source blocks.


Have fun,
-Brett.

One way to install Pint is:

#+BEGIN_SRC sh :results silent
  pip install --user pint
#+END_SRC

Here is an example:

#+name: uconv
#+header: :var val="10m" :var unit="inch" 
#+BEGIN_SRC python
  import pint
  units = pint.UnitRegistry()
  return units.Quantity(val).to(unit)
#+END_SRC

#+RESULTS: uconv
: 393.700787402 inch

Now call =uconv= do to some other conversion:

#+call: uconv("2.54cm","inch")

#+RESULTS:
: 1.0 inch


pgpGnSjfoKUX4.pgp
Description: PGP signature


Re: [O] Unit conversions and symbolic mathematics with Babel

2014-09-03 Thread Eric Abrahamsen
die...@duenenhof-wilhelm.de (H. Dieter Wilhelm) writes:

> Hello Org,
>
> for some engineering most often I need estimations with unit
> conversions, Emacs Calc is perfect for this.
>
> When it comes to reporting and documentation I'd like to do it with
> org-mode, of course.  For numerical stuff and visualisation purposes I'm
> using already R and TikZ. But for unit conversions and symbolic maths
> I'm in a bind when trying to do this with free programs.

If you're on a linux of some sort, you probably have the "units" command
line program installed. That's sort of the mother of all unit
converters, as far as I'm concerned -- you could call out to it and
return the results. What do you think?

> - R :: Do you have experience with a units packages under the R-project?
>   What I've seen so far is not so reassuring...
>
> - Maxima :: I think there is a nice and intuitive units package for
>   maxima.  But this would mean learning an additional tool and mentally
>   switching between both within one document.
>
> - Calc :: I'd prefer to do it with Calc but the Babel interface is
>   (yet?) restricted
>
>   For example, I can define functions and do some unit conversions:
>
> #+BEGIN_SRC emacs-lisp
> (defmath cvun(a b)
> (math-convert-units a b))
> #+END_SRC
>
>
> #+RESULTS:
> : calcFunc-cvun
>
> #+BEGIN_SRC calc :var L1 = "5 mm" 
> cvun( L1, m)
> #+END_SRC
>
>
> #+RESULTS:
> : 5e-3 m
>
>   But what is missing is to assign variables within a source block
>   
>
> #+BEGIN_SRC calc :var L1 = "5 mm" 
> L2 := cvun( L1, m)
> #+END_SRC
>
>   
>   Unfortunately this is not working.  Do you have an idea how to
>   implement this?
>
> - Are you using (better) alternatives?
>
> Thanks for your insights
>
>Dieter




Re: [O] Unit conversions and symbolic mathematics with Babel

2014-09-03 Thread Alan Schmitt
On 2014-09-03 08:54, die...@duenenhof-wilhelm.de (H. Dieter Wilhelm) writes:

>   But what is missing is to assign variables within a source block
>   
>
> #+BEGIN_SRC calc :var L1 = "5 mm" 
> L2 := cvun( L1, m)
> #+END_SRC
>
>   
>   Unfortunately this is not working.  Do you have an idea how to
>   implement this?

It seems that you could use block chaining. Here is an extract of some
code I use to compute stats for my book keeping:

#+name: monthly_average
#+begin_src emacs-lisp :var starty=2014 :var startm=3 :var amount=100 
  (let* ((tm (decode-time))
 (cmonth (nth 4 tm))
 (cyear (nth 5 tm))
 (nbmonths (+ (* 12 (- cyear starty)) (- cmonth startm
(calc-eval "round($ / $$, 2)" nil amount nbmonths))
#+end_src

#+name: ledger_average
#+begin_src emacs-lisp :var b="Quotidien" :var sy=2014 :var sm=3  :var 
sd="2014-03-01" :var p="Expenses:"
  (let* ((per (format "from %d-%d-01 to this month" sy sm))
 (a (org-sbe call_ledger (bucket (eval b)) (prefix (eval p)) (period 
(eval per)
(org-sbe monthly_average (amount (eval a)) (starty (eval sy)) (startm (eval 
sm
#+end_src

The last line of the second block calls the first block to do its
computation. The first block does not have to be in emacs-lisp, but
I don't know calc well enough to do it directly in calc.

Best,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7


signature.asc
Description: PGP signature


[O] Unit conversions and symbolic mathematics with Babel

2014-09-03 Thread H. Dieter Wilhelm
Hello Org,

for some engineering most often I need estimations with unit
conversions, Emacs Calc is perfect for this.

When it comes to reporting and documentation I'd like to do it with
org-mode, of course.  For numerical stuff and visualisation purposes I'm
using already R and TikZ. But for unit conversions and symbolic maths
I'm in a bind when trying to do this with free programs.

- R :: Do you have experience with a units packages under the R-project?
  What I've seen so far is not so reassuring...

- Maxima :: I think there is a nice and intuitive units package for
  maxima.  But this would mean learning an additional tool and mentally
  switching between both within one document.

- Calc :: I'd prefer to do it with Calc but the Babel interface is
  (yet?) restricted

  For example, I can define functions and do some unit conversions:

#+BEGIN_SRC emacs-lisp
(defmath cvun(a b)
  (math-convert-units a b))
#+END_SRC

#+RESULTS:
: calcFunc-cvun

#+BEGIN_SRC calc :var L1 = "5 mm" 
cvun( L1, m)
#+END_SRC

#+RESULTS:
: 5e-3 m

  But what is missing is to assign variables within a source block
  
#+BEGIN_SRC calc :var L1 = "5 mm" 
L2 := cvun( L1, m)
#+END_SRC
  
  Unfortunately this is not working.  Do you have an idea how to
  implement this?

- Are you using (better) alternatives?

Thanks for your insights

   Dieter
-- 
Best wishes
H. Dieter Wilhelm
Darmstadt, Germany