;;; -*- Emacs-Lisp -*- ;;; stat.el - Small Set of Statistical Functions Written in Emacs-Lisp ;;; (C)2005 by HIROSE, Yuuji [yuuji@gentei.org] ;;; $Id: stat.el,v 1.1 2005/09/20 18:01:24 yuuji Exp $ ;;; Last Modified Wed Sep 21 02:51:33 2005 on firestorm ;;; Update Count: 14 ;; Usage in ~/.emacs ;; ~~~~~~~~~~~~~~~~~ ;; (autoload 'stat "stat" "Statistical Functions" nil) ;; (autoload 'stat-auto-sum "stat" "Add all numbers in preceding lines" t) ;; (global-set-key "\C-c+" 'stat-auto-sum) ;; ;; Usage in *scratch* buffer ;; ~~~~~~~~~~~~~~~~~~~~~~~~~ ;; Call 'stat function with arbitrary length numbers and type C-j. ;; Ex. ;; (stat 1 2 3 4 5.9) [C-j] ;; ;; Usage of stat-auto-sum ;; ~~~~~~~~~~~~~~~~~~~~~~ ;; The function stat-auto-sum insert a summation of all numbers in the ;; end of preceding lines. If no numbers found in a contiguous line, ;; calculation is ceased at that line. ;; You can call 'stat-auto-sum repeatedly without erasing old summation ;; because this function removes old summation around the point ;; automatically. ;; ;; 行末に数値、という連続した行の次の行で 'stat-auto-sum を呼ぶと、ポイ ;; ント位置にそれらの数値の合計が自動挿入される。現在のポイント位置に既 ;; に数値がある場合はそれを消してから合計を計算する。 (defun stat1 (p) (cond ((and (listp p) (atom (car p))) (let* ((n (length p)) (a (/ (apply '+ p) (float n))) (s2 (apply '+ (mapcar '(lambda (i) (expt (- a i) 2)) p)))) (list (cons "n" n) (cons "min" (apply 'min p)) (cons "max" (apply 'max p)) (cons "avg" a) (cons "stdd" (sqrt (/ s2 (1- n))))))))) (defun stat (&rest p) (mapc '(lambda (i) (insert (format "%s\t%s\n" (car i) (prin1-to-string (cdr i))))) (stat1 p))) (defun stat-commaize (n) (let ((d (int-to-string (floor n))) (s (number-to-string n)) (r "")) (while (string-match "[0-9][0-9][0-9][0-9]$" d) (setq r (concat "," (substring d -3) r) d (substring d 0 -3))) (concat d r (if (string-match "\\." s) (substring s (match-beginning 0)))))) (defun stat-auto-sum () "Insert the summation of all numbers at the end of preceding lines." (interactive) (let ((sum 0) lim nstr comma) (save-excursion (skip-chars-backward "0-9") (if (looking-at "[0-9]+") (delete-region (point) (match-end 0))) (while (and (not (bobp)) (progn (forward-line -1) (setq lim (point)) (end-of-line) (re-search-backward "[0-9,.]" lim t))) (setq nstr "") (while (and (not (bobp)) (looking-at "[0-9.,]")) (let ((c (char-after (point)))) (cond ((= c ?,) (setq comma t)) (t (setq nstr (concat (char-to-string c) nstr))))) (forward-char -1)) (setq sum (+ sum (string-to-number nstr))))) (insert (if comma (stat-commaize sum) (number-to-string sum))))) ; Local variables: ; fill-prefix: ";; " ; paragraph-start: "^$\\| \\|;;$" ; paragraph-separate: "^$\\| \\|;;$" ; buffer-file-coding-system: euc-jp ; coding: euc-jp ; End: