From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Christopher Dimech Newsgroups: gmane.emacs.devel Subject: Adding a generic mathematical library Date: Wed, 17 Jul 2024 04:54:20 +0200 Message-ID: References: <8734o9sdig.fsf@posteo.net> <87wmllqq66.fsf@posteo.net> <87plrdqnhc.fsf@posteo.net> <87le21qldj.fsf_-_@posteo.net> <878qy1at52.fsf@dataswamp.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34209"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Emanuel Berg Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jul 17 04:55:22 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sTup7-0008hY-Rz for ged-emacs-devel@m.gmane-mx.org; Wed, 17 Jul 2024 04:55:21 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sTuoR-0006DA-DS; Tue, 16 Jul 2024 22:54:39 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sTuoL-00069p-EQ for emacs-devel@gnu.org; Tue, 16 Jul 2024 22:54:34 -0400 Original-Received: from mout.gmx.net ([212.227.15.15]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sTuoD-0002Xr-8t for emacs-devel@gnu.org; Tue, 16 Jul 2024 22:54:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.com; s=s31663417; t=1721184860; x=1721789660; i=dimech@gmx.com; bh=IbuZ6mZhWdvJ9P4NOUb/TzUgsLT0lO3yW1PsLvoHlxg=; h=X-UI-Sender-Class:MIME-Version:Message-ID:From:To:Cc:Subject: Content-Type:Date:In-Reply-To:References: Content-Transfer-Encoding:cc:content-transfer-encoding: content-type:date:from:message-id:mime-version:reply-to:subject: to; b=UdBETBMP1vTUO8RFeuaA095blPkgwTzdBsRNpminkc5KXuEMb5DyqOBUE1nYmJX9 ZzvPmdzZLwLZab5lSPjk3mrdfFu3UPERiGzcgcUt4AJZwKjYIdtrYXUiYS5yqh8do 2QP6D6iKYYAVXfPXLzPk31DyfpVrn/oGEHEiEkl4lyuLqQtnp55mwsZBNQqIdTiDf f4SQ9fpE3B7dxM1iv/jVwz7pMmyZDd/y/8yzFQLOXHj/vRv6OFhlDyvM8u3Y4vT08 0jQFoFxLzixS70DcmoaPqbng3to1O4ftja8/VqqS//Wr2pJI/1g5ougH177qKCpLW WkPr/zdNNU8juZFOsw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Original-Received: from [92.251.79.197] ([92.251.79.197]) by web-mail.gmx.net (3c-app-mailcom-bs09.server.lan [172.19.170.177]) (via HTTP); Wed, 17 Jul 2024 04:54:20 +0200 Importance: normal Sensitivity: Normal In-Reply-To: <878qy1at52.fsf@dataswamp.org> X-UI-Message-Type: mail X-Priority: 3 X-Provags-ID: V03:K1:JGf8klaOJ/1ERT3Jybqge9XX/ZrEQrGXkqB/tHsdyeqQ93SFqiZZnWGGKo9uhTetWJEPa hVWxkEpl3vnQAC6jzi/093t6aiIYELQakeFegVya8A1bpugMLEeIy2N2+ZRs3hMaXa2ylBNu6ZEH dNjTVjOeMG80OGVuWPiH+Um+DYK18ehZYAIsbbr2jhfUNouw5DNUQESOBJJSu1+DG5+Uuvjv0ajY iZxA3a6qRSUdW4dClbYhHs5kJd3xzS/MVDYXcOp52YW5ejhzDvHTDVjEKkicZJEjn9p9BnJpjAg8 LI= UI-OutboundReport: notjunk:1;M01:P0:XBVYyTT2Vys=;R7C+ERrsCNZZRhnJRJT3ZAx/+ZJ oA3BzC/xraItLvDNIZ/3WXIcBvr/TYt4MgHPuq0OtrrMtvw19D0GKx8fk6TsjrrE3GkECDV7c v0iTjlwTd+hrnZK9iYnNA5yzNGUWXb5Gdr0+MwqeUhlxchzrk8X5e395dO7gJDHR1QFGIlDjz dkllz0hJFe12Rp/By9zQMIAlKXfme7VaEPN4eCWALczjTqzR3bO9oYttc9zb+JVQ9tvm/RqeS nEjnG8MMj3Bdugk7VsG133w6zcQfcTOa/ECy4+zCUWBgVJHZ2gcU267DJkbENU+mBgIvXZrB5 YOKAZGI3hqTUtPWwwxhMGcY+JRoO4Z3krjHrgZvE8uDf5ndz5K7x7jUof67R17k9R/Pmcr2hx msPbInlgTKgDXwChJHMYK3PtGbwmHh6D/jtD+0/ubDFivjeh8HtIIB9cirS4l8FQ44Ofk5POR P4nH1I6+1BLYIaniwY0nVblCFD9zhXRU2GgSkDF+mPxLQpUG9aRJJZRelUZnTGE6B/gmY3uvg rotyVZ/NNjyx0vUxPs14lZBa0i5W6ICjOhjjMPqD7mBzGvUlMC1/mZtF2/tssl4WS/p0oQCKS xrVEf0VM1o5bfFIMRrOc9ZbIoqjbtnd9UQ1o8t7vgJCl1bDFTM06QN32TLQjWPVRazrw+Rx+l K5Ax5HH9w+rxBgTujCXfm6YXeCKy55Zf5o+vBDSd4A== Received-SPF: pass client-ip=212.227.15.15; envelope-from=dimech@gmx.com; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:321740 Archived-At: > Sent: Wednesday, July 17, 2024 at 10:06 AM > From: "Emanuel Berg" > To: emacs-devel@gnu=2Eorg > Subject: Re: Adding a generic mathematical library > > Philip Kaludercic wrote: >=20 > >> Someone=E2=84=A2 should propose a concrete suggestion=2E > > > > 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=2E Including an > > infix-notation macro and perhaps even overloaded operator > > support ala APL would be a nice thing to have=2E We'd have to > > weigh the cost of this kind of flexibility with performance, > > unless someone were to write a specialised byte-compiler=2E >=20 > Yeah, I've pushed for a math library for a long time=2E >=20 > I have several files that are very close to that (for stats, > time, permutations and so on) but I also have a math=2Eel which > I yank below if any of it can be used=2E Tools for permutations would be of much benefit=2E =20 > I'd love to stay in the loop and read more on this! >=20 > ;;; -*- lexical-binding: t -*- > ;; > ;; this file: > ;; https://dataswamp=2Eorg/~incal/emacs-init/math=2Eel >=20 > (require 'cl-lib) >=20 > (require 'dwim) ; https://dataswamp=2Eorg/~incal/emacs-init/dwim=2Eel > (require 'perm) ; https://dataswamp=2Eorg/~incal/emacs-init/perm=2Eel >=20 > (defun nand (&rest conditions) > (when (member nil conditions) > t) ) >=20 > ;; (nand) ; nil > ;; (nand 1 2 3) ; nil > ;; (nand nil) ; t > ;; (nand 1 2 nil 3) ; t >=20 > ;; (not (and)) ; nil > ;; (not (and 1 2 3)) ; nil > ;; (not (and nil)) ; t > ;; (not (and 1 2 nil 3)) ; t >=20 > (defalias '** #'expt) > (defalias 'ceil #'ceiling) >=20 > (defun percent-change (from to) > (if (=3D from to) > 0 > (/ (- to from) (abs from) 0=2E01))) >=20 > ;; (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=2E0e+INF > ;; (percent-change 0 0) ; 0 > ;; (percent-change 0 -1) ; -1=2E0e+INF > ;; > ;; (percent-change -1 1) ; 200 > ;; (percent-change -1 0) ; 100 > ;; (percent-change -1 -1) ; 0 > ;; (percent-change -1 -2) ; -100 >=20 > (defun distance-point (min max) > (+ min (/ (- max min) 2=2E0)) ) >=20 > ;; (distance-point 1 5) ; 3 > ;; (distance-point 1 10) ; 5=2E5 > ;; (distance-point 60 100) ; 80 >=20 > (defun // (nom denom) > (/ nom denom 1=2E0) ) >=20 > ;; (/ 8 256) ; 0 > ;; (// 8 256) ; 0=2E03125 >=20 > (defun arith-sum (n) > (cl-loop > with sum =3D 0 > for i from 1 to (1- n) > do (cl-incf sum i) > finally return sum) ) >=20 > (defun digs (n) > (* n (1- n)) ) >=20 > (defun mean-value (vs) > (let*((sum (apply #'+ vs)) > (mean (/ sum (length vs) 1=2E0)) ) > mean) ) >=20 > (defun mean-str (strs) > (mean-value (cl-map 'list #'length strs)) ) >=20 > (defun faster (old new) > (format "%=2E1f%%" (* 100 (1- (/ new old 1=2E0)))) ) >=20 > ;; (faster 0=2E2 0=2E5) ; 150=2E0% > ;; (faster 1=2E35 0=2E84) ; -37=2E8% > ;; (faster 200 400) ; 100=2E0% > ;; (faster 1000 1000) ; 0=2E0% > ;; (faster 1000 100) ; -90=2E0% >=20 > (defun percent (nom &optional denom str) > (or denom (setq denom 1)) > (let ((perc (/ nom denom 0=2E01))) > (if str > (format "%=2E1f%%" perc) > perc))) >=20 > ;; (percent 0=2E25) ; 25=2E0 > ;; (percent 50 75) ; 66=2E66 =2E=2E=2E > ;; (percent (// 1 2) nil t) ; 50=2E0% > ;; (percent 1019 22 t) ; 4631=2E8% >=20 > (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 (<=3D 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) ) >=20 > ;; (interval 10 5) ; Bogus interval > ;; (interval 1 3 -1) ; Bogus increment > ;; (interval 5 10) ; (5 6 7 8 9 10) > ;; (interval 1=2E8 2=2E0 0=2E1) ; (1=2E8 1=2E9 2=2E0) > ;; (interval) ; (0 1 2 3 4 5 6 7 8 9) > ;; (interval 19 99) ; (19 20 21 =2E=2E=2E 97 98 99) >=20 > (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:]=2E]+ " end t) > (cl-incf sum (string-to-number (match-string-no-properties 0))) ) > (prog1 > sum > (message "sum: %=2E1f" sum) ))) >=20 > (defalias 'di #'digits-sum) >=20 > (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 (=3D (current-column) 73) > (push (string-to-number (match-string-no-properties 0)) scores= ) ))) > (when scores > (message (format "%=2E3f" (mean-value scores))) ))) >=20 > (defun positive-integer-p (n) > (and > (integerp n) > (< 0 n) )) >=20 > ;; (positive-integer-p 1=2E5) ; nil > ;; (positive-integer-p 1) ; t > ;; (positive-integer-p 0) ; nil > ;; (positive-integer-p -1) ; nil >=20 > (defun string-to-number-number (str) > (let ((s (string-trim str))) > (when (string-match-p > "\\`[+-]?\\([[:digit:]]+\\|[[:digit:]]*\\=2E[[:digit:]]+\\)\\= '" s) > (string-to-number s) ))) >=20 > (defun string-to-number-number-test () > (cl-map 'list (lambda (e) > (pcase-let ((`(,a ,b) e)) > (if (and a b) > (=3D a b) > (eq a b) ))) > (list > (list (string-to-number-number " 10") 10) > (list (string-to-number-number " 1=2E5") 1= =2E5) > (list (string-to-number-number " +0") 0) > (list (string-to-number-number " 0") 0) > (list (string-to-number-number " -0=2E0") -0= =2E0) > (list (string-to-number-number " -1=2E5") -1= =2E5) > (list (string-to-number-number "-10") -10) > (list (string-to-number-number "123this used to work") nil) > (list (string-to-number-number "NAN") nil) = ))) >=20 > ;; (string-to-number-number-test) >=20 > (defun read-integer () > (let ((str) > (str-number) > (n) ) > (cl-loop until (progn > (setq str (read-string "integer: ")) > (if (string=3D str "0") > (setq n 0) > (setq str-number (string-to-number str)) > (unless (=3D str-number 0) > (setq n str-number) )) > (integerp n)) ) > n) ) >=20 > ;; (read-integer) >=20 > (defun compare (old new) > (format "%=2E2f%%" (* 100 (/ (- old new) > (* old 1=2E0) )))) > ;; (compare 185 80) ; 56=2E76% >=20 > (defun hypotenuse (c1 c2) > (sqrt (+ (* c1 c1) (* c2 c2))) ) >=20 > (defun speed (hour minute second km) > (let*((s (+ second (* 60 minute) (* 60 60 hour))) > (m (* 1000 km)) > (mps (/ m s)) ) > (* 3=2E6 mps) )) >=20 > ;; (speed 04 44 10 42=2E195) ; 8=2E909208211143694 > ;; (speed 02 01 39 42=2E195) ; 20=2E811344019728732 > ;; (speed 19 04 00 168 ) ; 7=2E2 >=20 > (defun binom (n k) > (/ (cl-factorial n) > (* (cl-factorial k) > (cl-factorial (- n k)) ))) >=20 > (defun product (l) > (cl-loop with prod =3D 1 > for i in l do > (setq prod (* prod i)) > finally return prod) ) >=20 > ;; (product '(1 2 3 4)) ; 24 >=20 > (defun tau-ceti (speed) > (let*((dist (* 11=2E8 9=2E46 (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)) )))) >=20 > ;; (tau-ceti 5000) ; 2 548 584 years >=20 > (provide 'math) >=20 > --=20 > underground experts united > https://dataswamp=2Eorg/~incal >=20 >=20 >