emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* getting calc-units working in table formulas
@ 2014-12-16  4:21 Eric Abrahamsen
  2014-12-16  6:33 ` Eric Abrahamsen
  2014-12-16 13:06 ` Michael Brand
  0 siblings, 2 replies; 9+ messages in thread
From: Eric Abrahamsen @ 2014-12-16  4:21 UTC (permalink / raw)
  To: emacs-orgmode

I've been playing with calc-units, and it's pretty amazing. See all the
units with `calc-view-units-table'.

Some calc-units stuff works out the box (maybe have to require
calc-units?), I think this should be mentioned in the manual:

| distance | time   | speed       |
|----------+--------+-------------|
| 3 km     | 2.5 hr | 1.2 km / hr |
#+TBLFM: @2$3=$1/$2

Who knew it could do that?! Probably everyone but me... It doesn't need
the constants.el package, and looks nicer in the input, to boot.

calc-units makes a few of its functions available via defmath:

| speed        | simplified speed |
|--------------+------------------|
| 40km / 2.5hr | 16. km / hr      |
|              |                  |
#+TBLFM: @2$2=usimplify($1)

But it's got a lot more tricks. I think unit conversion would be very
handy to have, but there's something I'm not getting about using
defmath. For instance, this works:

| km    |      ft |
|-------+---------|
| 2.5km | 8202.10 |
#+TBLFM: $2='(calc-eval (math-convert-units (calc-eval $1 'raw) (calc-eval "ft" 'raw))); %.2f

But this doesn't:

#+BEGIN_SRC emacs-lisp
  (defmath uconvert (expr target-units)
    (math-convert-units expr target-units))
#+END_SRC

| km    | ft                  |
|-------+---------------------|
| 2.5km | uconvert(2.5 km ft) |
#+TBLFM: $2=uconvert($1 ft)

I can't tell if I've written the calcFunc thing wrong, or if it's
somehow not getting installed correctly. I've tried several variants,
and they mostly all just give me the results above.

Does anyone know what I'm doing wrong?

Also, once that's figured out, wouldn't it be handy if Org came with a
few predefined units-related math functions? It would be a tiny bit of
code, for quite a bit more power.

At the very least, I'd like to provide a patch to the manual to make the
units stuff a little more explicit...

Eric

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting calc-units working in table formulas
  2014-12-16  4:21 getting calc-units working in table formulas Eric Abrahamsen
@ 2014-12-16  6:33 ` Eric Abrahamsen
  2014-12-16 13:06 ` Michael Brand
  1 sibling, 0 replies; 9+ messages in thread
From: Eric Abrahamsen @ 2014-12-16  6:33 UTC (permalink / raw)
  To: emacs-orgmode

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> I've been playing with calc-units, and it's pretty amazing. See all the
> units with `calc-view-units-table'.
>
> Some calc-units stuff works out the box (maybe have to require
> calc-units?), I think this should be mentioned in the manual:
>
> | distance | time   | speed       |
> |----------+--------+-------------|
> | 3 km     | 2.5 hr | 1.2 km / hr |
>
> #+TBLFM: @2$3=$1/$2
>
> Who knew it could do that?! Probably everyone but me... It doesn't need
> the constants.el package, and looks nicer in the input, to boot.
>
> calc-units makes a few of its functions available via defmath:
>
> | speed        | simplified speed |
> |--------------+------------------|
> | 40km / 2.5hr | 16. km / hr      |
> |              |                  |
>
> #+TBLFM: @2$2=usimplify($1)
>
> But it's got a lot more tricks. I think unit conversion would be very
> handy to have, but there's something I'm not getting about using
> defmath. For instance, this works:
>
> | km    |      ft |
> |-------+---------|
> | 2.5km | 8202.10 |
>
> #+TBLFM: $2='(calc-eval (math-convert-units (calc-eval $1 'raw) (calc-eval "ft" 'raw))); %.2f

I wonder if the problem is the 'raw in here. To the best of my
knowledge, calc-eval needs that 'raw flag to work properly with this.
But it doesn't look like `org-table-eval-formula' will ever send that
flag. I hope someone who knows this code better than me can comment...

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting calc-units working in table formulas
  2014-12-16  4:21 getting calc-units working in table formulas Eric Abrahamsen
  2014-12-16  6:33 ` Eric Abrahamsen
@ 2014-12-16 13:06 ` Michael Brand
  2014-12-17  1:09   ` Eric Abrahamsen
  1 sibling, 1 reply; 9+ messages in thread
From: Michael Brand @ 2014-12-16 13:06 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: Org Mode

Hi Eric

This answers only one of your questions:

On Tue, Dec 16, 2014 at 5:21 AM, Eric Abrahamsen
<eric@ericabrahamsen.net> wrote:
> #+BEGIN_SRC emacs-lisp
>   (defmath uconvert (expr target-units)
>     (math-convert-units expr target-units))
> #+END_SRC

I would prefer

#+BEGIN_SRC emacs-lisp
  (defmath uconv (expr target-units &optional pure)
    (math-convert-units expr target-units pure))
#+END_SRC

> | km    | ft                  |
> |-------+---------------------|
> | 2.5km | uconvert(2.5 km ft) |
> #+TBLFM: $2=uconvert($1 ft)

Calc syntax uses comma to separate the function arguments, see
examples in e. g.
http://orgmode.org/manual/Formula-syntax-for-Calc.html

| km     | ft           |
|--------+--------------|
| 2.5 km | 8202.0997 ft |
#+TBLFM: $2 = uconv($1, ft)

Btw, to have the units only in the column header:

|  km |        ft |
|-----+-----------|
| 2.5 | 8202.0997 |
#+TBLFM: $2 = uconv($1 * @<$1, @<$2, t)

The same without a user's defmath:

|  km |        ft |
|-----+-----------|
| 2.5 | 8202.0997 |
#+TBLFM: $2 = usimplify($1 * @<$1 / @<$2)

Michael

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting calc-units working in table formulas
  2014-12-16 13:06 ` Michael Brand
@ 2014-12-17  1:09   ` Eric Abrahamsen
  2014-12-17  6:39     ` Michael Brand
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Abrahamsen @ 2014-12-17  1:09 UTC (permalink / raw)
  To: emacs-orgmode

Michael Brand <michael.ch.brand@gmail.com> writes:

> Hi Eric
>
> This answers only one of your questions:
>
> On Tue, Dec 16, 2014 at 5:21 AM, Eric Abrahamsen
> <eric@ericabrahamsen.net> wrote:
>> #+BEGIN_SRC emacs-lisp
>>   (defmath uconvert (expr target-units)
>>     (math-convert-units expr target-units))
>> #+END_SRC
>
> I would prefer
>
> #+BEGIN_SRC emacs-lisp
>   (defmath uconv (expr target-units &optional pure)
>     (math-convert-units expr target-units pure))
> #+END_SRC
>
>
>> | km    | ft                  |
>> |-------+---------------------|
>> | 2.5km | uconvert(2.5 km ft) |
>> #+TBLFM: $2=uconvert($1 ft)
>
> Calc syntax uses comma to separate the function arguments, see
> examples in e. g.
> http://orgmode.org/manual/Formula-syntax-for-Calc.html

I was hoping I'd made a more interesting mistake than that! Thanks for
clearing that up.


> | km     | ft           |
> |--------+--------------|
> | 2.5 km | 8202.0997 ft |
>
> #+TBLFM: $2 = uconv($1, ft)
>
> Btw, to have the units only in the column header:
>
> |  km |        ft |
> |-----+-----------|
> | 2.5 | 8202.0997 |
>
> #+TBLFM: $2 = uconv($1 * @<$1, @<$2, t)
>
> The same without a user's defmath:
>
> |  km |        ft |
> |-----+-----------|
> | 2.5 | 8202.0997 |
>
> #+TBLFM: $2 = usimplify($1 * @<$1 / @<$2)

Wow, interesting stuff. I had no idea...

I'd still like to provide a small patch to the documentation, alerting
users to the fact that units work out of the box. Does anyone have any
opinions on including a defmath for `math-convert-units'? (The 'pure
variant seems like a fine choice.)

Lastly, is there a way to both use output formatting, and retain the
unit name (if desired)? Right now I get this:

| km    | units | conversion |
|-------+-------+------------|
| 2.5km | ft    |    8202.10 |
#+TBLFM: $3=uconvert($1, $2); %.2f


Ie, the "ft" falls off the results when I add the "%.2f". Is it possible
to retain that and have formatting?

Eric

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting calc-units working in table formulas
  2014-12-17  1:09   ` Eric Abrahamsen
@ 2014-12-17  6:39     ` Michael Brand
  2014-12-17  7:02       ` Eric Abrahamsen
  0 siblings, 1 reply; 9+ messages in thread
From: Michael Brand @ 2014-12-17  6:39 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: Org Mode

Hi Eric

On Wed, Dec 17, 2014 at 2:09 AM, Eric Abrahamsen
<eric@ericabrahamsen.net> wrote:
> Lastly, is there a way to both use output formatting, and retain the
> unit name (if desired)? Right now I get this:
>
> | km    | units | conversion |
> |-------+-------+------------|
> | 2.5km | ft    |    8202.10 |
> #+TBLFM: $3=uconvert($1, $2); %.2f
>
> Ie, the "ft" falls off the results when I add the "%.2f". Is it possible
> to retain that and have formatting?

Yes, see
"org-spreadsheet: formatting chops off units"
http://thread.gmane.org/gmane.emacs.orgmode/59928

Michael

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting calc-units working in table formulas
  2014-12-17  6:39     ` Michael Brand
@ 2014-12-17  7:02       ` Eric Abrahamsen
  2014-12-18 10:07         ` [PATCH] " Eric Abrahamsen
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Abrahamsen @ 2014-12-17  7:02 UTC (permalink / raw)
  To: emacs-orgmode

Michael Brand <michael.ch.brand@gmail.com> writes:

> Hi Eric
>
> On Wed, Dec 17, 2014 at 2:09 AM, Eric Abrahamsen
> <eric@ericabrahamsen.net> wrote:
>> Lastly, is there a way to both use output formatting, and retain the
>> unit name (if desired)? Right now I get this:
>>
>> | km    | units | conversion |
>> |-------+-------+------------|
>> | 2.5km | ft    |    8202.10 |
>> #+TBLFM: $3=uconvert($1, $2); %.2f
>>
>> Ie, the "ft" falls off the results when I add the "%.2f". Is it possible
>> to retain that and have formatting?
>
> Yes, see
> "org-spreadsheet: formatting chops off units"
> http://thread.gmane.org/gmane.emacs.orgmode/59928

I'm certainly late to this party! My only excuse is that it's far from
obvious, given the current manual, that all this is available (even
after your documentation patch in the referenced thread).

Just to note, the second of the two formatting solutions you mentioned
(manually adding the units as a string) seems to no longer work. The
first does, however.

I'll work up a slightly longer documentation patch in the next couple of
days -- I really think people should be alerted to what's possible with
units. The "uconvert" defmath could even just be an example in the
manual, rather than code that ships with Org.

E

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] getting calc-units working in table formulas
  2014-12-17  7:02       ` Eric Abrahamsen
@ 2014-12-18 10:07         ` Eric Abrahamsen
  2014-12-20 16:33           ` Nicolas Goaziou
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Abrahamsen @ 2014-12-18 10:07 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1411 bytes --]

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Michael Brand <michael.ch.brand@gmail.com> writes:
>
>> Hi Eric
>>
>> On Wed, Dec 17, 2014 at 2:09 AM, Eric Abrahamsen
>> <eric@ericabrahamsen.net> wrote:
>>> Lastly, is there a way to both use output formatting, and retain the
>>> unit name (if desired)? Right now I get this:
>>>
>>> | km    | units | conversion |
>>> |-------+-------+------------|
>>> | 2.5km | ft    |    8202.10 |
>>> #+TBLFM: $3=uconvert($1, $2); %.2f
>>>
>>> Ie, the "ft" falls off the results when I add the "%.2f". Is it possible
>>> to retain that and have formatting?
>>
>> Yes, see
>> "org-spreadsheet: formatting chops off units"
>> http://thread.gmane.org/gmane.emacs.orgmode/59928
>
> I'm certainly late to this party! My only excuse is that it's far from
> obvious, given the current manual, that all this is available (even
> after your documentation patch in the referenced thread).
>
> Just to note, the second of the two formatting solutions you mentioned
> (manually adding the units as a string) seems to no longer work. The
> first does, however.
>
> I'll work up a slightly longer documentation patch in the next couple of
> days -- I really think people should be alerted to what's possible with
> units. The "uconvert" defmath could even just be an example in the
> manual, rather than code that ships with Org.

Here's the doc patch, hope all is in order.

Eric


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Mention-calc-units-in-the-spreadsheet-documentation.patch --]
[-- Type: text/x-diff, Size: 1876 bytes --]

From da8e9914d4f63cb3bfa6e7beacd8ee762044b2e6 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <eric@ericabrahamsen.net>
Date: Thu, 18 Dec 2014 18:04:00 +0800
Subject: [PATCH] Mention calc-units in the spreadsheet documentation

* doc/org.texi: Outline using units in tables, link to appropriate
  part of the Calc manual.
---
 doc/org.texi | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/doc/org.texi b/doc/org.texi
index 7c464ca..aae0537 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -2756,6 +2756,26 @@ should be padded with 0 to the full size.
 You can add your own Calc functions defined in Emacs Lisp with @code{defmath}
 and use them in formula syntax for Calc.
 
+Calc also comes with support for unit calculations, via the @code{calc-units}
+package.  For a complete list of recognized units, call
+@code{calc-view-units-table}.  Units can be specified with or without a space
+between the number and the unit name, eg @samp{2 ft} is the same as
+@samp{2ft}.  Rates are handled automatically: multiplying @samp{3 m/s} by two
+will give @samp{6 m / s}.  By default, @code{calc-units} only defines one
+units-related function for use in tables, @code{usimplify}, which can take an
+expression such as @samp{42 km / 2.5 h} and return @samp{16.8 km / hr}.
+Another useful function to define might be @code{math-convert-units}, eg:
+
+@example
+(defmath uconv (expr target-units &optional pure)
+  (math-convert-units expr target-units pure))
+@end example
+
+Which would allow you to use @samp{uconv($1, ft)} to, for example, convert a
+distance specified in the cell @samp{$1} into feet.  The optional 'pure
+argument strips the units designator from the number in the results.
+@xref{Units, Operating on Units,,calc}.
+
 @node Formula syntax for Lisp
 @subsection Emacs Lisp forms as formulas
 @cindex Lisp forms, as table formulas
-- 
2.2.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] getting calc-units working in table formulas
  2014-12-18 10:07         ` [PATCH] " Eric Abrahamsen
@ 2014-12-20 16:33           ` Nicolas Goaziou
  2014-12-21  4:22             ` Eric Abrahamsen
  0 siblings, 1 reply; 9+ messages in thread
From: Nicolas Goaziou @ 2014-12-20 16:33 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: emacs-orgmode

Hello,

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Here's the doc patch, hope all is in order.

Thanks. Some comments follow.

> +Calc also comes with support for unit calculations, via the @code{calc-units}
> +package.  For a complete list of recognized units, call
> +@code{calc-view-units-table}.  Units can be specified with or without a space
> +between the number and the unit name, eg @samp{2 ft} is the same as

"eg" -> "e.g.,"

> +@samp{2ft}.  Rates are handled automatically: multiplying @samp{3 m/s} by two
> +will give @samp{6 m / s}.  By default, @code{calc-units} only defines one
> +units-related function for use in tables, @code{usimplify}, which can take an
> +expression such as @samp{42 km / 2.5 h} and return @samp{16.8 km / hr}.
> +Another useful function to define might be @code{math-convert-units},
> eg:

Ditto.

> +@example
> +(defmath uconv (expr target-units &optional pure)
> +  (math-convert-units expr target-units pure))
> +@end example

I think you should use @lisp instead of @example here.

> +Which would allow you to use @samp{uconv($1, ft)} to, for example, convert a
> +distance specified in the cell @samp{$1} into feet.  The optional
> 'pure

@code{pure} instead of 'pure


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] getting calc-units working in table formulas
  2014-12-20 16:33           ` Nicolas Goaziou
@ 2014-12-21  4:22             ` Eric Abrahamsen
  0 siblings, 0 replies; 9+ messages in thread
From: Eric Abrahamsen @ 2014-12-21  4:22 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1373 bytes --]

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> Hello,
>
> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> Here's the doc patch, hope all is in order.
>
> Thanks. Some comments follow.
>
>> +Calc also comes with support for unit calculations, via the @code{calc-units}
>> +package.  For a complete list of recognized units, call
>> +@code{calc-view-units-table}.  Units can be specified with or without a space
>> +between the number and the unit name, eg @samp{2 ft} is the same as
>
> "eg" -> "e.g.,"
>
>> +@samp{2ft}.  Rates are handled automatically: multiplying @samp{3 m/s} by two
>> +will give @samp{6 m / s}.  By default, @code{calc-units} only defines one
>> +units-related function for use in tables, @code{usimplify}, which can take an
>> +expression such as @samp{42 km / 2.5 h} and return @samp{16.8 km / hr}.
>> +Another useful function to define might be @code{math-convert-units},
>> eg:
>
> Ditto.
>
>> +@example
>> +(defmath uconv (expr target-units &optional pure)
>> +  (math-convert-units expr target-units pure))
>> +@end example
>
> I think you should use @lisp instead of @example here.
>
>> +Which would allow you to use @samp{uconv($1, ft)} to, for example, convert a
>> +distance specified in the cell @samp{$1} into feet.  The optional
>> 'pure
>
> @code{pure} instead of 'pure

Thanks for the notes -- here's another version.

Eric


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Mention-calc-units-in-the-spreadsheet-documentation.patch --]
[-- Type: text/x-diff, Size: 1909 bytes --]

From e8979feb063868c3b91b384072e2b790917e6054 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <eric@ericabrahamsen.net>
Date: Thu, 18 Dec 2014 18:04:00 +0800
Subject: [PATCH] Mention calc-units in the spreadsheet documentation

* doc/org.texi: Outline using units in tables, link to appropriate
  part of the Calc manual.
---
 doc/org.texi | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/doc/org.texi b/doc/org.texi
index 33a6a0d..d8a5785 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -2758,6 +2758,27 @@ should be padded with 0 to the full size.
 You can add your own Calc functions defined in Emacs Lisp with @code{defmath}
 and use them in formula syntax for Calc.
 
+Calc also comes with support for unit calculations, via the @code{calc-units}
+package.  For a complete list of recognized units, call
+@code{calc-view-units-table}.  Units can be specified with or without a space
+between the number and the unit name, e.g., @samp{2 ft} is equivalent to
+@samp{2ft}.  Rates are handled automatically: multiplying @samp{3 m/s} by two
+will give @samp{6 m / s}.  By default, @code{calc-units} only defines one
+units-related function for use in table formulas, @code{usimplify}, which can
+take an expression such as @samp{42 km / 2.5 h} and return @samp{16.8 km /
+hr}.  Another useful function to define might be @code{math-convert-units},
+e.g.:
+
+@lisp
+(defmath uconv (expr target-units &optional pure)
+  (math-convert-units expr target-units pure))
+@end lisp
+
+Which would allow you to use @samp{uconv($1, ft)} in table formulas to, for
+example, convert a distance specified in cell @samp{$1} into feet.  The
+optional @code{pure} argument strips the units designator from the number in
+the results.  @xref{Units, Operating on Units,,calc}.
+
 @node Formula syntax for Lisp
 @subsection Emacs Lisp forms as formulas
 @cindex Lisp forms, as table formulas
-- 
2.2.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-12-21  4:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-16  4:21 getting calc-units working in table formulas Eric Abrahamsen
2014-12-16  6:33 ` Eric Abrahamsen
2014-12-16 13:06 ` Michael Brand
2014-12-17  1:09   ` Eric Abrahamsen
2014-12-17  6:39     ` Michael Brand
2014-12-17  7:02       ` Eric Abrahamsen
2014-12-18 10:07         ` [PATCH] " Eric Abrahamsen
2014-12-20 16:33           ` Nicolas Goaziou
2014-12-21  4:22             ` Eric Abrahamsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).