help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [calc] routine tasks in batch mode


From: Jay Belanger
Subject: Re: [calc] routine tasks in batch mode
Date: Wed, 08 Dec 2010 15:15:54 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)


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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]