Skip to content

Instantly share code, notes, and snippets.

@thchr
Last active July 16, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thchr/3e046aa33b5597756a99b3c772bb90b5 to your computer and use it in GitHub Desktop.
Save thchr/3e046aa33b5597756a99b3c772bb90b5 to your computer and use it in GitHub Desktop.
Minimal reproducer for strange REPL/include behavior in Guile
; variables
(define uc-gvecs (list #(0 0 0) #(0 -1 -1) #(0 -1 1) #(-1 0 -1) #(-1 0 1) #(1 0 -1) #(1 0 1) #(0 1 -1) #(0 1 1) #(-1 -1 0) #(1 1 0) #(1 -1 0) #(-1 1 0) #(0 0 -2) #(0 0 2) #(0 -1 -2) #(0 -1 2) #(-1 0 -2) #(-1 0 2) #(1 0 -2) #(1 0 2) #(0 1 -2) #(0 1 2) #(0 -2 0) #(-2 0 0) #(2 0 0) #(0 2 0) #(-1 -1 -2) #(-1 -1 2) #(1 1 -2) #(1 1 2) #(1 -1 -2) #(1 -1 2) #(-1 1 -2) #(-1 1 2) #(0 -2 -1) #(0 -2 1) #(-2 0 -1) #(-2 0 1) #(2 0 -1) #(2 0 1) #(0 2 -1) #(0 2 1) #(0 -2 -2) #(0 -2 2) #(-2 0 -2) #(-2 0 2) #(2 0 -2) #(2 0 2) #(0 2 -2) #(0 2 2)))
(define uc-coefs (list 0.006509988713824982-0.07430991596602736i -0.14858501604673846+0.037798932181882614i 0.14858501604673843-0.03779893218188265i 0.1485850160467384-0.037798932181882586i -0.1485850160467385+0.0377989321818826i -0.1485850160467385+0.03779893218188263i 0.1485850160467385-0.03779893218188265i 0.14858501604673843-0.03779893218188259i -0.1485850160467385+0.03779893218188263i -0.05796045325126349+0.009834853427071608i -0.057960453251263505+0.00983485342707161i -0.07397410695454079-0.040613880878154165i -0.07397410695454078-0.040613880878154186i -0.055001995038864486+0.07342833704372212i -0.055001995038864486+0.07342833704372212i -0.045988480619645444+0.00967774700948442i 0.045988480619645444-0.009677747009484424i -0.045988480619645444+0.009677747009484417i 0.04598848061964546-0.00967774700948442i 0.04598848061964546-0.009677747009484422i -0.04598848061964546+0.009677747009484426i 0.04598848061964546-0.009677747009484419i -0.04598848061964547+0.009677747009484422i 0.0006733988398186295+0.005833383133757143i 0.0006733988398186281+0.005833383133757143i 0.0006733988398186298+0.0058333831337571435i 0.0006733988398186282+0.0058333831337571435i 0.022141415260758895-0.020356404143648046i 0.02214141526075885-0.02035640414364806i 0.022141415260758898-0.02035640414364805i 0.022141415260758853-0.02035640414364806i -0.008413912285000654-0.014572480450529158i -0.008413912285000658-0.014572480450529165i -0.008413912285000656-0.014572480450529165i -0.008413912285000666-0.014572480450529179i 0.0005102563342371311-0.029259171073349383i 0.0005102563342371369-0.029259171073349393i -0.0005102563342371481+0.029259171073349383i -0.0005102563342371405+0.029259171073349383i -0.0005102563342371329+0.029259171073349383i -0.0005102563342371369+0.029259171073349393i 0.0005102563342371483-0.029259171073349393i 0.0005102563342371407-0.029259171073349393i 0.002588281223189957-0.0006278232264738109i 0.002588281223189956-0.0006278232264738123i 0.002588281223189957-0.0006278232264738104i 0.002588281223189958-0.0006278232264738094i 0.002588281223189958-0.000627823226473811i 0.002588281223189957-0.000627823226473811i 0.002588281223189958-0.0006278232264738104i 0.002588281223189957-0.0006278232264738113i))
(define (vector3* a v) (vector (* a (vector-ref v 0)) (* a (vector-ref v 1)) (* a (vector-ref v 2)) ))
(define twopi 6.283185307179586)
(define uc-gvecs-twopi (map (lambda (g) (vector3* twopi g)) uc-gvecs))
; timing for stump expr
(define time (lambda (expr N)
(let ((t1 (get-internal-real-time)))
(do ((i 1 (+ i 1))) ((> i N))
(expr)
)
(display (exact->inexact (/ (- (get-internal-real-time) t1) (* N internal-time-units-per-second))))
(display " \"s\"\n")
)
))
; main method
(define vec-of-vecs-g-and-coefs
; store uc-gvecs-twopi and uc-coefs contiguously in memory, as an N-vector of 5-vectors; effectively, this is like an array
(list->vector ; N-vector
(map (lambda (g coef) ; 5-vector (gx, gy, gz, c-real, c-imag)
(vector (vector-ref g 0) (vector-ref g 1) (vector-ref g 2) (real-part coef) (imag-part coef))
) uc-gvecs-twopi uc-coefs)
)
)
(define N (vector-length vec-of-vecs-g-and-coefs))
(define (calc-fourier-sum-manual r)
(let ( (v 0.0) ; loop summand
(rx (vector-ref r 0)) ; licm of referencing r components
(ry (vector-ref r 1))
(rz (vector-ref r 2)) )
; no appropriate mapping routine for this vector-scenario: use do loop
(do ((i 0 (+ i 1))) ; loop counter
((>= i N) v) ; continue until test is true (then return v)
; loop body
(let* ( (g-and-coefs (vector-ref vec-of-vecs-g-and-coefs i))
(gx (vector-ref g-and-coefs 0)) ; w/ 2*pi factors
(gy (vector-ref g-and-coefs 1))
(gz (vector-ref g-and-coefs 2))
(c-real (vector-ref g-and-coefs 3))
(c-imag (vector-ref g-and-coefs 4))
(g-dot-r (+ (* gx rx) (* gy ry) (* gz rz))) )
; add real part of exponential term to v
(set! v (+ v (- (* c-real (cos g-dot-r))
(* c-imag (sin g-dot-r)) )))
)
)
)
)
; benchmark
(define rp #(.2 .1 .3)) ; default testing point
(define f-fourier-manual (lambda () (calc-fourier-sum-manual rp)))
(define bench (lambda (n)
(display "Timing:\t") (time f-fourier-manual n)
))
(display "--- Benchmarks ---\n")
(bench 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment