File: test2.R

package info (click to toggle)
quadprog 1.5-3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 228 kB
  • ctags: 118
  • sloc: fortran: 682; ansic: 46; makefile: 1
file content (34 lines) | stat: -rw-r--r-- 928 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
library(quadprog)

Dmat <- matrix(c(4,-2,-2,4),2,2)
dvec <- c(-6,0)
Amat <- matrix(c(1,0,0,1,1,1),2,3)
bvec <- c(0,0,2)
res<-solve.QP(Dmat,dvec,Amat,bvec=bvec)
print(res)
res<-solve.QP(solve(chol(Dmat)),dvec,Amat,bvec=bvec,fac=T)
print(res)

print(crv1 <- (crossprod(res$unc, Dmat)/2-dvec)%*%res$unc)
print(crv2 <- (crossprod(res$solution, Dmat)/2-dvec)%*%res$solution)
print(res$value)
print(crv2 >= crv1)
print(all(crossprod(Amat, res$solution) >= bvec))


Dmat       <- matrix(0,3,3)
diag(Dmat) <- 1
dvec       <- c(0,5,0)
Amat  <- matrix(c(-4,-3,0,2,1,0,0,-2,1),3,3)
bvec  <- c(-8,2,0)
res<-solve.QP(Dmat,dvec,Amat,bvec=bvec)
print(res)
res<-solve.QP(solve(chol(Dmat)),dvec,Amat,bvec=bvec,fac=T)
print(res)

print(crv1 <- (crossprod(res$unc, Dmat)/2-dvec)%*%res$unc)
print(crv2 <- (crossprod(res$solution, Dmat)/2-dvec)%*%res$solution)
print(res$value)
print(crv2 >= crv1)
print(all(crossprod(Amat, res$solution) >= bvec))