-
Notifications
You must be signed in to change notification settings - Fork 18
/
mcutest.c
2190 lines (1807 loc) · 77.4 KB
/
mcutest.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* A grand unified Matlab gateway for the CUTEst tools.
* This interface brings together the unconstrained, constrained,
* dense and sparse versions of the CUTEst tools.
*
* In order to unify the tools and be able to use the same Matlab commands on
* both constrained and unconstrained problems, the tool names in this
* interface differ from the those in the old Fortran gateway routine.
*
* Tool CUTEst library function(s) Purpose
* --------------------------------------------------------------------------
* dims cdimen Obtain problem dimensions
* setup usetup / csetup Setup problem data structure
* obj uofg / cofg Evaluate objective function value
* and its gradient if requested
*
* grad ugr / cgr / cigr Evaluate objective function or
* constraint gradient
*
* sobj cofsg Evaluate objective function value
* and its sparse gradient if requested
*
* sgrad ugr / cisgr Evaluate objective function or
* constraint gradient as a sparse vector
*
* objcons cfn Evaluate objective and constraints
*
* cons ccfg / ccifg Evaluate constraint bodies
* and their gradients if requested.
* Evaluate a single constraint value
* and its gradient if requested
*
* scons ccfsg / ccifsg Evaluate constraint bodies and
* Jacobian in sparse format.
* Evaluate a single constraint value
* and its gradient as a sparse vector
*
* lag clfg Evaluate Lagrangian function value
* and its gradient if requested
*
* lagjac cgr Evaluate Jacobian and gradient of
* either objective or Lagrangian
*
* slagjac csgr Evaluate Jacobian in sparse format
* and gradient of either objective or
* Lagrangian as a sparse vector
*
* Jprod cjprod Evaluate the matrix-vector product
* between the Jacobian and a vector
*
* Jtprod cjprod Evaluate the matrix-vector product
* between the transpose Jacobian and
* a vector
*
* hess udh / cdh Evaluate the Hessian matrix of the
* Lagrangian, or of the objective if
* the problem is unconstrained
*
* ihess udh / cidh Evaluate the Hessian matrix of the
* i-th problem function (i=0 is the
* objective function), or of the
* objective if problem is unconstrained
*
* hprod uprod / cprod Evaluate the matrix-vector product
* between the Hessian of the
* Lagrangian
* (or the objective if unconstrained)
* and a vector
*
* gradhess ugrdh / cgrdh Evaluate the gradient of either the
* objective or the Lagrangian, the
* Jacobian (or its transpose) and the
* Hessian of the Lagrangian in dense
* format
*
* sphess ush / csh Evaluate the Hessian matrix of the
* Lagrangian, or of the objective if
* the problem is unconstrained, in
* sparse format
*
* isphess ush / cish Evaluate the Hessian matrix of the
* i-th problem function (i=0 is the
* objective function), or of the
* objective if problem is
* unconstrained, in sparse format
*
* varnames varnames Obtain variable names as strings
*
* connames cnames Obtain constraint names as strings
*
* terminate uterminate / cterminate Remove existing internal workspace
*
* CUTEr version:
* D. Orban, Montreal, January 2007
* CUTEst version additions:
* Nick Gould, January 2013
* This version, March 13 2018 16:00 GMT
*/
/* -------------------------------------------------------------------------- */
/* Includes */
#include "mex.h"
#include "matrix.h"
#include "cutest.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* For versions of the Matlab API prior to 7.3 -
depricatd as Matlab no longer has the MX_API_VER preprocessor macro ... duh!
#if (MX_API_VER < 0x07030000)
typedef int mwSize;
typedef int mwIndex;
#endif
*/
#ifndef MWSIZE_MAX
#define mwIndex int
#define mwSignedIndex int
#define mwSize int
#endif
/* Safeguard against C++ symbol mangling */
#ifdef __cplusplus
extern "C" {
#endif
/* Macro-commands */
/* ----------------------------------------------------------------------- */
#define isInteger(x) (mxIsInt8((x)) || mxIsUint8((x)) || mxIsInt16((x)) || mxIsUint16((x)) || mxIsInt32((x)) || mxIsUint32((x)) || mxIsInt64((x)) || mxIsUint64((x)))
#define STR_LEN 10
/* ----------------------------------------------------------------------- */
/* Persistent data */
static integer CUTEst_nvar = 0; /* number of variables */
static integer CUTEst_ncon = 0; /* number of constraints */
static integer CUTEst_nnzj = 0; /* nnz in Jacobian */
static integer CUTEst_nnzh = 0; /* nnz in upper triangular Hessian */
static integer CUTEst_dertype = 2; /* derivative type */
static char setupCalled = 0; /* Flag to indicate if setup was called */
static char dataFileOpen = 0; /* Flag to indicate if OUTSDIf is open */
static char onlyConst[] = "%-s only available for constrained problems\n";
/* ------------------------------------------------------------------------*/
/* Prototypes */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]);
mxArray *SparseVector(int n, int nnz, integer *index, double *val);
mxArray *extractSparseVector(int nrow, int ncol, int nnz, int nnzV,
integer *irow, integer *jcol, double *val);
mxArray *coordToMatlabSparse(int nrow, int ncol, int nnz,
integer *irow, integer *jcol, double *val);
void quicksortFollow(mwIndex x[], double follower[],
mwIndex first, mwIndex last);
void quicksort_cutest(mwIndex numbers[], double values[],
mwIndex left, mwIndex right);
int partition(mwIndex y[], double follower[], mwIndex f, mwIndex l);
void swap(mwIndex y[], double follower[], mwIndex el1, mwIndex el2);
/* ----------------------------------------------------------------------- */
/* Main entry point */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
/* ------------------------------------------------------------------ */
/* Field names for problem data structure */
const char
field_n[] = "n",
field_m[] = "m",
field_nnzh[] = "nnzh",
field_nnzj[] = "nnzj",
field_x[] = "x",
field_bl[] = "bl",
field_bu[] = "bu",
field_v[] = "v",
field_cl[] = "cl",
field_cu[] = "cu",
field_equatn[] = "equatn",
field_linear[] = "linear",
field_pbname[] = "name";
const char *fieldNames[] = { field_n, field_m, field_nnzh, field_nnzj,
field_x, field_bl, field_bu, field_v,
field_cl, field_cu, field_equatn,
field_linear, field_pbname };
int nFields = sizeof(fieldNames)/sizeof(fieldNames[0]);
integer icon, *icon_ptr, *dertype_ptr;
integer zero = 0;
integer *irow, *jcol, *irow2, *jcol2;
integer nnzgci, nnzjplusn, offdiag_nnzh, nnzhi, nnzh2, lj;
char *toolName = NULL;
char fName[] = "OUTSDIF.d";
integer funit = 42; /* FORTRAN unit number for OUTSDIF.d */
integer iout = 6; /* FORTRAN unit number for error output */
integer ioErr; /* Exit flag from OPEN and CLOSE */
integer io_buffer = 11; /* FORTRAN unit internal input/output */
integer status; /* Exit flag from CUTEst tools */
char msgBuf[256];
int *iir = NULL;
int bufLen, errCopy;
double *nptr = NULL, *mptr = NULL, *nnzjptr = NULL, *nnzhptr = NULL;
doublereal *x = NULL, *bl = NULL, *bu = NULL, *v = NULL, *cl = NULL,
*cu = NULL, *f = NULL, *c = NULL, *g = NULL, *J = NULL,
*H = NULL;
doublereal *p, *r;
logical *equatn = NULL, *linear = NULL;
/* logical efirst = TRUE_, lfirst = TRUE_, nvfrst = FALSE_; */
integer e_order = 1, l_order = 1, v_order = 0;
logical somethingFalse = FALSE_, somethingTrue = TRUE_;
logical individual;
/* Matlab logicals are not the same as CUTEst logicals */
mxLogical *eFirst, *lFirst, *nvFirst;
bool *bool_equatn, *bool_linear;
char probName[STR_LEN+1];
char *cptr;
char *Fvnames, *Fcnames; /* For Fortran */
char **vNames, **cNames; /* C arrays of strings */
mxArray *Mn = NULL, *Mm = NULL, *Mnnzh = NULL, *Mnnzj = NULL,
*Mx = NULL, *Mbl = NULL, *Mbu = NULL, *Mequatn = NULL,
*Mlinear = NULL, *Mv = NULL, *Mcl = NULL, *Mcu = NULL,
*MprobName = NULL;
mxLogical *gradfptr, *jtransptr;
logical gradf, jtrans;
mwIndex *ir, *jptr;
int i, j;
mxArray *matrix; /* Output Matlab sparse matrix */
/* ------------------------------------------------------------------ */
if (nrhs == 0) mexErrMsgTxt("At least one argument must be given\n");
if (mxIsChar(prhs[0]) != 1)
mexErrMsgTxt("First argument must be the tool name\n");
bufLen = mxGetN(prhs[0]) + 1;
if (! (toolName = mxCalloc(bufLen, sizeof(char))))
mexErrMsgTxt("Could not allocate memory to read tool name\n");
errCopy = mxGetString(prhs[0], toolName, bufLen);
if (errCopy) mexWarnMsgTxt("Tool name was truncated by mistake\n");
#ifdef MXDEBUG
mexPrintf("Calling %-s with %-d input arg(s) and %-d output arg(s)\n",
toolName, nrhs-1, nlhs);
#endif
/* ------------------------------------------------------------------ */
/* Obtain problem dimensions.
* usage: cutest_dims()
*/
if (strcmp(toolName, "dims") == 0) {
if (nlhs != 2) mexErrMsgTxt("cutest_dims returns 2 output values\n");
if (nrhs > 1) mexWarnMsgTxt("cutest_dims takes no input argument\n");
#ifdef MXDEBUG
mexPrintf("Opening data file\n");
#endif
ioErr = 0;
if (! dataFileOpen) FORTRAN_open(&funit, fName, &ioErr);
if (ioErr) mexErrMsgTxt("Error opening file OUTSDIF.d\n");
dataFileOpen = 1;
#ifdef MXDEBUG
mexPrintf("Calling CDIMEN\n");
#endif
CUTEST_cdimen( &status, &funit, &CUTEst_nvar, &CUTEst_ncon);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
#ifdef MXDEBUG
mexPrintf(" n = %-d, m = %-d\n", CUTEst_nvar, CUTEst_ncon);
#endif
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
nptr = mxGetPr(plhs[0]);
*nptr = (double)CUTEst_nvar;
plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
mptr = mxGetPr(plhs[1]);
*mptr = (double)CUTEst_ncon;
mxFree((void *)toolName);
return;
}
/* Setup problem and return a Matlab structure with all data.
* Usage: prob = cutest_setup()
*/
if (strcmp(toolName, "setup") == 0) {
if (setupCalled) mexErrMsgTxt("cutest_setup: cutest_terminate must be called first\n");
if (nlhs != 1) mexErrMsgTxt("cutest_setup returns one output\n");
if (nrhs > 1) {
if (nrhs == 2) {
if (! isInteger(prhs[1]) && ! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_setup: derivative type must be integer\n");
if (isInteger(prhs[1])) {
dertype_ptr = (integer *)mxGetData(prhs[2]);
CUTEst_dertype = dertype_ptr[0];
} else {
CUTEst_dertype = (integer)*mxGetPr(prhs[1]);
}
}
else if (nrhs == 5) {
/* Check input arguments type */
if (! isInteger(prhs[1]) && ! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_setup: derivative type must be integer\n");
if (isInteger(prhs[1])) {
dertype_ptr = (integer *)mxGetData(prhs[1]);
CUTEst_dertype = dertype_ptr[0];
} else {
CUTEst_dertype = (integer)*mxGetPr(prhs[1]);
}
for (i = 2; i < 5; i++)
if (!mxIsLogicalScalar(prhs[i]))
mexWarnMsgTxt("Setup args 2-4 must be logicals\n");
/* Read input arguments */
eFirst = mxGetLogicals(prhs[2]);
lFirst = mxGetLogicals(prhs[3]);
nvFirst = mxGetLogicals(prhs[4]);
/* efirst = *eFirst ? TRUE_ : FALSE_;
lfirst = *lFirst ? TRUE_ : FALSE_;
nvfrst = *nvFirst ? TRUE_ : FALSE_; */
}
else {
mexErrMsgTxt("setup takes 0, 1 or 4 arguments\n");
}
}
else {
CUTEst_dertype = 2;
}
if (CUTEst_dertype < 0 || CUTEst_dertype > 2) {
CUTEst_dertype = 2;
}
#ifdef MXDEBUG
mexPrintf("Opening data file\n");
#endif
ioErr = 0;
if (! dataFileOpen) FORTRAN_open(&funit, fName, &ioErr);
if (ioErr) mexErrMsgTxt("Error opening file OUTSDIF.d\n");
#ifdef MXDEBUG
mexPrintf("Calling CDIMEN\n");
#endif
CUTEST_cdimen( &status, &funit, &CUTEst_nvar, &CUTEst_ncon);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
#ifdef MXDEBUG
mexPrintf(" n = %-d, m = %-d\n", CUTEst_nvar, CUTEst_ncon);
mexPrintf("Allocating double/logical work space\n");
#endif
Mx = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
Mbl = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
Mbu = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
Mv = mxCreateDoubleMatrix(CUTEst_ncon, 1, mxREAL);
Mcl = mxCreateDoubleMatrix(CUTEst_ncon, 1, mxREAL);
Mcu = mxCreateDoubleMatrix(CUTEst_ncon, 1, mxREAL);
#ifdef MXDEBUG
mexPrintf("Transfering pointers\n");
#endif
x = (doublereal *)mxGetData(Mx);
bl = (doublereal *)mxGetData(Mbl);
bu = (doublereal *)mxGetData(Mbu);
equatn = (logical *)mxCalloc(CUTEst_ncon, sizeof(logical));
linear = (logical *)mxCalloc(CUTEst_ncon, sizeof(logical));
v = (doublereal *)mxGetData(Mv);
cl = (doublereal *)mxGetData(Mcl);
cu = (doublereal *)mxGetData(Mcu);
#ifdef MXDEBUG
mexPrintf("Calling [UC]SETUP\n");
#endif
if (CUTEst_ncon > 0)
CUTEST_csetup( &status, &funit, &iout, &io_buffer,
&CUTEst_nvar, &CUTEst_ncon, x, bl, bu,
v, cl, cu, equatn, linear,
&e_order, &l_order, &v_order );
else
CUTEST_usetup( &status, &funit, &iout, &io_buffer,
&CUTEst_nvar, x, bl, bu );
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
#ifdef MXDEBUG
mexPrintf(" n = %-d, m = %-d\n", CUTEst_nvar, CUTEst_ncon);
#endif
/* Transfer equatn and logical */
Mequatn = mxCreateLogicalMatrix(CUTEst_ncon, 1);
Mlinear = mxCreateLogicalMatrix(CUTEst_ncon, 1);
bool_equatn = (bool *)mxGetData(Mequatn);
bool_linear = (bool *)mxGetData(Mlinear);
for (i = 0; i < CUTEst_ncon; i++) {
bool_equatn[i] = equatn[i] ? true : false;
bool_linear[i] = linear[i] ? true : false;
}
/* Free temporary logical arrays */
mxFree(equatn);
mxFree(linear);
#ifdef MXDEBUG
mexPrintf("Calling CDIMSH/CDIMSJ\n");
#endif
if (CUTEst_dertype == 2) {
if (CUTEst_ncon > 0)
CUTEST_cdimsh( &status, &CUTEst_nnzh);
else
CUTEST_udimsh( &status, &CUTEst_nnzh);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
} else {
CUTEst_nnzh = - 1 ;
}
if (CUTEst_dertype > 0) {
if (CUTEst_ncon > 0) {
CUTEST_cdimsj( &status, &CUTEst_nnzj);
CUTEst_nnzj -= CUTEst_nvar;
}
} else {
CUTEst_nnzj = - 1 ;
}
#ifdef MXDEBUG
mexPrintf(" nnzh = %-d, nnzj = %-d\n", CUTEst_nnzh, CUTEst_nnzj);
mexPrintf("Finding out problem name\n");
#endif
CUTEST_probname( &status, probName );
probName[STR_LEN] = '\0';
MprobName = mxCreateString(probName);
#ifdef MXDEBUG
mexPrintf(" %-s\n", probName);
mexPrintf("Closing data file\n");
#endif
FORTRAN_close(&funit, &ioErr);
if (ioErr) mexWarnMsgTxt("Error closing file OUTSDIF.d\n");
dataFileOpen = 0;
#ifdef MXDEBUG
mexPrintf("Storing integer data\n");
#endif
Mn = mxCreateDoubleMatrix(1, 1, mxREAL);
nptr = mxGetPr(Mn);
*nptr = (double)CUTEst_nvar;
Mm = mxCreateDoubleMatrix(1, 1, mxREAL);
mptr = mxGetPr(Mm);
*mptr = (double)CUTEst_ncon;
Mnnzh = mxCreateDoubleMatrix(1, 1, mxREAL);
nnzhptr = mxGetPr(Mnnzh);
*nnzhptr = (double)CUTEst_nnzh;
Mnnzj = mxCreateDoubleMatrix(1, 1, mxREAL);
nnzjptr = mxGetPr(Mnnzj);
*nnzjptr = (double)CUTEst_nnzj;
#ifdef MXDEBUG
mexPrintf("Building struct with %-d fields\n", nFields);
#endif
plhs[0] = mxCreateStructMatrix(1, 1, nFields, fieldNames);
mxSetField(plhs[0], 0, field_n, Mn );
mxSetField(plhs[0], 0, field_m, Mm );
mxSetField(plhs[0], 0, field_nnzh, Mnnzh );
mxSetField(plhs[0], 0, field_nnzj, Mnnzj );
mxSetField(plhs[0], 0, field_x, Mx );
mxSetField(plhs[0], 0, field_bl, Mbl );
mxSetField(plhs[0], 0, field_bu, Mbu );
mxSetField(plhs[0], 0, field_v, Mv );
mxSetField(plhs[0], 0, field_cl, Mcl );
mxSetField(plhs[0], 0, field_cu, Mcu );
mxSetField(plhs[0], 0, field_equatn, Mequatn );
mxSetField(plhs[0], 0, field_linear, Mlinear );
mxSetField(plhs[0], 0, field_pbname, MprobName);
setupCalled = 1;
mxFree((void *)toolName);
return;
}
/* ------------------------------------------------------------------ */
if (! setupCalled) mexErrMsgTxt("cutest_setup must be called first\n");
/* Obtain variable names
* Usage: vnames = cutest_varnames( &status, )
*/
if (strcmp(toolName, "varnames") == 0) {
if (nlhs != 1) mexErrMsgTxt("varnames returns a single output\n");
if (nrhs > 1)
mexWarnMsgTxt("varnames does not take input arguments\n");
MALLOC(Fvnames, CUTEst_nvar * STR_LEN, char);
if (!Fvnames)
mexErrMsgTxt("cutest_varnames: Error allocating room for variable names\n");
CUTEST_varnames( &status, &CUTEst_nvar, Fvnames);
/* Transfer to a C array of strings.
* If you know of a cleaner and portable way to do this, please
* let me know!
*/
MALLOC(vNames, CUTEst_nvar, char*);
for (i = 0; i < CUTEst_nvar; i++) {
MALLOC(vNames[i], STR_LEN+1, char);
cptr = Fvnames + i * STR_LEN;
for (j = 0; j < STR_LEN; j++) {
vNames[i][j] = *cptr;
cptr++;
}
vNames[i][STR_LEN] = '\0';
}
FREE(Fvnames);
plhs[0] = mxCreateCharMatrixFromStrings((mwSize)CUTEst_nvar,
(const char **)vNames);
for (i = 0; i < CUTEst_nvar; i++) FREE(vNames[i]);
FREE(vNames);
mxFree((void *)toolName);
return;
}
/* ------------------------------------------------------------------ */
/* Obtain constraint names
* Usage: cnames = cutest_connames( &status, )
*/
if (strcmp(toolName, "connames") == 0) {
if (CUTEst_ncon == 0) {
sprintf(msgBuf, onlyConst, toolName);
mexErrMsgTxt(msgBuf);
}
if (nlhs != 1) mexErrMsgTxt("cutest_connames returns a single output\n");
if (nrhs > 1)
mexWarnMsgTxt("cutest_connames does not take input arguments\n");
MALLOC(Fcnames, CUTEst_ncon * STR_LEN, char);
if (!Fcnames)
mexErrMsgTxt("cutest_connames: Error allocating room for constraint names\n");
CUTEST_connames( &status, &CUTEst_ncon, Fcnames);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
/* Transfer to a C array of strings.
* If you know of a cleaner and portable way to do this, please
* let me know!
*/
MALLOC(cNames, CUTEst_ncon, char*);
for (i = 0; i < CUTEst_ncon; i++) {
MALLOC(cNames[i], STR_LEN+1, char);
cptr = Fcnames + i * STR_LEN;
for (j = 0; j < STR_LEN; j++) {
cNames[i][j] = *cptr;
cptr++;
}
cNames[i][STR_LEN] = '\0';
}
FREE(Fcnames);
plhs[0] = mxCreateCharMatrixFromStrings((mwSize)CUTEst_ncon,
(const char **)cNames);
for (i = 0; i < CUTEst_ncon; i++) FREE(cNames[i]);
FREE(cNames);
mxFree((void *)toolName);
return;
}
/* ------------------------------------------------------------------ */
/* Evaluate objective function value and constraint bodies.
* Usage: [f,c] = cutest_objcons(x)
*/
if (strcmp(toolName, "objcons") == 0) {
if (CUTEst_ncon == 0) {
sprintf(msgBuf, onlyConst, toolName);
mexWarnMsgTxt(msgBuf);
}
if (nrhs != 2) mexErrMsgTxt("cutest_objcons: Please specify x\n");
if (nlhs != 2) mexErrMsgTxt("cutest_objcons: Need two output arguments\n");
if (! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_objcons: Input array must have type double\n");
if (mxGetNumberOfElements(prhs[1]) != CUTEst_nvar)
mexErrMsgTxt("cutest_objcons: Input array has erroneous size\n");
x = (doublereal *)mxGetData(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
f = (doublereal *)mxGetData(plhs[0]);
plhs[1] = mxCreateDoubleMatrix(CUTEst_ncon, 1, mxREAL);
c = (doublereal *)mxGetData(plhs[1]);
CUTEST_cfn( &status, &CUTEst_nvar, &CUTEst_ncon, x, f, c);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
mxFree((void *)toolName);
return;
}
/* =============== Dense first derivative tools ===================== */
/* Return function value and gradient if requested.
* Usage: f = cutest_obj(x) or [f,g] = cutest_obj(x)
*/
if (strcmp(toolName, "obj") == 0) {
if (nrhs != 2) mexErrMsgTxt("cutest_obj: Please specify x\n");
if (nlhs < 1) mexErrMsgTxt("cutest_obj: Please specify an output argument\n");
if (nlhs > 2) mexErrMsgTxt("cutest_obj: Too many output arguments\n");
if (! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_obj: Input array must have type double\n");
if (mxGetNumberOfElements(prhs[1]) != CUTEst_nvar)
mexErrMsgTxt("cutest_obj: Input array has erroneous size\n");
x = (doublereal *)mxGetData(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
f = (doublereal *)mxGetData(plhs[0]);
if (nlhs == 2) {
plhs[1] = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
g = (doublereal *)mxGetData(plhs[1]);
}
if (CUTEst_ncon == 0)
if (nlhs == 1)
CUTEST_uofg( &status, &CUTEst_nvar, x, f, NULL, &somethingFalse);
else
CUTEST_uofg( &status, &CUTEst_nvar, x, f, g, &somethingTrue);
else
if (nlhs == 1)
CUTEST_cofg( &status, &CUTEst_nvar, x, f, NULL, &somethingFalse);
else
CUTEST_cofg( &status, &CUTEst_nvar, x, f, g, &somethingTrue);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
mxFree((void *)toolName);
return;
}
/* Return function value and sparse gradient if requested.
* Usage: f = cutest_sobj(x) or [f,sg] = cutest_sobj(x)
*/
if (strcmp(toolName, "sobj") == 0) {
if (nrhs != 2) mexErrMsgTxt("cutest_sobj: Please specify x\n");
if (nlhs < 1) mexErrMsgTxt("cutest_sobj: Please specify an output argument\n");
if (nlhs > 2) mexErrMsgTxt("cutest_sobj: Too many output arguments\n");
if (! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_sobj: Input array must have type double\n");
if (mxGetNumberOfElements(prhs[1]) != CUTEst_nvar)
mexErrMsgTxt("cutest_sobj: Input array has erroneous size\n");
x = (doublereal *)mxGetData(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
f = (doublereal *)mxGetData(plhs[0]);
/*
if (nlhs == 2) {
plhs[1] = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
g = (doublereal *)mxGetData(plhs[1]);
} */
if (CUTEst_ncon == 0) {
/* sobj does not apply to unconstrained problems.
* Return dense gradient if requested.
*/
sprintf(msgBuf, onlyConst, toolName);
mexWarnMsgTxt(msgBuf);
if (nlhs == 1)
CUTEST_uofg( &status, &CUTEst_nvar, x, f, NULL, &somethingFalse);
else {
plhs[1] = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
g = (doublereal *)mxGetData(plhs[1]);
CUTEST_uofg( &status, &CUTEst_nvar, x, f, g, &somethingTrue);
}
}
else
if (nlhs == 1) {
CUTEST_cofsg( &status, &CUTEst_nvar, x, f, &zero, &zero,
NULL, NULL, &somethingFalse);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
}
else {
nnzgci = CUTEst_nvar;
ir = mxCalloc(nnzgci, sizeof(integer));
g = (doublereal *)mxCalloc(nnzgci, sizeof(doublereal));
integer nnzgci0 = nnzgci;
CUTEST_cofsg( &status, &CUTEst_nvar, x, f, &nnzgci, &nnzgci0, g,
(integer *)ir, &somethingTrue);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
plhs[1] = SparseVector(CUTEst_nvar, nnzgci, (integer *)ir, (double *)g);
mxFree(ir);
mxFree(g);
}
mxFree((void *)toolName);
return;
}
/* =============== Dense first derivative tools ===================== */
/* Return Lagrangian function value and gradient if requested.
* Usage: f = cutest_lag(x,y) or [f,g] = cutest_lag(x,y)
*/
if (strcmp(toolName, "lag") == 0) {
if (nrhs < 2 || nrhs > 3)
mexErrMsgTxt("cutest_lag: Please specify x and v\n");
if (nlhs < 1) mexErrMsgTxt("cutest_lag: Please specify an output argument\n");
if (nlhs > 2) mexErrMsgTxt("cutest_lag: Too many output arguments\n");
if (! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_lag: 1st input array must be of type double\n");
if (! mxIsDouble(prhs[2]))
mexErrMsgTxt("cutest_lag: 2nd input array must be of type double\n");
x = (doublereal *)mxGetData(prhs[1]);
v = (doublereal *)mxGetData(prhs[2]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
f = (doublereal *)mxGetData(plhs[0]);
if (nlhs == 2) {
plhs[1] = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
g = (doublereal *)mxGetData(plhs[1]);
}
if (nlhs == 1)
CUTEST_clfg( &status, &CUTEst_nvar, &CUTEst_ncon, x, v,
f, NULL, &somethingFalse);
else
CUTEST_clfg( &status, &CUTEst_nvar, &CUTEst_ncon,x, v,
f, g, &somethingTrue);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
mxFree((void *)toolName);
return;
}
/* ------------------------------------------------------------------ */
/* Return function gradient.
* Usage: g = cutest_grad(x) or g = cutest_grad(x,i)
*/
if (strcmp(toolName, "grad") == 0) {
if (nrhs < 2 || nrhs > 4)
mexErrMsgTxt("cutest_grad: Please specify x and possibly index\n");
if (nlhs < 1) mexErrMsgTxt("cutest_grad: Please specify an output argument\n");
if (nlhs > 2) mexErrMsgTxt("cutest_grad: Too many output arguments\n");
if (! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_grad: Input array must have type double\n");
if (mxGetNumberOfElements(prhs[1]) != CUTEst_nvar)
mexErrMsgTxt("cutest_grad: Input array has erroneous size\n");
if (nrhs == 3) {
if (! isInteger(prhs[2]) && ! mxIsDouble(prhs[2]))
mexErrMsgTxt("cutest_cons: Constraint index must be integer\n");
if (isInteger(prhs[2])) {
icon_ptr = (integer *)mxGetData(prhs[2]);
icon = icon_ptr[0];
} else
icon = (integer)*mxGetPr(prhs[2]);
if (icon < 0 || icon > CUTEst_ncon) {
sprintf(msgBuf,
"cutest_cons: Invalid constraint index %-d\n", icon);
mexErrMsgTxt(msgBuf);
}
} else {
icon = 0;
}
x = (doublereal *)mxGetData(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
g = (doublereal *)mxGetData(plhs[0]);
/* if (nrhs == 2) {
if (CUTEst_ncon > 0)
CUTEST_cigr( &status, &CUTEst_nvar, 0, x, g);
else
CUTEST_ugr( &status, &CUTEst_nvar, x, g);
} else { */
if (CUTEst_ncon > 0)
CUTEST_cigr( &status, &CUTEst_nvar, &icon, x, g);
else
CUTEST_ugr( &status, &CUTEst_nvar, x, g);
/* } */
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
mxFree((void *)toolName);
return;
}
/* ------------------------------------------------------------------ */
/* Return function gradient.
* Usage: g = cutest_sgrad(x) or g = cutest_sgrad(x,i)
*/
if (strcmp(toolName, "sgrad") == 0) {
if (nrhs < 2 || nrhs > 4)
mexErrMsgTxt("cutest_grad: Please specify x and possibly index\n");
if (nlhs < 1) mexErrMsgTxt("cutest_grad: Please specify an output argument\n");
if (nlhs > 2) mexErrMsgTxt("cutest_grad: Too many output arguments\n");
if (! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_grad: Input array must have type double\n");
if (mxGetNumberOfElements(prhs[1]) != CUTEst_nvar)
mexErrMsgTxt("cutest_grad: Input array has erroneous size\n");
if (nrhs == 3) {
if (! isInteger(prhs[2]) && ! mxIsDouble(prhs[2]))
mexErrMsgTxt("cutest_cons: Constraint index must be integer\n");
if (isInteger(prhs[2])) {
icon_ptr = (integer *)mxGetData(prhs[2]);
icon = icon_ptr[0];
} else
icon = (integer)*mxGetPr(prhs[2]);
if (icon < 0 || icon > CUTEst_ncon) {
sprintf(msgBuf,
"cutest_cons: Invalid constraint index %-d\n", icon);
mexErrMsgTxt(msgBuf);
}
} else {
icon = 0;
}
x = (doublereal *)mxGetData(prhs[1]);
if (CUTEst_ncon == 0) {
/* sobj does not apply to unconstrained problems.
* Return dense gradient if requested.
*/
sprintf(msgBuf, onlyConst, toolName);
mexWarnMsgTxt(msgBuf);
plhs[0] = mxCreateDoubleMatrix(CUTEst_nvar, 1, mxREAL);
g = (doublereal *)mxGetData(plhs[0]);
CUTEST_ugr( &status, &CUTEst_nvar, x, g );
} else {
nnzgci = CUTEst_nvar;
ir = mxCalloc(nnzgci, sizeof(integer));
g = (doublereal *)mxCalloc(nnzgci, sizeof(doublereal));
integer nnzgci0 = nnzgci;
CUTEST_cisgr( &status, &CUTEst_nvar, &icon, x, &nnzgci, &nnzgci0, g,
(integer *)ir);
if (status != 0) {
sprintf(msgBuf,"** CUTEst error, status = %d, aborting\n", status);
mexErrMsgTxt(msgBuf);
}
plhs[0] = SparseVector(CUTEst_nvar, nnzgci, (integer *)ir, (double *)g);
mxFree(ir);
mxFree(g);
}
mxFree((void *)toolName);
return;
}
/* ------------------------------------------------------------------ */
/* Return constraint bodies and Jacobian if requested.
* Usage: c = cutest_cons(x) or [c,J] = cutest_cons(x)
* ci = cutest_cons(x,i) or [ci,gi] = cutest_cons(x,i)
*/
if (strcmp(toolName, "cons") == 0) {
if (CUTEst_ncon == 0) {
sprintf(msgBuf, onlyConst, toolName);
mexWarnMsgTxt(msgBuf);
}
if (nrhs < 2 || nrhs > 4)
mexErrMsgTxt("cutest_cons: Please specify x and possibly index\n");
if (nlhs < 1) mexErrMsgTxt("cutest_cons: Please specify an output argument\n");
if (nlhs > 2) mexErrMsgTxt("cutest_cons: Too many output arguments\n");
if (! mxIsDouble(prhs[1]))
mexErrMsgTxt("cutest_cons: Input array must have type double\n");
if (mxGetNumberOfElements(prhs[1]) != CUTEst_nvar)
mexErrMsgTxt("cutest_cons: Input array has erroneous size\n");
if (nrhs == 3) {
if (! isInteger(prhs[2]) && ! mxIsDouble(prhs[2]))
mexErrMsgTxt("cutest_cons: Constraint index must be integer\n");
if (isInteger(prhs[2])) {
icon_ptr = (integer *)mxGetData(prhs[2]);
icon = icon_ptr[0];
} else
icon = (integer)*mxGetPr(prhs[2]);
if (icon <= 0 || icon > CUTEst_ncon) {
sprintf(msgBuf,
"cutest_cons: Invalid constraint index %-d\n", icon);
mexErrMsgTxt(msgBuf);
}
}
x = (doublereal *)mxGetData(prhs[1]);
if (nrhs == 2) {
/* Constraint bodies (and Jacobian) were requested */
plhs[0] = mxCreateDoubleMatrix(CUTEst_ncon, 1, mxREAL);
c = (doublereal *)mxGetData(plhs[0]);
if (nlhs == 2) {
plhs[1] =mxCreateDoubleMatrix(CUTEst_ncon,CUTEst_nvar,mxREAL);
J = (doublereal *)mxGetData(plhs[1]);
}