Skip to content

Instantly share code, notes, and snippets.

@trialsolution
Created August 23, 2013 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trialsolution/6316229 to your computer and use it in GitHub Desktop.
Save trialsolution/6316229 to your computer and use it in GitHub Desktop.
checks positive definiteness of a matirx (GAMS and R combined solution)
$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