unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Marc Tfardy <no@email.com>
To: help-gnu-emacs@gnu.org
Subject: How to use calc functions in own lisp program?
Date: Fri, 29 May 2009 19:29:38 +0200	[thread overview]
Message-ID: <gvp623$bin$3@news.onet.pl> (raw)

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



             reply	other threads:[~2009-05-29 17:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-29 17:29 Marc Tfardy [this message]
2009-05-29 19:39 ` How to use calc functions in own lisp program? 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='gvp623$bin$3@news.onet.pl' \
    --to=no@email.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).