* 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
* Re: How to use calc functions in own lisp program? 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 1 sibling, 1 reply; 7+ messages in thread From: Andreas Röhler @ 2009-05-29 19:39 UTC (permalink / raw) To: help-gnu-emacs Marc Tfardy wrote: > (vec (vec 1 2 3 4) (vec (float 11 -1) (float 42 -1) (float 92 -1) (float > 158 -1))) Beside of the excellent calc-tool here, if it pertains to pure math-programming, my impression is, sxemacs has the most for it. At sxemacs-devel@sxemacs.org you should got an answer. Cheers Andreas ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to use calc functions in own lisp program? 2009-05-29 19:39 ` Andreas Röhler @ 2009-06-01 9:18 ` Marc Tfardy 0 siblings, 0 replies; 7+ messages in thread From: Marc Tfardy @ 2009-06-01 9:18 UTC (permalink / raw) To: help-gnu-emacs Andreas Röhler schrieb: > Marc Tfardy wrote: > >> (vec (vec 1 2 3 4) (vec (float 11 -1) (float 42 -1) (float 92 -1) (float >> 158 -1))) > > Beside of the excellent calc-tool here, > > if it pertains to pure math-programming, > > my impression is, sxemacs has the most for it. > > At sxemacs-devel@sxemacs.org you should got an answer. As I see it is a another implementation of Emacs. But I work mit GNU Emacs and don't want to switch to another version. Anyway, thanks for this hint! reagrds Marc ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to use calc functions in own lisp program? 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-05-30 1:34 ` Jay Belanger 2009-06-01 9:13 ` Marc Tfardy 1 sibling, 1 reply; 7+ messages in thread From: Jay Belanger @ 2009-05-30 1:34 UTC (permalink / raw) To: help-gnu-emacs Marc Tfardy <no@email.com> writes: > 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: calc-curve-fit is designed to be used as an interactive Calc function, so it assumes it is being used from the Calc buffer and interacts with the stack, hence all your error messages. If you want to use a Calc function from outside of Calc, you'll need to use the algebraic form of the function; in this case `fit', which is Calc shorthand for `calcFunc-fit'. `fit' takes as arguments the curve model, the variables in the curve model, the coefficients and the data. (The Calc manual will tell the possibilities for these.) The Calc functions will assume that their input is in internal Calc form. The function `math-read-expr' will take a string and put it in internal Calc form. But you probably want to use `calc-eval', which will take as an argument an algebraic expression (as a string) that you could use in a Calc session and return the string that represents the Calc output. For your problem, in Calc you could enter fit(a*x^2+b*x+c,x,[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]]) and get your result, so outside of Calc you can evaluate (calc-eval "fit(a*x^2+b*x+c,[x],[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]])") Evaluating the above gives me "0.874999999998 x^2 + 0.53500000001 x - 0.325000000011" ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to use calc functions in own lisp program? 2009-05-30 1:34 ` Jay Belanger @ 2009-06-01 9:13 ` Marc Tfardy 2009-06-01 22:06 ` Jay Belanger 0 siblings, 1 reply; 7+ messages in thread From: Marc Tfardy @ 2009-06-01 9:13 UTC (permalink / raw) To: help-gnu-emacs Jay Belanger schrieb: > Marc Tfardy <no@email.com> writes: > >> 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: > > calc-curve-fit is designed to be used as an interactive Calc function, > so it assumes it is being used from the Calc buffer and interacts with > the stack, When this is true, why provide calc-curve-fit all needed data as call arguments? > If you want to use a Calc function from outside of Calc, you'll need to > use the algebraic form of the function; in this case `fit', which is Calc > shorthand for `calcFunc-fit'. `fit' takes as arguments the curve > model, the variables in the curve model, the coefficients and the data. > (The Calc manual will tell the possibilities for these.) > The Calc functions will assume that their input is in internal Calc > form. The function `math-read-expr' will take a string and put it in > internal Calc form. But you probably want to use `calc-eval', which > will take as an argument an algebraic expression (as a string) that you > could use in a Calc session and return the string that represents the > Calc output. For your problem, in Calc you could enter > fit(a*x^2+b*x+c,x,[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]]) > and get your result, so outside of Calc you can evaluate > > (calc-eval > "fit(a*x^2+b*x+c,[x],[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]])") > > Evaluating the above gives me > > "0.874999999998 x^2 + 0.53500000001 x - 0.325000000011" This is what I looking for! VERY VERY BIG THANKS! One problem is solved, but here comes the next ;-) The polynomial fit ist the first step. Now I must to compute values on this curve at certain points. In Calc it is very easy. I put the value for 'x' on the stack and press 'sl', Calc ask for variable, I type 'x RET' and get a polynom value for my 'x'. But how can I do in my lisp code? I would be very happy for the right answer! reagrds Marc ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to use calc functions in own lisp program? 2009-06-01 9:13 ` Marc Tfardy @ 2009-06-01 22:06 ` Jay Belanger 2009-06-02 9:01 ` Marc Tfardy 0 siblings, 1 reply; 7+ messages in thread From: Jay Belanger @ 2009-06-01 22:06 UTC (permalink / raw) To: help-gnu-emacs Marc Tfardy <no@email.com> writes: > Jay Belanger schrieb: .. >> calc-curve-fit is designed to be used as an interactive Calc function, >> so it assumes it is being used from the Calc buffer and interacts with >> the stack, > > When this is true, why provide calc-curve-fit all needed data as call > arguments? I don't know. :( > This is what I looking for! VERY VERY BIG THANKS! You're welcome. :) > The polynomial fit ist the first step. Now I must to compute > values on this curve at certain points. In Calc it is very easy. > I put the value for 'x' on the stack and press 'sl', Calc ask for > variable, I type 'x RET' and get a polynom value for my 'x'. But how can > I do in my lisp code? You could use `subst', which takes the form "subst(expr,var,value)". (Actually, instead of a variable any expression can be substituted for.) So, if you want the best fit curve evaluated at x=2, for example, you could use (calc-eval "simplify(subst(fit(a*x^2+b*x+c,[x],[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]]),x,2))") or even (string-to-number ...above...) (Of course, depending on what else you want Calc to do, it might be more efficient to write a lisp program that will do the curve fitting.) Jay ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to use calc functions in own lisp program? 2009-06-01 22:06 ` Jay Belanger @ 2009-06-02 9:01 ` Marc Tfardy 0 siblings, 0 replies; 7+ messages in thread From: Marc Tfardy @ 2009-06-02 9:01 UTC (permalink / raw) To: help-gnu-emacs Jay Belanger schrieb: > > Marc Tfardy <no@email.com> writes: > >> The polynomial fit ist the first step. Now I must to compute >> values on this curve at certain points. In Calc it is very easy. >> I put the value for 'x' on the stack and press 'sl', Calc ask for >> variable, I type 'x RET' and get a polynom value for my 'x'. But how can >> I do in my lisp code? > > You could use `subst', which takes the form "subst(expr,var,value)". > (Actually, instead of a variable any expression can be substituted for.) > So, if you want the best fit curve evaluated at x=2, for example, > you could use > (calc-eval > "simplify(subst(fit(a*x^2+b*x+c,[x],[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]]),x,2))") > or even > (string-to-number ...above...) It works! Great! Very big thanks again! :-) Now I can do: (setq fit (calc-eval "fit(a*x^2+b*x+c,[x],[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]])")) "0.874999999998 x^2 + 0.53500000001 x - 0.325000000011" (string-to-number (calc-eval (format "simplify(subst(%s,x,2))" fit))) 4.245 or: (string-to-number (calc-eval (concat "simplify(subst(" fit ",x,2))"))) 4.245 and this exactly what I want! > (Of course, depending on what else you want Calc to do, it might be more > efficient to write a lisp program that will do the curve fitting.) Yes, definitely, but this calculation will be started very occasionally and computes only some hundreds polynome values. For this task this is more then enough. Thanks again! 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).