Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Johannes Pfeifer
dynare
Commits
14a081c0
Commit
14a081c0
authored
Apr 28, 2010
by
Houtan Bastani
Browse files
add swz_fprintf_err to handle printing to stderr
parent
c955ae62
Changes
13
Hide whitespace changes
Inline
Side-by-side
matlab/swz/c-code/mex/modify_for_mex.c
View file @
14a081c0
#include
<stdlib.h>
#include
"mex.h"
#include
<stdlib.h>
#include
<stdarg.h>
#include
<stdarg.h>
#include
<string.h>
void
swz_fprintf_err
(
FILE
*
cad
,
const
char
*
str
,
...)
{
char
*
whole_str
=
(
char
*
)
NULL
;
char
*
msg_truncated
=
".....MSG TRUNCATED
\n
"
;
int
num_args
=
0
;
va_list
ap
;
va_start
(
ap
,
str
);
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
if
(
!
(
whole_str
=
(
char
*
)
malloc
(
sizeof
(
str
)
*
sizeof
(
char
))))
{
printf
(
"Could not allocate memory
\n
"
);
exit
(
0
);
}
strcpy
(
whole_str
,
str
);
while
(
strtok_r
(
whole_str
,
"%"
,
&
whole_str
)
!=
NULL
)
num_args
++
;
num_args
=
sizeof
(
str
)
*
sizeof
(
char
)
+
num_args
*
sizeof
(
long
double
);
if
(
!
(
whole_str
=
(
char
*
)
realloc
(
whole_str
,
num_args
+
strlen
(
msg_truncated
)
+
1
)))
{
printf
(
"Could not allocate memory
\n
"
);
exit
(
0
);
}
vsnprintf
(
whole_str
,
num_args
,
str
,
ap
);
if
(
strlen
(
whole_str
)
+
1
==
num_args
)
strcat
(
whole_str
,
msg_truncated
);
printf
(
"%s"
,
whole_str
);
free
(
whole_str
);
#else
vfprintf
(
stderr
,
str
,
ap
);
#endif
va_end
(
ap
);
}
void
swz_exit
(
int
status
)
{
...
...
matlab/swz/c-code/mex/modify_for_mex.h
View file @
14a081c0
#ifndef _MEXMOD
#define _MEXMOD
void
swz_exit
(
int
status
);
void
swz_fprintf_err
(
const
char
*
str
,
...);
#endif
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
...
...
matlab/swz/c-code/sbvar/switching/switch.c
View file @
14a081c0
...
...
@@ -122,13 +122,13 @@ TMarkovStateVariable* CreateMarkovStateVariable_Single(int nstates, int nobs, TM
if
(
!
CheckRestrictions
(
FreeDim
,
NonZeroIndex
,
MQ
,
nstates
))
{
fprintf
(
std
err
,
"CreateMarkovStateVariable_Single(): Error in restrictions
\n
"
);
swz_
fprintf
_
err
(
"CreateMarkovStateVariable_Single(): Error in restrictions
\n
"
);
exit
(
0
);
}
if
(
!
CheckPrior
(
Prior
,
nstates
)
||
!
CheckPriorOnFreeParameters
(
Prior
,
NonZeroIndex
,
nstates
))
{
fprintf
(
std
err
,
"CreateMarkovStateVariable_Single(): Error in priors
\n
"
);
swz_
fprintf
_
err
(
"CreateMarkovStateVariable_Single(): Error in priors
\n
"
);
exit
(
0
);
}
...
...
@@ -137,13 +137,13 @@ TMarkovStateVariable* CreateMarkovStateVariable_Single(int nstates, int nobs, TM
if
((
nstates
<=
0
)
||
(
nobs
<=
0
))
{
fprintf
(
std
err
,
"CreateMarkovStateVariable(): improper argument values
\n
"
);
swz_
fprintf
_
err
(
"CreateMarkovStateVariable(): improper argument values
\n
"
);
exit
(
0
);
}
if
(
!
(
sv
=
(
TMarkovStateVariable
*
)
malloc
(
sizeof
(
TMarkovStateVariable
))))
{
fprintf
(
std
err
,
"CreateMarkovStateVariable(): out of memory
\n
"
);
swz_
fprintf
_
err
(
"CreateMarkovStateVariable(): out of memory
\n
"
);
exit
(
0
);
}
...
...
@@ -696,7 +696,7 @@ TMarkovStateVariable* CreateMarkovStateVariable_Lags(int nlags, TMarkovStateVari
if
(
base
->
n_state_variables
>
1
)
{
fprintf
(
std
err
,
"CreateMarkovStateVariable_Lags(): multiple state variable for base."
);
swz_
fprintf
_
err
(
"CreateMarkovStateVariable_Lags(): multiple state variable for base."
);
exit
(
0
);
}
...
...
@@ -801,13 +801,13 @@ void Swap_SV(TMarkovStateVariable *sv, int i, int j)
TPermutation
X
;
if
(
sv
->
n_state_variables
>
1
)
{
fprintf
(
std
err
,
"Swap(): Can only swap indices for terminal state variables.
\n
"
);
swz_
fprintf
_
err
(
"Swap(): Can only swap indices for terminal state variables.
\n
"
);
exit
(
0
);
}
if
((
i
<
0
)
||
(
j
<
0
)
||
(
i
>=
sv
->
nbasestates
)
||
(
j
>=
sv
->
nbasestates
))
{
fprintf
(
std
err
,
"Swap(): Indicies out of range.
\n
"
);
swz_
fprintf
_
err
(
"Swap(): Indicies out of range.
\n
"
);
exit
(
0
);
}
...
...
@@ -829,7 +829,7 @@ void Swap_SV(TMarkovStateVariable *sv, int i, int j)
FreePermutation
(
X
);
if
(
!
Update_B_from_Q_SV
(
sv
))
{
fprintf
(
std
err
,
"Swap(): Restrictions violated.
\n
"
);
swz_
fprintf
_
err
(
"Swap(): Restrictions violated.
\n
"
);
exit
(
0
);
}
PropagateSwap_SV
(
sv
);
...
...
@@ -907,7 +907,7 @@ void DrawTransitionMatrix_SV(TMarkovStateVariable *sv)
for
(
k
=
dw_DimA
(
sv
->
FreeDim
)
-
1
;
k
>=
0
;
k
--
)
if
(
!
DrawDirichletVector
(
sv
->
b
[
k
],
sv
->
b
[
k
]))
{
fprintf
(
std
err
,
"Error drawing Dirichlet vector
\n
"
);
swz_
fprintf
_
err
(
"Error drawing Dirichlet vector
\n
"
);
exit
(
0
);
}
...
...
@@ -950,7 +950,7 @@ void DrawTransitionMatrixFromPrior_SV(TMarkovStateVariable *sv)
for
(
j
=
dw_DimA
(
sv
->
FreeDim
)
-
1
;
j
>=
0
;
j
--
)
if
(
!
DrawDirichletVector
(
sv
->
b
[
j
],
sv
->
b
[
j
]))
{
fprintf
(
std
err
,
"Error drawing Dirichlet vector
\n
"
);
swz_
fprintf
_
err
(
"Error drawing Dirichlet vector
\n
"
);
exit
(
0
);
}
...
...
@@ -1417,7 +1417,7 @@ TStateModel* CreateStateModel_new(TMarkovStateVariable *sv, ThetaRoutines *routi
if
(
!
(
model
=
(
TStateModel
*
)
malloc
(
sizeof
(
TStateModel
))))
{
fprintf
(
std
err
,
"CreateStateModel(): Out of memory
\n
"
);
swz_
fprintf
_
err
(
"CreateStateModel(): Out of memory
\n
"
);
exit
(
0
);
}
...
...
@@ -2136,7 +2136,7 @@ void ConvertQToFreeParameters(TStateModel *model, PRECISION *f)
TVector
*
ba
=
model
->
sv
->
ba
;
if
(
!
(
model
->
sv
->
valid_transition_matrix
))
{
fprintf
(
std
err
,
"ConvertQToFreeParameters(): Transition matrices not valid.
\n
"
);
swz_
fprintf
_
err
(
"ConvertQToFreeParameters(): Transition matrices not valid.
\n
"
);
exit
(
0
);
}
for
(
i
=
0
;
i
<
dw_DimA
(
ba
);
f
+=
k
,
i
++
)
...
...
@@ -2210,7 +2210,7 @@ void ConvertQToLogFreeParameters(TStateModel *model, PRECISION *f)
if
(
!
(
model
->
sv
->
valid_transition_matrix
))
{
fprintf
(
std
err
,
"ConvertQToFreeParameters(): Transition matrices not valid.
\n
"
);
swz_
fprintf
_
err
(
"ConvertQToFreeParameters(): Transition matrices not valid.
\n
"
);
exit
(
0
);
}
...
...
matlab/swz/c-code/sbvar/switching/switchio.c
View file @
14a081c0
...
...
@@ -1030,13 +1030,13 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
id
=
"//== Number Observations ==//"
;
if
(
!
dw_SetFilePosition
(
f_in
,
id
))
{
fprintf
(
std
err
,
"Line identifier ""%s"" not found.
\n
"
,
id
);
swz_
fprintf
_
err
(
"Line identifier ""%s"" not found.
\n
"
,
id
);
exit
(
0
);
}
fscanf
(
f_in
,
" %d "
,
&
nobs
);
if
(
nobs
<=
0
)
{
fprintf
(
std
err
,
"Number Observations must be positive
\n
"
);
swz_
fprintf
_
err
(
"Number Observations must be positive
\n
"
);
exit
(
0
);
}
}
...
...
@@ -1044,13 +1044,13 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
id
=
"//== Number Independent State Variables ==//"
;
if
(
!
dw_SetFilePosition
(
f_in
,
id
))
{
fprintf
(
std
err
,
"Line identifier ""%s"" not found.
\n
"
,
id
);
swz_
fprintf
_
err
(
"Line identifier ""%s"" not found.
\n
"
,
id
);
exit
(
0
);
}
fscanf
(
f_in
,
" %d "
,
&
n_state_variables
);
if
(
n_state_variables
<=
0
)
{
fprintf
(
std
err
,
"Number Independent State Variables must be positive
\n
"
);
swz_
fprintf
_
err
(
"Number Independent State Variables must be positive
\n
"
);
exit
(
0
);
}
...
...
@@ -1060,20 +1060,20 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
sprintf
(
id_buffer
,
"//== Number of states for state_variable[%d] ==//"
,
i
+
1
);
if
(
!
dw_SetFilePosition
(
f_in
,
id_buffer
))
{
fprintf
(
std
err
,
"Line identifier ""%s"" not found.
\n
"
,
id_buffer
);
swz_
fprintf
_
err
(
"Line identifier ""%s"" not found.
\n
"
,
id_buffer
);
exit
(
0
);
}
fscanf
(
f_in
,
" %d "
,
&
nstates
);
if
(
nstates
<=
0
)
{
fprintf
(
std
err
,
"Number of states for state_variable[%d] must be positive
\n
"
,
i
+
1
);
swz_
fprintf
_
err
(
"Number of states for state_variable[%d] must be positive
\n
"
,
i
+
1
);
exit
(
0
);
}
sprintf
(
id_buffer
,
"//== Transition matrix prior for state_variable[%d]. (n_states x n_states) ==//"
,
i
+
1
);
if
(
!
dw_SetFilePosition
(
f_in
,
id_buffer
))
{
fprintf
(
std
err
,
"Line identifier ""%s"" not found.
\n
"
,
id_buffer
);
swz_
fprintf
_
err
(
"Line identifier ""%s"" not found.
\n
"
,
id_buffer
);
exit
(
0
);
}
dw_ReadMatrix
(
f_in
,
prior
=
CreateMatrix
(
nstates
,
nstates
));
...
...
@@ -1088,7 +1088,7 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
sprintf
(
id_buffer
,
"//== Column restrictions for state_variable[%d] ==//"
,
i
+
1
);
if
(
!
dw_SetFilePosition
(
f_in
,
id_buffer
))
{
fprintf
(
std
err
,
"Line identifier ""%s"" not found.
\n
"
,
id_buffer
);
swz_
fprintf
_
err
(
"Line identifier ""%s"" not found.
\n
"
,
id_buffer
);
exit
(
0
);
}
restrictions
=
dw_CreateArray_matrix
(
nstates
);
...
...
@@ -1097,7 +1097,7 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
dw_ReadMatrix
(
f_in
,
restrictions
[
j
]
=
CreateMatrix
(
nstates
,
dims
[
j
]));
else
{
fprintf
(
std
err
,
"Free Dirichet dimensions for column %d of state_variable[%d] must be positive
\n
"
,
j
+
1
,
i
+
1
);
swz_
fprintf
_
err
(
"Free Dirichet dimensions for column %d of state_variable[%d] must be positive
\n
"
,
j
+
1
,
i
+
1
);
exit
(
0
);
}
...
...
matlab/swz/c-code/sbvar/var/PrintDraws.c
View file @
14a081c0
...
...
@@ -69,7 +69,7 @@ int main(int nargs, char **args)
printf
(
"Reading data...
\n
"
);
if
(
!
(
model
=
CreateTStateModelFromEstimateFinal
(
nargs
,
args
,
&
cmd
)))
{
fprintf
(
std
err
,
"Unable to read model or parameters
\n
"
);
swz_
fprintf
_
err
(
"Unable to read model or parameters
\n
"
);
exit
(
1
);
}
p
=
(
T_VAR_Parameters
*
)(
model
->
theta
);
...
...
matlab/swz/c-code/sbvar/var/VARbase.c
View file @
14a081c0
...
...
@@ -186,14 +186,14 @@ T_VAR_Parameters* CreateTheta_VAR(int flag, int nvars, int nlags, int nexg, int
if
((
nvars
<=
0
)
||
(
nlags
<
0
)
||
(
nexg
<
0
))
{
fprintf
(
std
err
,
"CreateTheta_VAR(): Invalid arguments passed.
\n
"
);
swz_
fprintf
_
err
(
"CreateTheta_VAR(): Invalid arguments passed.
\n
"
);
exit
(
0
);
}
//=== Allocate memory for T_VAR_Parameters ===
if
(
!
(
p
=
(
T_VAR_Parameters
*
)
malloc
(
sizeof
(
T_VAR_Parameters
))))
{
fprintf
(
std
err
,
"Out of memory
\n
"
);
swz_
fprintf
_
err
(
"Out of memory
\n
"
);
exit
(
0
);
}
...
...
@@ -2259,7 +2259,7 @@ void Update_bplus_from_lambda_psi(T_VAR_Parameters *p)
PRECISION
*
p_bplus
,
*
p_lambda
,
*
p_psi
;
if
(
!
(
p
->
Specification
&
SPEC_SIMS_ZHA
))
{
fprintf
(
std
err
,
"Update_bplus_from_lambda_psi() called without Sims-Zha specification
\n
"
);
swz_
fprintf
_
err
(
"Update_bplus_from_lambda_psi() called without Sims-Zha specification
\n
"
);
exit
(
0
);
}
for
(
j
=
p
->
nvars
-
1
;
j
>=
0
;
j
--
)
...
...
matlab/swz/c-code/sbvar/var/VARio_matlab.c
View file @
14a081c0
...
...
@@ -311,7 +311,7 @@ TStateModel* CreateStateModel_VAR_matlab(char *filename)
for
(
j
=
nvars
-
1
;
j
>=
0
;
j
--
)
if
(
IV
[
j
]
!=
npre
)
{
fprintf
(
std
err
,
"V[%d] not %d x %d
\n
"
,
j
,
npre
,
npre
);
swz_
fprintf
_
err
(
"V[%d] not %d x %d
\n
"
,
j
,
npre
,
npre
);
exit
(
0
);
}
V
=
dw_CreateArray_matrix
(
nvars
);
...
...
@@ -362,10 +362,10 @@ TStateModel* CreateStateModel_VAR_matlab(char *filename)
}
break
;
case
4
:
fprintf
(
std
err
,
"Case %d not implimented.
\n
"
,
4
);
swz_
fprintf
_
err
(
"Case %d not implimented.
\n
"
,
4
);
exit
(
0
);
default:
fprintf
(
std
err
,
"Unknown type.
\n
"
);
swz_
fprintf
_
err
(
"Unknown type.
\n
"
);
exit
(
0
);
}
dw_FreeArray
(
IM
);
...
...
matlab/swz/c-code/sbvar/var/create_init_file.c
View file @
14a081c0
...
...
@@ -26,7 +26,7 @@ int main(int nargs, char **args)
if
(
nargs
!=
4
)
{
fprintf
(
std
err
,
"Syntax:
\n
create_init_file <matlab filename> <specs filename> <file tag>
\n
"
);
swz_
fprintf
_
err
(
"Syntax:
\n
create_init_file <matlab filename> <specs filename> <file tag>
\n
"
);
exit
(
0
);
}
...
...
matlab/swz/c-code/sbvar/var/estimate.c
View file @
14a081c0
...
...
@@ -427,7 +427,7 @@ TStateModel* GetModelFromCommandLine(int nargs, char **args, TEstimateInfo *esti
free(filename);
//if (d2) free(d2);
//fprintf
(std
err
,
"GetModelFromCommandLine(): Unable to create model.\n");
//
swz_
fprintf
_
err
(
"GetModelFromCommandLine(): Unable to create model.\n");
goto ERROR;
}
...
...
@@ -452,7 +452,7 @@ TStateModel* GetModelFromCommandLine(int nargs, char **args, TEstimateInfo *esti
ERROR:
if (d2) free(d2);
//fprintf
(std
err
,
"GetModelFromCommandLine(): No specification file defined.\n");
//
swz_
fprintf
_
err
(
"GetModelFromCommandLine(): No specification file defined.\n");
return (TStateModel*)NULL;
}
...
...
matlab/swz/c-code/sbvar/var/mhm_VAR_main_1.c
View file @
14a081c0
...
...
@@ -271,7 +271,7 @@ T_MHM* CreateMHM_CommandLine(int nargs, char **args)
sprintf
(
spec_filename
=
(
char
*
)
malloc
(
strlen
(
d_in
)
+
strlen
(
fmt
)
+
strlen
(
tag
)
-
3
),
fmt
,
d_in
,
tag
);
if
(
!
(
f_in
=
fopen
(
spec_filename
,
"rt"
)))
{
fprintf
(
std
err
,
"CreateMHM_CommandLine: Unable to create model from %s tag.
\n
"
,
tag
);
swz_
fprintf
_
err
(
"CreateMHM_CommandLine: Unable to create model from %s tag.
\n
"
,
tag
);
if
(
mhm
)
FreeMHM
(
mhm
);
}
else
...
...
@@ -307,14 +307,14 @@ T_MHM* CreateMHM_CommandLine(int nargs, char **args)
}
else
{
fprintf
(
std
err
,
"CreateMHM_CommandLine(): No specification file given.
\n
"
);
swz_
fprintf
_
err
(
"CreateMHM_CommandLine(): No specification file given.
\n
"
);
if
(
mhm
)
FreeMHM
(
mhm
);
exit
(
0
);
}
if
(
!
mhm
)
{
fprintf
(
std
err
,
"CreateMHM_CommandLine: No mhm input data file specified.
\n
"
);
swz_
fprintf
_
err
(
"CreateMHM_CommandLine: No mhm input data file specified.
\n
"
);
exit
(
0
);
}
...
...
matlab/swz/c-code/sbvar/var/mhm_VAR_main_2.c
View file @
14a081c0
...
...
@@ -1595,7 +1595,7 @@ void ComputeMarginal_TruncatedGaussianProposal(int ndraws_proposal, TMatrix X, T
/* case TYPE_TABLE: */
/* return SetupSphericalFromPosterior_Table(X,n); */
/* default: */
/* fprintf
(std
err
,
"Unknown proposal type\n"); */
/*
swz_
fprintf
_
err
(
"Unknown proposal type\n"); */
/* exit(0); */
/* } */
/* } */
...
...
@@ -1621,7 +1621,7 @@ void ComputeMarginal_TruncatedGaussianProposal(int ndraws_proposal, TMatrix X, T
/* sprintf(filename,"table_md_%s.dat",tag); */
/* break; */
/* default: */
/* fprintf
(std
err
,
"Unknown proposal type\n"); */
/*
swz_
fprintf
_
err
(
"Unknown proposal type\n"); */
/* exit(0); */
/* } */
/* f_out=dw_CreateTextFile(filename); */
...
...
matlab/swz/c-code/sbvar/var/probabilities.c
View file @
14a081c0
...
...
@@ -88,14 +88,14 @@ int main(int nargs, char **args)
if
(
!
spec
)
{
fprintf
(
std
err
,
"No specification filename given
\n
"
);
fprintf
(
std
err
,
"Command line syntax:
\n
"
" -ft : file tag
\n
"
" -fs : specification filename
\n
"
" -fo : output filename (probablities.dat)
\n
"
" -fp : parameters filename (specification filename)
\n
"
" -fh : parameter header (Posterior mode: )
\n
"
);
swz_
fprintf
_
err
(
"No specification filename given
\n
"
);
swz_
fprintf
_
err
(
"Command line syntax:
\n
"
" -ft : file tag
\n
"
" -fs : specification filename
\n
"
" -fo : output filename (probablities.dat)
\n
"
" -fp : parameters filename (specification filename)
\n
"
" -fh : parameter header (Posterior mode: )
\n
"
);
exit
(
1
);
}
...
...
matlab/swz/c-code/utilities/DWCcode/spherical/spherical.c
View file @
14a081c0
...
...
@@ -163,7 +163,7 @@ PRECISION DrawSpherical(TVector x)
while
((
r
<
SPHERICAL_LOWER_TRUNCATE
)
||
(
SPHERICAL_UPPER_TRUNCATE
<
r
));
return
r
;
default:
fprintf
(
std
err
,
"Unknown spherical type
\n
"
);
swz_
fprintf
_
err
(
"Unknown spherical type
\n
"
);
exit
(
0
);
}
}
...
...
@@ -186,7 +186,7 @@ PRECISION LogSphericalDensity(PRECISION r)
case
SPHERICAL_TRUNCATED_GAUSSIAN
:
return
((
r
<
SPHERICAL_LOWER_TRUNCATE
)
||
(
r
>
SPHERICAL_UPPER_TRUNCATE
))
?
MINUS_INFINITY
:
-
0
.
5
*
r
*
r
+
SPHERICAL_CONSTANT
;
default:
fprintf
(
std
err
,
"Unknown spherical type
\n
"
);
swz_
fprintf
_
err
(
"Unknown spherical type
\n
"
);
exit
(
0
);
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment