all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [calc] routine tasks in batch mode
@ 2010-06-24 21:50 Giacomo Boffi
  2010-06-24 23:13 ` Pascal J. Bourguignon
  2010-06-25  9:02 ` Marc Mientki
  0 siblings, 2 replies; 11+ messages in thread
From: Giacomo Boffi @ 2010-06-24 21:50 UTC (permalink / raw)
  To: help-gnu-emacs

i searched the web for examples of elisp files that builds on calc and
can be used in batch mode to do routine tasks, but i was unable to
find one

anyone willing to share a couple of examples?

thank you in advance
                                                                g
-- 
Oh the sisters of mercy, they are not departed or gone.


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

* Re: [calc] routine tasks in batch mode
  2010-06-24 21:50 [calc] routine tasks in batch mode Giacomo Boffi
@ 2010-06-24 23:13 ` Pascal J. Bourguignon
  2010-06-25  5:48   ` Giacomo Boffi
  2010-06-25  9:02 ` Marc Mientki
  1 sibling, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2010-06-24 23:13 UTC (permalink / raw)
  To: help-gnu-emacs

Giacomo Boffi <giacomo.boffi@polimi.it> writes:

> i searched the web for examples of elisp files that builds on calc and
> can be used in batch mode to do routine tasks, but i was unable to
> find one
>
> anyone willing to share a couple of examples?

It might be hard to give an example of an interactive program used in
batch...

Perhaps you could do something with expect(1)?

But given that the purpose of calc is basically a four-operation
calculator,  you could more easily compute expressions in batch by NOT
using it.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: [calc] routine tasks in batch mode
  2010-06-24 23:13 ` Pascal J. Bourguignon
@ 2010-06-25  5:48   ` Giacomo Boffi
  2010-06-25 10:11     ` Pascal J. Bourguignon
  2010-06-25 23:44     ` Jay Belanger
  0 siblings, 2 replies; 11+ messages in thread
From: Giacomo Boffi @ 2010-06-25  5:48 UTC (permalink / raw)
  To: help-gnu-emacs

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> But given that the purpose of calc is basically a four-operation
> calculator [...]

you are talking of M-x calculator RET, i'm talking of M-x calc RET,
let's try to reach an agreement on the object of the discussion :-)

> It might be hard to give an example of an interactive program used
> in batch...

i use routinely calc to do different tasks, mostly leveraging its
symbolic algebra abilities

some of these tasks are suspiciously similar to each other, so i
thought that i can write some support elisp that, e.g., from a file
that contains an high level description of a problem can produce a
file with the problem solution, using in the intermediate steps the
algebraic abilities of calc

in the fine calc's manual i've found examples of writing and saving
keyboard macros that contain conditionals, loops and user input, and
a description of its elisp API

clearly i have not understood the whole stuff and for starting i'd
like to read some examples of elisp programs, batch oriented or not,
that use calc elisp API do do their stuff (if such examples exist, of
course: in my OP i said that i was not able to find any on the web)

i hope that this time i made more clear my question, should this be
the case thank you Pascal for giving me the opportunity of being more
explicit

ciao,
                                                                    g



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

* Re: [calc] routine tasks in batch mode
  2010-06-24 21:50 [calc] routine tasks in batch mode Giacomo Boffi
  2010-06-24 23:13 ` Pascal J. Bourguignon
@ 2010-06-25  9:02 ` Marc Mientki
  1 sibling, 0 replies; 11+ messages in thread
From: Marc Mientki @ 2010-06-25  9:02 UTC (permalink / raw)
  To: help-gnu-emacs

Am 24.06.2010 23:50, schrieb Giacomo Boffi:
> i searched the web for examples of elisp files that builds on calc and
> can be used in batch mode to do routine tasks, but i was unable to
> find one
>
> anyone willing to share a couple of examples?

Some times ago I wanted functionality of calc in my own elisp code (more
precisely: polynomial fitting). This was the result:

(require 'cl)

(defvar NUMERIC-PRECISION 18)


(defun take-first (lst length)
   "Liefert LENGTH ersten Elemente aus der LIST."
   (if (> length 0)
       (cons (car lst) (take-first (cdr lst) (- length 1)))))


(defun string-infix (string-list infix)
   "Erstellt einen String durch Aneinanderfügen aller Strings aus der 
STRING-LIST,
die durch INFIX getrennt werden."
   (mapconcat 'identity string-list infix))


(defun polynomial-expression-string (degree)
   "Generiert die Gleichung des Polynom DEGREE Grades als String."
   (let ((deg degree)
         (polynom ""))
     (if (<= degree 25) ; from 'a' to 'z'
         (progn
           (while (>= deg 1)
             (setq polynom (concat polynom (format "%s * x^%i + " 
(char-to-string (+ ?a (- degree deg))) deg)))
             (decf deg))
           (setq polynom (concat polynom (format "%s" (char-to-string (+ 
?a (- degree deg))))))
           polynom)
       polynom)))


(defun get-polynomial (x-list y-list polynomial-degree)
   "Berechnet das Polynom des POLYNOMIAL-DEGREE Grades und liefert es als
String mit der algebraischen Darstellung wie z.B.:
   \"1.4 x^3 + 2.3 x^2 - 4.3 x + 3.5\""
   ;; sehr mutig, aber (calc-precision NUMERIC-PRECISION) produziert 
"calc-select-buffer: Calculator buffer not available"
   (setq calc-internal-prec NUMERIC-PRECISION)
   ;; syntax for fit: fit(fitting-model-expression, [variable], 
unknown-parameters, [x-vector y-vector])
   (calc-eval (format "fit(%s, [x], [%s], [[%s], [%s]])"
                      (polynomial-expression-string polynomial-degree)
                      (string-infix (mapcar 'char-to-string (take-first 
(number-sequence ?a ?z) (+ polynomial-degree 1))) ", ")
                      (string-infix (mapcar 'number-to-string x-list) ", ")
                      (string-infix (mapcar 'number-to-string y-list) ", 
"))))


(defun get-polynomial-value (polynomial x)
   "Berechnet den Wert des Polynoms POLYNOMIAL an der Stelle X."
   (string-to-number (calc-eval (format "simplify(subst(%s, x, %f))" 
polynomial x))))



So now I can do:

(setq poly2 (get-polynomial '(2 3 4) '(4.1 9.1 16.1) 2))
"1.00000000001 x^2 - 7.51898096331e-11 x + 0.100000000106"


(get-polynomial-value poly2 5)
25.1


Most important are the calls to calc-eval, so investigate there. You
must study the internal anatomy of calc funcion but you can not call
this function directly - at least I had no success - and you must
go the way through string (e.g. "fit(....)", "simplify(...)").


HTH
regards
Marc




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

* Re: [calc] routine tasks in batch mode
  2010-06-25  5:48   ` Giacomo Boffi
@ 2010-06-25 10:11     ` Pascal J. Bourguignon
  2010-06-25 10:22       ` Marc Mientki
  2010-06-26  1:39       ` Eli Barzilay
  2010-06-25 23:44     ` Jay Belanger
  1 sibling, 2 replies; 11+ messages in thread
From: Pascal J. Bourguignon @ 2010-06-25 10:11 UTC (permalink / raw)
  To: help-gnu-emacs

Giacomo Boffi <giacomo.boffi@polimi.it> writes:

> pjb@informatimago.com (Pascal J. Bourguignon) writes:
>
>> But given that the purpose of calc is basically a four-operation
>> calculator [...]
>
> you are talking of M-x calculator RET, i'm talking of M-x calc RET,
> let's try to reach an agreement on the object of the discussion :-)

Perhaps as a mid point M-x calcul RET ?  ;-)


>> It might be hard to give an example of an interactive program used
>> in batch...
>
> i use routinely calc to do different tasks, mostly leveraging its
> symbolic algebra abilities
>
> some of these tasks are suspiciously similar to each other, so i
> thought that i can write some support elisp that, e.g., from a file
> that contains an high level description of a problem can produce a
> file with the problem solution, using in the intermediate steps the
> algebraic abilities of calc
>
> in the fine calc's manual i've found examples of writing and saving
> keyboard macros that contain conditionals, loops and user input, and
> a description of its elisp API
>
> clearly i have not understood the whole stuff and for starting i'd
> like to read some examples of elisp programs, batch oriented or not,
> that use calc elisp API do do their stuff (if such examples exist, of
> course: in my OP i said that i was not able to find any on the web)
>
> i hope that this time i made more clear my question, should this be
> the case thank you Pascal for giving me the opportunity of being more
> explicit

Actually, I understood M-x calc RET; where I may have misunderstood
you is when you mentionned 'batch'. I thought about: emacs -batch

I wasn't aware it was a symbolic calculator too.  This means the
hidden part of the iceberg is bigger than just its user interface and
makes it indeed worthwhile to try to call up from your own emacs lisp
code.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: [calc] routine tasks in batch mode
  2010-06-25 10:11     ` Pascal J. Bourguignon
@ 2010-06-25 10:22       ` Marc Mientki
  2010-06-25 10:44         ` Marc Mientki
  2010-06-26  1:39       ` Eli Barzilay
  1 sibling, 1 reply; 11+ messages in thread
From: Marc Mientki @ 2010-06-25 10:22 UTC (permalink / raw)
  To: help-gnu-emacs

Am 25.06.2010 12:11, schrieb Pascal J. Bourguignon:

> I wasn't aware it was a symbolic calculator too.  This means the
> hidden part of the iceberg is bigger than just its user interface and
> makes it indeed worthwhile to try to call up from your own emacs lisp
> code.

Emacs Calculator is really great piece of software. It replaces my
HP-41 both, original true hardware and virtual emulator :-)

regards
Marc



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

* Re: [calc] routine tasks in batch mode
  2010-06-25 10:22       ` Marc Mientki
@ 2010-06-25 10:44         ` Marc Mientki
  0 siblings, 0 replies; 11+ messages in thread
From: Marc Mientki @ 2010-06-25 10:44 UTC (permalink / raw)
  To: help-gnu-emacs

replaces = replaced

Marc




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

* Re: [calc] routine tasks in batch mode
  2010-06-25  5:48   ` Giacomo Boffi
  2010-06-25 10:11     ` Pascal J. Bourguignon
@ 2010-06-25 23:44     ` Jay Belanger
  2010-06-29  9:49       ` Giacomo Boffi
  1 sibling, 1 reply; 11+ messages in thread
From: Jay Belanger @ 2010-06-25 23:44 UTC (permalink / raw)
  To: help-gnu-emacs



Giacomo Boffi <giacomo.boffi@polimi.it> writes:
..
> clearly i have not understood the whole stuff and for starting i'd
> like to read some examples of elisp programs, batch oriented or not,
> that use calc elisp API do do their stuff (if such examples exist, of
> course: in my OP i said that i was not able to find any on the web)

This is briefly touched on in the Emacs Wiki:
http://www.emacswiki.org/emacs/AdvancedDeskCalculator

What sort of thing did you have in mind?  Calc operates on expressions
in it's own internal representation.  Although Calc can usually read
Elisp numbers, the result of a Calc calculation might not be a number in  
Elisp.  (math-add 2.3 6.5) returns 8.8, but
(math-add 123456789 4321) returns (bigpos 1110 2346 1).

**Quick summary of what follows**
If you give Calc the algebraic formula "integ(x*e^x,x)", 
you get "x e^x - e^x".
From an elisp program, you could get the same result with
  (calc-eval "integ(x*e^x,x)")
or
  (let ((calc-command-flags nil))
     (require 'calc-ext)
     (math-format-value 
         (calcFunc-integ (math-read-expr "x*e^x") '(var x var-x))))
Note that the result is a string.

**Long version of quick summary**
Marc mentioned using calc-eval, which is probably the
simplest way to use Calc in Elisp:
  (calc-eval "integ(x*e^x,x)")
would return the string
  "x e^x - e^x"
Even if you wanted to break it down into more lisp functions, you
probably don't want to enter the input expression in Calc's internal
format (Calc stores "x*e^x" as 
   `(* (var x var-x) (^ (var e var-e) (var x var-x)))'),  
so you could use `math-read-expr': 
  (math-read-expr "x*e^x")
would return `(* (var x var-x) (^ (var e var-e) (var x var-x)))'.
To operate on an expression, you'd want to use the algebraic form of
Calc's functions. If you were using Calc interactively, you would use 
`calc-integral' to integrate, but in an algebraic expression you would
use "integ".  An algebraic function "fn" in Calc is internally
`calcFunc-fn', so the "integ" function is really the lisp function
`calcFunc-integ'.  The expression  "integ(x*e^x,x)" would be read by
Calc as
  `(calcFunc-integ '(* (var x var-x) (^ (var e var-e) (var x var-x)))
                  '(var x var-x))'
This will return
   `(- (* (var x var-x) (^ (var e var-e) (var x var-x)))
       (^ (var e var-e) (var x var-x)))'
which probably isn't what you want.  The function `math-format-value'
will put it in a nicer (string) form: "x e^x - e^x"
So, instead of giving Calc the algebraic formula "integ(x*e^x,x)", you
could use the Elisp expression
`(math-format-value (calcFunc-integ (math-read-expr "x*e^x") '(var x var-x)))'

You might also want to set some other values using `let'; for example,
since by default Calc measures angles in degrees,
 (math-format-value (calcFunc-integ (math-read-expr "x*sin(x)") '(var x var-x)))
returns
 "32400 sin(x) / pi^2 - 180 x cos(x) / pi"
You might want to use radians by setting `calc-angle-mode' to `rad':
(let ((calc-angle-mode 'rad))
 (math-format-value (calcFunc-integ (math-read-expr "x*sin(x)") 
                                    '(var x var-x))))
returns
 "sin(x) - x cos(x)"

Finally, to make sure that the necessary files are loaded, you probably
want to start with `(require 'calc-ext)', and unless you are using the
latest Emacs you'll need to set `calc-command-flags' to nil:
(let ((calc-command-flags nil)
      (calc-angle-mode 'rad)
      ...)
  (require 'calc-ext)
  ..... )

I hope this helps, and let me know if there was something specific you
were trying to do.

Jay



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

* Re: [calc] routine tasks in batch mode
  2010-06-25 10:11     ` Pascal J. Bourguignon
  2010-06-25 10:22       ` Marc Mientki
@ 2010-06-26  1:39       ` Eli Barzilay
  1 sibling, 0 replies; 11+ messages in thread
From: Eli Barzilay @ 2010-06-26  1:39 UTC (permalink / raw)
  To: help-gnu-emacs

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> Giacomo Boffi <giacomo.boffi@polimi.it> writes:
>
>> pjb@informatimago.com (Pascal J. Bourguignon) writes:
>>
>>> But given that the purpose of calc is basically a four-operation
>>> calculator [...]
>>
>> you are talking of M-x calculator RET, i'm talking of M-x calc RET,
>> let's try to reach an agreement on the object of the discussion :-)

Yeah, using `calculator' for batch mode (for any definition of
"batch") would be useless.


> Perhaps as a mid point M-x calcul RET ?  ;-)

That's my fault...  I liked the idea behind `calc', but every time I
started it I could feel the horrible wastage of such a huge piece of
work when I just wanted to calculate some grade average or something
like that.  So I wrote a new package, and since `calc' was taken, I
used the next most fitting word...

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


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

* Re: [calc] routine tasks in batch mode
  2010-06-25 23:44     ` Jay Belanger
@ 2010-06-29  9:49       ` Giacomo Boffi
  2010-06-29 20:16         ` Andreas Politz
  0 siblings, 1 reply; 11+ messages in thread
From: Giacomo Boffi @ 2010-06-29  9:49 UTC (permalink / raw)
  To: help-gnu-emacs

Jay Belanger <jay.p.belanger@gmail.com> writes:

> http://www.emacswiki.org/emacs/AdvancedDeskCalculator

bookmarked

> What sort of thing did you have in mind? [...]  

solvers for the the dozen of typical problems i assign to students of
structural design and dynamics of structures

> I hope this helps, and let me know if there was something specific
> you were trying to do.

it helped. thank you very much
-- 
miao pinione                                     -- Theo, in IHC


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

* Re: [calc] routine tasks in batch mode
  2010-06-29  9:49       ` Giacomo Boffi
@ 2010-06-29 20:16         ` Andreas Politz
  0 siblings, 0 replies; 11+ messages in thread
From: Andreas Politz @ 2010-06-29 20:16 UTC (permalink / raw)
  To: help-gnu-emacs

Giacomo Boffi <giacomo.boffi@polimi.it> writes:

> Jay Belanger <jay.p.belanger@gmail.com> writes:
>
>> http://www.emacswiki.org/emacs/AdvancedDeskCalculator
>
> bookmarked
>
>> What sort of thing did you have in mind? [...]  
>
> solvers for the the dozen of typical problems i assign to students of
> structural design and dynamics of structures
>
>> I hope this helps, and let me know if there was something specific
>> you were trying to do.
>
> it helped. thank you very much


Maybe you write a simple batch processor, that pushes ordinary lines on
the stack and invokes the bound calc-function for ones starting with a
designated command-character.

-ap


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

end of thread, other threads:[~2010-06-29 20:16 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-06-24 21:50 [calc] routine tasks in batch mode Giacomo Boffi
2010-06-24 23:13 ` Pascal J. Bourguignon
2010-06-25  5:48   ` Giacomo Boffi
2010-06-25 10:11     ` Pascal J. Bourguignon
2010-06-25 10:22       ` Marc Mientki
2010-06-25 10:44         ` Marc Mientki
2010-06-26  1:39       ` Eli Barzilay
2010-06-25 23:44     ` Jay Belanger
2010-06-29  9:49       ` Giacomo Boffi
2010-06-29 20:16         ` Andreas Politz
2010-06-25  9:02 ` Marc Mientki

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.