options(warn=-1) # turns off warnings, to turn on: "options(warn=0)" library(imager) library(png) for (f in list.files(path="nt_toolbox/toolbox_general/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_general/", f, sep="")) } for (f in list.files(path="nt_toolbox/toolbox_signal/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_signal/", f, sep="")) } options(repr.plot.width=6, repr.plot.height=5) n <- 1000 x <- array(runif(2*n), c(2,n)) v <- 3*pi/2*(.1 + 2*x[1,]) X <- array(0, c(3,n)) X[2,] <- 20*x[2,] X[1,] <- - cos(v)*v X[3,] <- sin(v)*v ms <- 200 el <- 20; az <- -110 library(scatterplot3d) color_vec <- rev(rainbow(100)) color_function <- function(x, y, z){ idx <- round( x**2 + z**2 ) + 1 return(color_vec[idx]) } #swiss roll scatterplot3d(X[1,], X[2,], X[3,], axis=F, grid=F, box=F, type="p", pch=19, color=color_function(X[1,], X[2,], X[3,])) D1 <- array( rep(apply(X**2, 2, sum), n), c(n,n) ) D1 <- sqrt(pmax(D1 + t(D1) - 2*t(X)%*%X, 0)) k <- 6 DNN <- t( apply( D1, 1, function(l){return(l[order(l)])} ) ) NN <- t( apply( D1, 1, order) ) NN <- NN[,2:(k+1)] DNN <- DNN[,2:(k+1)] B <- t(array(rep(1:n,k), c(n, k))) I <- as.vector(B) ; J <- as.vector(t(NN)) A <- array(0, c(n,n)) for (idx in 1:(k*n)){ A[I[idx], J[idx]] <- 1} W <- array(0, c(n,n)) DNN_vector <- as.vector(t(DNN)) for (idx in 1:(k*n)){ W[I[idx], J[idx]] <- DNN_vector[idx]} color_vec <- rev(rainbow(100)) color_function <- function(x, y, z){ idx <- round( x**2 + z**2 ) + 1 return(color_vec[idx]) } #swiss roll s3d <- scatterplot3d(X[1,], X[2,], X[3,], axis=F, grid=F, box=F, type="p", pch=19, color=color_function(X[1,], X[2,], X[3,])) #graph xx <- array(0, c(2, k*n)) ; xx[1,] <- X[1,I] ; xx[2,] <- X[1,J] yy <- array(0, c(2, k*n)) ; yy[1,] <- X[2,I] ; yy[2,] <- X[2,J] zz <- array(0, c(2, k*n)) ; zz[1,] <- X[3,I] ; zz[2,] <- X[3,J] for (i in 1:length(I)){ s3d$points3d(xx[,i], yy[,i], zz[,i], type="l", lw=1.5) } D <- W D <- (D + t(D))/2. D[D == 0] <- Inf diag(D) <- Inf D[is.nan(D)] <- Inf source("nt_solutions/shapes_7_isomap/exo1.R") ## Insert your code here. Iremove <- (D[,1] == Inf) D[D == Inf] <- 0 source("nt_solutions/shapes_7_isomap/exo2.R") ## Insert your code here. ev <- eigen( (Xstrain %*% t(Xstrain))/n ) L <- ev$values ; U <- ev$vectors Xstrain1 <- -t(U) %*% Xstrain Xstrain1[,Iremove] <- Inf #plot points plot(Xstrain1[1,], Xstrain1[2,], axes=FALSE, ann=FALSE, pch=19, col=color_function(X[1,], X[2,], X[3,])) #plot vertices I <- as.vector(B) ; J <- as.vector(t(NN)) xx <- array(0, c(2, k*n)) ; xx[1,] <- Xstrain1[1,I] ; xx[2,] <- Xstrain1[1,J] yy <- array(0, c(2, k*n)) ; yy[1,] <- Xstrain1[2,I] ; yy[2,] <- Xstrain1[2,J] for (i in 1:length(I)){ lines(xx[,i], yy[,i], col="black", lw=1.5) } Y <- array(0, c(2,length(v))) Y[1,] <- v ; Y[2,] <- X[2,] Y[1,] <- rescale(Y[1,], min(Xstrain[1,]), max(Xstrain[1,])) Y[2,] <- rescale(Y[2,], min(Xstrain[2,]), max(Xstrain[2,])) #plot points plot(Y[1,], Y[2,], axes=FALSE, ann=FALSE, pch=19, col=color_function(X[1,], X[2,], X[3,])) #plot vertices I <- as.vector(B) ; J <- as.vector(t(NN)) xx <- array(0, c(2, k*n)) ; xx[1,] <- Y[1,I] ; xx[2,] <- Y[1,J] yy <- array(0, c(2, k*n)) ; yy[1,] <- Y[2,I] ; yy[2,] <- Y[2,J] for (i in 1:length(I)){ lines(xx[,i], yy[,i], col="black", lw=1.5) } options(repr.plot.width=8, repr.plot.height=6.5) source("nt_solutions/shapes_7_isomap/exo3.R") ## Insert your code here. options(repr.plot.width=6, repr.plot.height=5) plot(stress, type="o", col="blue", pch=19) ev <- eigen( (Xstress %*% t(Xstress))/n ) L <- ev$values ; U <- ev$vectors I <- order(L) L <- L[I] U <- U[,I[2:3]] Xstress1 <- -t(U) %*% Xstress Xstress1[,Iremove] <- Inf options(repr.plot.width=6, repr.plot.height=5) #plot points plot(Xstress1[2,], Xstress1[1,], axes=FALSE, ann=FALSE, pch=19, col=color_function(X[1,], X[2,], X[3,])) #plot vertices I <- as.vector(t(array(rep(1:n,k), c(n, k)))) ; J <- as.vector(t(NN)) xx <- array(0, c(2, k*n)) ; xx[1,] <- Xstress1[2,I] ; xx[2,] <- Xstress1[2,J] yy <- array(0, c(2, k*n)) ; yy[1,] <- Xstress1[1,I] ; yy[2,] <- Xstress1[1,J] for (i in 1:length(I)){ lines(xx[,i], yy[,i], col="black", lw=1.5) }