unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to use calc functions in own lisp program?
@ 2009-05-29 17:29 Marc Tfardy
  2009-05-29 19:39 ` Andreas Röhler
  2009-05-30  1:34 ` Jay Belanger
  0 siblings, 2 replies; 7+ messages in thread
From: Marc Tfardy @ 2009-05-29 17:29 UTC (permalink / raw)
  To: help-gnu-emacs

I want to compute the polynomial fitting in my own lisp program. The
Emacs Calc offers this functionality so my first thought was to use this
code. But I don't understand the call arguments to calc-curve-fit:

(defun calc-curve-fit (arg &optional calc-curve-model
                            calc-curve-coefnames calc-curve-varnames)
...)

(calc-curve-fit is defined .../emacs/lisp/calc/calcalg3.el)

calc-curve-fit works well when called interactively in Calc. Assume, we
have following vector an the top of the stack:

[ [  1,   2,   3,   4   ]
   [ 1.1, 4.2, 9.2, 15.8 ] ]

and we want to compute polynomial fitting of a second degree. In Calc
we press 'aF' and Calc ask for Model ("Fit to model" query) and then for
variable to fitting ("Fitting variables" query). When we enter '2' and
RET (default is 'x') we get as result:

0.874999999996 x^2 + 0.535000000019 x - 0.325000000019


To understand how Calc works I added to calc-curve-fit this four lines:

      (message (format "%S" arg))
      (message (format "%S" calc-curve-model))
      (message (format "%S" calc-curve-coefnames))
      (message (format "%S" calc-curve-varnames))

and the result was:

nil
(+ (+ (* (var c var-c) (^ (var x var-x) 2)) (* (var b var-b) (var x var-x))) (var a var-a))
(vec (var a var-a) (var b var-b) (var c var-c))
(vec (var x var-x))

The Calc representation of input data (vector) I got with:
(calc-top 1)

The result was:
(vec (vec 1 2 3 4) (vec (float 11 -1) (float 42 -1) (float 92 -1) (float 158 -1)))


But, when I try to evaluate following code:

(calc-curve-fit '(vec (vec 1 2 3 4) (vec (float 11 -1) (float 42 -1) (float 92 -1) (float 158 -1)))
                 '(+ (+ (* (var c var-c) (^ (var x var-x) 2)) (* (var b var-b) (var x var-x))) (var a var-a))
                 '(vec (var a var-a) (var b var-b) (var c var-c))
                 '(vec (var x var-x)))

I run in error. The debugger shows:

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
   signal(wrong-type-argument (number-or-marker-p nil))
   byte-code("...blablabla...
   calc-do((lambda nil (setq calc-aborted-prefix nil) (let (... key ... n calc-curve-nvars temp data ... ...) (message "----") (message ...) (while ... ... ... ...) (message ...) (message ...) (message ...) (message ...) (message "----") (let ... ... ...))) 3639)
   (calc-slow-wrapper (setq calc-aborted-prefix nil) (let (... key ... n calc-curve-nvars temp data ... ...) (message "----") (message ...) (while ... ... ... ...) (message ...) (message ...) (message ...) (message ...) (message "----") (let ... ... ...)))
   calc-curve-fit((vec (vec 1 2 3 4) (vec (float 11 -1) (float 42 -1) (float 92 -1) (float 158 -1))) (+ (+ (* ... ...) (* ... ...)) (var a var-a)) (vec (var a var-a) (var b var-b) (var c var-c)) (vec (var x var-x)))
   eval((calc-curve-fit (quote (vec ... ...)) (quote (+ ... ...)) (quote (vec ... ... ...)) (quote (vec ...))))
   eval-last-sexp-1(t)
   eval-last-sexp(t)
   eval-print-last-sexp()
   call-interactively(eval-print-last-sexp)

What is wrong? Please, help me!

regards
Marc



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

end of thread, other threads:[~2009-06-02  9:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-05-29 17:29 How to use calc functions in own lisp program? Marc Tfardy
2009-05-29 19:39 ` Andreas Röhler
2009-06-01  9:18   ` Marc Tfardy
2009-05-30  1:34 ` Jay Belanger
2009-06-01  9:13   ` Marc Tfardy
2009-06-01 22:06     ` Jay Belanger
2009-06-02  9:01       ` Marc Tfardy

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).