# Examples¶

## Use statistics and sort functions¶

This example shows how to use statistics functions and how to sort an array using GSL sort:

include gsl.ampl;

set A := 1..5;
# Note that the parameter cannot have values assigned in the declaration
# or the sorting below will not work, as its values will be const.
param data{a in A} default 6-a;

display gsl_stats_mean({a in A} data[a]);
display gsl_stats_variance({a in A} data[a]);
display gsl_stats_max({a in A} data[a]);
display gsl_stats_min({a in A} data[a]);

call gsl_sort({a in A} data[a]);

# Now the data array is sorted, can use functions that
# expect sorted data
display gsl_stats_quantile_from_sorted_data({a in A} data[a], 0.66)


## Finding Minimum of the Gamma Function¶

The following model (gamma.mod) finds a minimum of the gamma function $$\Gamma(x)$$ for $$x > 0$$:

# Find minimum of the gamma function for x > 0.

include gsl.ampl;

var x >= 1e-5;
minimize obj: gsl_sf_gamma(x);
solve;
print x;


Solving this models gives an optimal solution $$x \approx 1.46163$$ which can be verified here.

## Hock and Schittkowski Models¶

The hs068.mod and hs069.mod models demonstrate the use of gsl_cdf_ugaussian_P(). They are taken form the Robert Vanderbei’s collection of nonlinear models and adapted for AMPLGSL. These models can be solved with a nonlinear AMPL solver such as MINOS:

\$ ampl hs068.mod
obj = -0.261841

MINOS 5.51: optimal solution found.
29 iterations, objective -0.9204250037
Nonlin evals: obj = 57, grad = 56, constrs = 57, Jac = 56.
x [*] :=
1  0.0678586
2  3.64621
3  0.000266135
4  0.894855
;

obj = -0.920425

Best known objective value: -0.920425026
{"ft_lang_label":"__label__en","ft_lang_prob":0.58734125,"math_prob":0.9743933,"size":1613,"snap":"2023-40-2023-50","text_gpt3_token_len":470,"char_repetition_ratio":0.12740833,"word_repetition_ratio":0.007874016,"special_character_ratio":0.32734036,"punctuation_ratio":0.1691843,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99555814,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-26T06:15:10Z\",\"WARC-Record-ID\":\"<urn:uuid:2d6ca3af-9e38-400e-9ba9-975e5c392ad0>\",\"Content-Length\":\"21334\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:75636655-b25e-4664-a63c-45b341a54bf3>\",\"WARC-Concurrent-To\":\"<urn:uuid:fab253b0-ac22-4125-8982-f43d25970b8a>\",\"WARC-IP-Address\":\"104.17.33.82\",\"WARC-Target-URI\":\"https://amplgsl.readthedocs.io/en/latest/amplgsl/examples.html\",\"WARC-Payload-Digest\":\"sha1:IGAAGGCCLO7HHPQEL4TJ6H7LFSV5COOA\",\"WARC-Block-Digest\":\"sha1:KUDNQY3AEHD7Q3UYKM4NYNX3K3DP5D2F\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510149.21_warc_CC-MAIN-20230926043538-20230926073538-00164.warc.gz\"}"}