From: Christopher Dimech <dimech@gmx.com>
To: Emanuel Berg <incal@dataswamp.org>
Cc: emacs-devel@gnu.org
Subject: Adding a generic mathematical library
Date: Wed, 17 Jul 2024 04:54:20 +0200 [thread overview]
Message-ID: <trinity-896a9b60-94cf-4014-a50d-bd6d4f2062be-1721184860264@3c-app-mailcom-bs09> (raw)
In-Reply-To: <878qy1at52.fsf@dataswamp.org>
> Sent: Wednesday, July 17, 2024 at 10:06 AM
> From: "Emanuel Berg" <incal@dataswamp.org>
> To: emacs-devel@gnu.org
> Subject: Re: Adding a generic mathematical library
>
> Philip Kaludercic wrote:
>
> >> Someone™ should propose a concrete suggestion.
> >
> > I've rename the thread to get some more attention, and also
> > wanted to mention, that if we do so, I think putting some
> > thought into the design wouldn't be bad. Including an
> > infix-notation macro and perhaps even overloaded operator
> > support ala APL would be a nice thing to have. We'd have to
> > weigh the cost of this kind of flexibility with performance,
> > unless someone were to write a specialised byte-compiler.
>
> Yeah, I've pushed for a math library for a long time.
>
> I have several files that are very close to that (for stats,
> time, permutations and so on) but I also have a math.el which
> I yank below if any of it can be used.
Tools for permutations would be of much benefit.
> I'd love to stay in the loop and read more on this!
>
> ;;; -*- lexical-binding: t -*-
> ;;
> ;; this file:
> ;; https://dataswamp.org/~incal/emacs-init/math.el
>
> (require 'cl-lib)
>
> (require 'dwim) ; https://dataswamp.org/~incal/emacs-init/dwim.el
> (require 'perm) ; https://dataswamp.org/~incal/emacs-init/perm.el
>
> (defun nand (&rest conditions)
> (when (member nil conditions)
> t) )
>
> ;; (nand) ; nil
> ;; (nand 1 2 3) ; nil
> ;; (nand nil) ; t
> ;; (nand 1 2 nil 3) ; t
>
> ;; (not (and)) ; nil
> ;; (not (and 1 2 3)) ; nil
> ;; (not (and nil)) ; t
> ;; (not (and 1 2 nil 3)) ; t
>
> (defalias '** #'expt)
> (defalias 'ceil #'ceiling)
>
> (defun percent-change (from to)
> (if (= from to)
> 0
> (/ (- to from) (abs from) 0.01)))
>
> ;; (percent-change 1 2) ; 100
> ;; (percent-change 1 1) ; 0
> ;; (percent-change 1 0) ; -100
> ;; (percent-change 1 -1) ; -200
> ;;
> ;; (percent-change 0 1) ; 1.0e+INF
> ;; (percent-change 0 0) ; 0
> ;; (percent-change 0 -1) ; -1.0e+INF
> ;;
> ;; (percent-change -1 1) ; 200
> ;; (percent-change -1 0) ; 100
> ;; (percent-change -1 -1) ; 0
> ;; (percent-change -1 -2) ; -100
>
> (defun distance-point (min max)
> (+ min (/ (- max min) 2.0)) )
>
> ;; (distance-point 1 5) ; 3
> ;; (distance-point 1 10) ; 5.5
> ;; (distance-point 60 100) ; 80
>
> (defun // (nom denom)
> (/ nom denom 1.0) )
>
> ;; (/ 8 256) ; 0
> ;; (// 8 256) ; 0.03125
>
> (defun arith-sum (n)
> (cl-loop
> with sum = 0
> for i from 1 to (1- n)
> do (cl-incf sum i)
> finally return sum) )
>
> (defun digs (n)
> (* n (1- n)) )
>
> (defun mean-value (vs)
> (let*((sum (apply #'+ vs))
> (mean (/ sum (length vs) 1.0)) )
> mean) )
>
> (defun mean-str (strs)
> (mean-value (cl-map 'list #'length strs)) )
>
> (defun faster (old new)
> (format "%.1f%%" (* 100 (1- (/ new old 1.0)))) )
>
> ;; (faster 0.2 0.5) ; 150.0%
> ;; (faster 1.35 0.84) ; -37.8%
> ;; (faster 200 400) ; 100.0%
> ;; (faster 1000 1000) ; 0.0%
> ;; (faster 1000 100) ; -90.0%
>
> (defun percent (nom &optional denom str)
> (or denom (setq denom 1))
> (let ((perc (/ nom denom 0.01)))
> (if str
> (format "%.1f%%" perc)
> perc)))
>
> ;; (percent 0.25) ; 25.0
> ;; (percent 50 75) ; 66.66 ...
> ;; (percent (// 1 2) nil t) ; 50.0%
> ;; (percent 1019 22 t) ; 4631.8%
>
> (let ((min-def 0)
> (max-def 9)
> (inc-def 1) )
> (defun interval (&optional min max inc)
> (interactive `(,(read-number "min: " min-def)
> ,(read-number "max: " max-def)
> ,(read-number "inc: " inc-def)) )
> (or min (setq min min-def))
> (or max (setq max max-def))
> (or inc (setq inc inc-def))
> (unless (<= min max)
> (error "Bogus interval") )
> (unless (> inc 0)
> (error "Bogus increment") )
> (cl-loop
> for i from min to max by inc
> collect i) )
> (declare-function interval nil) )
>
> ;; (interval 10 5) ; Bogus interval
> ;; (interval 1 3 -1) ; Bogus increment
> ;; (interval 5 10) ; (5 6 7 8 9 10)
> ;; (interval 1.8 2.0 0.1) ; (1.8 1.9 2.0)
> ;; (interval) ; (0 1 2 3 4 5 6 7 8 9)
> ;; (interval 19 99) ; (19 20 21 ... 97 98 99)
>
> (defun digits-sum (&optional beg end)
> (interactive (use-region))
> (or beg (setq beg (point-min)))
> (or end (setq end (point-max)))
> (let ((sum 0))
> (goto-char beg)
> (while (re-search-forward " [[:digit:].]+ " end t)
> (cl-incf sum (string-to-number (match-string-no-properties 0))) )
> (prog1
> sum
> (message "sum: %.1f" sum) )))
>
> (defalias 'di #'digits-sum)
>
> (defun film-mean ()
> (interactive)
> (let ((scores)
> (beg (point-min))
> (end (point-max)) )
> (save-excursion
> (goto-char beg)
> (while (re-search-forward "[[:digit:]]" end t)
> (when (= (current-column) 73)
> (push (string-to-number (match-string-no-properties 0)) scores) )))
> (when scores
> (message (format "%.3f" (mean-value scores))) )))
>
> (defun positive-integer-p (n)
> (and
> (integerp n)
> (< 0 n) ))
>
> ;; (positive-integer-p 1.5) ; nil
> ;; (positive-integer-p 1) ; t
> ;; (positive-integer-p 0) ; nil
> ;; (positive-integer-p -1) ; nil
>
> (defun string-to-number-number (str)
> (let ((s (string-trim str)))
> (when (string-match-p
> "\\`[+-]?\\([[:digit:]]+\\|[[:digit:]]*\\.[[:digit:]]+\\)\\'" s)
> (string-to-number s) )))
>
> (defun string-to-number-number-test ()
> (cl-map 'list (lambda (e)
> (pcase-let ((`(,a ,b) e))
> (if (and a b)
> (= a b)
> (eq a b) )))
> (list
> (list (string-to-number-number " 10") 10)
> (list (string-to-number-number " 1.5") 1.5)
> (list (string-to-number-number " +0") 0)
> (list (string-to-number-number " 0") 0)
> (list (string-to-number-number " -0.0") -0.0)
> (list (string-to-number-number " -1.5") -1.5)
> (list (string-to-number-number "-10") -10)
> (list (string-to-number-number "123this used to work") nil)
> (list (string-to-number-number "NAN") nil) )))
>
> ;; (string-to-number-number-test)
>
> (defun read-integer ()
> (let ((str)
> (str-number)
> (n) )
> (cl-loop until (progn
> (setq str (read-string "integer: "))
> (if (string= str "0")
> (setq n 0)
> (setq str-number (string-to-number str))
> (unless (= str-number 0)
> (setq n str-number) ))
> (integerp n)) )
> n) )
>
> ;; (read-integer)
>
> (defun compare (old new)
> (format "%.2f%%" (* 100 (/ (- old new)
> (* old 1.0) ))))
> ;; (compare 185 80) ; 56.76%
>
> (defun hypotenuse (c1 c2)
> (sqrt (+ (* c1 c1) (* c2 c2))) )
>
> (defun speed (hour minute second km)
> (let*((s (+ second (* 60 minute) (* 60 60 hour)))
> (m (* 1000 km))
> (mps (/ m s)) )
> (* 3.6 mps) ))
>
> ;; (speed 04 44 10 42.195) ; 8.909208211143694
> ;; (speed 02 01 39 42.195) ; 20.811344019728732
> ;; (speed 19 04 00 168 ) ; 7.2
>
> (defun binom (n k)
> (/ (cl-factorial n)
> (* (cl-factorial k)
> (cl-factorial (- n k)) )))
>
> (defun product (l)
> (cl-loop with prod = 1
> for i in l do
> (setq prod (* prod i))
> finally return prod) )
>
> ;; (product '(1 2 3 4)) ; 24
>
> (defun tau-ceti (speed)
> (let*((dist (* 11.8 9.46 (expt 10 12)))
> (time (/ dist speed) )
> (days (/ time 24))
> (whole-days (round days)) )
> (format-seconds "%y years"
> (- (float-time (encode-time 0 0 0 whole-days 0 0))
> (float-time (encode-time 0 0 0 0 0 0)) ))))
>
> ;; (tau-ceti 5000) ; 2 548 584 years
>
> (provide 'math)
>
> --
> underground experts united
> https://dataswamp.org/~incal
>
>
>
next prev parent reply other threads:[~2024-07-17 2:54 UTC|newest]
Thread overview: 127+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-12 16:47 Add elisa to GNU ELPA Sergey Kostyaev
2024-07-16 12:54 ` Philip Kaludercic
2024-07-16 13:57 ` Sergey Kostyaev
2024-07-16 16:04 ` Philip Kaludercic
2024-07-16 16:35 ` Sergey Kostyaev
2024-07-17 13:21 ` Andrew Hyatt
2024-07-16 16:41 ` Sergey Kostyaev
2024-07-16 17:02 ` Philip Kaludercic
2024-07-16 17:47 ` Adding a generic mathematical library Philip Kaludercic
2024-07-16 22:06 ` Emanuel Berg
2024-07-17 2:54 ` Christopher Dimech [this message]
2024-07-17 5:58 ` Emanuel Berg
2024-07-19 16:16 ` Richard Stallman
2024-07-19 17:38 ` Christopher Dimech
2024-07-21 5:20 ` Emanuel Berg
2024-07-20 12:45 ` Max Nikulin
2024-07-20 13:53 ` Christopher Dimech
2024-07-21 5:19 ` Emanuel Berg
2024-07-21 6:15 ` Emanuel Berg
2024-07-21 7:40 ` Emanuel Berg
2024-07-21 8:45 ` Emanuel Berg
2024-07-21 8:29 ` Emanuel Berg
2024-07-21 7:27 ` Christopher Dimech
2024-07-21 8:03 ` Emanuel Berg
2024-07-21 9:14 ` Christopher Dimech
2024-07-21 9:48 ` Emanuel Berg
2024-07-21 11:20 ` Emanuel Berg
2024-07-21 11:53 ` Christopher Dimech
2024-07-21 12:10 ` Emanuel Berg
2024-07-21 12:27 ` Emanuel Berg
2024-07-21 12:46 ` Emanuel Berg
2024-07-21 13:03 ` Christopher Dimech
2024-07-21 13:17 ` Emanuel Berg
2024-07-21 14:33 ` Eli Zaretskii
2024-07-21 14:41 ` Christopher Dimech
2024-07-21 14:49 ` Eli Zaretskii
2024-07-21 14:58 ` Christopher Dimech
2024-07-21 15:02 ` Eli Zaretskii
2024-07-21 15:18 ` Christopher Dimech
2024-07-21 13:18 ` Christopher Dimech
2024-07-21 13:26 ` Emanuel Berg
2024-07-21 14:35 ` Christopher Dimech
2024-07-21 19:28 ` Emanuel Berg
2024-07-21 19:33 ` Emanuel Berg
2024-07-21 19:51 ` Emanuel Berg
2024-07-21 20:01 ` Emanuel Berg
2024-07-21 20:17 ` Emanuel Berg
2024-07-21 12:41 ` Christopher Dimech
2024-07-21 13:13 ` Emanuel Berg
2024-07-21 13:41 ` Emanuel Berg
2024-07-21 12:20 ` Emanuel Berg
2024-07-21 12:04 ` Emanuel Berg
2024-07-21 14:30 ` hypotenuse (was: Re: Adding a generic mathematical library) Emanuel Berg
2024-07-21 14:44 ` Eli Zaretskii
2024-07-21 15:00 ` Eli Zaretskii
2024-07-21 15:12 ` Christopher Dimech
2024-07-21 15:42 ` Eli Zaretskii
2024-07-21 16:13 ` Christopher Dimech
2024-07-21 15:54 ` hypotenuse Max Nikulin
2024-07-21 16:12 ` hypotenuse Eli Zaretskii
2024-07-21 16:17 ` hypotenuse Christopher Dimech
2024-07-21 19:01 ` hypotenuse (was: Re: Adding a generic mathematical library) Emanuel Berg
2024-07-21 19:13 ` Emanuel Berg
2024-07-21 17:38 ` tomas
2024-07-17 7:09 ` Adding a generic mathematical library Michael Heerdegen via Emacs development discussions.
2024-07-17 7:54 ` Philip Kaludercic
2024-07-17 7:56 ` Michael Heerdegen via Emacs development discussions.
2024-07-18 6:07 ` Emanuel Berg
2024-07-18 6:45 ` Christopher Dimech
2024-07-18 7:12 ` Emanuel Berg
2024-07-18 7:49 ` Christopher Dimech
2024-07-21 4:56 ` Emanuel Berg
2024-07-18 7:29 ` Eli Zaretskii
2024-07-18 7:57 ` Emanuel Berg
2024-07-18 9:03 ` Eli Zaretskii
2024-07-21 4:52 ` Emanuel Berg
2024-07-18 8:15 ` Emanuel Berg
2024-07-18 9:04 ` Eli Zaretskii
2024-07-18 9:13 ` Christopher Dimech
2024-07-21 4:59 ` Emanuel Berg
2024-07-19 13:22 ` Emanuel Berg
2024-07-19 16:12 ` Christopher Dimech
2024-07-19 16:15 ` Stefan Kangas
2024-07-19 16:29 ` Christopher Dimech
2024-07-19 16:16 ` Richard Stallman
2024-07-19 18:00 ` Christopher Dimech
2024-07-17 21:26 ` Add elisa to GNU ELPA Sergey Kostyaev
2024-07-17 22:12 ` Philip Kaludercic
2024-07-18 3:45 ` Sergey Kostyaev
2024-07-18 11:06 ` Sergey Kostyaev
-- strict thread matches above, loose matches on Subject: below --
2024-07-23 20:00 Adding a generic mathematical library Kepa
2024-07-25 4:19 ` Emanuel Berg
2024-07-25 11:32 ` Christopher Dimech
2024-07-25 14:20 ` Stefan Kangas
2024-07-25 15:16 ` T.V Raman
2024-07-25 15:34 ` Christopher Dimech
2024-07-25 17:12 ` Emanuel Berg
2024-07-25 17:25 ` Emanuel Berg
2024-07-25 14:24 ` Emanuel Berg
2024-07-25 15:49 ` Christopher Dimech
2024-07-25 17:23 ` Emanuel Berg
2024-07-27 14:57 Shouran Ma
2024-07-27 15:49 ` Emanuel Berg
2024-07-27 19:07 Shouran Ma
2024-07-27 19:23 ` Christopher Dimech
2024-07-28 0:29 ` Emanuel Berg
2024-07-28 10:58 ` Christopher Dimech
2024-07-28 11:39 ` Emanuel Berg
2024-07-28 12:28 ` Christopher Dimech
2024-07-28 13:02 ` Emanuel Berg
2024-07-30 2:53 ` Richard Stallman
2024-07-30 3:53 ` Emanuel Berg
2024-07-30 6:26 ` Christopher Dimech
2024-07-30 7:20 ` Emanuel Berg
2024-07-30 11:30 ` Christopher Dimech
2024-07-30 22:56 ` Emanuel Berg
2024-07-31 16:00 ` Michael Heerdegen via Emacs development discussions.
2024-07-31 21:15 ` Emanuel Berg
2024-08-01 6:05 ` Eli Zaretskii
2024-08-01 6:42 ` Emanuel Berg
2024-07-31 21:26 ` Emanuel Berg
2024-07-27 19:40 ` Christopher Dimech
2024-07-28 0:24 ` Emanuel Berg
2024-07-28 13:34 ` Immanuel Litzroth
2024-07-28 13:56 ` Christopher Dimech
2024-07-28 14:07 ` Emanuel Berg
2024-07-28 10:46 ` Emanuel Berg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=trinity-896a9b60-94cf-4014-a50d-bd6d4f2062be-1721184860264@3c-app-mailcom-bs09 \
--to=dimech@gmx.com \
--cc=emacs-devel@gnu.org \
--cc=incal@dataswamp.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).