;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/all-substance.el (require 'cl-lib) (defun half-time-to-day-drop (ht) (if (< ht 24) (/ ht 24 2.0) (- 1 (/ 24 ht 2.0)) )) ;; (half-time-to-day-drop 12) ; 0.25 ;; (half-time-to-day-drop 48) ; 0.75 (defun all-substance (q d ht &optional verbose day-q) "Q is the quantity, for example 10. This is a number; do not use a unit. D is the number of days, for example 20. HT is the half-time in hours, for example 6. Set VERBOSE to return a string. Set DAY-Q if Q instead is submitted the daily quantity, for example 0.5." (cl-loop with daily = (if day-q q (/ q d 1.0)) with day-drop = (half-time-to-day-drop ht) for i downfrom (1- d) to 0 sum (* daily (expt day-drop i)) into total finally return (if verbose (message "daily %.2f, days %d, total %.2f" daily d total) total) )) ;; (all-substance 300 365 6) ; 0.94 ;; (all-substance 0.5 30 6 nil t) ; 0.57 ;; (all-substance 365 365 6 t) ; daily 1.00, days 365, total 1.14 (defun wash-out (q ht &optional days) (or days (setq days 5)) (cl-loop with day-drop = (half-time-to-day-drop ht) for d from 1 to days collect (* q (expt day-drop d)) into res finally return res) ) ;; (wash-out 1.14 6) ; 0.1 1 day w/o resupply ;; 0.02 2 .. ;; 0.002 3 .. ;; 0.0003 4 .. ;; 0.00003 5 .. (provide 'all-substance)