Created
August 23, 2013 06:46
-
-
Save trialsolution/6316229 to your computer and use it in GitHub Desktop.
checks positive definiteness of a matirx (GAMS and R combined solution)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ontext | |
"Is there some GAMS template around to determine the definiteness of a given | |
matrix? (At the moment, I need it for a 5x5 matrix, but the dimension will | |
eventually be higher.) | |
Or is it advisable to switch to another math package for solving this? | |
Stefan" | |
This version uses the 'corpor' package in R to check definiteness | |
$offtext | |
$setlocal R_executable C:\Users\himics\Documents\R\R-3.0.1\bin\x64\R.exe | |
scalar answer; | |
set i /i1*i5/; | |
alias(i,j); | |
parameter matrix(i,i); | |
matrix(i,j) = uniform(1,10); | |
* test: the identity matrix is pos. definite | |
*matrix(i,j) = 0; | |
*matrix(i,i) = 1; | |
execute_unload "is_definite.gdx", matrix; | |
$onecho > pos_def.r | |
library(gdxrrw) | |
igdx("S:/24.1/") | |
library(corpcor) | |
mymatrix <- rgdx.param("is_definite.gdx", "matrix") | |
m <- as.matrix(tapply(mymatrix$value, mymatrix[, c("i","j")], sum)) | |
m[is.na(m)] <- 0 | |
answer <- is.positive.definite(m)*1 | |
attr(answer, "symName") <- "answer" | |
wgdx.scalar("definite.gdx", answer) | |
$offecho | |
* | |
* --- call R terminal | |
* | |
execute '%R_executable% CMD BATCH pos_def.r pos_def.log'; | |
execute_load "definite.gdx", answer; | |
if (answer, | |
display "the matrix is positive definite", answer; | |
else | |
display "the matrix is not positive definite", answer; | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment