Skip to content

Instantly share code, notes, and snippets.

@isomorphisms
Last active August 29, 2015 14:15
Show Gist options
  • Save isomorphisms/21a3428960ead9715b9a to your computer and use it in GitHub Desktop.
Save isomorphisms/21a3428960ead9715b9a to your computer and use it in GitHub Desktop.
roots of polynomials: watch the commutator, permutations, braids, as you twiddle the roots
#OUTPUT: https://twitter.com/isomorphisms/status/566965475188670464
#AFTER: https://www.youtube.com/watch?v=RhpVSV6iCko
#ORIGIN: V. I. Arnol'd
#main parts are:
# — polyroot
# — concatenation of the list called "dance"
#convenience
rcomplex <- function(...) complex(real=rnorm(...), imaginary=rnorm(...))
i <- complex(imaginary=1)
arg <- function(z) Arg(z)/2/pi*360
require(magrittr)
#quintic
coefficients <- rcomplex(6) #5+1 to move the origin around (x°=constant term)
#one set of answers:
options(digits=1)
coefficients %>% polyroot %>% arg %>% print
#(came from what again?)
coefficients %>% arg %>% print
#all right, now stop goofing off and get back on task...
coefficients %>% polyroot %>% polyroot %>% arg %>% print
coefficients %>% polyroot %>% polyroot %>% polyroot %>% arg %>% print
coefficients %>% polyroot %>% polyroot %>% polyroot %>% polyroot %>% arg %>% print
#LOGIC SECTION
#list concatenation is a bit strange in R. Test out with c( list(1,2,3), 1,2,3 ) in REPL.
#I'm using a pattern like c( list(1:3), list(6:8) )
#and then LongList <- c( LongList, list( NewStuff ) )
#as well as NewList looking like: start + modifications**j
#so I'll load up a list-of-6-at-a-time, then loop through each
dance <- list( coefficients )
for (j in 1:157) {
#list concatenation
dance <- c( dance, coefficients + list( #coefficients is the name of the INITIAL positions/values
#TWEAK THIS TO CHANGE THE DANCE
0,0,0,coefficients[4] + i^(.1 * j),0,0 #six components bc six coefficients
) )
} #end of dance
#GRAPHICS SECTION
#polyroot does almost all the work of solving here
devtools::install_github('karthik/wesanderson'); require(wesanderson)
par(mfrow=c(2,1)) #side-by-side
for(j in 1:length(dance)) {
#coeff plot
plot( coefficients[[j]],
pch=19, col=rgb(0,0,0,1-1:6/10), #descending transparency
xlab='ℝ', ylab='√−1', axes=F, mar=0, #clear
ylim=c(-3,3), xlim=c(-3,3) #hold steady
);
#roots plot
plot( coefficients[[j]] %>% polyroot,
pch=19, cex=2, col=wes_palette('Zissou',5),
xlab='ℝ', ylab='√−1', axes=F, mar=0, #clear
ylim=c(-3,3), xlim=c(-3,3) #hold steady
);
Sys.sleep(.03)
} #end of animation
#you can then use require(animation); ?saveGIF
#when you want to save the plot. The command
#strings for interactive loop and saveGIF()
#differ a little, for example you'll need to
#move par(mfrow=c(2,1)) inside the loop, and
#take out Sys.sleep() from the loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment