Skip to content

Instantly share code, notes, and snippets.

View sritchie's full-sized avatar
🎯
Focusing

Sam Ritchie sritchie

🎯
Focusing
View GitHub Profile
;; Polynomial interpolation in Clojure, written as a fold.
;;
;; This allows you to generate an estimate of the value of some polynomial at x,
;; where you have a sequence of points that the polynomial has to pass through.
;;
;; See [this namespace](https://github.com/sicmutils/sicmutils/blob/main/src/sicmutils/polynomial/interpolate.cljc#L20)
;; for a much longer build-up to these simple functions!
(defn prepare [[x fx]]
[x x fx fx])
@sritchie
sritchie / macro.clj
Last active January 5, 2022 07:52
Macro for generating kahan-babushka-klein folds.
;; general impl of https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Further_enhancements
(defn- klein-term [acc delta]
`[sum# (+ ~acc ~delta)
~delta (if (>= (Math/abs ~(with-meta acc {:tag 'double}))
(Math/abs ~(with-meta delta {:tag 'double})))
(+ (- ~acc sum#) ~delta)
(+ (- ~delta sum#) ~acc))
~acc sum#])
@sritchie
sritchie / notes.clj
Last active December 22, 2021 23:13
(ns tutorial.petageconvert)
;; woohoo, some comments!
;;
;; A note about `pet-multiplier`... for your keys, symbols are great, but
;; Clojure provides an additional idea of keywords over Scheme. Keywords like
;; `:dog` evaluate to themselves, so you don't have to quote them. Symbols are fine, but you have one less thing to remember (the quote).
(def pet-multipler
{'dog 7 'cat 5 'fish 10})

Chapter 5: Integration

We know how to integrate real-valued functions of a real variable. We want to extend this idea to manifolds, in such a way that the integral is independent of the coordinate system used to compute it.

The integral of a real-valued function of a real variable is the limit of a sum of products of the values of the function on subintervals and the lengths of the increments of the independent variable in those subintervals:

@sritchie
sritchie / deps.edn
Created November 19, 2021 05:11
Double Pendulum in Clerk
;; here were the deps I needed for this... I don't know enough vega-lite to go without the Hanami example I had
;; already built, so excuse me there!
{:paths ["dev"]
:deps {io.github.nextjournal/clerk {:local/root "../clerk"}
notespace-sicmutils {:mvn/version "0.16.2"}
aerial.hanami/aerial.hanami {:mvn/version "0.12.7"}
sicmutils/sicmutils {:git/url "https://github.com/sicmutils/sicmutils"
:sha "8658c0c8883b8225a742b9422061f40b852f375d"}}}
@sritchie
sritchie / extend.clj
Created November 18, 2021 14:43
Print writer extensions for functions.
(defn install-fn-printers!
"NOTE: If you are using Emacs + cider-nrepl, these two `print-method`
implementations will not be installed. I'm currently working on a fix to get
this enabled in the Emacs environment... But if you want to test this feature
out, simply execute these forms.
https://github.com/clojure-emacs/cider-nrepl/blob/master/src/cider/nrepl/print_method.clj#L42-L71
TODO write a with-methods instead to do this..."
[]
#?(:clj
(defn ^:no-doc careful-def [ns]
#?(:cljs
(fn [sym form]
`(def ~sym ~form))
:clj
(let [ns-sym (ns-name ns)
nsm (ns-map ns)
remote? (fn [sym]
(when-let [v (nsm sym)]
shadow-cljs - running: lein run -m shadow.cljs.devtools.cli --npm clj-repl
Syntax error (UnsatisfiedLinkError) compiling at (/private/var/folders/xw/0lq56zhn4hb4lknppw_k086c0000gn/T/form-init10880055894866318920.clj:1:126).
/private/var/folders/xw/0lq56zhn4hb4lknppw_k086c0000gn/T/jna5901602065421044698.tmp: dlopen(/private/var/folders/xw/0lq56zhn4hb4lknppw_k086c0000gn/T/jna5901602065421044698.tmp, 1): no suitable image found. Did find:
/private/var/folders/xw/0lq56zhn4hb4lknppw_k086c0000gn/T/jna5901602065421044698.tmp: no matching architecture in universal wrapper
/private/var/folders/xw/0lq56zhn4hb4lknppw_k086c0000gn/T/jna5901602065421044698.tmp: no matching architecture in universal wrapper
Full report at:
/var/folders/xw/0lq56zhn4hb4lknppw_k086c0000gn/T/clojure-14930451110325842276.edn
@sritchie
sritchie / 50th.scm
Created April 28, 2021 14:25
50th derivative
(/
(+ (* -717897987691852588770249/1125899906842624 (expt x 74) (sqrt x) (cos (+ (* x (sqrt x)) (* 3 (sqrt x)))))
(* -17947449692296314719256225/562949953421312 (expt x 73) (sqrt x) (cos (+ (* x (sqrt x)) (* 3 (sqrt x)))))
(* -412791342922815238542893175/1125899906842624 (expt x 73) (sin (+ (* x (sqrt x)) (* 3 (sqrt x)))))
(* -879425034922519421243555025/1125899906842624 (expt x 72) (sqrt x) (cos (+ (* x (sqrt x)) (* 3 (sqrt x)))))
(* -19640492446602933741106062225/1125899906842624 (expt x 72) (sin (+ (* x (sqrt x)) (* 3 (sqrt x)))))
(* 23549048157369686724410751225/281474976710656 (expt x 71) (sqrt x) (cos (+ (* x (sqrt x)) (* 3 (sqrt x)))))
(* -57162627269963762380831076625/140737488355328 (expt x 71) (sin (+ (* x (sqrt x)) (* 3 (sqrt x)))))
(* 1180872394115405245080929164125/281474976710656 (expt x 70) (sqrt x) (cos (+ (* x (sqrt x)) (* 3 (sqrt x)))))
(* 80027678177949267333163507275/8796093022208 (expt x 70) (sin (+ (* x (sqrt x)) (* 3 (sqrt x)))))
@sritchie
sritchie / multivariate.cljc
Last active January 18, 2021 11:47
multivariate redo attempt...
;;
;; [[jacobian]] handles this main logic. [[jacobian]] can only take a structural
;; input. [[euclidean]] and [[multivariate]] below widen handle, respectively,
;; optionally-structural and multivariable arguments.
(defn- jacobian
"Takes:
- some function `f` of a single [[s/structure?]] argument
- the unperturbed structural `input`