emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding a generic mathematical library


From: Emanuel Berg
Subject: Re: Adding a generic mathematical library
Date: Sun, 21 Jul 2024 07:19:02 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Richard Stallman wrote:

>> but I also have a math.el which I yank below if any of it
>> can be used.
>
> Most of the fnctions in this library are very close
> to trivial.

Oh, my! Eli thinks they are so exotic only I use them, RMS
thinks they are close to trivial.

But I have many more files, I'll see what I have that can
maybe impress you and Eli more.

I have already showed you math.el and perm.el, here are two
more. But I'll look thru it all some other day and make
a directory with all of them.

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/stats.el

(require 'cl-lib)

(defun decade ()
  (interactive)
  (cl-loop
    with entries = ()
    for d from 190 to 202
    do (push (how-many (format "^.\\{14\\}%d[[:digit:]]" d)) entries)
    finally (message "%s" (nreverse entries)) ))

(defun random-distribution (n sum &optional min no-error)
  "Return a list of N random numbers.
\nThe sum of the numbers is SUM.
\nEvery number is at least MIN. (default: 0)
\nIf NO-ERROR, recompute unsolvable systems with MIN = 0. (default: nil)"
  (or min (setq min 0))
  (let ((min-share (* n min)))
    (if (< sum min-share)
        (if no-error
            (random-distribution n sum)
          (error "Bogus indata, unsolvable") )
      (let ((vals (make-list n min))
            (pool (- sum min-share)) )
        (cl-loop while (< 0 pool)
                 do (cl-incf (nth (random n) vals))
                    (cl-decf pool) )
        vals) )))

(defalias 'rd #'random-distribution)

;; (apply #'+ (rd 10 100 3)) ; sum is 100, OK
;; (rd 10 100)               ; omitted/default min = 0, OK
;; (rd 10 100 10)            ; border case, but OK
;; (rd 10 100 20)            ; error: Bogus indata, unsolvable
;; (rd 10 100 20 t)          ; unsolvable but no-error, recompute for min = 0
;;
;; 10 example distributions for
;; n = 10, sum = 100, and min = 3,
;; i.e. (rd 10 100 3):
;;
;;   (12  8  8 10  7  6 19  9  7 14)
;;   ( 8 12  7 12  9 10  9  9 12 12)
;;   (14  6 15 11  9  9  6  8 11 11)
;;   ( 8 12  7  9 10  9 10 13 10 12)
;;   (12 12 15  6 10  8  6 10 14  7)
;;   ( 9  7 10  8 11 10  9 10 13 13)
;;   ( 7 13 13 10 13 10  5 13  5 11)
;;   (13  8  6  9 13  8 11  7 10 15)
;;   ( 7 10  5  6 17 14  8 10 14  9)
;;   (11 10  8  9 11 10 14 11  8  8)

(provide 'stats)

;;; -*- lexical-binding: t -*-
;;
;; help from:
;;   https://codereview.stackexchange.com/q/186840
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/str-to-bits.el

(require 'esh-util)

(defun num-to-bits (num)
  (let ((s ""))
    (while (> num 0)
      (setq s (concat (number-to-string (logand num 1)) s))
      (setq num (ash num -1)) )
    (if (string= "" s)
        "0"
      s) ))

(defalias 'dec2bits  #'num-to-bits)
(defalias 'char2bits #'num-to-bits)

(defun str-to-bits (str &optional delim)
  (interactive "sString: ")
  (or delim (setq delim " "))
  (let ((bits))
    (dolist (c (string-to-list str) (mapconcat #'eshell-stringify bits delim))
      (push (num-to-bits c) bits) )))

;; (str-to-bits "What's up Emacs community?") ; 111111 1110000 1110101 [...]

(provide 'str-to-bits)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]