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: Marc Mientki
Subject: Re: [calc] routine tasks in batch mode
Date: Wed, 08 Dec 2010 15:15:50 -0000
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.10) Gecko/20100512 Thunderbird/3.0.5

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




reply via email to

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