## ----echo=FALSE,message=FALSE-------------------------------------------------
knitr::opts_chunk$set(comment=NA,
               fig.align="center",
               results="markup")

## -----------------------------------------------------------------------------
r <- seq(from=0,to=2*pi,length=24)
a1 <- cos(r)*4 + 0.00001*rnorm(r)
a2 <- sin(r)*4 + 0.00001*rnorm(r)
b1 <- c(.5,-.5,-.5,.5)*3 + 5
b2 <- c(.5,.5,-.5,-.5)*3 + 1

## -----------------------------------------------------------------------------
D1 <- outer(b1,a1,"-")
D2 <- outer(b2,a2,"-")
Dsq <- D1^2+D2^2

## -----------------------------------------------------------------------------
library(munfold)
Dsq.uf<-unfold(Dsq,squared=TRUE)
Dsq.uf

## -----------------------------------------------------------------------------
biplot(Dsq.uf,type="b")

## -----------------------------------------------------------------------------
A <- cbind(a1,a2)
B <- cbind(b1,b2)
orig <- rbind(A,B)
xlim <- ylim <- range(orig)#*1.5

plot(A,type="b",pch=1,asp=1,
    xlim=xlim,ylim=ylim,
    xlab="Dimension 1",ylab="Dimension 2")
lines(B,type="b",pch=3,lty=2)
abline(h=0,v=0,lty=3)

## -----------------------------------------------------------------------------
oldpar <- par(mfrow=c(1,2))
plot(A,type="b",pch=1,
    xlim=xlim,ylim=ylim,
    xlab="Dimension 1",ylab="Dimension 2",main=expression("Original data"),asp=1)
lines(B,type="b",pch=3,lty=2)
abline(h=0,v=0,lty=3)

biplot(Dsq.uf,type="b",
    xlim=xlim,ylim=ylim,
    main=expression(paste(italic(unfold)," solution")),asp=1)
par(oldpar)

## -----------------------------------------------------------------------------
r <- seq(from=0,to=2*pi,length=24)
a1_0 <- cos(r)*4 # NB: no random noise added here
a2_0 <- sin(r)*4
b1_0 <- c(.5,-.5,-.5,.5)*3 + 5
b2_0 <- c(.5,.5,-.5,-.5)*3 + 1
D1_0 <- outer(b1_0,a1_0,"-")
D2_0 <- outer(b2_0,a2_0,"-")
Dsq0 <- D1_0^2+D2_0^2
Dsq.uf0 <- unfold(Dsq0,squared=TRUE)
Dsq.uf0

## -----------------------------------------------------------------------------
biplot(Dsq.uf0,type="b")

## -----------------------------------------------------------------------------
Dsq.uf0.cg<-unfold(Dsq,squared=TRUE,method="CG")
Dsq.uf0.cg

## -----------------------------------------------------------------------------
biplot(Dsq.uf0.cg,type="b")

## -----------------------------------------------------------------------------
Dsq.uf1 <- unfold(Dsq,squared=FALSE)
Dsq.uf1

## -----------------------------------------------------------------------------
biplot(Dsq.uf1,type="b")

## -----------------------------------------------------------------------------
Dsq.uf2 <- unfold(sqrt(Dsq),squared=TRUE)
Dsq.uf2

## -----------------------------------------------------------------------------
biplot(Dsq.uf2,type="b")

