1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-10 09:12:15 +00:00
emacs/lisp/calc/calcalg3.el

1941 lines
64 KiB
EmacsLisp
Raw Normal View History

* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
;;; calcalg3.el --- more algebraic functions for Calc -*- lexical-binding:t -*-
;; Copyright (C) 1990-1993, 2001-2021 Free Software Foundation, Inc.
;; Author: David Gillespie <daveg@synaptics.com>
2001-11-06 18:59:06 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
2001-11-06 18:59:06 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
2001-11-06 18:59:06 +00:00
;;; Commentary:
2001-11-06 18:59:06 +00:00
;;; Code:
2001-11-06 18:59:06 +00:00
;; This file is autoloaded from calc-ext.el.
(require 'calc-ext)
2001-11-06 18:59:06 +00:00
(require 'calc-macs)
;; Declare functions which are defined elsewhere.
(declare-function calc-fit-s-shaped-logistic-curve "calc-nlfit" (arg))
(declare-function calc-fit-bell-shaped-logistic-curve "calc-nlfit" (arg))
(declare-function calc-fit-hubbert-linear-curve "calc-nlfit" (&optional sdv))
(declare-function calc-graph-add-curve "calc-graph" (xdata ydata &optional zdata))
(declare-function calc-graph-lookup "calc-graph" (thing))
(declare-function calc-graph-set-styles "calc-graph" (lines points &optional yerr))
(declare-function math-min-list "calc-arith" (a b))
(declare-function math-max-list "calc-arith" (a b))
(defun math-map-binop (binop args1 args2)
lisp/*.el: Fix typos and other trivial doc fixes * lisp/allout-widgets.el (allout-widgets-auto-activation) (allout-current-decorated-p): * lisp/auth-source.el (auth-source-protocols): * lisp/autorevert.el (auto-revert-set-timer): * lisp/battery.el (battery-mode-line-limit): * lisp/calc/calcalg3.el (math-map-binop): * lisp/calendar/cal-dst.el (calendar-dst-find-startend): * lisp/calendar/cal-mayan.el (calendar-mayan-long-count-to-absolute): * lisp/calendar/calendar.el (calendar-date-echo-text) (calendar-generate-month, calendar-string-spread) (calendar-cursor-to-date, calendar-read, calendar-read-date) (calendar-mark-visible-date, calendar-dayname-on-or-before): * lisp/calendar/diary-lib.el (diary-ordinal-suffix): * lisp/cedet/ede/autoconf-edit.el (autoconf-new-program) (autoconf-find-last-macro, autoconf-parameter-strip): * lisp/cedet/ede/config.el (ede-target-with-config-build): * lisp/cedet/ede/linux.el (ede-linux--detect-architecture) (ede-linux--get-architecture): * lisp/cedet/semantic/complete.el (semantic-collector-calculate-cache) (semantic-displayer-abstract, semantic-displayer-point-position): * lisp/cedet/semantic/format.el (semantic-format-face-alist) (semantic-format-tag-short-doc): * lisp/cedet/semantic/fw.el (semantic-find-file-noselect): * lisp/cedet/semantic/idle.el (semantic-idle-scheduler-work-idle-time) (semantic-idle-breadcrumbs-display-function) (semantic-idle-breadcrumbs-format-tag-list-function): * lisp/cedet/semantic/lex.el (semantic-lex-map-types) (define-lex, define-lex-block-type-analyzer): * lisp/cedet/semantic/senator.el (senator-search-default-tag-filter): * lisp/cedet/semantic/symref.el (semantic-symref-result) (semantic-symref-hit-to-tag-via-db): * lisp/cedet/semantic/symref.el (semantic-symref-tool-baseclass): * lisp/cedet/semantic/tag.el (semantic-tag-new-variable) (semantic-tag-new-include, semantic-tag-new-package) (semantic-tag-set-faux, semantic-create-tag-proxy) (semantic-tag-function-parent) (semantic-tag-components-with-overlays): * lisp/cedet/srecode/cpp.el (srecode-cpp-namespaces) (srecode-semantic-handle-:c, srecode-semantic-apply-tag-to-dict): * lisp/cedet/srecode/dictionary.el (srecode-create-dictionary) (srecode-dictionary-add-entries, srecode-dictionary-lookup-name) (srecode-create-dictionaries-from-tags): * lisp/cmuscheme.el (scheme-compile-region): * lisp/color.el (color-lab-to-lch): * lisp/doc-view.el (doc-view-image-width) (doc-view-set-up-single-converter): * lisp/dynamic-setting.el (font-setting-change-default-font) (dynamic-setting-handle-config-changed-event): * lisp/elec-pair.el (electric-pair-text-pairs) (electric-pair-skip-whitespace-function) (electric-pair-string-bound-function): * lisp/emacs-lisp/avl-tree.el (avl-tree--del-balance) (avl-tree-member, avl-tree-mapcar, avl-tree-iter): * lisp/emacs-lisp/bytecomp.el (byte-compile-generate-call-tree): * lisp/emacs-lisp/checkdoc.el (checkdoc-autofix-flag) (checkdoc-spellcheck-documentation-flag, checkdoc-ispell) (checkdoc-ispell-current-buffer, checkdoc-ispell-interactive) (checkdoc-ispell-message-interactive) (checkdoc-ispell-message-text, checkdoc-ispell-start) (checkdoc-ispell-continue, checkdoc-ispell-comments) (checkdoc-ispell-defun): * lisp/emacs-lisp/cl-generic.el (cl--generic-search-method): * lisp/emacs-lisp/eieio-custom.el (eieio-read-customization-group): * lisp/emacs-lisp/lisp.el (forward-sexp, up-list): * lisp/emacs-lisp/package-x.el (package--archive-contents-from-file): * lisp/emacs-lisp/package.el (package-desc) (package--make-autoloads-and-stuff, package-hidden-regexps): * lisp/emacs-lisp/tcover-ses.el (ses-exercise-startup): * lisp/emacs-lisp/testcover.el (testcover-nohits) (testcover-1value): * lisp/epg.el (epg-receive-keys, epg-start-edit-key): * lisp/erc/erc-backend.el (erc-server-processing-p) (erc-split-line-length, erc-server-coding-system) (erc-server-send, erc-message): * lisp/erc/erc-button.el (erc-button-face, erc-button-alist) (erc-browse-emacswiki): * lisp/erc/erc-ezbounce.el (erc-ezbounce, erc-ezb-get-login): * lisp/erc/erc-fill.el (erc-fill-variable-maximum-indentation): * lisp/erc/erc-log.el (erc-current-logfile): * lisp/erc/erc-match.el (erc-log-match-format) (erc-text-matched-hook): * lisp/erc/erc-netsplit.el (erc-netsplit, erc-netsplit-debug): * lisp/erc/erc-networks.el (erc-server-alist) (erc-networks-alist, erc-current-network): * lisp/erc/erc-ring.el (erc-input-ring-index): * lisp/erc/erc-speedbar.el (erc-speedbar) (erc-speedbar-update-channel): * lisp/erc/erc-stamp.el (erc-timestamp-only-if-changed-flag): * lisp/erc/erc-track.el (erc-track-position-in-mode-line) (erc-track-remove-from-mode-line, erc-modified-channels-update) (erc-track-last-non-erc-buffer, erc-track-sort-by-importance) (erc-track-get-active-buffer): * lisp/erc/erc.el (erc-get-channel-user-list) (erc-echo-notice-hook, erc-echo-notice-always-hook) (erc-wash-quit-reason, erc-format-@nick): * lisp/ffap.el (ffap-latex-mode): * lisp/files.el (abort-if-file-too-large) (dir-locals--get-sort-score, buffer-stale--default-function): * lisp/filesets.el (filesets-tree-max-level, filesets-data) (filesets-update-pre010505): * lisp/gnus/gnus-agent.el (gnus-agent-flush-cache): * lisp/gnus/gnus-art.el (gnus-article-encrypt-protocol) (gnus-button-prefer-mid-or-mail): * lisp/gnus/gnus-cus.el (gnus-group-parameters): * lisp/gnus/gnus-demon.el (gnus-demon-handlers) (gnus-demon-run-callback): * lisp/gnus/gnus-dired.el (gnus-dired-print): * lisp/gnus/gnus-icalendar.el (gnus-icalendar-event-from-buffer): * lisp/gnus/gnus-range.el (gnus-range-normalize): * lisp/gnus/gnus-spec.el (gnus-pad-form): * lisp/gnus/gnus-srvr.el (gnus-server-agent, gnus-server-cloud) (gnus-server-opened, gnus-server-closed, gnus-server-denied) (gnus-server-offline): * lisp/gnus/gnus-sum.el (gnus-refer-thread-use-nnir) (gnus-refer-thread-limit-to-thread) (gnus-summary-limit-include-thread, gnus-summary-refer-thread) (gnus-summary-find-matching): * lisp/gnus/gnus-util.el (gnus-rescale-image): * lisp/gnus/gnus.el (gnus-summary-line-format, gnus-no-server): * lisp/gnus/mail-source.el (mail-source-incoming-file-prefix): * lisp/gnus/message.el (message-cite-reply-position) (message-cite-style-outlook, message-cite-style-thunderbird) (message-cite-style-gmail, message--send-mail-maybe-partially): * lisp/gnus/mm-extern.el (mm-inline-external-body): * lisp/gnus/mm-partial.el (mm-inline-partial): * lisp/gnus/mml-sec.el (mml-secure-message-sign) (mml-secure-message-sign-encrypt, mml-secure-message-encrypt): * lisp/gnus/mml2015.el (mml2015-epg-key-image) (mml2015-epg-key-image-to-string): * lisp/gnus/nndiary.el (nndiary-reminders, nndiary-get-new-mail): * lisp/gnus/nnheader.el (nnheader-directory-files-is-safe): * lisp/gnus/nnir.el (nnir-search-history) (nnir-imap-search-other, nnir-artlist-length) (nnir-artlist-article, nnir-artitem-group, nnir-artitem-number) (nnir-artitem-rsv, nnir-article-group, nnir-article-number) (nnir-article-rsv, nnir-article-ids, nnir-categorize) (nnir-retrieve-headers-override-function) (nnir-imap-default-search-key, nnir-hyrex-additional-switches) (gnus-group-make-nnir-group, nnir-run-namazu, nnir-read-parms) (nnir-read-parm, nnir-read-server-parm, nnir-search-thread): * lisp/gnus/nnmairix.el (nnmairix-default-group) (nnmairix-propagate-marks): * lisp/gnus/smime.el (smime-keys, smime-crl-check) (smime-verify-buffer, smime-noverify-buffer): * lisp/gnus/spam-report.el (spam-report-url-ping-mm-url): * lisp/gnus/spam.el (spam-spamassassin-positive-spam-flag-header) (spam-spamassassin-spam-status-header, spam-sa-learn-rebuild) (spam-classifications, spam-check-stat, spam-spamassassin-score): * lisp/help.el (describe-minor-mode-from-symbol): * lisp/hippie-exp.el (hippie-expand-ignore-buffers): * lisp/htmlfontify.el (hfy-optimizations, hfy-face-resolve-face) (hfy-begin-span): * lisp/ibuf-ext.el (ibuffer-update-saved-filters-format) (ibuffer-saved-filters, ibuffer-old-saved-filters-warning) (ibuffer-filtering-qualifiers, ibuffer-repair-saved-filters) (eval, ibuffer-unary-operand, file-extension, directory): * lisp/image-dired.el (image-dired-cmd-pngcrush-options): * lisp/image-mode.el (image-toggle-display): * lisp/international/ccl.el (ccl-compile-read-multibyte-character) (ccl-compile-write-multibyte-character): * lisp/international/kkc.el (kkc-save-init-file): * lisp/international/latin1-disp.el (latin1-display): * lisp/international/ogonek.el (ogonek-name-encoding-alist) (ogonek-information, ogonek-lookup-encoding) (ogonek-deprefixify-region): * lisp/isearch.el (isearch-filter-predicate) (isearch--momentary-message): * lisp/jsonrpc.el (jsonrpc-connection-send) (jsonrpc-process-connection, jsonrpc-shutdown) (jsonrpc--async-request-1): * lisp/language/tibet-util.el (tibetan-char-p): * lisp/mail/feedmail.el (feedmail-queue-use-send-time-for-date) (feedmail-last-chance-hook, feedmail-before-fcc-hook) (feedmail-send-it-immediately-wrapper, feedmail-find-eoh): * lisp/mail/hashcash.el (hashcash-generate-payment) (hashcash-generate-payment-async, hashcash-insert-payment) (hashcash-verify-payment): * lisp/mail/rmail.el (rmail-movemail-variant-in-use) (rmail-get-attr-value): * lisp/mail/rmailmm.el (rmail-mime-prefer-html, rmail-mime): * lisp/mail/rmailsum.el (rmail-summary-show-message): * lisp/mail/supercite.el (sc-raw-mode-toggle): * lisp/man.el (Man-start-calling): * lisp/mh-e/mh-acros.el (mh-do-at-event-location) (mh-iterate-on-messages-in-region, mh-iterate-on-range): * lisp/mh-e/mh-alias.el (mh-alias-system-aliases) (mh-alias-reload, mh-alias-ali) (mh-alias-canonicalize-suggestion, mh-alias-add-alias-to-file) (mh-alias-add-alias): * lisp/mouse.el (mouse-save-then-kill): * lisp/net/browse-url.el (browse-url-default-macosx-browser): * lisp/net/eudc.el (eudc-set, eudc-variable-protocol-value) (eudc-variable-server-value, eudc-update-variable) (eudc-expand-inline): * lisp/net/eudcb-bbdb.el (eudc-bbdb-format-record-as-result): * lisp/net/eudcb-ldap.el (eudc-ldap-get-field-list): * lisp/net/pop3.el (pop3-list): * lisp/net/soap-client.el (soap-namespace-put) (soap-xs-parse-sequence, soap-parse-envelope): * lisp/net/soap-inspect.el (soap-inspect-xs-complex-type): * lisp/nxml/rng-xsd.el (rng-xsd-date-to-days): * lisp/org/ob-C.el (org-babel-prep-session:C) (org-babel-load-session:C): * lisp/org/ob-J.el (org-babel-execute:J): * lisp/org/ob-asymptote.el (org-babel-prep-session:asymptote): * lisp/org/ob-awk.el (org-babel-execute:awk): * lisp/org/ob-core.el (org-babel-process-file-name): * lisp/org/ob-ebnf.el (org-babel-execute:ebnf): * lisp/org/ob-forth.el (org-babel-execute:forth): * lisp/org/ob-fortran.el (org-babel-execute:fortran) (org-babel-prep-session:fortran, org-babel-load-session:fortran): * lisp/org/ob-groovy.el (org-babel-execute:groovy): * lisp/org/ob-io.el (org-babel-execute:io): * lisp/org/ob-js.el (org-babel-execute:js): * lisp/org/ob-lilypond.el (org-babel-default-header-args:lilypond) (org-babel-lilypond-compile-post-tangle) (org-babel-lilypond-display-pdf-post-tangle) (org-babel-lilypond-tangle) (org-babel-lilypond-execute-tangled-ly) (org-babel-lilypond-compile-lilyfile) (org-babel-lilypond-check-for-compile-error) (org-babel-lilypond-process-compile-error) (org-babel-lilypond-mark-error-line) (org-babel-lilypond-parse-error-line) (org-babel-lilypond-attempt-to-open-pdf) (org-babel-lilypond-attempt-to-play-midi) (org-babel-lilypond-switch-extension) (org-babel-lilypond-set-header-args): * lisp/org/ob-lua.el (org-babel-prep-session:lua): * lisp/org/ob-picolisp.el (org-babel-execute:picolisp): * lisp/org/ob-processing.el (org-babel-prep-session:processing): * lisp/org/ob-python.el (org-babel-prep-session:python): * lisp/org/ob-scheme.el (org-babel-scheme-capture-current-message) (org-babel-scheme-execute-with-geiser, org-babel-execute:scheme): * lisp/org/ob-shen.el (org-babel-execute:shen): * lisp/org/org-agenda.el (org-agenda-entry-types) (org-agenda-move-date-from-past-immediately-to-today) (org-agenda-time-grid, org-agenda-sorting-strategy) (org-agenda-filter-by-category, org-agenda-forward-block): * lisp/org/org-colview.el (org-columns--overlay-text): * lisp/org/org-faces.el (org-verbatim, org-cycle-level-faces): * lisp/org/org-indent.el (org-indent-set-line-properties): * lisp/org/org-macs.el (org-get-limited-outline-regexp): * lisp/org/org-mobile.el (org-mobile-files): * lisp/org/org.el (org-use-fast-todo-selection) (org-extend-today-until, org-use-property-inheritance) (org-refresh-effort-properties, org-open-at-point-global) (org-track-ordered-property-with-tag, org-shiftright): * lisp/org/ox-html.el (org-html-checkbox-type): * lisp/org/ox-man.el (org-man-source-highlight) (org-man-verse-block): * lisp/org/ox-publish.el (org-publish-sitemap-default): * lisp/outline.el (outline-head-from-level): * lisp/progmodes/dcl-mode.el (dcl-back-to-indentation-1) (dcl-calc-command-indent, dcl-indent-to): * lisp/progmodes/flymake.el (flymake-make-diagnostic) (flymake--overlays, flymake-diagnostic-functions) (flymake-diagnostic-types-alist, flymake--backend-state) (flymake-is-running, flymake--collect, flymake-mode): * lisp/progmodes/gdb-mi.el (gdb-threads-list, gdb, gdb-non-stop) (gdb-buffers, gdb-gud-context-call, gdb-jsonify-buffer): * lisp/progmodes/grep.el (grep-error-screen-columns): * lisp/progmodes/gud.el (gud-prev-expr): * lisp/progmodes/ps-mode.el (ps-mode, ps-mode-target-column) (ps-run-goto-error): * lisp/progmodes/python.el (python-eldoc-get-doc) (python-eldoc-function-timeout-permanent, python-eldoc-function): * lisp/shadowfile.el (shadow-make-group): * lisp/speedbar.el (speedbar-obj-do-check): * lisp/textmodes/flyspell.el (flyspell-auto-correct-previous-hook): * lisp/textmodes/reftex-cite.el (reftex-bib-or-thebib): * lisp/textmodes/reftex-index.el (reftex-index-goto-entry) (reftex-index-kill, reftex-index-undo): * lisp/textmodes/reftex-parse.el (reftex-context-substring): * lisp/textmodes/reftex.el (reftex-TeX-master-file): * lisp/textmodes/rst.el (rst-next-hdr, rst-toc) (rst-uncomment-region, rst-font-lock-extend-region-internal): * lisp/thumbs.el (thumbs-mode): * lisp/vc/ediff-util.el (ediff-restore-diff): * lisp/vc/pcvs-defs.el (cvs-cvsroot, cvs-force-dir-tag): * lisp/vc/vc-hg.el (vc-hg--ignore-patterns-valid-p): * lisp/wid-edit.el (widget-field-value-set, string): * lisp/x-dnd.el (x-dnd-version-from-flags) (x-dnd-more-than-3-from-flags): Assorted docfixes.
2019-09-20 22:27:53 +00:00
"Apply BINOP to the elements of the lists ARGS1 and ARGS2."
(if args1
(cons
(funcall binop (car args1) (car args2))
(funcall 'math-map-binop binop (cdr args1) (cdr args2)))))
2001-11-06 18:59:06 +00:00
(defun calc-find-root (var)
(interactive "sVariable(s) to solve for: ")
(calc-slow-wrapper
(let ((func (if (calc-is-hyperbolic) 'calcFunc-wroot 'calcFunc-root)))
(if (or (equal var "") (equal var "$"))
(calc-enter-result 2 "root" (list func
(calc-top-n 3)
(calc-top-n 1)
(calc-top-n 2)))
(let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
(not (string-match "\\[" var)))
(math-read-expr (concat "[" var "]"))
(math-read-expr var))))
(if (eq (car-safe var) 'error)
(error "Bad format in expression: %s" (nth 1 var)))
(calc-enter-result 1 "root" (list func
(calc-top-n 2)
var
(calc-top-n 1))))))))
2001-11-06 18:59:06 +00:00
(defun calc-find-minimum (var)
(interactive "sVariable(s) to minimize over: ")
(calc-slow-wrapper
(let ((func (if (calc-is-inverse)
(if (calc-is-hyperbolic)
'calcFunc-wmaximize 'calcFunc-maximize)
(if (calc-is-hyperbolic)
'calcFunc-wminimize 'calcFunc-minimize)))
(tag (if (calc-is-inverse) "max" "min")))
(if (or (equal var "") (equal var "$"))
(calc-enter-result 2 tag (list func
(calc-top-n 3)
(calc-top-n 1)
(calc-top-n 2)))
(let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
(not (string-match "\\[" var)))
(math-read-expr (concat "[" var "]"))
(math-read-expr var))))
(if (eq (car-safe var) 'error)
(error "Bad format in expression: %s" (nth 1 var)))
(calc-enter-result 1 tag (list func
(calc-top-n 2)
var
(calc-top-n 1))))))))
2001-11-06 18:59:06 +00:00
(defun calc-find-maximum (var)
(interactive "sVariable to maximize over: ")
(calc-invert-func)
(calc-find-minimum var))
2001-11-06 18:59:06 +00:00
(defun calc-poly-interp (arg)
(interactive "P")
(calc-slow-wrapper
(let ((data (calc-top 2)))
(if (or (consp arg) (eq arg 0) (eq arg 2))
(setq data (cons 'vec (calc-top-list 2 2)))
(or (null arg)
(error "Bad prefix argument")))
(if (calc-is-hyperbolic)
(calc-enter-result 1 "rati" (list 'calcFunc-ratint data (calc-top 1)))
(calc-enter-result 1 "poli" (list 'calcFunc-polint data
(calc-top 1)))))))
2001-11-06 18:59:06 +00:00
;; The variables calc-curve-nvars, calc-curve-varnames, calc-curve-model and calc-curve-coefnames are local to calc-curve-fit, but are
;; used by calc-get-fit-variables which is called by calc-curve-fit.
(defvar calc-curve-nvars)
(defvar calc-curve-varnames)
(defvar calc-curve-model)
(defvar calc-curve-coefnames)
2001-11-06 18:59:06 +00:00
(defvar calc-curve-fit-history nil
"History for calc-curve-fit.")
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(defvar calc-graph-no-auto-view)
(defvar calc-fit-to-trail nil)
(defun calc-curve-fit (arg &optional curve-model
curve-coefnames curve-varnames)
2001-11-06 18:59:06 +00:00
(interactive "P")
(calc-slow-wrapper
(setq calc-aborted-prefix nil)
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(let ((calc-curve-model curve-model)
(calc-curve-coefnames curve-coefnames)
(calc-curve-varnames curve-varnames)
(func (if (calc-is-inverse) 'calcFunc-xfit
2001-11-06 18:59:06 +00:00
(if (calc-is-hyperbolic) 'calcFunc-efit
'calcFunc-fit)))
key (which 0)
(nonlinear nil)
(plot nil)
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
n calc-curve-nvars data ;; temp
2001-11-06 18:59:06 +00:00
(homog nil)
(msgs '( "(Press ? for help)"
"1 = linear or multilinear"
"2-9 = polynomial fits; i = interpolating polynomial"
"p = a x^b, ^ = a b^x"
"e = a exp(b x), x = exp(a + b x), l = a + b ln(x)"
"E = a 10^(b x), X = 10^(a + b x), L = a + b log10(x)"
"q = a + b (x-c)^2"
"g = (a/b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2)"
"s = a/(1 + exp(b (x - c)))"
"b = a exp(b (x - c))/(1 + exp(b (x - c)))^2"
"o = (y/x) = a (1 - x/b)"
2001-11-06 18:59:06 +00:00
"h prefix = homogeneous model (no constant term)"
"P prefix = plot result"
2001-11-06 18:59:06 +00:00
"' = alg entry, $ = stack, u = Model1, U = Model2")))
(while (not calc-curve-model)
(message
"Fit to model: %s:%s%s"
(nth which msgs)
(if plot "P" " ")
(if homog "h" ""))
2001-11-06 18:59:06 +00:00
(setq key (read-char))
(cond ((= key ?\C-g)
(keyboard-quit))
((= key ??)
(setq which (% (1+ which) (length msgs))))
((memq key '(?h ?H))
(setq homog (not homog)))
((= key ?P)
(if plot
(setq plot nil)
(let ((data (calc-top 1)))
(if (or
(calc-is-hyperbolic)
(calc-is-inverse)
(not (= (length data) 3)))
(setq plot "Can't plot")
(setq plot data)))))
2001-11-06 18:59:06 +00:00
((progn
(if (eq key ?\$)
(setq n 1)
(setq n 0))
(cond ((null arg)
(setq n (1+ n)
data (calc-top n)))
((or (consp arg) (eq arg 0))
(setq n (+ n 2)
data (calc-top n)
data (if (math-matrixp data)
(append data (list (calc-top (1- n))))
(list 'vec data (calc-top (1- n))))))
((> (setq arg (prefix-numeric-value arg)) 0)
(setq data (cons 'vec (calc-top-list arg (1+ n)))
n (+ n arg)))
(t (error "Bad prefix argument")))
(or (math-matrixp data) (not (cdr (cdr data)))
(error "Data matrix is not a matrix!"))
(setq calc-curve-nvars (- (length data) 2)
calc-curve-coefnames nil
calc-curve-varnames nil)
2001-11-06 18:59:06 +00:00
nil))
((= key ?1) ; linear or multilinear
(calc-get-fit-variables calc-curve-nvars
(1+ calc-curve-nvars) (and homog 0))
(setq calc-curve-model
(math-mul calc-curve-coefnames
(cons 'vec (cons 1 (cdr calc-curve-varnames))))))
2001-11-06 18:59:06 +00:00
((and (>= key ?2) (<= key ?9)) ; polynomial
(calc-get-fit-variables 1 (- key ?0 -1) (and homog 0))
(setq calc-curve-model
(math-build-polynomial-expr (cdr calc-curve-coefnames)
(nth 1 calc-curve-varnames))))
2001-11-06 18:59:06 +00:00
((= key ?i) ; exact polynomial
(calc-get-fit-variables 1 (1- (length (nth 1 data)))
(and homog 0))
(setq calc-curve-model
(math-build-polynomial-expr (cdr calc-curve-coefnames)
(nth 1 calc-curve-varnames))))
2001-11-06 18:59:06 +00:00
((= key ?p) ; power law
(calc-get-fit-variables calc-curve-nvars
(1+ calc-curve-nvars) (and homog 1))
(setq calc-curve-model
(math-mul
(nth 1 calc-curve-coefnames)
(calcFunc-reduce
'(var mul var-mul)
(calcFunc-map
'(var pow var-pow)
calc-curve-varnames
(cons 'vec (cdr (cdr calc-curve-coefnames))))))))
2001-11-06 18:59:06 +00:00
((= key ?^) ; exponential law
(calc-get-fit-variables calc-curve-nvars
(1+ calc-curve-nvars) (and homog 1))
(setq calc-curve-model
(math-mul (nth 1 calc-curve-coefnames)
(calcFunc-reduce
'(var mul var-mul)
(calcFunc-map
'(var pow var-pow)
(cons 'vec (cdr (cdr calc-curve-coefnames)))
calc-curve-varnames)))))
((= key ?s)
(setq nonlinear t)
(setq calc-curve-model t)
(require 'calc-nlfit)
(calc-fit-s-shaped-logistic-curve func))
((= key ?b)
(setq nonlinear t)
(setq calc-curve-model t)
(require 'calc-nlfit)
(calc-fit-bell-shaped-logistic-curve func))
((= key ?o)
(setq nonlinear t)
(setq calc-curve-model t)
(require 'calc-nlfit)
(if (and plot (not (stringp plot)))
(setq plot
(list 'vec
(nth 1 plot)
(cons
'vec
(math-map-binop 'calcFunc-div
(cdr (nth 2 plot))
(cdr (nth 1 plot)))))))
(calc-fit-hubbert-linear-curve func))
2001-11-06 18:59:06 +00:00
((memq key '(?e ?E))
(calc-get-fit-variables calc-curve-nvars
(1+ calc-curve-nvars) (and homog 1))
(setq calc-curve-model
(math-mul (nth 1 calc-curve-coefnames)
(calcFunc-reduce
'(var mul var-mul)
(calcFunc-map
(if (eq key ?e)
'(var exp var-exp)
'(calcFunc-lambda
(var a var-a)
(^ 10 (var a var-a))))
(calcFunc-map
'(var mul var-mul)
(cons 'vec (cdr (cdr calc-curve-coefnames)))
calc-curve-varnames))))))
2001-11-06 18:59:06 +00:00
((memq key '(?x ?X))
(calc-get-fit-variables calc-curve-nvars
(1+ calc-curve-nvars) (and homog 0))
(setq calc-curve-model
(math-mul calc-curve-coefnames
(cons 'vec (cons 1 (cdr calc-curve-varnames)))))
(setq calc-curve-model (if (eq key ?x)
(list 'calcFunc-exp calc-curve-model)
(list '^ 10 calc-curve-model))))
2001-11-06 18:59:06 +00:00
((memq key '(?l ?L))
(calc-get-fit-variables calc-curve-nvars
(1+ calc-curve-nvars) (and homog 0))
(setq calc-curve-model
(math-mul calc-curve-coefnames
(cons 'vec
(cons 1 (cdr (calcFunc-map
(if (eq key ?l)
'(var ln var-ln)
'(var log10
var-log10))
calc-curve-varnames)))))))
2001-11-06 18:59:06 +00:00
((= key ?q)
(calc-get-fit-variables calc-curve-nvars
(1+ (* 2 calc-curve-nvars)) (and homog 0))
(let ((c calc-curve-coefnames)
(v calc-curve-varnames))
(setq calc-curve-model (nth 1 c))
2001-11-06 18:59:06 +00:00
(while (setq v (cdr v) c (cdr (cdr c)))
(setq calc-curve-model (math-add
calc-curve-model
2001-11-06 18:59:06 +00:00
(list '*
(car c)
(list '^
(list '- (car v) (nth 1 c))
2)))))))
((= key ?g)
(setq
calc-curve-model
(math-read-expr
"(AFit / BFit sqrt(2 pi)) exp(-0.5 * ((XFit - CFit) / BFit)^2)")
calc-curve-varnames '(vec (var XFit var-XFit))
calc-curve-coefnames '(vec (var AFit var-AFit)
(var BFit var-BFit)
(var CFit var-CFit)))
(calc-get-fit-variables 1 (1- (length calc-curve-coefnames))
(and homog 1)))
2001-11-06 18:59:06 +00:00
((memq key '(?\$ ?\' ?u ?U))
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(let* (;; (defvars nil)
2001-11-06 18:59:06 +00:00
(record-entry nil))
(if (eq key ?\')
(let* ((calc-dollar-values calc-arg-values)
(calc-dollar-used 0)
(calc-hashes-used 0))
(setq calc-curve-model
(calc-do-alg-entry "" "Model formula: "
nil 'calc-curve-fit-history))
(if (/= (length calc-curve-model) 1)
2001-11-06 18:59:06 +00:00
(error "Bad format"))
(setq calc-curve-model (car calc-curve-model)
2001-11-06 18:59:06 +00:00
record-entry t)
(if (> calc-dollar-used 0)
(setq calc-curve-coefnames
2001-11-06 18:59:06 +00:00
(cons 'vec
(nthcdr (- (length calc-arg-values)
calc-dollar-used)
(reverse calc-arg-values))))
(if (> calc-hashes-used 0)
(setq calc-curve-coefnames
2001-11-06 18:59:06 +00:00
(cons 'vec (calc-invent-args
calc-hashes-used))))))
(progn
(setq calc-curve-model (cond ((eq key ?u)
2001-11-06 18:59:06 +00:00
(calc-var-value 'var-Model1))
((eq key ?U)
(calc-var-value 'var-Model2))
(t (calc-top 1))))
(or calc-curve-model (error "User model not yet defined"))
(if (math-vectorp calc-curve-model)
(if (and (memq (length calc-curve-model) '(3 4))
(not (math-objvecp (nth 1 calc-curve-model)))
(math-vectorp (nth 2 calc-curve-model))
(or (null (nth 3 calc-curve-model))
(math-vectorp (nth 3 calc-curve-model))))
(setq calc-curve-varnames (nth 2 calc-curve-model)
calc-curve-coefnames
(or (nth 3 calc-curve-model)
(cons 'vec
(math-all-vars-but
calc-curve-model
calc-curve-varnames)))
calc-curve-model (nth 1 calc-curve-model))
2001-11-06 18:59:06 +00:00
(error "Incorrect model specifier")))))
(or calc-curve-varnames
(let ((with-y
(eq (car-safe calc-curve-model) 'calcFunc-eq)))
(if calc-curve-coefnames
(calc-get-fit-variables
(if with-y (1+ calc-curve-nvars) calc-curve-nvars)
(1- (length calc-curve-coefnames))
(math-all-vars-but
calc-curve-model calc-curve-coefnames)
nil with-y)
(let* ((coefs (math-all-vars-but calc-curve-model nil))
2001-11-06 18:59:06 +00:00
(vars nil)
(n (-
(length coefs)
calc-curve-nvars
(if with-y 2 1)))
2001-11-06 18:59:06 +00:00
p)
(if (< n 0)
(error "Not enough variables in model"))
(setq p (nthcdr n coefs))
(setq vars (cdr p))
(setcdr p nil)
(calc-get-fit-variables
(if with-y (1+ calc-curve-nvars) calc-curve-nvars)
(length coefs)
vars coefs with-y)))))
2001-11-06 18:59:06 +00:00
(if record-entry
(calc-record (list 'vec calc-curve-model
calc-curve-varnames calc-curve-coefnames)
2001-11-06 18:59:06 +00:00
"modl"))))
(t (beep))))
(unless nonlinear
(let ((calc-fit-to-trail t))
(calc-enter-result n (substring (symbol-name func) 9)
(list func calc-curve-model
(if (= (length calc-curve-varnames) 2)
(nth 1 calc-curve-varnames)
calc-curve-varnames)
(if (= (length calc-curve-coefnames) 2)
(nth 1 calc-curve-coefnames)
calc-curve-coefnames)
data))
(if (consp calc-fit-to-trail)
(calc-record (calc-normalize calc-fit-to-trail) "parm"))))
(when plot
(if (stringp plot)
* erc-stamp.el (erc-echo-timestamp): * erc-lang.el (language): * erc-backend.el (erc-server-connect): Fix buggy call to `message'. * gnus-sum.el (gnus-summary-simplify-subject-query): * ecomplete.el (ecomplete-display-matches): Fix buggy call to `message'. * textmodes/reftex.el (reftex-select-with-char): * textmodes/reftex-toc.el (reftex-toc-do-promote) (reftex-toc-visit-location, reftex-toc-find-section): * textmodes/reftex-index.el (reftex-index-show-entry): * textmodes/org.el (org-cycle-hide-archived-subtrees) (org-table-rotate-recalc-marks, org-mark-ring-push) (org-follow-info-link, org-mhe-get-message-folder-from-index) (org-auto-repeat-maybe, org-store-log-note, org-delete-property) (org-evaluate-time-range, org-edit-agenda-file-list): * textmodes/artist.el (artist-select-next-op-in-list) (artist-select-prev-op-in-list): * term/mac-win.el (mac-service-insert-text): * startup.el (fancy-about-screen): * progmodes/vhdl-mode.el (vhdl-decision-query): * progmodes/idlwave.el (idlwave-template) (idlwave-scroll-completions, idlwave-display-completion-list): * progmodes/ebrowse.el (ebrowse-show-progress): * progmodes/cperl-mode.el (cperl-find-pods-heres): * progmodes/antlr-mode.el (antlr-insert-option-do): * play/mpuz.el (mpuz-close-game): * net/rcirc.el (rcirc-next-active-buffer): * mail/reporter.el (reporter-update-status): * kmacro.el (kmacro-display): * international/ja-dic-cnv.el (skkdic-set-okuri-nasi): * emulation/viper-util.el (viper-save-setting): * emacs-lisp/lisp-mnt.el (lm-verify): * emacs-lisp/edebug.el (edebug-set-mode): * emacs-lisp/checkdoc.el (checkdoc-rogue-spaces, checkdoc-defun): * calendar/calendar.el (calendar-print-day-of-year): * calc/calcalg3.el (calc-curve-fit): * calc/calcalg2.el (math-integral): * calc/calc.el (calc-read-key-sequence, calc-version): * calc/calc-mode.el (calc-set-simplify-mode): * calc/calc-ext.el (calc-fancy-prefix): Fix buggy call to `message'.
2007-12-08 01:02:29 +00:00
(message "%s" plot)
(let ((calc-graph-no-auto-view t))
(calc-graph-delete t)
(calc-graph-add-curve
(calc-graph-lookup (nth 1 plot))
(calc-graph-lookup (nth 2 plot)))
(unless (math-contains-sdev-p (nth 2 data))
(calc-graph-set-styles nil nil)
(calc-graph-point-style nil))
(setq plot (cdr (nth 1 plot)))
(setq plot
(list 'intv
3
(math-sub
(math-min-list (car plot) (cdr plot))
'(float 5 -1))
(math-add
'(float 5 -1)
(math-max-list (car plot) (cdr plot)))))
(calc-graph-add-curve (calc-graph-lookup plot)
(calc-graph-lookup (calc-top-n 1)))
(calc-graph-plot nil)))))))
2001-11-06 18:59:06 +00:00
(defun calc-invent-independent-variables (n &optional but)
(calc-invent-variables n but '(x y z t) "x"))
2001-11-06 18:59:06 +00:00
(defun calc-invent-parameter-variables (n &optional but)
(calc-invent-variables n but '(a b c d) "a"))
2001-11-06 18:59:06 +00:00
(defun calc-invent-variables (num but names base)
(let ((vars nil)
(n num) (nn 0)
var)
(while (and (> n 0) names)
(setq var (math-build-var-name (if (consp names)
(car names)
(concat base (int-to-string
(setq nn (1+ nn)))))))
2001-11-06 18:59:06 +00:00
(or (math-expr-contains (cons 'vec but) var)
(setq vars (cons var vars)
n (1- n)))
(or (symbolp names) (setq names (cdr names))))
(if (= n 0)
(nreverse vars)
(calc-invent-variables num but t base))))
2001-11-06 18:59:06 +00:00
(defun calc-get-fit-variables (nv nc &optional defv defc with-y homog)
(or (= nv (if with-y (1+ calc-curve-nvars) calc-curve-nvars))
2001-11-06 18:59:06 +00:00
(error "Wrong number of data vectors for this type of model"))
(if (integerp defv)
(setq homog defv
defv nil))
(if homog
(setq nc (1- nc)))
(or defv
(setq defv (calc-invent-independent-variables nv)))
(or defc
(setq defc (calc-invent-parameter-variables nc defv)))
Use `format-prompt' when prompting with default values * lisp/woman.el (woman-file-name): * lisp/wid-edit.el (widget-file-prompt-value) (widget-coding-system-prompt-value): * lisp/w32-fns.el (w32-set-system-coding-system): * lisp/vc/vc.el (vc-print-root-log): * lisp/vc/vc-annotate.el (vc-annotate): * lisp/vc/emerge.el (emerge-read-file-name): * lisp/vc/ediff.el (ediff-directories) (ediff-directory-revisions, ediff-directories3) (ediff-merge-directories, ) (ediff-merge-directories-with-ancestor) (ediff-merge-directory-revisions) (ediff-merge-directory-revisions-with-ancestor) (ediff-merge-revisions, ediff-merge-revisions-with-ancestor) (ediff-revision): * lisp/vc/ediff-util.el (ediff-toggle-regexp-match): * lisp/vc/ediff-mult.el (ediff-filegroup-action): * lisp/vc/add-log.el (prompt-for-change-log-name): * lisp/textmodes/table.el (table-insert-row-column) (table-span-cell, table-split-cell-horizontally) (table-split-cell, table-justify, table-generate-source) (table-insert-sequence, table-capture) (table--read-from-minibuffer, table--query-justification): * lisp/textmodes/sgml-mode.el (sgml-tag, sgml-tag-help): * lisp/textmodes/reftex-ref.el (reftex-goto-label): * lisp/textmodes/refer.el (refer-get-bib-files): * lisp/textmodes/css-mode.el (css-lookup-symbol): * lisp/term.el (serial-read-name, serial-read-speed): * lisp/speedbar.el (speedbar-change-initial-expansion-list): * lisp/simple.el (previous-matching-history-element) (set-variable): * lisp/ses.el (ses-read-cell, ses-set-column-width): * lisp/replace.el (query-replace-read-from) (occur-read-primary-args): * lisp/rect.el (string-rectangle, string-insert-rectangle): * lisp/progmodes/tcl.el (tcl-help-on-word): * lisp/progmodes/sh-script.el (sh-set-shell): * lisp/progmodes/python.el (python-eldoc-at-point): * lisp/progmodes/octave.el (octave-completing-read) (octave-update-function-file-comment, octave-insert-defun): * lisp/progmodes/inf-lisp.el (lisp-symprompt): * lisp/progmodes/cperl-mode.el (cperl-info-on-command) (cperl-perldoc): * lisp/progmodes/compile.el (compilation-find-file): * lisp/net/rcirc.el (rcirc-prompt-for-encryption): * lisp/net/eww.el (eww): * lisp/net/browse-url.el (browse-url-with-browser-kind): * lisp/man.el (man): * lisp/mail/sendmail.el (sendmail-query-user-about-smtp): * lisp/mail/mailalias.el (build-mail-aliases): * lisp/mail/mailabbrev.el (merge-mail-abbrevs) (rebuild-mail-abbrevs): * lisp/locate.el (locate-prompt-for-search-string): * lisp/isearch.el (isearch-occur): * lisp/international/ogonek.el (ogonek-read-encoding) (ogonek-read-prefix): * lisp/international/mule.el (read-buffer-file-coding-system) (set-terminal-coding-system, set-keyboard-coding-system) (set-next-selection-coding-system, recode-region): * lisp/international/mule-cmds.el () (universal-coding-system-argument, search-unencodable-char) (select-safe-coding-system-interactively): * lisp/info.el (Info-search, Info-search-backward, Info-menu): * lisp/info-look.el (info-lookup-interactive-arguments): * lisp/imenu.el (imenu--completion-buffer): * lisp/ibuf-ext.el (mode, used-mode, ibuffer-mark-by-mode): * lisp/hi-lock.el (hi-lock-unface-buffer) (hi-lock-read-face-name): * lisp/help.el (view-emacs-news, where-is): * lisp/help-fns.el (describe-variable, describe-symbol) (describe-keymap): * lisp/gnus/mm-decode.el (mm-save-part): * lisp/gnus/gnus-sum.el (gnus-summary-browse-url): * lisp/gnus/gnus-group.el (gnus-group--read-bug-ids) (gnus-group-set-current-level): * lisp/frame.el (make-frame-on-monitor) (close-display-connection, select-frame-by-name): * lisp/format.el (format-encode-buffer, format-encode-region): * lisp/files.el (recode-file-name): * lisp/files-x.el (read-file-local-variable) (read-file-local-variable-value, ) (read-file-local-variable-mode): * lisp/ffap.el (ffap-menu-ask): * lisp/faces.el (face-read-string): * lisp/facemenu.el (facemenu-set-charset): * lisp/erc/erc-dcc.el (erc-dcc-do-GET-command): * lisp/emulation/edt-mapper.el (edt-mapper): * lisp/emacs-lisp/trace.el (trace--read-args) (trace-function-foreground, trace-function-background): * lisp/emacs-lisp/smie.el (smie-config-set-indent): * lisp/emacs-lisp/re-builder.el (reb-change-syntax): * lisp/emacs-lisp/package.el (describe-package): * lisp/emacs-lisp/find-func.el (read-library-name) (find-function-read): * lisp/emacs-lisp/ert.el (ert-read-test-name) (ert-run-tests-interactively): * lisp/emacs-lisp/disass.el (disassemble): * lisp/emacs-lisp/debug.el (debug-on-entry) (debug-on-variable-change): * lisp/emacs-lisp/advice.el (ad-read-advised-function) (ad-read-advice-class, ad-read-advice-name, ad-read-regexp): * lisp/dired-x.el (dired--mark-suffix-interactive-spec): * lisp/dired-aux.el (dired-diff): * lisp/cus-edit.el (custom-variable-prompt, customize-mode) (customize-changed-options): * lisp/completion.el (interactive-completion-string-reader): * lisp/calendar/timeclock.el (timeclock-ask-for-project): * lisp/calc/calcalg3.el (calc-get-fit-variables): * lisp/calc/calc-store.el (calc-edit-variable): * lisp/calc/calc-bin.el (calc-word-size): * lisp/bookmark.el (bookmark-set-internal): * lisp/abbrev.el (read-abbrev-file): Use `format-prompt' for prompting (bug#12443).
2020-09-06 14:56:44 +00:00
(let ((vars (read-string (format-prompt
"Fitting variables"
2020-09-10 18:58:26 +00:00
(format "%s; %s"
Use `format-prompt' when prompting with default values * lisp/woman.el (woman-file-name): * lisp/wid-edit.el (widget-file-prompt-value) (widget-coding-system-prompt-value): * lisp/w32-fns.el (w32-set-system-coding-system): * lisp/vc/vc.el (vc-print-root-log): * lisp/vc/vc-annotate.el (vc-annotate): * lisp/vc/emerge.el (emerge-read-file-name): * lisp/vc/ediff.el (ediff-directories) (ediff-directory-revisions, ediff-directories3) (ediff-merge-directories, ) (ediff-merge-directories-with-ancestor) (ediff-merge-directory-revisions) (ediff-merge-directory-revisions-with-ancestor) (ediff-merge-revisions, ediff-merge-revisions-with-ancestor) (ediff-revision): * lisp/vc/ediff-util.el (ediff-toggle-regexp-match): * lisp/vc/ediff-mult.el (ediff-filegroup-action): * lisp/vc/add-log.el (prompt-for-change-log-name): * lisp/textmodes/table.el (table-insert-row-column) (table-span-cell, table-split-cell-horizontally) (table-split-cell, table-justify, table-generate-source) (table-insert-sequence, table-capture) (table--read-from-minibuffer, table--query-justification): * lisp/textmodes/sgml-mode.el (sgml-tag, sgml-tag-help): * lisp/textmodes/reftex-ref.el (reftex-goto-label): * lisp/textmodes/refer.el (refer-get-bib-files): * lisp/textmodes/css-mode.el (css-lookup-symbol): * lisp/term.el (serial-read-name, serial-read-speed): * lisp/speedbar.el (speedbar-change-initial-expansion-list): * lisp/simple.el (previous-matching-history-element) (set-variable): * lisp/ses.el (ses-read-cell, ses-set-column-width): * lisp/replace.el (query-replace-read-from) (occur-read-primary-args): * lisp/rect.el (string-rectangle, string-insert-rectangle): * lisp/progmodes/tcl.el (tcl-help-on-word): * lisp/progmodes/sh-script.el (sh-set-shell): * lisp/progmodes/python.el (python-eldoc-at-point): * lisp/progmodes/octave.el (octave-completing-read) (octave-update-function-file-comment, octave-insert-defun): * lisp/progmodes/inf-lisp.el (lisp-symprompt): * lisp/progmodes/cperl-mode.el (cperl-info-on-command) (cperl-perldoc): * lisp/progmodes/compile.el (compilation-find-file): * lisp/net/rcirc.el (rcirc-prompt-for-encryption): * lisp/net/eww.el (eww): * lisp/net/browse-url.el (browse-url-with-browser-kind): * lisp/man.el (man): * lisp/mail/sendmail.el (sendmail-query-user-about-smtp): * lisp/mail/mailalias.el (build-mail-aliases): * lisp/mail/mailabbrev.el (merge-mail-abbrevs) (rebuild-mail-abbrevs): * lisp/locate.el (locate-prompt-for-search-string): * lisp/isearch.el (isearch-occur): * lisp/international/ogonek.el (ogonek-read-encoding) (ogonek-read-prefix): * lisp/international/mule.el (read-buffer-file-coding-system) (set-terminal-coding-system, set-keyboard-coding-system) (set-next-selection-coding-system, recode-region): * lisp/international/mule-cmds.el () (universal-coding-system-argument, search-unencodable-char) (select-safe-coding-system-interactively): * lisp/info.el (Info-search, Info-search-backward, Info-menu): * lisp/info-look.el (info-lookup-interactive-arguments): * lisp/imenu.el (imenu--completion-buffer): * lisp/ibuf-ext.el (mode, used-mode, ibuffer-mark-by-mode): * lisp/hi-lock.el (hi-lock-unface-buffer) (hi-lock-read-face-name): * lisp/help.el (view-emacs-news, where-is): * lisp/help-fns.el (describe-variable, describe-symbol) (describe-keymap): * lisp/gnus/mm-decode.el (mm-save-part): * lisp/gnus/gnus-sum.el (gnus-summary-browse-url): * lisp/gnus/gnus-group.el (gnus-group--read-bug-ids) (gnus-group-set-current-level): * lisp/frame.el (make-frame-on-monitor) (close-display-connection, select-frame-by-name): * lisp/format.el (format-encode-buffer, format-encode-region): * lisp/files.el (recode-file-name): * lisp/files-x.el (read-file-local-variable) (read-file-local-variable-value, ) (read-file-local-variable-mode): * lisp/ffap.el (ffap-menu-ask): * lisp/faces.el (face-read-string): * lisp/facemenu.el (facemenu-set-charset): * lisp/erc/erc-dcc.el (erc-dcc-do-GET-command): * lisp/emulation/edt-mapper.el (edt-mapper): * lisp/emacs-lisp/trace.el (trace--read-args) (trace-function-foreground, trace-function-background): * lisp/emacs-lisp/smie.el (smie-config-set-indent): * lisp/emacs-lisp/re-builder.el (reb-change-syntax): * lisp/emacs-lisp/package.el (describe-package): * lisp/emacs-lisp/find-func.el (read-library-name) (find-function-read): * lisp/emacs-lisp/ert.el (ert-read-test-name) (ert-run-tests-interactively): * lisp/emacs-lisp/disass.el (disassemble): * lisp/emacs-lisp/debug.el (debug-on-entry) (debug-on-variable-change): * lisp/emacs-lisp/advice.el (ad-read-advised-function) (ad-read-advice-class, ad-read-advice-name, ad-read-regexp): * lisp/dired-x.el (dired--mark-suffix-interactive-spec): * lisp/dired-aux.el (dired-diff): * lisp/cus-edit.el (custom-variable-prompt, customize-mode) (customize-changed-options): * lisp/completion.el (interactive-completion-string-reader): * lisp/calendar/timeclock.el (timeclock-ask-for-project): * lisp/calc/calcalg3.el (calc-get-fit-variables): * lisp/calc/calc-store.el (calc-edit-variable): * lisp/calc/calc-bin.el (calc-word-size): * lisp/bookmark.el (bookmark-set-internal): * lisp/abbrev.el (read-abbrev-file): Use `format-prompt' for prompting (bug#12443).
2020-09-06 14:56:44 +00:00
(mapconcat 'symbol-name
2020-11-16 15:53:24 +00:00
(mapcar (lambda (v)
(nth 1 v))
Use `format-prompt' when prompting with default values * lisp/woman.el (woman-file-name): * lisp/wid-edit.el (widget-file-prompt-value) (widget-coding-system-prompt-value): * lisp/w32-fns.el (w32-set-system-coding-system): * lisp/vc/vc.el (vc-print-root-log): * lisp/vc/vc-annotate.el (vc-annotate): * lisp/vc/emerge.el (emerge-read-file-name): * lisp/vc/ediff.el (ediff-directories) (ediff-directory-revisions, ediff-directories3) (ediff-merge-directories, ) (ediff-merge-directories-with-ancestor) (ediff-merge-directory-revisions) (ediff-merge-directory-revisions-with-ancestor) (ediff-merge-revisions, ediff-merge-revisions-with-ancestor) (ediff-revision): * lisp/vc/ediff-util.el (ediff-toggle-regexp-match): * lisp/vc/ediff-mult.el (ediff-filegroup-action): * lisp/vc/add-log.el (prompt-for-change-log-name): * lisp/textmodes/table.el (table-insert-row-column) (table-span-cell, table-split-cell-horizontally) (table-split-cell, table-justify, table-generate-source) (table-insert-sequence, table-capture) (table--read-from-minibuffer, table--query-justification): * lisp/textmodes/sgml-mode.el (sgml-tag, sgml-tag-help): * lisp/textmodes/reftex-ref.el (reftex-goto-label): * lisp/textmodes/refer.el (refer-get-bib-files): * lisp/textmodes/css-mode.el (css-lookup-symbol): * lisp/term.el (serial-read-name, serial-read-speed): * lisp/speedbar.el (speedbar-change-initial-expansion-list): * lisp/simple.el (previous-matching-history-element) (set-variable): * lisp/ses.el (ses-read-cell, ses-set-column-width): * lisp/replace.el (query-replace-read-from) (occur-read-primary-args): * lisp/rect.el (string-rectangle, string-insert-rectangle): * lisp/progmodes/tcl.el (tcl-help-on-word): * lisp/progmodes/sh-script.el (sh-set-shell): * lisp/progmodes/python.el (python-eldoc-at-point): * lisp/progmodes/octave.el (octave-completing-read) (octave-update-function-file-comment, octave-insert-defun): * lisp/progmodes/inf-lisp.el (lisp-symprompt): * lisp/progmodes/cperl-mode.el (cperl-info-on-command) (cperl-perldoc): * lisp/progmodes/compile.el (compilation-find-file): * lisp/net/rcirc.el (rcirc-prompt-for-encryption): * lisp/net/eww.el (eww): * lisp/net/browse-url.el (browse-url-with-browser-kind): * lisp/man.el (man): * lisp/mail/sendmail.el (sendmail-query-user-about-smtp): * lisp/mail/mailalias.el (build-mail-aliases): * lisp/mail/mailabbrev.el (merge-mail-abbrevs) (rebuild-mail-abbrevs): * lisp/locate.el (locate-prompt-for-search-string): * lisp/isearch.el (isearch-occur): * lisp/international/ogonek.el (ogonek-read-encoding) (ogonek-read-prefix): * lisp/international/mule.el (read-buffer-file-coding-system) (set-terminal-coding-system, set-keyboard-coding-system) (set-next-selection-coding-system, recode-region): * lisp/international/mule-cmds.el () (universal-coding-system-argument, search-unencodable-char) (select-safe-coding-system-interactively): * lisp/info.el (Info-search, Info-search-backward, Info-menu): * lisp/info-look.el (info-lookup-interactive-arguments): * lisp/imenu.el (imenu--completion-buffer): * lisp/ibuf-ext.el (mode, used-mode, ibuffer-mark-by-mode): * lisp/hi-lock.el (hi-lock-unface-buffer) (hi-lock-read-face-name): * lisp/help.el (view-emacs-news, where-is): * lisp/help-fns.el (describe-variable, describe-symbol) (describe-keymap): * lisp/gnus/mm-decode.el (mm-save-part): * lisp/gnus/gnus-sum.el (gnus-summary-browse-url): * lisp/gnus/gnus-group.el (gnus-group--read-bug-ids) (gnus-group-set-current-level): * lisp/frame.el (make-frame-on-monitor) (close-display-connection, select-frame-by-name): * lisp/format.el (format-encode-buffer, format-encode-region): * lisp/files.el (recode-file-name): * lisp/files-x.el (read-file-local-variable) (read-file-local-variable-value, ) (read-file-local-variable-mode): * lisp/ffap.el (ffap-menu-ask): * lisp/faces.el (face-read-string): * lisp/facemenu.el (facemenu-set-charset): * lisp/erc/erc-dcc.el (erc-dcc-do-GET-command): * lisp/emulation/edt-mapper.el (edt-mapper): * lisp/emacs-lisp/trace.el (trace--read-args) (trace-function-foreground, trace-function-background): * lisp/emacs-lisp/smie.el (smie-config-set-indent): * lisp/emacs-lisp/re-builder.el (reb-change-syntax): * lisp/emacs-lisp/package.el (describe-package): * lisp/emacs-lisp/find-func.el (read-library-name) (find-function-read): * lisp/emacs-lisp/ert.el (ert-read-test-name) (ert-run-tests-interactively): * lisp/emacs-lisp/disass.el (disassemble): * lisp/emacs-lisp/debug.el (debug-on-entry) (debug-on-variable-change): * lisp/emacs-lisp/advice.el (ad-read-advised-function) (ad-read-advice-class, ad-read-advice-name, ad-read-regexp): * lisp/dired-x.el (dired--mark-suffix-interactive-spec): * lisp/dired-aux.el (dired-diff): * lisp/cus-edit.el (custom-variable-prompt, customize-mode) (customize-changed-options): * lisp/completion.el (interactive-completion-string-reader): * lisp/calendar/timeclock.el (timeclock-ask-for-project): * lisp/calc/calcalg3.el (calc-get-fit-variables): * lisp/calc/calc-store.el (calc-edit-variable): * lisp/calc/calc-bin.el (calc-word-size): * lisp/bookmark.el (bookmark-set-internal): * lisp/abbrev.el (read-abbrev-file): Use `format-prompt' for prompting (bug#12443).
2020-09-06 14:56:44 +00:00
defv)
",")
(mapconcat 'symbol-name
2020-11-16 15:53:24 +00:00
(mapcar (lambda (v)
(nth 1 v))
Use `format-prompt' when prompting with default values * lisp/woman.el (woman-file-name): * lisp/wid-edit.el (widget-file-prompt-value) (widget-coding-system-prompt-value): * lisp/w32-fns.el (w32-set-system-coding-system): * lisp/vc/vc.el (vc-print-root-log): * lisp/vc/vc-annotate.el (vc-annotate): * lisp/vc/emerge.el (emerge-read-file-name): * lisp/vc/ediff.el (ediff-directories) (ediff-directory-revisions, ediff-directories3) (ediff-merge-directories, ) (ediff-merge-directories-with-ancestor) (ediff-merge-directory-revisions) (ediff-merge-directory-revisions-with-ancestor) (ediff-merge-revisions, ediff-merge-revisions-with-ancestor) (ediff-revision): * lisp/vc/ediff-util.el (ediff-toggle-regexp-match): * lisp/vc/ediff-mult.el (ediff-filegroup-action): * lisp/vc/add-log.el (prompt-for-change-log-name): * lisp/textmodes/table.el (table-insert-row-column) (table-span-cell, table-split-cell-horizontally) (table-split-cell, table-justify, table-generate-source) (table-insert-sequence, table-capture) (table--read-from-minibuffer, table--query-justification): * lisp/textmodes/sgml-mode.el (sgml-tag, sgml-tag-help): * lisp/textmodes/reftex-ref.el (reftex-goto-label): * lisp/textmodes/refer.el (refer-get-bib-files): * lisp/textmodes/css-mode.el (css-lookup-symbol): * lisp/term.el (serial-read-name, serial-read-speed): * lisp/speedbar.el (speedbar-change-initial-expansion-list): * lisp/simple.el (previous-matching-history-element) (set-variable): * lisp/ses.el (ses-read-cell, ses-set-column-width): * lisp/replace.el (query-replace-read-from) (occur-read-primary-args): * lisp/rect.el (string-rectangle, string-insert-rectangle): * lisp/progmodes/tcl.el (tcl-help-on-word): * lisp/progmodes/sh-script.el (sh-set-shell): * lisp/progmodes/python.el (python-eldoc-at-point): * lisp/progmodes/octave.el (octave-completing-read) (octave-update-function-file-comment, octave-insert-defun): * lisp/progmodes/inf-lisp.el (lisp-symprompt): * lisp/progmodes/cperl-mode.el (cperl-info-on-command) (cperl-perldoc): * lisp/progmodes/compile.el (compilation-find-file): * lisp/net/rcirc.el (rcirc-prompt-for-encryption): * lisp/net/eww.el (eww): * lisp/net/browse-url.el (browse-url-with-browser-kind): * lisp/man.el (man): * lisp/mail/sendmail.el (sendmail-query-user-about-smtp): * lisp/mail/mailalias.el (build-mail-aliases): * lisp/mail/mailabbrev.el (merge-mail-abbrevs) (rebuild-mail-abbrevs): * lisp/locate.el (locate-prompt-for-search-string): * lisp/isearch.el (isearch-occur): * lisp/international/ogonek.el (ogonek-read-encoding) (ogonek-read-prefix): * lisp/international/mule.el (read-buffer-file-coding-system) (set-terminal-coding-system, set-keyboard-coding-system) (set-next-selection-coding-system, recode-region): * lisp/international/mule-cmds.el () (universal-coding-system-argument, search-unencodable-char) (select-safe-coding-system-interactively): * lisp/info.el (Info-search, Info-search-backward, Info-menu): * lisp/info-look.el (info-lookup-interactive-arguments): * lisp/imenu.el (imenu--completion-buffer): * lisp/ibuf-ext.el (mode, used-mode, ibuffer-mark-by-mode): * lisp/hi-lock.el (hi-lock-unface-buffer) (hi-lock-read-face-name): * lisp/help.el (view-emacs-news, where-is): * lisp/help-fns.el (describe-variable, describe-symbol) (describe-keymap): * lisp/gnus/mm-decode.el (mm-save-part): * lisp/gnus/gnus-sum.el (gnus-summary-browse-url): * lisp/gnus/gnus-group.el (gnus-group--read-bug-ids) (gnus-group-set-current-level): * lisp/frame.el (make-frame-on-monitor) (close-display-connection, select-frame-by-name): * lisp/format.el (format-encode-buffer, format-encode-region): * lisp/files.el (recode-file-name): * lisp/files-x.el (read-file-local-variable) (read-file-local-variable-value, ) (read-file-local-variable-mode): * lisp/ffap.el (ffap-menu-ask): * lisp/faces.el (face-read-string): * lisp/facemenu.el (facemenu-set-charset): * lisp/erc/erc-dcc.el (erc-dcc-do-GET-command): * lisp/emulation/edt-mapper.el (edt-mapper): * lisp/emacs-lisp/trace.el (trace--read-args) (trace-function-foreground, trace-function-background): * lisp/emacs-lisp/smie.el (smie-config-set-indent): * lisp/emacs-lisp/re-builder.el (reb-change-syntax): * lisp/emacs-lisp/package.el (describe-package): * lisp/emacs-lisp/find-func.el (read-library-name) (find-function-read): * lisp/emacs-lisp/ert.el (ert-read-test-name) (ert-run-tests-interactively): * lisp/emacs-lisp/disass.el (disassemble): * lisp/emacs-lisp/debug.el (debug-on-entry) (debug-on-variable-change): * lisp/emacs-lisp/advice.el (ad-read-advised-function) (ad-read-advice-class, ad-read-advice-name, ad-read-regexp): * lisp/dired-x.el (dired--mark-suffix-interactive-spec): * lisp/dired-aux.el (dired-diff): * lisp/cus-edit.el (custom-variable-prompt, customize-mode) (customize-changed-options): * lisp/completion.el (interactive-completion-string-reader): * lisp/calendar/timeclock.el (timeclock-ask-for-project): * lisp/calc/calcalg3.el (calc-get-fit-variables): * lisp/calc/calc-store.el (calc-edit-variable): * lisp/calc/calc-bin.el (calc-word-size): * lisp/bookmark.el (bookmark-set-internal): * lisp/abbrev.el (read-abbrev-file): Use `format-prompt' for prompting (bug#12443).
2020-09-06 14:56:44 +00:00
defc)
",")))))
2001-11-06 18:59:06 +00:00
(coefs nil))
(setq vars (if (string-match "\\[" vars)
(math-read-expr vars)
(math-read-expr (concat "[" vars "]"))))
(if (eq (car-safe vars) 'error)
(error "Bad format in expression: %s" (nth 2 vars)))
(or (math-vectorp vars)
(error "Expected a variable or vector of variables"))
(if (equal vars '(vec))
(setq vars (cons 'vec defv)
coefs (cons 'vec defc))
(if (math-vectorp (nth 1 vars))
(if (and (= (length vars) 3)
(math-vectorp (nth 2 vars)))
(setq coefs (nth 2 vars)
vars (nth 1 vars))
(error
"Expected independent variables vector, then parameters vector"))
(setq coefs (cons 'vec defc))))
(or (= nv (1- (length vars)))
(and (not with-y) (= (1+ nv) (1- (length vars))))
(error "Expected %d independent variable%s" nv (if (= nv 1) "" "s")))
(or (= nc (1- (length coefs)))
(error "Expected %d parameter variable%s" nc (if (= nc 1) "" "s")))
(if homog
(setq coefs (cons 'vec (cons homog (cdr coefs)))))
(if calc-curve-varnames
(setq calc-curve-model (math-multi-subst calc-curve-model (cdr calc-curve-varnames) (cdr vars))))
(if calc-curve-coefnames
(setq calc-curve-model (math-multi-subst calc-curve-model (cdr calc-curve-coefnames) (cdr coefs))))
(setq calc-curve-varnames vars
calc-curve-coefnames coefs)))
2001-11-06 18:59:06 +00:00
;;; The following algorithms are from Numerical Recipes chapter 9.
;;; "rtnewt" with safety kludges
(defvar var-DUMMY)
2001-11-06 18:59:06 +00:00
(defun math-newton-root (expr deriv guess orig-guess limit)
(math-working "newton" guess)
(let* ((var-DUMMY guess)
next dval)
(setq next (math-evaluate-expr expr)
dval (math-evaluate-expr deriv))
(if (and (Math-numberp next)
(Math-numberp dval)
(not (Math-zerop dval)))
(progn
(setq next (math-sub guess (math-div next dval)))
(if (math-nearly-equal guess (setq next (math-float next)))
(progn
(setq var-DUMMY next)
(list 'vec next (math-evaluate-expr expr)))
(if (Math-lessp (math-abs-approx (math-sub next orig-guess))
limit)
(math-newton-root expr deriv next orig-guess limit)
(math-reject-arg next "*Newton's method failed to converge"))))
(math-reject-arg next "*Newton's method encountered a singularity"))))
2001-11-06 18:59:06 +00:00
;;; Inspired by "rtsafe"
(defun math-newton-search-root (expr deriv guess vguess ostep oostep
low vlow high vhigh)
(let ((var-DUMMY guess)
(better t)
pos step next vnext)
(if guess
(math-working "newton" (list 'intv 0 low high))
(math-working "bisect" (list 'intv 0 low high))
(setq ostep (math-mul-float (math-sub-float high low)
'(float 5 -1))
guess (math-add-float low ostep)
var-DUMMY guess
vguess (math-evaluate-expr expr))
(or (Math-realp vguess)
(progn
(setq ostep (math-mul-float ostep '(float 6 -1))
guess (math-add-float low ostep)
var-DUMMY guess
vguess (math-evaluate-expr expr))
(or (math-realp vguess)
(progn
(setq ostep (math-mul-float ostep '(float 123456 -5))
guess (math-add-float low ostep)
var-DUMMY guess
vguess nil))))))
(or vguess
(setq vguess (math-evaluate-expr expr)))
(or (Math-realp vguess)
(math-reject-arg guess "*Newton's method encountered a singularity"))
(setq vguess (math-float vguess))
(if (eq (Math-negp vlow) (setq pos (Math-posp vguess)))
(setq high guess
vhigh vguess)
(if (eq (Math-negp vhigh) pos)
(setq low guess
vlow vguess)
(setq better nil)))
(if (or (Math-zerop vguess)
(math-nearly-equal low high))
(list 'vec guess vguess)
(setq step (math-evaluate-expr deriv))
(if (and (Math-realp step)
(not (Math-zerop step))
(setq step (math-div-float vguess (math-float step))
next (math-sub-float guess step))
(not (math-lessp-float high next))
(not (math-lessp-float next low)))
(progn
(setq var-DUMMY next
vnext (math-evaluate-expr expr))
(if (or (Math-zerop vnext)
(math-nearly-equal next guess))
(list 'vec next vnext)
(if (and better
(math-lessp-float (math-abs (or oostep
(math-sub-float
high low)))
(math-abs
(math-mul-float '(float 2 0)
step))))
(math-newton-search-root expr deriv nil nil nil ostep
low vlow high vhigh)
(math-newton-search-root expr deriv next vnext step ostep
low vlow high vhigh))))
(if (or (and (Math-posp vlow) (Math-posp vhigh))
(and (Math-negp vlow) (Math-negp vhigh)))
(math-search-root expr deriv low vlow high vhigh)
(math-newton-search-root expr deriv nil nil nil ostep
low vlow high vhigh))))))
2001-11-06 18:59:06 +00:00
;;; Search for a root in an interval with no overt zero crossing.
;; The variable math-root-widen is local to math-find-root, but
;; is used by math-search-root, which is called (directly and
;; indirectly) by math-find-root.
(defvar math-root-widen)
2001-11-06 18:59:06 +00:00
(defun math-search-root (expr deriv low vlow high vhigh)
(let (found)
(if math-root-widen
2001-11-06 18:59:06 +00:00
(let ((iters 0)
(iterlim (if (eq math-root-widen 'point)
2001-11-06 18:59:06 +00:00
(+ calc-internal-prec 10)
20))
(factor (if (eq math-root-widen 'point)
2001-11-06 18:59:06 +00:00
'(float 9 0)
'(float 16 -1)))
(prev nil) vprev waslow
diff)
(while (or (and (math-posp vlow) (math-posp vhigh))
(and (math-negp vlow) (math-negp vhigh)))
(math-working "widen" (list 'intv 0 low high))
(if (> (setq iters (1+ iters)) iterlim)
(math-reject-arg (list 'intv 0 low high)
"*Unable to bracket root"))
(if (= iters calc-internal-prec)
(setq factor '(float 16 -1)))
(setq diff (math-mul-float (math-sub-float high low) factor))
(if (Math-zerop diff)
(setq high (calcFunc-incr high 10))
(if (math-lessp-float (math-abs vlow) (math-abs vhigh))
(setq waslow t
prev low
low (math-sub low diff)
var-DUMMY low
vprev vlow
vlow (math-evaluate-expr expr))
(setq waslow nil
prev high
high (math-add high diff)
var-DUMMY high
vprev vhigh
vhigh (math-evaluate-expr expr)))))
(if prev
(if waslow
(setq high prev vhigh vprev)
(setq low prev vlow vprev)))
(setq found t))
(or (Math-realp vlow)
(math-reject-arg vlow 'realp))
(or (Math-realp vhigh)
(math-reject-arg vhigh 'realp))
(let ((xvals (list low high))
(yvals (list vlow vhigh))
(pos (Math-posp vlow))
(levels 0)
(step (math-sub-float high low))
xp yp var-DUMMY)
(while (and (<= (setq levels (1+ levels)) 5)
(not found))
(setq xp xvals
yp yvals
step (math-mul-float step '(float 497 -3)))
(while (and (cdr xp) (not found))
(if (Math-realp (car yp))
(setq low (car xp)
vlow (car yp)))
(setq high (math-add-float (car xp) step)
var-DUMMY high
vhigh (math-evaluate-expr expr))
(math-working "search" high)
(if (and (Math-realp vhigh)
(eq (math-negp vhigh) pos))
(setq found t)
(setcdr xp (cons high (cdr xp)))
(setcdr yp (cons vhigh (cdr yp)))
(setq xp (cdr (cdr xp))
yp (cdr (cdr yp))))))))
(if found
(if (Math-zerop vhigh)
(list 'vec high vhigh)
(if (Math-zerop vlow)
(list 'vec low vlow)
(if deriv
(math-newton-search-root expr deriv nil nil nil nil
low vlow high vhigh)
(math-bisect-root expr low vlow high vhigh))))
(math-reject-arg (list 'intv 3 low high)
"*Unable to find a sign change in this interval"))))
2001-11-06 18:59:06 +00:00
;;; "rtbis" (but we should be using Brent's method)
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(defun math-bisect-root (expr low _vlow high vhigh)
2001-11-06 18:59:06 +00:00
(let ((step (math-sub-float high low))
(pos (Math-posp vhigh))
var-DUMMY
mid vmid)
(while (not (or (math-nearly-equal low
(setq step (math-mul-float
step '(float 5 -1))
mid (math-add-float low step)))
(progn
(setq var-DUMMY mid
vmid (math-evaluate-expr expr))
(Math-zerop vmid))))
(math-working "bisect" mid)
(if (eq (Math-posp vmid) pos)
(setq high mid
vhigh vmid)
(setq low mid
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
;; vlow vmid
)))
(list 'vec mid vmid)))
2001-11-06 18:59:06 +00:00
;;; "mnewt"
(defvar math-root-vars [(var DUMMY var-DUMMY)])
2001-11-06 18:59:06 +00:00
(defun math-newton-multi (expr jacob n guess orig-guess limit)
(let ((m -1)
(p guess)
p2 expr-val jacob-val next)
(while (< (setq p (cdr p) m (1+ m)) n)
(set (nth 2 (aref math-root-vars m)) (car p)))
(setq expr-val (math-evaluate-expr expr)
jacob-val (math-evaluate-expr jacob))
(unless (and (math-constp expr-val)
(math-constp jacob-val))
(math-reject-arg guess "*Newton's method encountered a singularity"))
2001-11-06 18:59:06 +00:00
(setq next (math-add guess (math-div (math-float (math-neg expr-val))
(math-float jacob-val)))
p guess p2 next)
(math-working "newton" next)
(while (and (setq p (cdr p) p2 (cdr p2))
(math-nearly-equal (car p) (car p2))))
(if p
(if (Math-lessp (math-abs-approx (math-sub next orig-guess))
limit)
(math-newton-multi expr jacob n next orig-guess limit)
(math-reject-arg nil "*Newton's method failed to converge"))
(list 'vec next expr-val))))
2001-11-06 18:59:06 +00:00
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(defun math-find-root (expr var guess root-widen)
(let ((math-root-widen root-widen))
2001-11-06 18:59:06 +00:00
(if (eq (car-safe expr) 'vec)
(let ((n (1- (length expr)))
(calc-symbolic-mode nil)
(var-DUMMY nil)
(jacob (list 'vec))
p p2 m row)
(unless (eq (car-safe var) 'vec)
(math-reject-arg var 'vectorp))
(unless (= (length var) (1+ n))
(math-dimension-error))
2001-11-06 18:59:06 +00:00
(setq expr (copy-sequence expr))
(while (>= n (length math-root-vars))
(let ((symb (intern (concat "math-root-v"
(int-to-string
(length math-root-vars))))))
(setq math-root-vars (vconcat math-root-vars
(vector (list 'var symb symb))))))
(setq m -1)
(while (< (setq m (1+ m)) n)
(set (nth 2 (aref math-root-vars m)) nil))
(setq m -1 p var)
(while (setq m (1+ m) p (cdr p))
(or (eq (car-safe (car p)) 'var)
(math-reject-arg var "*Expected a variable"))
(setq p2 expr)
(while (setq p2 (cdr p2))
(setcar p2 (math-expr-subst (car p2) (car p)
(aref math-root-vars m)))))
(unless (eq (car-safe guess) 'vec)
(math-reject-arg guess 'vectorp))
(unless (= (length guess) (1+ n))
(math-dimension-error))
2001-11-06 18:59:06 +00:00
(setq guess (copy-sequence guess)
p guess)
(while (setq p (cdr p))
(or (Math-numberp (car guess))
(math-reject-arg guess 'numberp))
(setcar p (math-float (car p))))
(setq p expr)
(while (setq p (cdr p))
(if (assq (car-safe (car p)) calc-tweak-eqn-table)
(setcar p (math-sub (nth 1 (car p)) (nth 2 (car p)))))
(setcar p (math-evaluate-expr (car p)))
(setq row (list 'vec)
m -1)
(while (< (setq m (1+ m)) n)
(nconc row (list (math-evaluate-expr
(or (calcFunc-deriv (car p)
(aref math-root-vars m)
nil t)
(math-reject-arg
expr
"*Formulas must be differentiable"))))))
(nconc jacob (list row)))
(setq m (math-abs-approx guess))
(math-newton-multi expr jacob n guess guess
(if (math-zerop m) '(float 1 3) (math-mul m 10))))
(unless (eq (car-safe var) 'var)
(math-reject-arg var "*Expected a variable"))
(unless (math-expr-contains expr var)
(math-reject-arg expr "*Formula does not contain specified variable"))
2001-11-06 18:59:06 +00:00
(if (assq (car expr) calc-tweak-eqn-table)
(setq expr (math-sub (nth 1 expr) (nth 2 expr))))
(math-with-extra-prec 2
(setq expr (math-expr-subst expr var '(var DUMMY var-DUMMY)))
(let* ((calc-symbolic-mode nil)
(var-DUMMY nil)
(expr (math-evaluate-expr expr))
(deriv (calcFunc-deriv expr '(var DUMMY var-DUMMY) nil t))
low high vlow vhigh)
(and deriv (setq deriv (math-evaluate-expr deriv)))
(setq guess (math-float guess))
(if (and (math-numberp guess)
deriv)
(math-newton-root expr deriv guess guess
(if (math-zerop guess) '(float 1 6)
(math-mul (math-abs-approx guess) 100)))
(if (Math-realp guess)
(setq low guess
high guess
var-DUMMY guess
vlow (math-evaluate-expr expr)
vhigh vlow
math-root-widen 'point)
2001-11-06 18:59:06 +00:00
(if (eq (car guess) 'intv)
(progn
(or (math-constp guess) (math-reject-arg guess 'constp))
(setq low (nth 2 guess)
high (nth 3 guess))
(if (memq (nth 1 guess) '(0 1))
(setq low (calcFunc-incr low 1 high)))
(if (memq (nth 1 guess) '(0 2))
(setq high (calcFunc-incr high -1 low)))
(setq var-DUMMY low
vlow (math-evaluate-expr expr)
var-DUMMY high
vhigh (math-evaluate-expr expr)))
(if (math-complexp guess)
(math-reject-arg "*Complex root finder must have derivative")
(math-reject-arg guess 'realp))))
(if (Math-zerop vlow)
(list 'vec low vlow)
(if (Math-zerop vhigh)
(list 'vec high vhigh)
(if (and deriv (Math-numberp vlow) (Math-numberp vhigh))
(math-newton-search-root expr deriv nil nil nil nil
low vlow high vhigh)
(if (or (and (Math-posp vlow) (Math-posp vhigh))
(and (Math-negp vlow) (Math-negp vhigh))
(not (Math-numberp vlow))
(not (Math-numberp vhigh)))
(math-search-root expr deriv low vlow high vhigh)
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(math-bisect-root expr low vlow high vhigh)))))))))))
2001-11-06 18:59:06 +00:00
(defun calcFunc-root (expr var guess)
(math-find-root expr var guess nil))
2001-11-06 18:59:06 +00:00
(defun calcFunc-wroot (expr var guess)
(math-find-root expr var guess t))
2001-11-06 18:59:06 +00:00
;;; The following algorithms come from Numerical Recipes, chapter 10.
(defvar math-min-vars [(var DUMMY var-DUMMY)])
2001-11-06 18:59:06 +00:00
(defun math-min-eval (expr a)
(if (Math-vectorp a)
(let ((m -1))
(while (setq m (1+ m) a (cdr a))
(set (nth 2 (aref math-min-vars m)) (car a))))
(setq var-DUMMY a))
(setq a (math-evaluate-expr expr))
(if (Math-ratp a)
(math-float a)
(if (eq (car a) 'float)
a
(math-reject-arg a 'realp))))
2001-11-06 18:59:06 +00:00
(defvar math-min-or-max "minimum")
2001-11-06 18:59:06 +00:00
;;; A bracket for a minimum is a < b < c where f(b) < f(a) and f(b) < f(c).
;;; "mnbrak"
(defun math-widen-min (expr a b)
(let ((done nil)
(iters 30)
incr c va vb vc u vu r q ulim bc ba qr)
(or b (setq b (math-mul a '(float 101 -2))))
(setq va (math-min-eval expr a)
vb (math-min-eval expr b))
(if (math-lessp-float va vb)
(setq u a a b b u
vu va va vb vb vu))
(setq c (math-add-float b (math-mul-float '(float 161803 -5)
(math-sub-float b a)))
vc (math-min-eval expr c))
(while (and (not done) (math-lessp-float vc vb))
(math-working "widen" (list 'intv 0 a c))
(if (= (setq iters (1- iters)) 0)
(math-reject-arg nil (format "*Unable to find a %s near the interval"
math-min-or-max)))
(setq bc (math-sub-float b c)
ba (math-sub-float b a)
r (math-mul-float ba (math-sub-float vb vc))
q (math-mul-float bc (math-sub-float vb va))
qr (math-sub-float q r))
(if (math-lessp-float (math-abs qr) '(float 1 -20))
(setq qr (if (math-negp qr) '(float -1 -20) '(float 1 -20))))
(setq u (math-sub-float
b
(math-div-float (math-sub-float (math-mul-float bc q)
(math-mul-float ba r))
(math-mul-float '(float 2 0) qr)))
ulim (math-add-float b (math-mul-float '(float -1 2) bc))
incr (math-negp bc))
(if (if incr (math-lessp-float b u) (math-lessp-float u b))
(if (if incr (math-lessp-float u c) (math-lessp-float c u))
(if (math-lessp-float (setq vu (math-min-eval expr u)) vc)
(setq a b va vb
b u vb vu
done t)
(if (math-lessp-float vb vu)
(setq c u vc vu
done t)
(setq u (math-add-float c (math-mul-float '(float -161803 -5)
bc))
vu (math-min-eval expr u))))
(if (if incr (math-lessp-float u ulim) (math-lessp-float ulim u))
(if (math-lessp-float (setq vu (math-min-eval expr u)) vc)
(setq b c vb vc
c u vc vu
u (math-add-float c (math-mul-float
'(float -161803 -5)
(math-sub-float b c)))
vu (math-min-eval expr u)))
(setq u ulim
vu (math-min-eval expr u))))
(setq u (math-add-float c (math-mul-float '(float -161803 -5)
bc))
vu (math-min-eval expr u)))
(setq a b va vb
b c vb vc
c u vc vu))
(if (math-lessp-float a c)
(list a va b vb c vc)
(list c vc b vb a va))))
2001-11-06 18:59:06 +00:00
(defun math-narrow-min (expr a c intv)
(let ((xvals (list a c))
(yvals (list (math-min-eval expr a)
(math-min-eval expr c)))
(levels 0)
(step (math-sub-float c a))
(found nil)
xp yp b)
(while (and (<= (setq levels (1+ levels)) 5)
(not found))
(setq xp xvals
yp yvals
step (math-mul-float step '(float 497 -3)))
(while (and (cdr xp) (not found))
(setq b (math-add-float (car xp) step))
(math-working "search" b)
(setcdr xp (cons b (cdr xp)))
(setcdr yp (cons (math-min-eval expr b) (cdr yp)))
(if (and (math-lessp-float (nth 1 yp) (car yp))
(math-lessp-float (nth 1 yp) (nth 2 yp)))
(setq found t)
(setq xp (cdr xp)
yp (cdr yp))
(if (and (cdr (cdr yp))
(math-lessp-float (nth 1 yp) (car yp))
(math-lessp-float (nth 1 yp) (nth 2 yp)))
(setq found t)
(setq xp (cdr xp)
yp (cdr yp))))))
(if found
(list (car xp) (car yp)
(nth 1 xp) (nth 1 yp)
(nth 2 xp) (nth 2 yp))
(or (if (math-lessp-float (car yvals) (nth 1 yvals))
(and (memq (nth 1 intv) '(2 3))
(let ((min (car yvals)))
(while (and (setq yvals (cdr yvals))
(math-lessp-float min (car yvals))))
(and (not yvals)
(list (nth 2 intv) min))))
(and (memq (nth 1 intv) '(1 3))
(setq yvals (nreverse yvals))
(let ((min (car yvals)))
(while (and (setq yvals (cdr yvals))
(math-lessp-float min (car yvals))))
(and (not yvals)
(list (nth 3 intv) min)))))
(math-reject-arg nil (format "*Unable to find a %s in the interval"
math-min-or-max))))))
2001-11-06 18:59:06 +00:00
;;; "brent"
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(defun math-brent-min (expr prec a _va x vx b _vb)
2001-11-06 18:59:06 +00:00
(let ((iters (+ 20 (* 5 prec)))
(w x)
(vw vx)
(v x)
(vv vx)
(tol (list 'float 1 (- -1 prec)))
(zeps (list 'float 1 (- -5 prec)))
(e '(float 0 0))
d u vu xm tol1 tol2 etemp p q r xv xw)
2001-11-06 18:59:06 +00:00
(while (progn
(setq xm (math-mul-float '(float 5 -1)
(math-add-float a b))
tol1 (math-add-float
zeps
(math-mul-float tol (math-abs x)))
tol2 (math-mul-float tol1 '(float 2 0)))
(math-lessp-float (math-sub-float tol2
(math-mul-float
'(float 5 -1)
(math-sub-float b a)))
(math-abs (math-sub-float x xm))))
(if (= (setq iters (1- iters)) 0)
(math-reject-arg nil (format "*Unable to converge on a %s"
math-min-or-max)))
(math-working "brent" x)
(if (math-lessp-float (math-abs e) tol1)
(setq e (if (math-lessp-float x xm)
(math-sub-float b x)
(math-sub-float a x))
d (math-mul-float '(float 381966 -6) e))
(setq xw (math-sub-float x w)
r (math-mul-float xw (math-sub-float vx vv))
xv (math-sub-float x v)
q (math-mul-float xv (math-sub-float vx vw))
p (math-sub-float (math-mul-float xv q)
(math-mul-float xw r))
q (math-mul-float '(float 2 0) (math-sub-float q r)))
(if (math-posp q)
(setq p (math-neg-float p))
(setq q (math-neg-float q)))
(setq etemp e
e d)
(if (and (math-lessp-float (math-abs p)
(math-abs (math-mul-float
'(float 5 -1)
(math-mul-float q etemp))))
(math-lessp-float (math-mul-float
q (math-sub-float a x)) p)
(math-lessp-float p (math-mul-float
q (math-sub-float b x))))
(progn
(setq d (math-div-float p q)
u (math-add-float x d))
(if (or (math-lessp-float (math-sub-float u a) tol2)
(math-lessp-float (math-sub-float b u) tol2))
(setq d (if (math-lessp-float xm x)
(math-neg-float tol1)
tol1))))
(setq e (if (math-lessp-float x xm)
(math-sub-float b x)
(math-sub-float a x))
d (math-mul-float '(float 381966 -6) e))))
(setq u (math-add-float x
(if (math-lessp-float (math-abs d) tol1)
(if (math-negp d)
(math-neg-float tol1)
tol1)
d))
vu (math-min-eval expr u))
(if (math-lessp-float vx vu)
(progn
(if (math-lessp-float u x)
(setq a u)
(setq b u))
(if (or (equal w x)
(not (math-lessp-float vw vu)))
(setq v w vv vw
w u vw vu)
(if (or (equal v x)
(equal v w)
(not (math-lessp-float vv vu)))
(setq v u vv vu))))
(if (math-lessp-float u x)
(setq b x)
(setq a x))
(setq v w vv vw
w x vw vx
x u vx vu)))
(list 'vec x vx)))
2001-11-06 18:59:06 +00:00
;;; "powell"
(defun math-powell-min (expr n guesses prec)
(let* ((f1dim (math-line-min-func expr n))
(xi (calcFunc-idn 1 n))
(p (cons 'vec (mapcar 'car guesses)))
(pt p)
(ftol (list 'float 1 (- prec)))
(fret (math-min-eval expr p))
fp ptt fptt xit i ibig del diff res)
(while (progn
(setq fp fret
ibig 0
del '(float 0 0)
i 0)
(while (<= (setq i (1+ i)) n)
(setq fptt fret
res (math-line-min f1dim p
(math-mat-col xi i)
n prec)
p (let ((calc-internal-prec prec))
(math-normalize (car res)))
fret (nth 2 res)
diff (math-abs (math-sub-float fptt fret)))
(if (math-lessp-float del diff)
(setq del diff
ibig i)))
(math-lessp-float
(math-mul-float ftol
(math-add-float (math-abs fp)
(math-abs fret)))
(math-mul-float '(float 2 0)
(math-abs (math-sub-float fp
fret)))))
(setq ptt (math-sub (math-mul '(float 2 0) p) pt)
xit (math-sub p pt)
pt p
fptt (math-min-eval expr ptt))
(if (and (math-lessp-float fptt fp)
(math-lessp-float
(math-mul-float
(math-mul-float '(float 2 0)
(math-add-float
(math-sub-float fp
(math-mul-float '(float 2 0)
fret))
fptt))
(math-sqr-float (math-sub-float
(math-sub-float fp fret) del)))
(math-mul-float del
(math-sqr-float (math-sub-float fp fptt)))))
(progn
(setq res (math-line-min f1dim p xit n prec)
p (car res)
fret (nth 2 res)
i 0)
(while (<= (setq i (1+ i)) n)
(setcar (nthcdr ibig (nth i xi))
(nth i (nth 1 res)))))))
(list 'vec p fret)))
2001-11-06 18:59:06 +00:00
(defun math-line-min-func (expr n)
(let ((m -1))
(while (< (setq m (1+ m)) n)
(set (nth 2 (aref math-min-vars m))
(list '+
(list '*
'(var DUMMY var-DUMMY)
(list 'calcFunc-mrow '(var line-xi line-xi) (1+ m)))
(list 'calcFunc-mrow '(var line-p line-p) (1+ m)))))
(math-evaluate-expr expr)))
2001-11-06 18:59:06 +00:00
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(defun math-line-min (f1dim line-p line-xi _n prec)
2001-11-06 18:59:06 +00:00
(let* ((var-DUMMY nil)
(expr (math-evaluate-expr f1dim))
(params (math-widen-min expr '(float 0 0) '(float 1 0)))
(res (apply 'math-brent-min expr prec params))
(xi (math-mul (nth 1 res) line-xi)))
(list (math-add line-p xi) xi (nth 2 res))))
2001-11-06 18:59:06 +00:00
(defun math-find-minimum (expr var guess min-widen)
(let* ((calc-symbolic-mode nil)
(n 0)
(var-DUMMY nil)
(isvec (math-vectorp var))
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
guesses) ;; g
2001-11-06 18:59:06 +00:00
(or (math-vectorp var)
(setq var (list 'vec var)))
(or (math-vectorp guess)
(setq guess (list 'vec guess)))
(or (= (length var) (length guess))
(math-dimension-error))
(while (setq var (cdr var) guess (cdr guess))
(or (eq (car-safe (car var)) 'var)
(math-reject-arg (car var) "*Expected a variable"))
2001-11-06 18:59:06 +00:00
(or (math-expr-contains expr (car var))
(math-reject-arg (car var)
"*Formula does not contain specified variable"))
(while (>= (1+ n) (length math-min-vars))
(let ((symb (intern (concat "math-min-v"
(int-to-string
(length math-min-vars))))))
(setq math-min-vars (vconcat math-min-vars
(vector (list 'var symb symb))))))
(set (nth 2 (aref math-min-vars n)) nil)
(set (nth 2 (aref math-min-vars (1+ n))) nil)
(if (math-complexp (car guess))
(setq expr (math-expr-subst expr
(car var)
(list '+ (aref math-min-vars n)
(list '*
(aref math-min-vars (1+ n))
'(cplx 0 1))))
guesses (let ((g (math-float (math-complex (car guess)))))
(cons (list (nth 2 g) nil nil)
(cons (list (nth 1 g) nil nil t)
guesses)))
n (+ n 2))
(setq expr (math-expr-subst expr
(car var)
(aref math-min-vars n))
guesses (cons (if (math-realp (car guess))
(list (math-float (car guess)) nil nil)
(if (and (eq (car-safe (car guess)) 'intv)
(math-constp (car guess)))
(list (math-mul
(math-add (nth 2 (car guess))
(nth 3 (car guess)))
'(float 5 -1))
(math-float (nth 2 (car guess)))
(math-float (nth 3 (car guess)))
(car guess))
(math-reject-arg (car guess) 'realp)))
guesses)
n (1+ n))))
(setq guesses (nreverse guesses)
expr (math-evaluate-expr expr))
(if (= n 1)
(let* ((params (if (nth 1 (car guesses))
(if min-widen
(math-widen-min expr
(nth 1 (car guesses))
(nth 2 (car guesses)))
(math-narrow-min expr
(nth 1 (car guesses))
(nth 2 (car guesses))
(nth 3 (car guesses))))
(math-widen-min expr
(car (car guesses))
nil)))
(prec calc-internal-prec)
(res (if (cdr (cdr params))
(math-with-extra-prec (+ calc-internal-prec 2)
(apply 'math-brent-min expr prec params))
(cons 'vec params))))
(if isvec
(list 'vec (list 'vec (nth 1 res)) (nth 2 res))
res))
(let* ((prec calc-internal-prec)
(res (math-with-extra-prec (+ calc-internal-prec 2)
(math-powell-min expr n guesses prec)))
(p (nth 1 res))
(vec (list 'vec)))
(while (setq p (cdr p))
(if (nth 3 (car guesses))
(progn
(nconc vec (list (math-normalize
(list 'cplx (car p) (nth 1 p)))))
(setq p (cdr p)
guesses (cdr guesses)))
(nconc vec (list (car p))))
(setq guesses (cdr guesses)))
(if isvec
(list 'vec vec (nth 2 res))
(list 'vec (nth 1 vec) (nth 2 res)))))))
2001-11-06 18:59:06 +00:00
(defun calcFunc-minimize (expr var guess)
(let ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
(math-min-or-max "minimum"))
(math-find-minimum (math-normalize expr)
(math-normalize var)
(math-normalize guess) nil)))
2001-11-06 18:59:06 +00:00
(defun calcFunc-wminimize (expr var guess)
(let ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
(math-min-or-max "minimum"))
(math-find-minimum (math-normalize expr)
(math-normalize var)
(math-normalize guess) t)))
2001-11-06 18:59:06 +00:00
(defun calcFunc-maximize (expr var guess)
(let* ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
(math-min-or-max "maximum")
(res (math-find-minimum (math-normalize (math-neg expr))
(math-normalize var)
(math-normalize guess) nil)))
(list 'vec (nth 1 res) (math-neg (nth 2 res)))))
2001-11-06 18:59:06 +00:00
(defun calcFunc-wmaximize (expr var guess)
(let* ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
(math-min-or-max "maximum")
(res (math-find-minimum (math-normalize (math-neg expr))
(math-normalize var)
(math-normalize guess) t)))
(list 'vec (nth 1 res) (math-neg (nth 2 res)))))
2001-11-06 18:59:06 +00:00
;;; The following algorithms come from Numerical Recipes, chapter 3.
(defun calcFunc-polint (data x)
(or (math-matrixp data) (math-reject-arg data 'matrixp))
(or (= (length data) 3)
(math-reject-arg data "*Wrong number of data rows"))
(or (> (length (nth 1 data)) 2)
(math-reject-arg data "*Too few data points"))
(if (and (math-vectorp x) (or (math-constp x) math-expand-formulas))
2020-11-16 15:53:24 +00:00
(cons 'vec (mapcar (lambda (x) (calcFunc-polint data x))
2001-11-06 18:59:06 +00:00
(cdr x)))
(or (math-objectp x) math-expand-formulas (math-reject-arg x 'objectp))
(math-with-extra-prec 2
(cons 'vec (math-poly-interp (cdr (nth 1 data)) (cdr (nth 2 data)) x
nil)))))
2001-11-06 18:59:06 +00:00
(put 'calcFunc-polint 'math-expandable t)
(defun calcFunc-ratint (data x)
(or (math-matrixp data) (math-reject-arg data 'matrixp))
(or (= (length data) 3)
(math-reject-arg data "*Wrong number of data rows"))
(or (> (length (nth 1 data)) 2)
(math-reject-arg data "*Too few data points"))
(if (and (math-vectorp x) (or (math-constp x) math-expand-formulas))
2020-11-16 15:53:24 +00:00
(cons 'vec (mapcar (lambda (x) (calcFunc-ratint data x))
2001-11-06 18:59:06 +00:00
(cdr x)))
(or (math-objectp x) math-expand-formulas (math-reject-arg x 'objectp))
(math-with-extra-prec 2
(cons 'vec (math-poly-interp (cdr (nth 1 data)) (cdr (nth 2 data)) x
(cdr (cdr (cdr (nth 1 data)))))))))
2001-11-06 18:59:06 +00:00
(put 'calcFunc-ratint 'math-expandable t)
(defun math-poly-interp (xa ya x ratp)
(let ((n (length xa))
(dif nil)
(ns nil)
(xax nil)
(c (copy-sequence ya))
(d (copy-sequence ya))
(i 0)
(m 0)
y dy (xp xa) xpm cp dp temp)
(while (<= (setq i (1+ i)) n)
(setq xax (cons (math-sub (car xp) x) xax)
xp (cdr xp)
temp (math-abs (car xax)))
(if (or (null dif) (math-lessp temp dif))
(setq dif temp
ns i)))
(setq xax (nreverse xax)
ns (1- ns)
y (nth ns ya))
(if (math-zerop dif)
(list y 0)
(while (< (setq m (1+ m)) n)
(setq i 0
xp xax
xpm (nthcdr m xax)
cp c
dp d)
(while (<= (setq i (1+ i)) (- n m))
(if ratp
(let ((t2 (math-div (math-mul (car xp) (car dp)) (car xpm))))
(setq temp (math-div (math-sub (nth 1 cp) (car dp))
(math-sub t2 (nth 1 cp))))
(setcar dp (math-mul (nth 1 cp) temp))
(setcar cp (math-mul t2 temp)))
(if (math-equal (car xp) (car xpm))
(math-reject-arg (cons 'vec xa) "*Duplicate X values"))
(setq temp (math-div (math-sub (nth 1 cp) (car dp))
(math-sub (car xp) (car xpm))))
(setcar dp (math-mul (car xpm) temp))
(setcar cp (math-mul (car xp) temp)))
(setq cp (cdr cp)
dp (cdr dp)
xp (cdr xp)
xpm (cdr xpm)))
(if (< (+ ns ns) (- n m))
(setq dy (nth ns c))
(setq ns (1- ns)
dy (nth ns d)))
(setq y (math-add y dy)))
(list y dy))))
2001-11-06 18:59:06 +00:00
;;; The following algorithms come from Numerical Recipes, chapter 4.
(defun calcFunc-ninteg (expr var lo hi)
(setq lo (math-evaluate-expr lo)
hi (math-evaluate-expr hi))
(or (math-numberp lo) (math-infinitep lo) (math-reject-arg lo 'numberp))
(or (math-numberp hi) (math-infinitep hi) (math-reject-arg hi 'numberp))
(if (math-lessp hi lo)
(math-neg (calcFunc-ninteg expr var hi lo))
(setq expr (math-expr-subst expr var '(var DUMMY var-DUMMY)))
(let ((var-DUMMY nil)
(calc-symbolic-mode nil)
(calc-prefer-frac nil)
(sum 0))
(setq expr (math-evaluate-expr expr))
(if (equal lo '(neg (var inf var-inf)))
(let ((thi (if (math-lessp hi '(float -2 0))
hi '(float -2 0))))
(setq sum (math-ninteg-romberg
'math-ninteg-midpoint expr
(math-float lo) (math-float thi) 'inf)
lo thi)))
(if (equal hi '(var inf var-inf))
(let ((tlo (if (math-lessp '(float 2 0) lo)
lo '(float 2 0))))
(setq sum (math-add sum
(math-ninteg-romberg
'math-ninteg-midpoint expr
(math-float tlo) (math-float hi) 'inf))
hi tlo)))
(or (math-equal lo hi)
(setq sum (math-add sum
(math-ninteg-romberg
'math-ninteg-midpoint expr
(math-float lo) (math-float hi) nil))))
sum)))
2001-11-06 18:59:06 +00:00
;;; Open Romberg method; "qromo" in section 4.4.
;; The variable math-ninteg-temp is local to math-ninteg-romberg,
;; but is used by math-ninteg-midpoint, which is used by
;; math-ninteg-romberg.
(defvar math-ninteg-temp)
2003-02-04 13:24:35 +00:00
(defun math-ninteg-romberg (func expr lo hi mode)
2001-11-06 18:59:06 +00:00
(let ((curh '(float 1 0))
(h nil)
(s nil)
(j 0)
(ss nil)
(prec calc-internal-prec)
(math-ninteg-temp nil))
2001-11-06 18:59:06 +00:00
(math-with-extra-prec 2
;; Limit on "j" loop must be 14 or less to keep "it" from overflowing.
(or (while (and (null ss) (<= (setq j (1+ j)) 8))
(setq s (nconc s (list (funcall func expr lo hi mode)))
h (nconc h (list curh)))
(if (>= j 3)
(let ((res (math-poly-interp h s '(float 0 0) nil)))
(if (math-lessp (math-abs (nth 1 res))
(calcFunc-scf (math-abs (car res))
(- prec)))
(setq ss (car res)))))
2001-11-06 18:59:06 +00:00
(if (>= j 5)
(setq s (cdr s)
h (cdr h)))
(setq curh (math-div-float curh '(float 9 0))))
ss
(math-reject-arg nil (format "*Integral failed to converge"))))))
2001-11-06 18:59:06 +00:00
(defun math-ninteg-evaluate (expr x mode)
(if (eq mode 'inf)
(setq x (math-div '(float 1 0) x)))
(let* ((var-DUMMY x)
(res (math-evaluate-expr expr)))
(or (Math-numberp res)
(math-reject-arg res "*Integrand does not evaluate to a number"))
(if (eq mode 'inf)
(setq res (math-mul res (math-sqr x))))
res))
2001-11-06 18:59:06 +00:00
(defun math-ninteg-midpoint (expr lo hi mode) ; uses "math-ninteg-temp"
2001-11-06 18:59:06 +00:00
(if (eq mode 'inf)
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(let (;; (math-infinite-mode t) ;Unused!
temp)
2001-11-06 18:59:06 +00:00
(setq temp (math-div 1 lo)
lo (math-div 1 hi)
hi temp)))
(if math-ninteg-temp
(let* ((it3 (* 3 (car math-ninteg-temp)))
(math-working-step-2 (* 2 (car math-ninteg-temp)))
2001-11-06 18:59:06 +00:00
(math-working-step 0)
(range (math-sub hi lo))
(del (math-div range (math-float it3)))
(del2 (math-add del del))
(del3 (math-add del del2))
(x (math-add lo (math-mul '(float 5 -1) del)))
(sum '(float 0 0))
(j 0) temp)
(while (<= (setq j (1+ j)) (car math-ninteg-temp))
2001-11-06 18:59:06 +00:00
(setq math-working-step (1+ math-working-step)
temp (math-ninteg-evaluate expr x mode)
math-working-step (1+ math-working-step)
sum (math-add sum (math-add temp (math-ninteg-evaluate
expr (math-add x del2)
mode)))
x (math-add x del3)))
(setq math-ninteg-temp (list it3
(math-add (math-div (nth 1 math-ninteg-temp)
'(float 3 0))
(math-mul sum del)))))
(setq math-ninteg-temp (list 1 (math-mul
(math-sub hi lo)
(math-ninteg-evaluate
expr
(math-mul (math-add lo hi) '(float 5 -1))
mode)))))
(nth 1 math-ninteg-temp))
2001-11-06 18:59:06 +00:00
;;; The following algorithms come from Numerical Recipes, chapter 14.
(defvar math-dummy-vars [(var DUMMY var-DUMMY)])
(defvar math-dummy-counter 0)
2001-11-06 18:59:06 +00:00
(defun math-dummy-variable ()
(if (= math-dummy-counter (length math-dummy-vars))
(let ((symb (intern (format "math-dummy-%d" math-dummy-counter))))
(setq math-dummy-vars (vconcat math-dummy-vars
(vector (list 'var symb symb))))))
(set (nth 2 (aref math-dummy-vars math-dummy-counter)) nil)
(prog1
(aref math-dummy-vars math-dummy-counter)
(setq math-dummy-counter (1+ math-dummy-counter))))
2001-11-06 18:59:06 +00:00
(defvar math-in-fit 0)
2001-11-06 18:59:06 +00:00
(defun calcFunc-fit (expr vars &optional coefs data)
(let ((math-in-fit 10))
(math-with-extra-prec 2
(math-general-fit expr vars coefs data nil))))
2001-11-06 18:59:06 +00:00
(defun calcFunc-efit (expr vars &optional coefs data)
(let ((math-in-fit 10))
(math-with-extra-prec 2
(math-general-fit expr vars coefs data 'sdev))))
2001-11-06 18:59:06 +00:00
(defun calcFunc-xfit (expr vars &optional coefs data)
(let ((math-in-fit 10))
(math-with-extra-prec 2
(math-general-fit expr vars coefs data 'full))))
2001-11-06 18:59:06 +00:00
;; The variables math-fit-first-var, math-fit-first-coef and
;; math-fit-new-coefs are local to math-general-fit, but are used by
;; calcFunc-fitvar, calcFunc-fitparam and calcFunc-fitdummy
;; (respectively), which are used by math-general-fit.
(defvar math-fit-first-var)
(defvar math-fit-first-coef)
(defvar math-fit-new-coefs)
2001-11-06 18:59:06 +00:00
(defun math-general-fit (expr vars coefs data mode)
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
(defvar var-YVAL) (defvar var-YVALX)
2001-11-06 18:59:06 +00:00
(let ((calc-simplify-mode nil)
(math-dummy-counter math-dummy-counter)
(math-in-fit 1)
(extended (eq mode 'full))
(math-fit-first-coef math-dummy-counter)
math-fit-first-var
2001-11-06 18:59:06 +00:00
(plain-expr expr)
orig-expr
have-sdevs need-chisq chisq
(x-funcs nil)
(y-filter nil)
y-dummy
(coef-filters nil)
math-fit-new-coefs
2001-11-06 18:59:06 +00:00
(xy-values nil)
(weights nil)
(var-YVAL nil) (var-YVALX nil)
covar beta
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
n m mm v dummy p) ;; nn
2001-11-06 18:59:06 +00:00
;; Validate and parse arguments.
(or data
(if coefs
(setq data coefs
coefs nil)
(if (math-vectorp expr)
(if (memq (length expr) '(3 4))
(setq data vars
vars (nth 2 expr)
coefs (nth 3 expr)
expr (nth 1 expr))
(math-dimension-error))
(setq data vars
vars nil
coefs nil))))
(or (math-matrixp data) (math-reject-arg data 'matrixp))
(setq v (1- (length data))
n (1- (length (nth 1 data))))
(or (math-vectorp vars) (null vars)
(setq vars (list 'vec vars)))
(or (math-vectorp coefs) (null coefs)
(setq coefs (list 'vec coefs)))
(or coefs
(setq coefs (cons 'vec (math-all-vars-but expr vars))))
(or vars
(if (<= (1- (length coefs)) v)
(math-reject-arg coefs "*Not enough variables in model")
(setq coefs (copy-sequence coefs))
(let ((p (nthcdr (- (length coefs) v
(if (eq (car-safe expr) 'calcFunc-eq) 1 0))
coefs)))
(setq vars (cons 'vec (cdr p)))
(setcdr p nil))))
(or (= (1- (length vars)) v)
(= (length vars) v)
(math-reject-arg vars "*Number of variables does not match data"))
(setq m (1- (length coefs)))
(if (< m 1)
(math-reject-arg coefs "*Need at least one parameter"))
;; Rewrite expr in terms of fitparam and fitvar, make into an equation.
(setq p coefs)
(while (setq p (cdr p))
(or (eq (car-safe (car p)) 'var)
(math-reject-arg (car p) "*Expected a variable"))
(setq dummy (math-dummy-variable)
expr (math-expr-subst expr (car p)
(list 'calcFunc-fitparam
(- math-dummy-counter math-fit-first-coef)))))
(setq math-fit-first-var math-dummy-counter
2001-11-06 18:59:06 +00:00
p vars)
(while (setq p (cdr p))
(or (eq (car-safe (car p)) 'var)
(math-reject-arg (car p) "*Expected a variable"))
(setq dummy (math-dummy-variable)
expr (math-expr-subst expr (car p)
(list 'calcFunc-fitvar
(- math-dummy-counter math-fit-first-var)))))
(if (< math-dummy-counter (+ math-fit-first-var v))
2001-11-06 18:59:06 +00:00
(setq dummy (math-dummy-variable))) ; dependent variable may be unnamed
(setq y-dummy dummy
orig-expr expr)
(or (eq (car-safe expr) 'calcFunc-eq)
(setq expr (list 'calcFunc-eq (list 'calcFunc-fitvar v) expr)))
(let ((calc-symbolic-mode nil))
;; Apply rewrites to put expr into a linear-like form.
(setq expr (math-evaluate-expr expr)
expr (math-rewrite (list 'calcFunc-fitmodel expr)
'(var FitRules var-FitRules))
math-in-fit 2
expr (math-evaluate-expr expr))
(or (and (eq (car-safe expr) 'calcFunc-fitsystem)
(= (length expr) 4)
(math-vectorp (nth 2 expr))
(math-vectorp (nth 3 expr))
(> (length (nth 2 expr)) 1)
(= (length (nth 3 expr)) (1+ m)))
(math-reject-arg plain-expr "*Model expression is too complex"))
(setq y-filter (nth 1 expr)
x-funcs (vconcat (cdr (nth 2 expr)))
coef-filters (nth 3 expr)
mm (length x-funcs))
(if (equal y-filter y-dummy)
(setq y-filter nil))
;; Build the (square) system of linear equations to be solved.
(setq beta (cons 'vec (make-list mm 0))
covar (cons 'vec (mapcar 'copy-sequence (make-list mm beta))))
(let* ((ptrs (vconcat (cdr data)))
(isigsq 1)
(xvals (make-vector mm 0))
(i 0)
* lisp/calc/: Use lexical scoping in all the files Includes the following pervasive changes: - Move some defvars earlier in the file so they cover earlier let-bindings - Change dynamically scoped `calc-FOO` or `math-FOO` function arguments to just FOO and then let-bind the `calc-FOO` or `math-FOO` variable explicitly in the body of the function. In some cases, the beginning of the function was changed to refer to FOO so as to delay the binding to a nearby `let` when I could ensure that it did not make a difference. - Add an underscore in front of unused vars or comment them out altogether. - Replace unused `err` arg to `condition-case` with nil. Plus the additional itemized changes below. * lisp/calc/calc-map.el (calcFunc-reducer): * lisp/calc/calc-arith.el (math-setup-declarations): * lisp/calc/calc-help.el (calc-full-help, calc-help-index-entries) (calc-full-help): Use `ignore-errors`. * lisp/calc/calc-embed.el (calc-embedded-modes-change): Declare `the-language` and `the-display-just` as dynamically scoped. * lisp/calc/calc-forms.el (math-setup-year-holidays): Use `dolist`. * lisp/calc/calc-graph.el (calc-graph-set-styles): Use `symbol-value` rather than `eval.` (calc-graph-delete-temps, calc-graph-set-styles): Use ignore-errors. * lisp/calc/calc-macs.el (calc-with-trail-buffer): Add artificial use of `save-buf` to silence compiler warnings in all the cases where `body` doesn't make use of it. * lisp/calc/calc-math.el (math-largest-emacs-expt) (math-smallest-emacs-expt, math-use-emacs-fn): Use ignore-errors. * lisp/calc/calc-mode.el (calc-total-algebraic-mode): Remove "P" from interactive spec since it's not used anyway. * lisp/calc/calc-rewr.el (calc-match): Simplify. * lisp/calc/calc.el (calc-buffer): Give it a global nil value, so it's automatically declared dynbound in any file that requires `calc`. (calcDigit-nondigit): Adjust accordingly. * lisp/calc/calcalg2.el (calcFunc-table): Declare `var-dummy` as dynbound. (math-scan-for-limits): Comment out dead code. * lisp/calc/calcalg3.el (math-general-fit): Declare `var-YVAL` and `var-YVALX` as dynbound.
2020-10-10 20:00:51 +00:00
j k xval yval sigmasqr wt covj covjk covk betaj) ;; lud
2001-11-06 18:59:06 +00:00
(while (<= (setq i (1+ i)) n)
;; Assign various independent variables for this data point.
(setq j 0
sigmasqr nil)
(while (< j v)
(aset ptrs j (cdr (aref ptrs j)))
(setq xval (car (aref ptrs j)))
(if (= j (1- v))
(if sigmasqr
(progn
(if (eq (car-safe xval) 'sdev)
(setq sigmasqr (math-add (math-sqr (nth 2 xval))
sigmasqr)
xval (nth 1 xval)))
(if y-filter
(setq xval (math-make-sdev xval
(math-sqrt sigmasqr))))))
(if (eq (car-safe xval) 'sdev)
(setq sigmasqr (math-add (math-sqr (nth 2 xval))
(or sigmasqr 0))
xval (nth 1 xval))))
(set (nth 2 (aref math-dummy-vars (+ math-fit-first-var j))) xval)
2001-11-06 18:59:06 +00:00
(setq j (1+ j)))
;; Compute Y value for this data point.
(if y-filter
(setq yval (math-evaluate-expr y-filter))
(setq yval (symbol-value (nth 2 y-dummy))))
(if (eq (car-safe yval) 'sdev)
(setq sigmasqr (math-sqr (nth 2 yval))
yval (nth 1 yval)))
(if (= i 1)
(setq have-sdevs sigmasqr
need-chisq (or extended
(and (eq mode 'sdev) (not have-sdevs)))))
(if have-sdevs
(if sigmasqr
(progn
(setq isigsq (math-div 1 sigmasqr))
(if need-chisq
(setq weights (cons isigsq weights))))
(math-reject-arg yval "*Mixed error forms and plain numbers"))
(if sigmasqr
(math-reject-arg yval "*Mixed error forms and plain numbers")))
;; Compute X values for this data point and update covar and beta.
(if (eq (car-safe xval) 'sdev)
(set (nth 2 y-dummy) (nth 1 xval)))
(setq j 0
covj covar
betaj beta)
(while (< j mm)
(setq wt (math-evaluate-expr (aref x-funcs j)))
(aset xvals j wt)
(setq wt (math-mul wt isigsq)
betaj (cdr betaj)
covjk (car (setq covj (cdr covj)))
k 0)
(while (<= k j)
(setq covjk (cdr covjk))
(setcar covjk (math-add (car covjk)
(math-mul wt (aref xvals k))))
(setq k (1+ k)))
(setcar betaj (math-add (car betaj) (math-mul wt yval)))
(setq j (1+ j)))
(if need-chisq
(setq xy-values (cons (append xvals (list yval)) xy-values))))
;; Fill in symmetric half of covar matrix.
(setq j 0
covj covar)
(while (< j (1- mm))
(setq k j
j (1+ j)
covjk (nthcdr j (car (setq covj (cdr covj))))
covk (nthcdr j covar))
(while (< (setq k (1+ k)) mm)
(setq covjk (cdr covjk)
covk (cdr covk))
(setcar covjk (nth j (car covk))))))
;; Solve the linear system.
(if mode
(progn
(setq covar (math-matrix-inv-raw covar))
(if covar
(setq beta (math-mul covar beta))
(if (math-zerop (math-abs beta))
(setq covar (calcFunc-diag 0 (1- (length beta))))
(math-reject-arg orig-expr "*Singular matrix")))
(or (math-vectorp covar)
(setq covar (list 'vec (list 'vec covar)))))
(setq beta (math-div beta covar)))
;; Compute chi-square statistic if necessary.
(if need-chisq
(let (bp xp sum)
(setq chisq 0)
(while xy-values
(setq bp beta
xp (car xy-values)
sum 0)
(while (setq bp (cdr bp))
(setq sum (math-add sum (math-mul (car bp) (car xp)))
xp (cdr xp)))
(setq sum (math-sqr (math-sub (car xp) sum)))
(if weights (setq sum (math-mul sum (car weights))))
(setq chisq (math-add chisq sum)
weights (cdr weights)
xy-values (cdr xy-values)))))
;; Convert coefficients back into original terms.
(setq math-fit-new-coefs (copy-sequence beta))
(let* ((bp math-fit-new-coefs)
2001-11-06 18:59:06 +00:00
(cp covar)
(sigdat 1)
(math-in-fit 3)
(j 0))
(and mode (not have-sdevs)
(setq sigdat (if (<= n mm)
0
(math-div chisq (- n mm)))))
(if mode
(while (setq bp (cdr bp))
(setcar bp (math-make-sdev
(car bp)
(math-sqrt (math-mul (nth (setq j (1+ j))
(car (setq cp (cdr cp))))
sigdat))))))
(setq math-fit-new-coefs (math-evaluate-expr coef-filters))
2001-11-06 18:59:06 +00:00
(if calc-fit-to-trail
(let ((bp math-fit-new-coefs)
2001-11-06 18:59:06 +00:00
(cp coefs)
(vec nil))
(while (setq bp (cdr bp) cp (cdr cp))
(setq vec (cons (list 'calcFunc-eq (car cp) (car bp)) vec)))
(setq calc-fit-to-trail (cons 'vec (nreverse vec)))))))
;; Substitute best-fit coefficients back into original formula.
(setq expr (math-multi-subst
orig-expr
(let ((n v)
(vec nil))
(while (>= n 1)
(setq vec (cons (list 'calcFunc-fitvar n) vec)
n (1- n)))
(setq n m)
(while (>= n 1)
(setq vec (cons (list 'calcFunc-fitparam n) vec)
n (1- n)))
vec)
(append (cdr math-fit-new-coefs) (cdr vars))))
2001-11-06 18:59:06 +00:00
;; Package the result.
(math-normalize
(if extended
(list 'vec expr beta covar
(let ((p coef-filters)
(n 0))
(while (and (setq n (1+ n) p (cdr p))
(eq (car-safe (car p)) 'calcFunc-fitdummy)
(eq (nth 1 (car p)) n)))
(if p
coef-filters
(list 'vec)))
chisq
(if (and have-sdevs (> n mm))
(list 'calcFunc-utpc chisq (- n mm))
'(var nan var-nan)))
expr))))
2001-11-06 18:59:06 +00:00
(defun calcFunc-fitvar (x)
(if (>= math-in-fit 2)
(progn
(setq x (aref math-dummy-vars (+ math-fit-first-var x -1)))
2001-11-06 18:59:06 +00:00
(or (calc-var-value (nth 2 x)) x))
(math-reject-arg x)))
2001-11-06 18:59:06 +00:00
(defun calcFunc-fitparam (x)
(if (>= math-in-fit 2)
(progn
(setq x (aref math-dummy-vars (+ math-fit-first-coef x -1)))
2001-11-06 18:59:06 +00:00
(or (calc-var-value (nth 2 x)) x))
(math-reject-arg x)))
2001-11-06 18:59:06 +00:00
(defun calcFunc-fitdummy (x)
(if (= math-in-fit 3)
(nth x math-fit-new-coefs)
(math-reject-arg x)))
2001-11-06 18:59:06 +00:00
(defun calcFunc-hasfitvars (expr)
(if (Math-primp expr)
0
(if (eq (car expr) 'calcFunc-fitvar)
(nth 1 expr)
(apply 'max (mapcar 'calcFunc-hasfitvars (cdr expr))))))
2001-11-06 18:59:06 +00:00
(defun calcFunc-hasfitparams (expr)
(if (Math-primp expr)
0
(if (eq (car expr) 'calcFunc-fitparam)
(nth 1 expr)
(apply 'max (mapcar 'calcFunc-hasfitparams (cdr expr))))))
2001-11-06 18:59:06 +00:00
(defun math-all-vars-but (expr but)
(let* ((vars (math-all-vars-in expr))
(p but))
(while p
(setq vars (delq (assoc (car-safe p) vars) vars)
p (cdr p)))
2020-11-16 15:53:24 +00:00
(sort (mapcar #'car vars)
(lambda (x y) (string< (nth 1 x) (nth 1 y))))))
2001-11-06 18:59:06 +00:00
;; The variables math-all-vars-vars (the vars for math-all-vars) and
;; math-all-vars-found are local to math-all-vars-in, but are used by
;; math-all-vars-rec which is called by math-all-vars-in.
(defvar math-all-vars-vars)
(defvar math-all-vars-found)
2001-11-06 18:59:06 +00:00
(defun math-all-vars-in (expr)
(let ((math-all-vars-vars nil)
math-all-vars-found)
2001-11-06 18:59:06 +00:00
(math-all-vars-rec expr)
math-all-vars-vars))
2001-11-06 18:59:06 +00:00
(defun math-all-vars-rec (expr)
(if (Math-primp expr)
(if (eq (car-safe expr) 'var)
(or (math-const-var expr)
(if (setq math-all-vars-found (assoc expr math-all-vars-vars))
(setcdr math-all-vars-found (1+ (cdr math-all-vars-found)))
(setq math-all-vars-vars (cons (cons expr 1) math-all-vars-vars)))))
2001-11-06 18:59:06 +00:00
(while (setq expr (cdr expr))
(math-all-vars-rec (car expr)))))
2001-11-06 18:59:06 +00:00
(provide 'calcalg3)
;;; calcalg3.el ends here