# Ox Function Examples

## Chapter contents:

| A |
acf | acos | aggregatec | aggregater | any
arglist | array
| B |
binand | bincomp | binor | binpop | binxor
| C |
cabs | cdiv | cerf | cexp | clog
cmul | csqrt | ceil | choleski | columns
constant | correlation | countc | countr | cumprod
cumsum | cumulate
| D |
date | dayofcalendar | dayofeaster | dayofmonth | dayofweek
decldl | decldlband | declu | decqr | decqrmul
decqrupdate | decschur | decschurgen | decsvd | deletec
deleter | deleteifc | deleteifr | determinant | dfft
diag | diagcat | diagonal | diagonalize | diff0
discretize | dropc | dropr
| E |
eigen | eigensym | eigensymgen | eprint | exclusion
exp
| F |
fabs | fclose | fft | fft1d | find
findsample | floor | fmod | fprint | fprintln
fread | fscan | fseek | fwrite
| G |
gammafunc | getfiles | getfolders
| I |
idiv | imod | insertc | int | intersection
invert | invertgen | invertsym | isdotfeq | isfeq
isdotmissing | isdotnan | ismissing | isnan
| L |
lag0 | limits | loadmat | loadsheet | log
log10 | loggamma | lower
| M |
max | maxc | maxcindex | maxr | meanc
min | moments
| N |
nans | norm | nullspace
| O |
ols2c | ols2r | olsc | olsr | ones
outer | oxprintlevel | oxwarning
| P |
periodogram | polydiv | polyeval | polygamma | polymake
polymul | polyroots | pow | print | println
probchi | probf | probn | probt | prodc
prodr
| Q |
quanchi | quanf | quann | quant | quantilec
quantiler
| R |
range | rank | rann | ranseed | ranu
reflect | replace | reshape | reversec | reverser
round | rows
| S |
savemat | scan | selectc | selectr | selectifc
selectifr | selectrc | setbounds | setdiagonal | setlower
setupper | shape | sizec | sizeof | sizer
sizerc | solveldl | solveldlband | solvelu | solvetoeplitz
sortbyc | sortbyr | sortc | sortcindex | sortr
spline | sprint | sqr | sqrt | sscan
standardize | strfind | strfindr | strifind | strifindr
strlwr | strtrim | strupr | sumc | sumr
sumsqrc
| T |
tailchi | tailf | tailn | tailt | thinc
thinr | time | timeofday | timer | timespan
timestr | timing | today | toeplitz | trace
trunc | truncf
| U |
union | unit | unvech | upper
| V |
va_arglist | varc | varr | variance | vec
vech | vecindex | vecr | vecrindex
| Z |
zeros

## acf

The example computes a correlogram twice, once using the library function, and once `manually' (in the matrix `macf`).

    #include <oxstd.oxh>
    main()
    {
        decl i, m1 = rann(200,2), m1m, macf, ilag = 5;

        macf = new matrix[ilag + 1][2];
        m1m = m1 - meanc(m1);         // in deviation from mean

        for (i = 0; i <= ilag; ++i)
            macf[i][] = diagonal(m1m'lag0(m1m, i));
        macf = macf ./ macf[0][];          // scale by variance

        print( acf(m1, ilag) ~ macf);
    }

produces

           1.0000       1.0000       1.0000       1.0000
       -0.0021973    -0.046870   -0.0021973    -0.046870
        -0.041011    -0.051470    -0.041011    -0.051470
        -0.050879    -0.039346    -0.050879    -0.039346
         0.056525    -0.093980     0.056525    -0.093980
         0.021034      0.12671     0.021034      0.12671

Definition

## acos

    #include <oxstd.oxh>
    main()
    {
        print( acos(<0,1>) );
        print( asin(<0,1>) );
        print( atan(<0,1>) );
        print( cos(<0,1>) );
        print( cosh(<0,1>) );
        print( sin(<0,1>) );
        print( sinh(<0,1>) );
        print( tan(<0,1>) );
        print( tanh(<0,1>) );
    }

produces

         1.5708      0.00000
        0.00000       1.5708
        0.00000      0.78540
         1.0000      0.54030
         1.0000       1.5431
        0.00000      0.84147
        0.00000       1.1752
        0.00000       1.5574
        0.00000      0.76159

Definition

## aggregatec, aggregater

    #include <oxstd.oxh>
    main()
    {
        decl x = ones(20,1) ~ range(1,20)';

        println(aggregatec(x, 5));
        println(aggregatec(x, 6));
        println(aggregater(x', 5));
        println(aggregater(x', 6));
    }

produces

           5.0000       15.000
           5.0000       40.000
           5.0000       65.000
           5.0000       90.000

           6.0000       21.000
           6.0000       57.000
           6.0000       93.000
           2.0000       39.000

           5.0000       5.0000       5.0000       5.0000
           15.000       40.000       65.000       90.000

           6.0000       6.0000       6.0000       2.0000
           21.000       57.000       93.000       39.000

Definition

## any

    #include <oxstd.oxh>
    main()
    {
        decl m1 = unit(2), m2 = zeros(2,2);

        if (m1 == 0)       print ("TRUE ");
        else               print ("FALSE ");
        if (any(m1 .== 0)) print ("TRUE ");
        else               print ("FALSE ");
        if (!(m1 == 0))    print ("TRUE ");
        else               print ("FALSE ");
        if (any(m1 .!= 0)) print ("TRUE ");
        else               print ("FALSE ");

        if (m2 == 0)       print ("TRUE ");
        else               print ("FALSE ");
        if (any(m2 .== 0)) print ("TRUE ");
        else               print ("FALSE ");
        if (m2 != 0)       print ("TRUE ");
        else               print ("FALSE ");
        if (any(m2 .!= 0)) print ("TRUE ");
        else               print ("FALSE ");
    }

produces: `FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE`

Definition

## arglist

Running the following `arglist.ox` program:

    #include <oxstd.oxh>
    main()
    {
        decl args = arglist(), s, i, j;

        for (i = 0; i < sizeof(args); ++i)
        {
            sscan(args[i], "%d", &j);
            println("argument ", i, ": ", args[i], " integer value:", j);
        }
    }

as `oxl arglist.ox aa 12` (the arguments before `arglist.ox` are passed to `oxl`, those after to `arglist.ox`), produces:

        argument 0: arglist.ox integer value:0
        argument 1: aa integer value:0
        argument 2: 12 integer value:12

Definition

## array

The `array` cast can be useful when an array indexation must remain an array. For example, a single index on an array of strings returns a string, whereas a multiple index returns an array of strings:

    #include <oxstd.oxh>
    main()
    {
        decl as = {"ax", "bx", "cx"};

        print("single index is string: ", as[0],
            "\nmultiple index is array of strings:", as[0:1],
            "keep single index as array:", array(as[0]) );
    }

which produces:

    single index is string: ax
    multiple index is array of strings:
    [0] = ax
    [1] = bx
    keep single index as array:
    [0] = ax

Definition

## binand, bincomp, binor, binpop, binxor

    #include <oxstd.oxh>
    main()
    {
        print( binand(1,2,4), " ", binor(1,2,4) );
    }

produces: `0 7`

Definition

## cabs, cdiv, cerf, cexp, clog, cmul, csqrt

    #include <oxstd.oxh>
    main()
    {
        decl v = <1, -1, -2>, rv = csqrt(v);
        rv[0][1] = 1;/* change to a more interesting value */

        print(v, rv, cabs(rv), cdiv(rv, rv), cmul(rv, rv),
            cmul(rv, cdiv(ones(1,3), rv)) );
        print(cexp(clog(rv)) );
    }

produces

           1.0000      -1.0000      -2.0000

           1.0000       1.0000      0.00000
          0.00000       1.0000       1.4142

           1.0000       1.4142       1.4142

           1.0000       1.0000       1.0000
          0.00000      0.00000      0.00000

           1.0000      0.00000      -2.0000
          0.00000       2.0000      0.00000

           1.0000       1.0000       1.0000
          0.00000      0.00000      0.00000

           1.0000       1.0000  8.6593e-017
          0.00000       1.0000       1.4142

In the second example the complex functions are used to check if the computed roots of a polynomial indeed correspond to zeros of the polynomial:

    #include <oxstd.oxh>
    main()
    {
        decl v1 = <-1, 1.2274, -0.017197, -0.28369, -0.01028>, roots, cr;

        polyroots(v1, &roots);

        cr = columns(roots);
        print("roots", roots,
              "inverse roots", cdiv(ones(1,cr), roots) );

        decl x1, x2, x3, x4, check;
        x1 = roots;
        x2 = cmul(x1, x1);                  /* roots ^ 2 */
        x3 = cmul(x2, x1);                  /* roots ^ 3 */
        x4 = cmul(x2, x2);                  /* roots ^ 4 */
        check = v1[0][4] * (ones(1,cr) | zeros(1,cr)) +
                v1[0][3] * x1 + v1[0][2] * x2 +
                v1[0][1] * x3 + v1[0][0] * x4;

        print("check (near-zeros could be different "
              "with other Ox versions):", check);
    }

which produces:

    roots
          0.82865      0.82865     -0.39337    -0.036535
          0.16923     -0.16923      0.00000      0.00000
    inverse roots
           1.1585       1.1585      -2.5422      -27.371
         -0.23659      0.23659      0.00000      0.00000
    check (near-zeros could be different with other Ox versions):
          0.00000      0.00000 -1.7000e-016 -8.4441e-018
     -2.2204e-016  2.2204e-016      0.00000      0.00000

The final example considers the complex logarithm:

    #include <oxstd.oxh>
    main()
    {
        decl z, z1, z2, zm;
        z = -1|-0.0; println("%c", {"z","clog(z)"}, z ~ clog(z));
        z = -1|0;    println(z ~ clog(z));
        z =  1|-0.0; println(z ~ clog(z));
        z =  1|0;    println(z ~ clog(z));

        z1=-2|1; z2 = -1|2;
        zm = cmul(z1, z2);
        println("%c", {"clog(z_1z_2)","clog(z_1)+clog(z_2)"},
            clog(zm) ~ clog(z1)+clog(z2));
        println("ph(z_1)=", atan2(z1[1], z1[0]),
                " ph(z_2)=", atan2(z2[1], z2[0]));
    }

which produces:

                z      clog(z)
          -1.0000      0.00000
         -0.00000      -3.1416

          -1.0000      0.00000
          0.00000       3.1416

           1.0000      0.00000
         -0.00000     -0.00000

           1.0000      0.00000
          0.00000      0.00000

     clog(z_1z_2)clog(z_1)+clog(z_2)
           1.6094       1.6094
          -1.5708       4.7124
    ph(z_1)=2.67795 ph(z_2)=2.03444

Definition

## ceil

    #include <oxstd.oxh>
    main()
    {
        print( ceil(<-1.8, -1.2, 1.2, 1.8>) );
        print( floor(<-1.8, -1.2, 1.2, 1.8>) );
        print( round(<-1.8, -1.2, 1.2, 1.8>) );
        print( trunc(<-1.8, -1.2, 1.2, 1.8>) );

        print( int(-1.8), " ", int(-1.2), " ",
               int(1.2), " ", int(1.8) );
    }

produces

          -1.0000      -1.0000       2.0000       2.0000
          -2.0000      -2.0000       1.0000       1.0000
          -2.0000      -1.0000       1.0000       2.0000
          -1.0000      -1.0000       1.0000       1.0000
    -1 -1 1 1

Definition

## choleski

The example also shows how `solvelu` may be used to obtain P^-1.

    #include <oxstd.oxh>
    main()
    {
        decl mp;

        mp = choleski(<4,1;1,3>);
        print(mp, mp*mp');

        print(1/mp ~ solvelu(mp, 0, 0, unit(2)) );
    }

produces

         2.0000      0.00000
       0.500000       1.6583

         4.0000       1.0000
         1.0000       3.0000

         0.50000      0.00000      0.50000      0.00000
        -0.15076      0.60302     -0.15076      0.60302

Definition

## columns

    #include <oxstd.oxh>
    main()
    {
        println(columns(<0,1;1,2;3,4>), " ", columns("taylor"));
        println(   rows(<0,1;1,2;3,4>), " ",    rows("taylor"));
        println( sizerc(<0,1;1,2;3,4>), " ",  sizeof("taylor"));
    }

produces

    2 6
    3 6
    6 6

Definition

## constant

    #include <oxstd.oxh>
    main()
    {
        print( constant(1.5, 2, 2) );
    }

produces

         1.5000       1.5000
         1.5000       1.5000

Definition

## correlation

    #include <oxstd.oxh>
    main()
    {
        decl m1 = rann(100,2), m2;

        m2 = standardize(m1);
        print( correlation(m1), m2'm2/rows(m2) );
    }

produces

           1.0000    -0.039218
        -0.039218       1.0000

           1.0000    -0.039218
        -0.039218       1.0000

Definition

## countc

    #include <oxstd.oxh>
    main()
    {
        print( countc(<0:3;1:4;2:5>, <2,4>) );
        print( countr(<0:3;1:4;2:5>, <2>) );
    }

produces

         3.0000       2.0000       1.0000      0.00000
        0.00000       1.0000       2.0000       2.0000
        0.00000      0.00000      0.00000       1.0000

         3.0000       1.0000
         2.0000       2.0000
         1.0000       3.0000

Definition

## cumsum

    #include <oxstd.oxh>
    main()
    {
        decl mx = ones(5,1);
        print( mx ~ cumsum(mx, <0.5>)
                  ~ cumsum(mx, <1, 0.5>, <1;2>)
                  ~ cumprod(mx * 2)
                  ~ cumprod(mx * 2, 2) );

        print(cumsum(mx, <0.5;0.5;0.5;1;1>)' );
    }

produces

         1.0000     1.0000     1.0000     2.0000     2.0000
         1.0000     1.5000     2.0000     4.0000     2.0000
         1.0000     1.7500     3.5000     8.0000     8.0000
         1.0000     1.8750     5.5000     16.000     32.000
         1.0000     1.9375     8.2500     32.000     512.00

         1.0000     1.5000     1.7500     2.7500     3.7500

Definition

## cumulate

    #include <oxstd.oxh>
    main()
    {
        print( ones(5,1) ~ cumulate(ones(5,1))
               ~ cumulate(ones(5,1), <0.5>)
               ~ cumulate(ones(5,1), <1>, <0.5>)
               ~ cumulate(ones(5,1), {<1>, <0.5>}) );
    }

produces

         1.0000     1.0000     1.0000     1.0000     1.0000
         1.0000     2.0000     1.5000     2.0000     2.0000
         1.0000     3.0000     1.7500     3.5000     3.5000
         1.0000     4.0000     1.8750     5.5000     5.5000
         1.0000     5.0000     1.9375     8.2500     8.2500

Definition

## date

    #include <oxstd.oxh>
    main()
    {
        println("\ntime=", time(), " date=", date());
    }

prints the current time and date.

Definition

## dayofcalendar, dayofeaster, dayofmonth, dayofweek

    #include <oxstd.oxh>
    main()
    {
        println("1-Jan-2000 was weekday ",
            dayofweek(2000, 1, 1), " (7 is Saturday)");
        println("1-Jan-2000 was yearday ",
            dayofcalendar(2000, 1, 1));
        println("2000 had ", dayofcalendar(2001, 1, 1)
            - dayofcalendar(2000, 1, 1), " days");
        println("2001 had ", dayofcalendar(2002, 1, 1)
            - dayofcalendar(2001, 1, 1), " days");

        println("%c", {"Easter Sunday", "Last Wed in May"},
            "%17C", dayofeaster(range(2005, 2010)')
                    ~ dayofmonth(range(2005, 2010)', 5, 2, -1));

        println("today ", "%C", dayofcalendar());
    }

produces

    1-Jan-2000 was weekday 7 (7 is Saturday)
    1-Jan-2000 was yearday 2451545
    2000 had 366 days
    2001 had 365 days

        Easter Sunday  Last Wed in May
           2005-03-27       2005-05-30
           2006-04-16       2006-05-29
           2007-04-08       2007-05-28
           2008-03-23       2008-05-26
           2009-04-12       2009-05-25
           2010-04-04       2010-05-31

    today 2012-11-28

Definition

## decldl

    #include <oxstd.oxh>
    main()
    {
        decl ma = <4,1;1,3>, md, ml, mi;

        print("result = ", decldl(ma, &ml, &md));
        print(" L =", ml, "D =", md);
        print(ml*diag(1 ./ md)*ml');

        mi = solveldl(ml, md, unit(2));
        print(mi*ma);
    }

Note that `diag(1 ./ md)` and `diag(1./md)` are not the same. The program produces (the final matrix could have values of around 1e-16 instead of 0):

    result = 1 L =
           1.0000      0.00000
          0.25000       1.0000
    D =
          0.25000      0.36364

           4.0000       1.0000
           1.0000       3.0000

           1.0000      0.00000
          0.00000       1.0000

Definition

## decldlband

    #include <oxstd.oxh>
    main()
    {
        decl i, j, k, m, mab, ma, ml, md, ct = 5, cb = 2;

        ma = toeplitz(<5,4,3>, ct);   // create test matrix ma
        for (i = 0; i < ct; ++i)
            ma[i][i] += i;

        mab = diagonal(ma, cb);  // create band matrix version
        print("original matrix", ma, "band version", mab);

        if (decldlband(mab, &ml, &md))  // decompose and solve
            print("solved:", solveldlband(ml, md, <1;2;3;4;5>)' );

        // undo banded storage:store L in lower diagonal of ma
        for (i = 0, m = -cb; i < ct; ++i, m++)
            for (j = max(0,m), k = j - m; j < i; ++j, ++k)
                ma[i][j] =  ml[k][i];

        print("band L=", ml, "L:U=", ma);
    }

produces

    original matrix
           5.0000       4.0000       3.0000      0.00000      0.00000
           4.0000       6.0000       4.0000       3.0000      0.00000
           3.0000       4.0000       7.0000       4.0000       3.0000
          0.00000       3.0000       4.0000       8.0000       4.0000
          0.00000      0.00000       3.0000       4.0000       9.0000
    band version
          0.00000      0.00000       3.0000       3.0000       3.0000
          0.00000       4.0000       4.0000       4.0000       4.0000
           5.0000       6.0000       7.0000       8.0000       9.0000
    solved:
         0.012378      0.26172    -0.036251      0.17507      0.48983
    band L=
          0.00000      0.00000      0.60000       1.0714      0.70000
          0.00000      0.80000      0.57143      0.53333      0.67290
           1.0000       1.0000       1.0000       1.0000       1.0000
    L:U=
           5.0000       4.0000       3.0000      0.00000      0.00000
          0.80000       6.0000       4.0000       3.0000      0.00000
          0.60000      0.57143       7.0000       4.0000       3.0000
          0.00000       1.0714      0.53333       8.0000       4.0000
          0.00000      0.00000      0.70000      0.67290       9.0000

Definition

## declu

    #include <oxstd.oxh>
    main()
    {
        decl ma, ml, mu, vp, mx;

        ma = <3,17,10;2,4,-2;6,18,-12>;
        declu(ma, &ml, &mu, &vp);
        print( (ml*mu)[ vp[0][] ][], (unit(rows(ma)))[ vp[0][] ][] );

        mx = solvelu(ml, mu, vp, ma);
        print(mx);
    }

produces (note that the last matrix is the identity matrix: whether it has zeros, or nearly zeros, could dependent on which Ox version was used):

           3.0000       17.000       10.000
           2.0000       4.0000      -2.0000
           6.0000       18.000      -12.000

          0.00000       1.0000      0.00000
          0.00000      0.00000       1.0000
           1.0000      0.00000      0.00000

           1.0000      0.00000      0.00000
          0.00000       1.0000      0.00000
          0.00000      0.00000       1.0000

Definition

## decqr

    #include <oxstd.oxh>
    main()
    {
        decl ma, mht, mr, mp, vp;
        ma = <2,1,4;5,1,7;8,1,9;11,1,12>;

        decqr(ma, &mht, &mr, &mp);
        vp = mp[0][];
        print("A=", ma, "A\'A", ma'ma,
            "R\'R (ignoring pivoting)", mr'mr,
            "R\'R (after undoing pivoting)", (mr'mr)[vp][vp]);
        println("Note that mp[0][] contains P':", vp);
        println("The pivots on A (where AP=QR) are:",
            sortcindex(vp') ');
    }

    A=
           2.0000       1.0000       4.0000
           5.0000       1.0000       7.0000
           8.0000       1.0000       9.0000
           11.000       1.0000       12.000
    A'A
           214.00       26.000       247.00
           26.000       4.0000       32.000
           247.00       32.000       290.00
    R'R (ignoring pivoting)
           290.00       247.00       32.000
           247.00       214.00       26.000
           32.000       26.000       4.0000
    R'R (after undoing pivoting)
           214.00       26.000       247.00
           26.000       4.0000       32.000
           247.00       32.000       290.00
    Note that mp[0][] contains P':
           1.0000       2.0000      0.00000
    The pivots on A (where AP=QR) are:
           2.0000      0.00000       1.0000

Definition

## decqrmul

The example shows how to obtain Q', reconstructs the original matrix, and implements regression using the QR decomposition (note that `olsc` is also QR based). Because the input matrix is singular, the solution is not unique. Different versions of Ox may find different solutions depending on differences in accumulation of rounding errors.

    #include <oxstd.oxh>
    main()
    {
        decl iret, ma, maa, mht, mr, mp, mq, mb, vy;

        ma = <1,2,3;1,5,6;1,8,9;1,11,12>;

        iret = decqr(ma, &mht, &mr, &mp);
        if (iret < 0)
            println("Input matrix is singular");
        print("H\'=", mht', "R1=", mr, "pivots", mp);

        mq = decqrmul(mht);
        maa = mq' * (mr | <0,0,0>);
        print("Q\'=", mq', "ma (pivoted)=", maa,
            "ma=", maa[][mp[0][]]);

        vy = <2;1;2;4>;
        olsc(vy, ma, &mb);
        print("regression coefficients (transposed)", mb');

        decl rank = sumr(fabs(diagonal(mr)) .> 1e-14);
        println("rank=", rank);
        mr[rank:][] = 0;

        mb = solvelu(0, mr, 0, decqrmul(mht, vy)[:2][]);
        print("from QR", mb', "in correct order", mb[mp[0][]][]');
    }

    Input matrix is singular
    H'=
           1.0000      0.00000      0.00000
          0.30877       1.0000      0.00000
          0.46316     -0.32710       1.0000
          0.61755     -0.78925      0.46524
    R1=
          -16.432      -1.8257      -14.606
          0.00000     -0.81650      0.81650
          0.00000      0.00000  2.7577e-015
    pivots
           1.0000       2.0000      0.00000
           2.0000       2.0000       2.0000
    Q'=
         -0.18257     -0.81650      0.54384     0.065078
         -0.36515     -0.40825     -0.77363      0.31859
         -0.54772  5.5511e-017    -0.084268     -0.83241
         -0.73030      0.40825      0.31406      0.44874
    ma (pivoted)=
           3.0000       1.0000       2.0000
           6.0000       1.0000       5.0000
           9.0000       1.0000       8.0000
           12.000       1.0000       11.000
    ma=
           1.0000       2.0000       3.0000
           1.0000       5.0000       6.0000
           1.0000       8.0000       9.0000
           1.0000       11.000       12.000
    regression coefficients (transposed)
          0.50000      0.00000      0.23333
    rank=
           2.0000
    from QR
          0.23333      0.50000      0.00000
    in correct order
          0.50000      0.00000      0.23333

Definition

## decqrupdate

The example shows first how the QR decomposition of an upper Hessenberg matrix (a matrix with zeros below the subdiagonal) can be computed, and then updates after appending a column to a lower triangular matrix.

    #include <oxstd.oxh>
    main()
    {
        decl ma, maa, mht, mr, mp, mq, mb, vy;

        ma = <1,2,3,4,5;1,5,6,7,8;0,1,8,9,10;0,0,1,11,12>;

        println("Upper Hessenberg matrix A", ma);
        mr = ma;
        mq = unit(sizer(ma));
        decqrupdate(&mq, &mr, 1);
        println("triangular R:", mr);
        println("original:", mq*mr);

        mr[1:][0] = 1;
        mq = unit(sizer(ma));
        println("Column 0 changed:", mr);
        decqrupdate(&mq, &mr, 0, sizer(mr));
        println("Made triangular:", mr);
        println("original:", mq*mr);
    }

    Upper Hessenberg matrix A
           1.0000       2.0000       3.0000       4.0000       5.0000
           1.0000       5.0000       6.0000       7.0000       8.0000
          0.00000       1.0000       8.0000       9.0000       10.000
          0.00000      0.00000       1.0000       11.000       12.000
    triangular R:
           1.4142       4.9497       6.3640       7.7782       9.1924
          0.00000       2.3452       5.3300       5.7564       6.1828
          0.00000      0.00000       6.4102       8.8637       9.9131
          0.00000      0.00000      0.00000       9.7365       10.583
    original:
           1.0000       2.0000       3.0000       4.0000       5.0000
           1.0000       5.0000       6.0000       7.0000       8.0000
          0.00000       1.0000       8.0000       9.0000       10.000
          0.00000      0.00000       1.0000       11.000       12.000
    Column 0 changed:
           1.4142       4.9497       6.3640       7.7782       9.1924
           1.0000       2.3452       5.3300       5.7564       6.1828
           1.0000      0.00000       6.4102       8.8637       9.9131
           1.0000      0.00000      0.00000       9.7365       10.583
    Made triangular:
           2.2361       4.1793       9.2753       15.812       17.745
          0.00000       3.5403       1.4789      -3.9779      -4.0002
          0.00000      0.00000       4.6671     -0.80946     -0.78739
          0.00000      0.00000      0.00000     -0.70954      -1.2216
    original:
           1.4142       4.9497       6.3640       7.7782       9.1924
           1.0000       2.3452       5.3300       5.7564       6.1828
           1.0000  3.8760e-016       6.4102       8.8637       9.9131
           1.0000  3.8760e-016  8.6736e-018       9.7365       10.583

Definition

## decschur, decschurgen

    #include <oxstd.oxh>

    main()
    {
        decl a, b, ev, t, s, v, i, alpha, beta, vl, vr;
        a = rann(4,4);  b = rann(4,4);
        print("a", a);

        i = decschur(a, &ev, &s);
        print("eigenvalues", ev);
        print("s", s);

        i = decschur(a, &ev, &s, &v);
        print("v*s*v'", v*s*v');

        i = decschur(a, &ev, &s, &v, 0, 1);
        print("cabs(eigenvalues) between 0 and 1 first, S=", s);

        i = decschurgen(a, b, &alpha, &beta, &s, &t, &vl, &vr);
        print("b", b);
        println("decschurgen i=", i);
        print("alpha", alpha);
        print("beta", beta);
        // print("s", s, "vl*s*vr'", vl*s*vr');
        print("t", t, "vl*t*vr'", vl*t*vr');

        decschurgen(a, unit(rows(a)), &alpha, &beta, &s, &t, &vl, &vr,0,1);
        println("selecting gen. eigenvalues between 0 and 1 first");
        print("generalized eigenvalues", alpha ./ beta);
    }

produces

    a
          0.22489       1.7400     -0.20426     -0.91760
         -0.67417     -0.34353      0.22335     -0.14139
         -0.18338      0.68035     0.090558     -0.83328
          0.81350       1.1174      0.31499     -0.50031
    eigenvalues
         -0.25959     -0.25959   -0.0046060   -0.0046060
           1.3775      -1.3775      0.32694     -0.32694
    s
         -0.25959      -2.1654      -1.2665     -0.37296
          0.87631     -0.25959     -0.51481      0.18777
          0.00000      0.00000   -0.0046060      0.16910
          0.00000      0.00000     -0.63214   -0.0046060
    v*s*v'
          0.22489       1.7400     -0.20426     -0.91760
         -0.67417     -0.34353      0.22335     -0.14139
         -0.18338      0.68035     0.090558     -0.83328
          0.81350       1.1174      0.31499     -0.50031
    cabs(eigenvalues) between 0 and 1 first, S=
       -0.0046060     -0.20780      0.49340      0.64443
          0.51441   -0.0046060      0.66321      0.24688
          0.00000      0.00000     -0.25959      0.78487
          0.00000      0.00000      -2.4177     -0.25959
    b
          -1.6268      0.61943      -1.4574      -1.8035
           2.0016      0.57912     -0.70797      0.59336
         -0.58939       1.4674    -0.020230      0.73706
           1.4795     -0.26881       1.2282       1.5784
    decschurgen i=0
    alpha
           1.9293      0.70758     -0.68938     -0.22323
    beta
         0.089639       3.2454       2.0066       1.7759
    t
         0.089639      0.68167     -0.46602     -0.52514
          0.00000       3.2454       1.6897     -0.89339
          0.00000      0.00000       2.0066     -0.75847
          0.00000      0.00000      0.00000       1.7759
    vl*t*vr'
          -1.6268      0.61943      -1.4574      -1.8035
           2.0016      0.57912     -0.70797      0.59336
         -0.58939       1.4674    -0.020230      0.73706
           1.4795     -0.26881       1.2282       1.5784
    selecting gen. eigenvalues between 0 and 1 first
    generalized eigenvalues
       -0.0046060   -0.0046060     -0.25959     -0.25959
          0.32694     -0.32694       1.3775      -1.3775

Definition

## decsvd

    #include <oxstd.oxh>
    main()
    {
        decl x=<1,2;3,4;5,6>, mu, mv, mw;
        print("singular values: ", decsvd(x));
        print("result = ", decsvd(x, &mu, &mw, &mv));
        print(" A =", mu * diag(mw) * mv');
        decsvd(x', &mu, &mw, &mv);
        print(" A =", mu * diag(mw) * mv');
    }

produces

    singular values:
           9.5255      0.51430
    result = 0 A =
           1.0000       2.0000
           3.0000       4.0000
           5.0000       6.0000
     A =
           1.0000       3.0000       5.0000
           2.0000       4.0000       6.0000

Definition

## deletec, deleter, deleteifc, deleteifr

    #include <oxstd.h>
    main()
    {
        decl m = <.,1,2,3;4:7;8,9,.,11>;
        print(m, "Rows with .NaN deleted", deleter(m));
        print("%r", {"deleter","deleteifr"},
            deleter(m, <1,.NaN>) | deleteifr(m, m .< 6 .|| m .>= 14));
    }

produces:

             .NaN       1.0000       2.0000       3.0000
           4.0000       5.0000       6.0000       7.0000
           8.0000       9.0000         .NaN       11.000
    Rows with .NaN deleted
           4.0000       5.0000       6.0000       7.0000
    deleter             4.0000       5.0000       6.0000       7.0000
    deleteifr           8.0000       9.0000         .NaN       11.000

Definition

## determinant

    #include <oxstd.oxh>
    main()
    {
        print( determinant(<2,1;1,4>) );
    }

produces: `7`

Definition

## dfft

    #include <oxstd.oxh>
    main()
    {
        print( "dfft", dfft(<1,0,1>), "fft1d", fft1d(<1,0,1>),
            "inverse dfft(dfft))", dfft(dfft(<1,0,1>), 2) );
    }

produces

    dfft
           2.0000      0.50000      0.50000
          0.00000      0.86603     -0.86603
    fft1d
           2.0000      0.50000      0.50000
          0.00000      0.86603     -0.86603
    inverse dfft(dfft))
           1.0000 -1.4599e-016       1.0000

Definition

## diag

    #include <oxstd.oxh>
    main()
    {
        print( diag(<1,1>), diag(<1;1>) );
    }

produces

         1.0000      0.00000
        0.00000       1.0000

         1.0000      0.00000
        0.00000       1.0000

Definition

## diagcat

    #include <oxstd.oxh>
    main()
    {
        print( diagcat(<2,2>, unit(2)) );
    }

produces

           2.0000       2.0000      0.00000      0.00000
          0.00000      0.00000       1.0000      0.00000
          0.00000      0.00000      0.00000       1.0000

Definition

## diagonal

    #include <oxstd.oxh>
    main()
    {
        decl x = <1:5;11:15;21:25>;
        print( "%6.0f", diagonal(x) );
        print( "%6.0f", diagonal(x, 1, -1) );
    }

produces

         1    12    23

         0     2    13    24
         1    12    23     0
        11    22     0     0

Definition

## diagonalize

    #include <oxstd.oxh>
    main()
    {
        print( diagonalize( constant(2, 3, 4) ) );
    }

produces

         2.0000      0.00000      0.00000      0.00000
        0.00000       2.0000      0.00000      0.00000
        0.00000      0.00000       2.0000      0.00000

Definition

## diff0

    #include <oxstd.oxh>
    main()
    {
        print( diff0(<1:5>',2) );
    }

produces

        0.00000
        0.00000
         2.0000
         2.0000
         2.0000

Definition

## discretize

In this example, the three intervals are [-3,-1), [-1,1) and [1,3). So the last observation of `x` will be ignored. The raw discretization simply counts the numbers in each interval, giving the first line of output. The weighted version looks at the distance to the points -2,0,2 (also printed as the last output line): -3 is to the left of the minimum, so fully assigned to the first interval. Apart from -1, all observations are exactly on a point, so fully assigned; -1 falls halfway between -2 and 0, so half is assigned to the first interval, and half to the second (if the value would have been -1.5, 0.75 would go to the first interval, 0.25 to the second.

    #include <oxstd.oxh>
    main()
    {
        decl a = -2, b = 2, m = 3, t;
        decl x = <-3,-2,-1,0,2,3>;

        t = a + (b - a) * range(0, m - 1) / (m - 1);
        print( discretize(x, a, b, m, 0)
            | discretize(x, a, b, m, 1) | t);
    }

produces

           2.0000       2.0000       1.0000
           2.5000       1.5000       1.0000
          -2.0000      0.00000       2.0000

Definition

## dropc, dropr

    #include <oxstd.oxh>
    main()
    {
        decl x = <1,2,3;4,5,6>;
        print( dropc(x, 1), dropr(x, 1) );
        print( insertc(x, 0, 1) );

        decl a = {{"A","B"},{1,2},{<1>,<2>}};
        println("dropr(a, <1,2>)", dropr(a, <1,2>));
        println("a[0]=", a[0], "dropr(a[0], <1>)", dropr(a[0], <1>));
        println("insertr(a[0], 0, 2)", insertr(a[0], 0, 2));
    }

produces

           1.0000       3.0000
           4.0000       6.0000

           1.0000       2.0000       3.0000

          0.00000       1.0000       2.0000       3.0000
          0.00000       4.0000       5.0000       6.0000
    dropr(a, <1,2>)
    [0][0] = A
    [0][1] = B
    a[0]=
    [0] = A
    [1] = B
    dropr(a[0], <1>)
    [0] = A
    insertr(a[0], 0, 2)
    [0] = .Null
    [1] = .Null
    [2] = A
    [3] = B

Definition

## eigen, eigensym

    #include <oxstd.oxh>
    main()
    {
        decl meval, mevec;

        print("result=", eigensym(<2,1;1,3>, &meval, &mevec));
        print(" eigenvalues:", meval, "eigenvectors:", mevec);

        print("result=", eigen(<2,1;-3,1>, &meval));
        print(" eigenvalues:", "%r",
            {"real", "imaginary"}, meval);
    }

produces

    result=0 eigenvalues:
         3.6180       1.3820
    eigenvectors:
       -0.52573      0.85065
       -0.85065     -0.52573
    result=0 eigenvalues:
    real                1.5000       1.5000
    imaginary           1.6583      -1.6583

Definition

## eigensymgen

    #include <oxstd.oxh>
    main()
    {
        decl meval, mevec;

        print("result = ",
            eigensymgen(<2,1;1,3>,<1,0;0,1>, &meval, &mevec));
        print(" generalized eigenvectors:", mevec);
    }

produces

    result = 0 generalized eigenvectors:
         -0.52573      0.85065
         -0.85065     -0.52573

Definition

## eprint

    #include <oxstd.oxh>
    main()
    {
        eprint( "\nerror message\n" );
    }

prints `error message` to the console (even when the output is redirected to a file).

Definition

## exclusion

    #include <oxstd.oxh>
    main()
    {
        decl x = <-1,1,.,-2,-2,.,4>, y = <3,3,.,-2,1>;
        format("%5.1g");
        print("exclusion", exclusion(x, y) );
        print("intersection", intersection(x, y) );
        print("union", union(x, y) );
        print("union using unique", unique(x ~ y) );
    }

produces

    exclusion
       -1    4
    intersection
       -2    1
    union
       -2   -1    1    3    4
    union using unique
       -2   -1    1    3    4

Definition

## exp

    #include <oxstd.oxh>
    main()
    {
        print( exp(<0,1>) );
    }

produces

         1.0000       2.7183

Definition

## fabs

    #include <oxstd.oxh>
    main()
    {
        print( fabs(<-1.1,1.1>) );
    }

produces

         1.1000       1.1000

Definition

## fft, fft1d

    #include <oxstd.oxh>
    main()
    {
        print( fft(<1,0,1>), fft(fft(<1,0,1>), 2) );
    }

produces

           2.0000      0.00000       2.0000      0.00000
          0.00000     -0.00000      0.00000      0.00000

           1.0000      0.00000       1.0000     -0.00000

Definition

## find

    #include <oxstd.oxh>
    main()
    {
        decl x1 = <4;0;3> ~ <0;4;1>, xm = <4;3;2;1;0> ~ <.;.;2;3;.>, x2;

        x2 = xm;
        println("before, x1=", x1, "x2=", x2);
        println("vecindex - sorted indices (column):",
            vecindex(x2[][0], x1[][0]));

        x2 = xm;
        println("find returns the locations (column):",
            find(x2[][0], x1[][0]));

        // no find failures:
        decl sel = find(x2[][0], x1[][0]);
        x2[sel][1] = x1[][1];
        println("can be used to insert the missing values in x2:", x2);

        // not all found:
        x2 = xm;
        sel = find(x2[][0], x1[][0] | 5);
        decl selfound = vecindex(sel .>= 0);
        x2[sel[selfound]][1] = x1[selfound][1];
        println("redo, but now with a value that is not found:", x2);
    }

produces:

    before, x1=
           4.0000      0.00000
          0.00000       4.0000
           3.0000       1.0000
    x2=
           4.0000         .NaN
           3.0000         .NaN
           2.0000       2.0000
           1.0000       3.0000
          0.00000         .NaN
    vecindex - sorted indices (column):
          0.00000
           1.0000
           4.0000
    find returns the locations (column):
           0.0000
           4.0000
           1.0000
    can be used to insert the missing values in x2:
           4.0000      0.00000
           3.0000       1.0000
           2.0000       2.0000
           1.0000       3.0000
          0.00000       4.0000
    redo, but now with a value that is not found:
           4.0000      0.00000
           3.0000       1.0000
           2.0000       2.0000
           1.0000       3.0000
          0.00000       4.0000

Definition

## findsample

    #include <oxstd.oxh>
    main()
    {
        decl x = range(0,5)' ~ range(10,15)', t1, t2;
        x[2][1] = x[5][1] = .NaN;  x[4][] = .NaN;

        println(x);
        findsample(x, <>, <>, 0, -1, SAM_ALLVALID, &t1, &t2);
        println("SAM_ALLVALID: t1=", t1, " t2=", t2);
        findsample(x, <>, <>, 0, -1, SAM_ENDSVALID, &t1, &t2);
        println("SAM_ENDSVALID:t1=", t1, " t2=", t2);
        findsample(x, <>, <>, 0, -1, SAM_ANYVALID, &t1, &t2);
        println("SAM_ANYVALID: t1=", t1, " t2=", t2);

        findsample(x, <0,0>, <0,1>, 0, -1, SAM_ALLVALID, &t1, &t2);
        println("SAM_ALLVALID: t1=", t1, " t2=", t2, " column 0,lags 0-1");
    }

%produces

          0.00000       10.000
           1.0000       11.000
           2.0000         .NaN
           3.0000       13.000
             .NaN         .NaN
           5.0000         .NaN
    SAM_ALLVALID:  t1=0 t2=1
    SAM_ENDSVALID: t1=0 t2=3
    SAM_ANYVALID:  t1=0 t2=5
    SAM_ALLVALID:  t1=1 t2=3 column 0, lags 0-1

Definition

## fmod

    #include <oxstd.oxh>
    main()
    {
        print( fmod(3,2), " ", fmod(-3,2), " ",
               fmod(3,-2), " ", fmod(-3,-2) );
    }

produces: `1 -1 1 -1`

Definition

## fprint, fprintln

    #include <oxstd.oxh>
    main()
    {
        decl file = fopen("test.tmp", "w");

        if (isfile(file))
        {
            fprintln(file, "some text" );

            fclose(file);
        }
    }

produces a file test.tmp with the specified text.

Definition

## fread

A number of input/output examples is in the `samples/inout` directory. Below is `samples/inout/inout7.ox`. The programs `inout10` and `inout11` show how data can be read and written in blocks when the file is not a `.fmt` file. This example writes a matrix as a `.fmt` file using `savemat`. Then the matrix is written using `fread`, in such a way that the same format is used. Note that under Windows and MS-DOS these files are identical, but that on some platforms (such as the Sun) the files differ: `iotest7.ox` is little endian, but reading here assumes the platform ordering (which is big endian on a Sun).

    #include <oxstd.oxh>
    main()
    {
        decl file, x;
        decl s, r, c, rc8;

        x = rann(2,3);
        x[0][] = double("tinker");
        savemat("iotest7.fmt", x);
                    // open mode: read, binary, little-endian
        file = fopen("iotest7.fmt", "rbe");
        fread(file, &s, 'c', 4);

        if (s == "\xDD\xEE\x86")
            println("signature OK");
        else
        {   println("signature NOT OK!");
            exit(1);
        }

        fread(file, &r, 'i');
        fread(file, &c, 'i');
        println("rows=", r, " columns=", c);

        fread(file, &rc8, 'i');
        fread(file, &x, 'f', r, c);
        println("-1 indicates eof: ", fread(file, &s, 'c', 1));
        if (feof(file))
            println("Was indeed end of file.");

        fclose(file);

        println(string(x[0][0]), x[1:][]);
    }

produces:

    signature OK
    rows=2 columns=3
    -1 indicates eof: -1
    Was indeed end of file.
    tinker
         -0.91760     -0.67417     -0.34353

Definition

## fscan

The example (`samples/inout/iotest2.ox`) writes a file, and reads it twice. The first time, the string read is `tinker123`, but then reading gets stuck, because the word tailor can not be read is an integer, double or matrix. Failure to read the matrix dimension generates an error message. The second time, the file is read properly.

    #include <oxstd.oxh>
    main()
    {
        decl file;

        file = fopen("iotest2.txt", "w");
        fprint(file, "tinker123\ntailor456.78\n 2 2 1 0 0 1\n");
        fclose(file);

        decl c = -2, s, i = 0, d = 0, m = 0;

        file = fopen("iotest2.txt");
        println("Next statement will print message: "
              "\"load matrix: no matrix elements\"");

        c = fscan(file, "%s", &s,       // stops after &s
            "%d", &i, "%f", &d, "%m", &m);
        fclose(file);

        print("\nitems read=", c, " s=", s, " int=", i,
            " dbl=", d, " mat=", m);

        file = fopen("iotest2.txt");
        c = fscan(file, "tinker%d", &i, " tailor%f", &d, "%m", &m);
        fclose(file);

        print("\nitems read=", c, " int=", i, " dbl=", d,
            " mat=", m);

        // token example:
        decl str = "GMM(\"a\", 1.5, -3);";
        decl func, arg0, arg1, arg2, arg3;

        println("\ntoken string: ", str);
        sscan(str, "%t", &func, "( %t", &arg0, ", %f", &arg1,
            ", %d", &arg2);
        println("scanned using \"%t\": ", func, " ", arg0,
            " ", arg1, " ", arg2);

        sscan(str, "%T", &func, "(%T", &arg0, ", %T", &arg1,
            ", %T", &arg2, "%T", &arg3);
        println("scanned using \"%T\":", func, arg0, arg1, arg2, arg3);
    }

produces

    Next statement will print message: "load matrix: no matrix elements"
    load matrix: no matrix elements

    items read=1 s=tinker123 int=0 dbl=0 mat=0
    items read=3 int=123 dbl=456.78 mat=
           1.0000      0.00000
          0.00000       1.0000

    token string: GMM("a", 1.5, -3);
    scanned using "%t": GMM a 1.5 -3
    scanned using "%T":
    [0] = GMM
    [1] = GMM
    [2] = 0

    [0] = a
    [1] = "a"
    [2] = 1

    [0] = 1.5
    [1] = 1.5
    [2] = 1

    [0] = -
    [1] = -
    [2] = 2

    [0] = 3
    [1] = 3
    [2] = 1

Definition

## fseek

This example (`samples/inout/iotest9.ox`) reads and writes to a matrix opened with the f format. In that case, the number of columns applies to the whole file, and seeking is by row. Once the file file holds data, each subsequent write must match the number of columns already in the file.

    #include <oxstd.oxh>
    main()
    {
        decl file, x, i;

        file = fopen("iotest9.fmt", "wbf");  // write

        fwrite(file, ones(1, 4));
        fwrite(file, 1 + ones(1, 4));
        fwrite(file, zeros(27, 4));

        fclose(file);

        file = fopen("iotest9.fmt", "abf");  // append
        println("file is ", rows(file), " by ", columns(file));

        fwrite(file, 2 + ones(1, 4));
        fclose(file);

        file = fopen("iotest9.fmt", "rbf");    // read
        println("file is ", rows(file), " by ", columns(file));

        fseek(file, 'f', 1);             // second row
        fread(file, &x, 'f', 1);            // read it
        print("row of twos:", x);

        fseek(file, 'f', rows(file)-1);  // second row
        fread(file, &x, 'f', 1);   // read it
        print("row of threes:", x);
    }

produces:

    file is 29 by 4
    file is 30 by 4
    row of twos:
           2.0000       2.0000       2.0000       2.0000
    row of threes:
           3.0000       3.0000       3.0000       3.0000

Definition

## fwrite

A number of input/output examples is in the `samples/inout` directory. Below is `samples/inout/inout6.ox`, which saves a matrix as a `.ftm` file using `savemat`. Then the matrix is written using `fwrite`, in such a way that the same format is used. See under `fread` for a read example. Note that under Windows and MS-DOS these files are identical, but that on some platforms (such as the Sun) the files differ: `iotest6a.fmt` is little endian, but `iotest6b.fmt` big endian. So on a Sun, using `loadmat` on `iotest6b.fmt` fails to read the matrix correctly. The example also shows how a short string can be stores in matrix, and retrieved from it. newpage

    #include <oxstd.oxh>
    main()
    {
        decl file, x = rann(2,3);
        x[0][] = double("tinker");
        savemat("iotest6a.fmt", x);
                                     // force little-endian mode
        file = fopen("iotest6b.fmt", "wbe");
    //  two ways if writing signature, first:
    //  decl s = new string[4];      // need four bytes
    //  s[0:2] = "\xDD\xEE\x86";     // signature is DDEE8600
    //  fwrite(file, s);
    //
    //  and second way:
        fprint(file, "%c", 0xdd, "%c", 0xee, "%c", 0x86, "%c", 0x00);

        fwrite(file, rows(x));
        fwrite(file, columns(x));
        fwrite(file, rows(x) * columns(x) * 8);
        fwrite(file, x);
        fclose(file);

        decl y = loadmat("iotest6b.fmt");

        println(string(x[0][0]), string(y[0][0]), x[1][1]-y[1][1]);
    }

produces: `tinkertinker0`

Definition

## gammafunc

    #include <oxstd.oxh>
    #include <oxprob.oxh>       // required for probgamma
    main()
    {
        print(probgamma(5.99, 1, 0.5), " ",
              gammafunc(5.99 * 0.5, 1), "\n");
        print(probgamma(5.99, 0.5, 1), " ",
              gammafunc(5.99, 0.5) );
    }

produces

    0.949963 0.949963
    0.999462 0.999462

Definition

## getfiles, getfolders

    #include <oxstd.oxh>
    main()
    {
        println("Current folder = ", getcwd());
        chdir("D:\\OxMetrics6\\ox\\include");
        //default:chdir("C:\\Program Files\\OxMetrics6\\ox\\include");
        println("Current folder = ", getcwd());
        println("Files in folder: ", getfiles("*.ox"));
    }

produces

    Current folder = D:\Waste
    Current folder = D:\OxMetrics6\ox\include
    Files in folder:
    [0] = g2ox.ox
    [1] = oxgauss.ox
    [2] = ox_init.ox

Definition

## idiv, imod

    #include <oxstd.oxh>
    main()
    {
        print( idiv(3,2), " ", idiv(-4,2), " ",
               idiv(3,-2), " ", idiv(-4,-2), " ");
        print( imod(3,2), " ", imod(-3,2), " ",
               imod(3,-2), " ", imod(-3,-2) );
    }

produces: `1 -2 -1 2 1 -1 1 -1`

Definition

## invert

    #include <oxstd.oxh>
    main()
    {
        decl mp = <4,1;1,3>;
        print(invert(mp)*mp ~ invertsym(mp)*mp);
    }

produces (note that the both matrices are the identity matrix: whether it has zeros, or nearly zeros, could dependent on which Ox version was used):

           1.0000      0.00000       1.0000      0.00000
          0.00000       1.0000      0.00000       1.0000

Definition

## invertgen

    #include <oxstd.oxh>
    main()
    {
        decl x, xx;
        x = rann(20,2);
        x = x ~ x[][0];
        xx = x'x;

        println("\nAA^A=A:");
        print(xx * invertgen(xx, 30) * xx - xx);
        print(xx * invertgen(x, 40) * xx - xx);

        println("These generalized inverses are different:");
        print("Choleski failed, so use SVD", invertgen(xx, 3));
        print("Using QR", invertgen(x, 4));
    }

produces (note that the exact value of the zeros can depend on the computer platform and the version of Ox):

    AA^A=A:
    Warning: invertgen: invertsym failed, proceeding with
    generalized p.s.d. inverse
    invertgen.ox (10): main

     -1.4211e-014 -4.4409e-016 -1.4211e-014
     -2.2204e-016  1.4211e-014 -2.2204e-016
     -1.4211e-014 -4.4409e-016 -1.4211e-014
    Warning: invertgen: matrix has reduced rank
    invertgen.ox (11): main

     -7.1054e-015 -6.6613e-016 -7.1054e-015
     -8.8818e-016  3.5527e-015 -8.8818e-016
     -7.1054e-015 -6.6613e-016 -7.1054e-015
    These generalized inverses are different:
    Choleski failed, so use SVD
         0.014260   -0.0023020     0.014260
       -0.0023020     0.049276   -0.0023020
         0.014260   -0.0023020     0.014260
    Using QR
         0.057041   -0.0046039      0.00000
       -0.0046039     0.049276      0.00000
          0.00000      0.00000      0.00000

Definition

## isdotfeq, isfeq

    #include <oxstd.oxh>
    main()
    {
        decl m1 = <1+1e-17,1-1e-17;1+1e-17,1-1e-17 >;
        decl m2 = <1+1e-17,1-1e-10;1+1e-17,1-1e-17 >;

        print( "m1 is ", isfeq(m1,1) ? "" : "*** not *** ",
            "fuzzy equal to 1\n");
        print( "m2 is ", isfeq(m2,1) ? "" : "*** not *** ",
            "fuzzy equal to 1\n");
        print(isdotfeq(m1,1));
    }

produces

    m1 is fuzzy equal to 1
    m2 is *** not *** fuzzy equal to 1
           1.0000       1.0000
           1.0000       1.0000

Definition

## isdotmissing, isdotnan, ismissing, isnan

    #include <oxstd.oxh>
    main()
    {
        decl m1 = <1,2,3;4,5,6;7,8,9 >;
        decl m2 = <1,.,3;4,5,.;7,8,9 >;

        print( "m1 has ", isnan(m1) ? "" : "*** no *** ",
            "missing values\n");
        print( "m2 has ", isnan(m2) ? "" : "*** no *** ",
            "missing values\n");

        print(isdotnan(m2));
        print("m2", m2, "rows with NaN deleted",
            deleter(m2), deleteifr(m2, isdotnan(m2)));
    }

produces

    m1 has *** no *** missing values
    m2 has missing values

          0.00000       1.0000      0.00000
          0.00000      0.00000       1.0000
          0.00000      0.00000      0.00000
    m2
           1.0000         .NaN       3.0000
           4.0000       5.0000         .NaN
           7.0000       8.0000       9.0000
    rows with NaN deleted
           7.0000       8.0000       9.0000

           7.0000       8.0000       9.0000

Definition

## lag0

    #include <oxstd.oxh>
    #include <oxfloat.oxh>            // reguired for M_NAN
    main()
    {
        print( lag0(<1:5>',2) ~ lag0(<1:5>',2, M_NAN) );
    }

produces

          0.00000         .NaN
          0.00000         .NaN
           1.0000       1.0000
           2.0000       2.0000
           3.0000       3.0000

Definition

## limits

    #include <oxstd.oxh>
    main()
    {
        decl m = rann(7,2);
        print( range(0, rows(m)-1)' ~ m,
            "%r", {"column min","column max",
            "row index of min","row index of max"}, limits(m) );
    }

produces

          0.00000      0.22489       1.7400
           1.0000     -0.20426     -0.91760
           2.0000     -0.67417     -0.34353
           3.0000      0.22335     -0.14139
           4.0000     -0.18338      0.68035
           5.0000     0.090558     -0.83328
           6.0000      0.81350       1.1174

    column min           -0.67417     -0.91760
    column max            0.81350       1.7400
    row index of min       2.0000       1.0000
    row index of max       6.0000      0.00000

Definition

## loadmat

    #include <oxstd.oxh>
    main()
    {
        decl m = unit(2), as;

        savemat("t.mat", m);
        print(m, loadmat("t.mat"));

        savemat("t.in7", m, {"AA", "BB"});
        loadmat("t.in7", &as);
        println("names", as);
    }

produces

           1.0000      0.00000
          0.00000       1.0000

           1.0000      0.00000
          0.00000       1.0000
    names
    [0] = AA
    [1] = BB

and a file called t.mat:

    2 2
       1.0000000000000000e+000   0.0000000000000000e+000
       0.0000000000000000e+000   1.0000000000000000e+000

Definition

## loadsheet

    #include <oxstd.oxh>

    main()
    {
        decl convertdates = 1;
        decl as1 = loadsheet("nodata.xlsx", 0, convertdates);
        decl as2 = loadsheet("nodata.xlsx", 1, convertdates);

        println("xlsx sheet 1:\n", "%v", as1);
        if (convertdates)
        {
            println("date/time elements in sheet 1:");
            println("[1][3]=", "%C", as1[1][3]);
            println("[2][0]=", "%C", as1[2][0]);
            println("[3][3]=", "%C", as1[3][3]);
        }
        println("ismissing on cell 0,2: ", ismissing(as1[0][2]));

        println("xlsx sheet 2:\n", "%v", as2);

        decl as3 = loadsheet("nodata.csv", 0, convertdates);
        println("csv sheet:\n", "%v", as3);
    }

produces (after inserting some additional line breaks):

    xlsx sheet 1:
    {   {"some text,","and text",.Null,.Null},
        {.Null,"12bb",.Null,0.5},
        {2455628.25,.Null,.NaN,.Null},
        {.Null,15.16,.Null,2455628}
    }
    date/time elements in sheet 1:
    [1][3]=12:00:00
    [2][0]=2011-03-07T06:00:00
    [3][3]=2011-03-07
    ismissing on cell 0,2: 1
    xlsx sheet 2:
    {   {"some more",.Null},
        {.Null,99.900000000000006},
        {"in second sheet",.Null}
    }
    csv sheet:
    {   {"some text,","and text",.Null,.Null},
        {.Null,"12bb",.Null,0.5},
        {2455628.25,.Null,.NaN,.Null},
        {.Null,15.16,.Null,2455628}
    }

Definition

## log, log10

    #include <oxstd.oxh>
    main()
    {
        print( log(<1,10>) );
        print( log10(<1,10>) );

        // the following shows how to prevent log(0)
        // in the computation of y*log(y) using the
        // dot-conditional operator:
        decl y = range(0,4);
        print(y .* log(y .> 0 .? y .: 1));
    }

produces

          0.00000   2.3026
          0.00000   1.0000
          0.00000  0.00000   1.3863   3.2958   5.5452

Definition

## loggamma

    #include <oxstd.oxh>
    main()
    {
        print( loggamma(<0.5,1,10>) );
    }

produces

          0.57236      0.00000       12.802

Definition

## lower

    #include <oxstd.oxh>
    main()
    {
        print( lower(ones(3,3)) );
        print( upper(ones(3,3)) );
    }

produces

           1.0000      0.00000      0.00000
           1.0000       1.0000      0.00000
           1.0000       1.0000       1.0000

           1.0000       1.0000       1.0000
          0.00000       1.0000       1.0000
          0.00000      0.00000       1.0000

Definition

## max

    #include <oxstd.oxh>
    main()
    {
        print( min(<1.5,12.5>, 1, 6), " ", max(<1.5,12.5>, 1, 6) );
    }

produces: `1 12.5`

Definition

## maxc, maxcindex, maxr

    #include <oxstd.oxh>
    main()
    {
        decl x = <11,12;10,15>;
        print("x = ", x);
        println("maxc and maxcindex", maxc(x) ~ maxcindex(x));
        println("minc and mincindex", minc(x) ~ mincindex(x));
    }

produces

    x =
           11.000       12.000
           10.000       15.000
    maxc and maxcindex
           11.000       15.000      0.00000       1.0000
    minc and mincindex
           10.000       12.000       1.0000      0.00000

Definition

## moments

The normal distribution N[mu,sigma^2] has central moments: vspace1ex [ mu_r=sf Eleft[X-sf EXright]^r=left beginarrayll 0 & textif r is odd,
fracr!(r/2)!fracsigma^r2^r/2 & textif r is even.
endarray right. ] So the standard normal distribution has skewness [ sqrtbeta_1=mu_3/mu_2^3/2=0, ] and kurtosis [ beta_2=mu_4/mu_2^2=3. ] The exponential distribution exp(lambda) has moments about zero: [ mu_r'=sf EX^r=fracr!lambda^r. ] Therefore, when lambda=2, the mean is 1/2, the variance 1/2-1/4=1/4, etc.

    #include <oxstd.oxh>
    #include <oxprob.oxh>
    main()
    {
        decl m1 = rann(10000,1) ~ ranexp(10000,1, 2);

        print("moment ratios",
              "%r", {"T","mean","std.dev.","skewness","kurtosis"},
              "%c", {"normal", "exp(2)"}, moments(m1));
        print("first 6 central moments",
              "%r", {"mean", "variance", "m3", "m4", "m5", "m6"},
              moments(m1, 6, 0)[1:][]);
    }

produces

    moment ratios
                        normal       exp(2)
    T                   10000.       10000.
    mean             -0.011605      0.49592
    std.dev.            1.0033      0.50088
    skewness          0.010556       1.9876
    kurtosis            3.0314       8.4267
    first 6 central moments
    mean             -0.011605      0.49592
    variance            1.0066      0.25088
    m3                0.010660      0.24976
    m4                  3.0713      0.53039
    m5                 0.13868       1.1581
    m6                  15.774       2.9434

Definition

## nans

    #include <oxstd.oxh>
    main()
    {
        print( nans(2, 2) );
    }

produces

             .NaN         .NaN
             .NaN         .NaN

Definition

## norm

    #include <oxstd.oxh>
    main()
    {
        decl x = <1,2;3,4;5,6>;

        print( norm(x), " " );
        print( norm(x, 1), " " );
        print( norm(x, 2), " " );
        print( norm(x, 'F') );
    }

produces: `11 12 9.52552 9.53939`

Definition

## nullspace

    #include <oxstd.oxh>
    main()
    {
        decl ma = zeros(4,2);
        ma[0][0] = ma[0][1] = 1;

        print(ma, nullspace(ma));
    }

produces

           1.0000       1.0000
          0.00000      0.00000
          0.00000      0.00000
          0.00000      0.00000

          0.00000      0.00000      0.00000
          0.00000      0.00000      -1.0000
          0.00000       1.0000     -0.00000
           1.0000      0.00000     -0.00000

Definition

## ols2c, ols2r, olsc, olsr

    #include <oxstd.oxh>
    main()
    {
        decl mx, my, cy = 2, ct = 50, ck = 3, mb, mxtx, mxtxi;
        mx = ranu(ct,ck);
        my = rann(ct,cy) / 10 + mx * ones(ck,1);

        olsc(my, mx, &mb);
        print(mb);
        olsr(my', mx', &mb, &mxtxi, &mxtx);
        print(mb, mxtx ~ mxtxi);

        print((1/mx)*my, mx'mx ~ invert(mx'mx));
    }

produces:

      1.0992   0.98022
      1.1068   0.95734
     0.78966    1.0401

      1.0992    1.1068   0.78966
     0.98022   0.95734    1.0401

      16.842    13.139    12.740   0.23380  -0.11726  -0.10967
      13.139    15.095    11.872  -0.11726   0.24566 -0.098336
      12.740    11.872    14.467  -0.10967 -0.098336   0.24639

      1.0992   0.98022
      1.1068   0.95734
     0.78966    1.0401

      16.842    13.139    12.740   0.23380  -0.11726  -0.10967
      13.139    15.095    11.872  -0.11726   0.24566 -0.098336
      12.740    11.872    14.467  -0.10967 -0.098336   0.24639

Definition

## ones

    #include <oxstd.oxh>
    main()
    {
        print( ones(2, 2) );
    }

produces

       1.0000       1.0000
       1.0000       1.0000

Definition

## outer

    #include <oxstd.oxh>
    main()
    {
        decl x = rann(2,3), y = ranu(3,3), s = y'y;
        print( outer(x, s, 'd') | diagonal(outer(x, s))
            | diagonal(x * s * x') );
    }

produces

           3.7646       4.2561
           3.7646       4.2561
           3.7646       4.2561

Definition

## oxprintlevel

    #include <oxstd.oxh>
    test()
    {
        oxprintlevel(0);        // output off
        // do some simulations which otherwise have output
        for (decl i = 0; i < 1000; ++i)
            println("i=", i);
        oxprintlevel(1);        // output on
        // do some simulations which has output and warning
        oxprintlevel(-1);       // output and warnings off
        for (decl i = 0; i < 1000; ++i)
            println("i=", i, " invert(0):", invert(0));
        oxprintlevel(1);        // output on
        // do some simulations which have warnings
        decl oldwarnings = oxwarning(0);    // all warnings off
        for (decl i = 0; i < 1000; ++i)
            invert(0);
        oxwarning(oldwarnings);             // reset warning levels
    }
    main()
    {
        // comment the next line in to overrule oxprintlevel calls
    //  oxprintlevel(2);
        test();
    }

Prints nothing unless the `oxprintlevel(2)` statement is commented in.

Definition

## periodogram

    #include <oxstd.oxh>
    #include <oxfloat.oxh>        // required for M_2PI

    main()
    {
        decl ct = 2^3 + 7, x, y, yzt, p1, p2;

        y = cumulate(rann(ct,1), 0.9);
        p1 = periodogram(y) / ct;
        x = M_2PI * range(0, int(ct/2))' / ct;

        yzt = (y - meanc(y))';// FFT expects data in row
        p2 = sqr(cabs(fft1d(yzt))') / (ct * M_2PI );
        print("%c", {"periodogram", "frequencies", "FFT"},
            p1 ~ x ~ p2);
    }

produces (the zeros at the end of the periodogram and frequencies are added in the concatenation with fft):

      periodogram  frequencies          FFT
          0.00000      0.00000  1.1253e-033
          0.49542      0.41888      0.49542
         0.060270      0.83776     0.060270
         0.024741       1.2566     0.024741
          0.16432       1.6755      0.16432
         0.036133       2.0944     0.036133
         0.019385       2.5133     0.019385
         0.023846       2.9322     0.023846
          0.00000      0.00000     0.023846
          0.00000      0.00000     0.019385
          0.00000      0.00000     0.036133
          0.00000      0.00000      0.16432
          0.00000      0.00000     0.024741
          0.00000      0.00000     0.060270
          0.00000      0.00000      0.49542

Definition

## polyeval

    #include <oxstd.oxh>
    main()
    {
        decl a = <1,-0.8,-0.1>;

        println("a(x)=a[0]+a[1]*x+a[2]*x^2; a(3)=", polyeval(a, 3));
    }

produces

    a(x)=a[0]+a[1]*x+a[2]*x^2; a(3)=-2.3

Definition

## polygamma

    #include <oxstd.oxh>
    #include <oxfloat.oxh>        // required for M_EULER

    main()
    {
        print(polygamma(<0.5,1>, 0), -M_EULER - 2*log(2) ~ -M_EULER);
        print("%12.7g", polygamma(0.5, <0,1,2,3>));
    }

produces

          -1.9635     -0.57722
          -1.9635     -0.57722
         -1.96351     4.934802     -16.8288     97.40909

Definition

## polymul

    #include <oxstd.oxh>
    main()
    {
        decl a, b, c, ff;
        format("%10.4f");

        a = <1,-0.9>;  b = <1,-0.8,-0.1>;

        print(polymul(a, b));
        c = polymul(b, a);
        print(polydiv(c, a, 5));

           // multiply the two FFTs, padded with zeros
        ff = cmul( fft(a~zeros(b)), fft(b~zeros(a)) );
        ff = fft(ff, 2);     // apply inverse real FFT
        print( ff[][:columns(a)+columns(b)-2] );

             // divide the two FFTs, padded with zeros
        ff = cdiv( fft(c~zeros(a)), fft(a~zeros(c)) );
        ff = fft(ff, 2);     // apply inverse real FFT
        print( ff[][:4] );
    }

produces

        1.0000   -1.7000    0.6200    0.0900
        1.0000   -0.8000   -0.1000   -0.0000   -0.0000
        1.0000   -1.7000    0.6200    0.0900
        1.0000   -0.8000   -0.1000    0.0000    0.0000

Definition

## polyroots

    #include <oxstd.oxh>
    main()
    {
        decl v1 = <-1, 1.2274, -0.017197, -0.28369, -0.01028>, roots;

        polyroots(v1, &roots);
        print(v1, "roots", roots, "inverse roots", cdiv(ones(roots),roots),
            "polynomial", polymake(roots) );
    }

%produces

        -1.0000      1.2274   -0.017197    -0.28369   -0.010280
    roots
        0.82865     0.82865    -0.39337   -0.036535
        0.16923    -0.16923     0.00000     0.00000
    inverse roots
         1.1585      1.1585     -2.5422     -27.371
       -0.23659     0.23659     0.00000     0.00000
    polynomial
         1.0000     -1.2274    0.017197     0.28369    0.010280
        0.00000     0.00000     0.00000     0.00000     0.00000

Definition

## print, println

    #include <oxstd.oxh>
    main()
    {
        print( "%r", {"row 1", "row 2"},
               "%c", {"col 1", "col 2"}, "%6.1g", unit(2) );

        decl xp = 9*rann(2,1)~ranu(2,1);
        print( "%c", {"x ", "p "},
               "%cf",{"%8.4g", " [%4.2f]"}, xp);

        decl x = rann(10,2);
        print("\nLower diagonal:", "%lwr", x'x);
    }

produces

          col 1 col 2
    row 1     1     0
    row 2     0     1

          x      p
       2.024 [0.42]
       15.66 [0.16]

    Lower diagonal:
           10.585
           3.1110       7.1178

In the second example we show the output from the `"%v"` format.

    #include <oxstd.oxh>

    class VClass
    {
        decl m_mMatrix;
        decl m_aArray;
        VClass();
    }
    VClass::VClass()
    {
        m_mMatrix = range(1,3);
        m_aArray = {"a", "b", "c"};
    }
    main()
    {
        decl vc = new VClass();
        print("\nobject using %v:\n", "%v", vc);
    }

produces

    object using %v:
    ::VClass
    {
    .m_mMatrix = <1,2,3>;
    .m_aArray = {"a","b","c"};
    }

Definition

## probchi, probf, probn, probt

    #include <oxstd.oxh>
    main()
    {
        decl m = <0,4.61,5.99>;

        print("%r", {"chi:   "}, probchi(m, 2));
        print("%r", {"normal:"}, probn(<-1.96, 0, 1.96>) );
        print("%r", {"t:     "}, probt(<-1.96, 0, 1.96>, 4) );
                              /* additional argument types: */
        print("%r", {"chi:   "}, probchi(5.99, <2,3,4>),
              "%r", {"chi:   "}, probchi(<6,7,8>, <2,3,4>) );
        print("%r", {"nc chi:"}, probchi(m, 2, 5));
        print("%r", {"nc t:  "}, probt(<-1.96, 0, 1.96>, 4, 5));
    }

produces

    chi:               0.00000      0.90024      0.94996
    normal:           0.024998      0.50000      0.97500
    t:                0.060777      0.50000      0.93922
    chi:               0.94996      0.88790      0.80010
    chi:               0.95021      0.92810      0.90842
    nc chi:            0.00000      0.37210      0.49621
    nc t:          7.3581e-010  2.8665e-007    0.0052148

Definition

## prodc, prodr

    #include <oxstd.oxh>
    main()
    {
        print( prodc(<0:3;1:4;2:5>) );
        print( prodr(<0:3;1:4;2:5>) );
    }

produces

          0.00000       6.0000       24.000       60.000

          0.00000
           24.000
           120.00

Definition

## quanchi, quanf, quann, quant

    #include <oxstd.oxh>
    main()
    {   decl t = range(1,10), tt = (t - 5) / 5;
        print("%14.10g", probf(t,10,10)' ~ quanf(probf(t,10,10),10,10)'
          ~ probt(tt,2)' ~ quant(probt(tt,2),2)' );
    }

%produces

               0.5             1   0.253817018          -0.8
       0.855154194             2  0.3047166335          -0.6
      0.9510726929             3  0.3639172365          -0.4
        0.98041856             4  0.4299859958          -0.2
      0.9910499384             5           0.5             0
      0.9954702686             6  0.5700140042           0.2
      0.9975177199             7  0.6360827635           0.4
      0.9985507194             8  0.6952833665           0.6
        0.99910908             9   0.746182982           0.8
      0.9994284475            10  0.7886751346             1

Definition

## quantilec, quantiler

    #include <oxstd.oxh>
    main()
    {
        print( quantilec(<3;2;1;4>, <1/4,2/4,3/4>) );
        print( quantilec(<3;2;1;4>) );

        decl m = rann(2,10000);        /* generate m */

        print( quantiler(m, <0.8,0.9,0.95,0.975>) );
        print( quantilec(m', <0.8,0.9,0.95,0.975>) );

        m = sortr(m);                      /* sort m */
        print( m[][columns(m) * <0.8,0.9,0.95,0.975> ] );
    }

produces:

           1.7500
           2.5000
           3.2500

           2.5000

          0.83516       1.2728       1.6457       1.9635
          0.84842       1.2740       1.6248       1.9570

          0.83516      0.84842
           1.2728       1.2740
           1.6457       1.6248
           1.9635       1.9570

          0.83536       1.2734       1.6459       1.9638
          0.84871       1.2744       1.6255       1.9585

Definition

## range

    #include <oxstd.oxh>
    main()
    {
        print( range(1,4), range(4,1), range(1,6,2));
        print( range(1.2,4), range(1,6,2.1));
    }

produces

           1.0000       2.0000       3.0000       4.0000
           4.0000       3.0000       2.0000       1.0000
           1.0000       3.0000       5.0000
           1.2000       2.2000       3.2000
           1.0000       3.1000       5.2000

Definition

## rank

    #include <oxstd.oxh>
    main()
    {
        print( rank(<1,0;1,0>), " " );
        print( rank(<1e-200>), " " );
        print( rank(0), " " );
        print( rank(<1e-200>, inverteps(0)) );
    }

produces: `1 1 0 0`

Definition

## rann

    #include <oxstd.oxh>
    main()
    {
        print( sumc( rann(1000,1) ) / 1000 );

        ranseed(-1);
        print(rann(1,5));
        ranseed(-1);
        print(rann(1,3) ~ rann(1,2));
    }

produces

        -0.035817

       0.22489     1.7400   -0.20426   -0.91760   -0.67417
       0.22489     1.7400   -0.20426   -0.91760   -0.67417

Definition

## ranseed

    #include <oxstd.oxh>
    main()
    {
        decl seed = ranseed(0);
        print("RNG=", ranseed(""), " initial seeds: ",
            seed[0], " ... ", seed[sizeof(seed) - 1]);
        print( meanc(rann(10000,2)) | meanc(rann(10000,2)) );
        seed = ranseed(0);
        print("current seed: ",
            seed[0], " ... ", seed[sizeof(seed) - 1]);
        ranseed(-1);
        print( meanc(rann(10000,2)) );

        ranseed("GM");
        print("RNG=", ranseed(""), " initial seed: ",  ranseed(0) );
        print( meanc(rann(10000,2)) | meanc(rann(10000,2)) );
        ranseed(-1);
        print( meanc(rann(10000,2)) );
    }

produces

    RNG=MWC_52 initial seeds: 1013904223 ... 362436
        0.0011722   -0.0070313
       -0.0024659   -0.0065795
    current seed: 866497328 ... 759508397
        0.0011722   -0.0070313
    RNG=GM initial seed:
    [0] = 362436069
    [1] = 521288629

       -0.0046842     0.015912
        0.0037562     0.017064

       -0.0046842     0.015912

Definition

## ranu

    #include <oxstd.oxh>
    main()
    {
        print( ranu(2,3) );
    }

produces

          0.56444      0.76994      0.41641
          0.15881     0.098209      0.37477

Definition

## reflect

    #include <oxstd.oxh>
    main()
    {
        print( reflect(<2,1;1,4>) );
    }

produces

       4.0000       1.0000
       1.0000       2.0000

Definition

## replace

    #include <oxstd.oxh>

    main()
    {
        decl sarr = {"Aa", "BbAaAa", "Aa", "Cc"};

        println(replace("aAaAbBaAa", "bB",  "xx"));
        println(replace("aAaAbBaAa", "bB",  ""  ));
        println(replace("aAaAbBaAa", "aAa", "1zzz"));
        println(replace("aAaAbBaAa", "AAA", "1zzz", "i"));
        println(replace(sarr,        "AA",  "1zzz", "i1"));
        println(replace(sarr,        "AA",  "1zzz", "i.1"));
        println(replace(unit(3),     <1,0>, <2,5>));
        println(unit(3) .== 0 .? 5 .: 2);
        println(replace(unit(3),     0,     2   ));
        println(replace({0,1,2,0},   0,     2   ));
    }

produces:

        aAaAxxaAa
        aAaAaAa
        1zzzAbB1zzz
        1zzzAbB1zzz

        [0] = 1zzz
        [1] = BbAaAa
        [2] = Aa
        [3] = Cc

        [0] = 1zzz
        [1] = Bb1zzzAa
        [2] = 1zzz
        [3] = Cc

               2.0000       5.0000       5.0000
               5.0000       2.0000       5.0000
               5.0000       5.0000       2.0000

               2.0000       5.0000       5.0000
               5.0000       2.0000       5.0000
               5.0000       5.0000       2.0000

               1.0000       2.0000       2.0000
               2.0000       1.0000       2.0000
               2.0000       2.0000       1.0000

        [0] = 2
        [1] = 1
        [2] = 2
        [3] = 2

Definition

## reshape

    #include <oxstd.oxh>
    main()
    {
        print( reshape(<1:3>, 4, 3)' );
    }

%produces

           1.0000       1.0000       1.0000       1.0000
           2.0000       2.0000       2.0000       2.0000
           3.0000       3.0000       3.0000       3.0000

Definition

## reversec, reverser

    #include <oxstd.oxh>
    main()
    {
        decl m = <0:3;4:7;8:11;12:15>;
        print( reversec(m), reverser(m) );
    }

%produces:

           12.000       13.000       14.000       15.000
           8.0000       9.0000       10.000       11.000
           4.0000       5.0000       6.0000       7.0000
          0.00000       1.0000       2.0000       3.0000

           3.0000       2.0000       1.0000      0.00000
           7.0000       6.0000       5.0000       4.0000
           11.000       10.000       9.0000       8.0000
           15.000       14.000       13.000       12.000

Definition

## scan

The following example reads one input line at a time (leading spaces in each line are skipped, because of the starting space in `" %z"`, and reads from that string using `scan`. The `*` in `"%*d"` suppresses assignment, so the integer is skipped in the file.

    #include <oxstd.oxh>
    main()
    {
        decl c, i, d, m;

        c = scan("Enter an integer: %d", &i,
                 "Enter a double:   %f", &d);
        print("items read=", c, " int=", i, " dbl=", d, "\n");

        c = scan("Enter a 2 x 2 matrix: %#m", 2, 2, &m);
        print("items read=", c, " mat=", m);

        c = scan("Enter a matrix with dimensions: %m", &m);
        print("items read=", c, " mat=", m);
    }

This program produces (keyboard input is written in italics):
`Enter an integer:` 24
`Enter a double:` 25
`items read=2 int=24 dbl=25`
`Enter a 2 x 2 matrix:` 1 0 0 1

    items read=1 mat=
           1.0000      0.00000
          0.00000       1.0000

`Enter a matrix with dimensions:` 2 2 1 0 0 1

    items read=1 mat=
           1.0000      0.00000
          0.00000       1.0000

Definition

## selectc, selectr, selectifc, selectifr, selectrc

    #include <oxstd.oxh>
    main()
    {
        decl m = <0:3;4:7;8:11;12:15>, sel = <1,9,10,14>;
        print(m, "select", selectc(m, sel), selectr(m, sel));
        print("selectif", selectifr(m, m .< 0 || m .> 14));
        print("selectrc", selectrc(m, <2,3,4>, <2,3,4>));
    }

produces:

          0.00000       1.0000       2.0000       3.0000
           4.0000       5.0000       6.0000       7.0000
           8.0000       9.0000       10.000       11.000
           12.000       13.000       14.000       15.000
    select
           1.0000       2.0000
           5.0000       6.0000
           9.0000       10.000
           13.000       14.000

          0.00000       1.0000       2.0000       3.0000
           8.0000       9.0000       10.000       11.000
           12.000       13.000       14.000       15.000
    selectif
           12.000       13.000       14.000       15.000
    selectrc
           10.000       15.000         .NaN

Definition

## setbounds

    #include <oxstd.oxh>
    main()
    {
        decl x = <1,2,3;4,5,6>;
        print( setbounds(x, 3, 4) );
        print( setbounds(x, -.Inf, 4) );
        print( setbounds(x, 2, .Inf) );
    }

produces

           3.0000       3.0000       3.0000
           4.0000       4.0000       4.0000

           1.0000       2.0000       3.0000
           4.0000       4.0000       4.0000

           2.0000       2.0000       3.0000
           4.0000       5.0000       6.0000

Definition

## setdiagonal, setlower, setupper

    #include <oxstd.oxh>
    main()
    {
        decl ma = ones(2,2), mb = rann(3,3);
        print(setdiagonal(ma, zeros(2,1)), setdiagonal(ma, 0),
              setdiagonal(ma, zeros(2,2)) );

        ma = ones(3,3);
        print(setlower(ma, mb, mb), setupper(ma, 0), setupper(ma, 0, 2));
    }

%produces

          0.00000       1.0000
           1.0000      0.00000

          0.00000       1.0000
           1.0000      0.00000

          0.00000       1.0000
           1.0000      0.00000

          0.22489       1.0000       1.0000
         -0.91760     -0.67417       1.0000
          0.22335     -0.14139     -0.18338

           1.0000      0.00000      0.00000
           1.0000       1.0000      0.00000
           1.0000       1.0000       1.0000

           2.0000      0.00000      0.00000
           1.0000       2.0000      0.00000
           1.0000       1.0000       2.0000

Definition

## shape

    #include <oxstd.oxh>
    main()
    {
        print( shape(<0:5>, 2, 4) );
        print( shape(<0:5>, 4, 2)' );
    }

produces

          0.00000       2.0000       4.0000      0.00000
           1.0000       3.0000       5.0000      0.00000

          0.00000       1.0000       2.0000       3.0000
           4.0000       5.0000      0.00000      0.00000

Definition

## sizec, sizeof, sizer, sizerc

    #include <oxstd.oxh>
    main()
    {
        decl i, d, m, s, a, res;
        i = 0; d = 0.0;
        m = unit(3,2);
        s = "aap", a = {"a", "b"};
        res = columns(i)~rows(i)~sizec(i)~sizer(i)~sizerc(i)
            | columns(d)~rows(d)~sizec(d)~sizer(d)~sizerc(d)
            | columns(m)~rows(m)~sizec(m)~sizer(m)~sizerc(m)
            | columns(s)~rows(s)~sizec(s)~sizer(s)~sizerc(s)
            | columns(a)~rows(a)~sizec(a)~sizer(a)~sizerc(a);

        print("%r",
              {"int","double","matrix[3][2]","string[3]","array[2]"},
              "%c",
              {"columns","rows","sizec","sizer","sizerc"},
              "%8.1g", res);
    }

produces:

                 columns    rows   sizec   sizer  sizerc
    int                0       0       1       1       1
    double             0       0       1       1       1
    matrix[3][2]       2       3       2       3       6
    string[3]          3       3       3       1       3
    array[2]           2       2       2       1       2

Definition

## solvetoeplitz

    #include <oxstd.oxh>
    main()
    {
        decl ct = 10, mb, mt, mx;

        mb = <2;3;4;5;6>;
        mx = solvetoeplitz(<3,.5,.2,.1>, 5, mb);
        print(mx');
        mx = invertsym( toeplitz(<3,.5,.2,.1>,5) ) * mb;
        print(mx');
    }

produces

        0.46189     0.63974     0.88536     1.1737     1.7240
        0.46189     0.63974     0.88536     1.1737     1.7240

Definition

## sortbyc, sortbyr

    #include <oxstd.oxh>
    main()
    {
        decl m = <1,0,3;0,4,4;4,3,0>;
        print( sortbyc(m,0), sortbyr(m,0) );

        m = <1,3;1,2;3,4;3,5;2,3;2,2>;

        print("%4.1g", m ~ sortbyc(m, 0) ~ sortbyc(m, 0~1));
    }

produces

          0.00000       4.0000       4.0000
           1.0000      0.00000       3.0000
           4.0000       3.0000      0.00000

          0.00000       1.0000       3.0000
           4.0000      0.00000       4.0000
           3.0000       4.0000      0.00000

        1    3    1    3    1    2
        1    2    1    2    1    3
        3    4    2    2    2    2
        3    5    2    3    2    3
        2    3    3    5    3    4
        2    2    3    4    3    5

Definition

## sortc, sortcindex, sortr

    #include <oxstd.oxh>
    main()
    {
        decl m = <1,0,3;0,4,4;4,3,0>;
        print( sortc(m), sortr(m) );
        print( sortcindex(m[0][]) );
        print( sortc( {"x", "", 2, "aa", 1} ) );
    }

produces

          0.00000      0.00000      0.00000
           1.0000       3.0000       3.0000
           4.0000       4.0000       4.0000

          0.00000       1.0000       3.0000
          0.00000       4.0000       4.0000
          0.00000       3.0000       4.0000

           1.0000
          0.00000
           2.0000

    [0] =
    [1] = aa
    [2] = x
    [3] = 1
    [4] = 2

Definition

## spline

The following example first smoothes the four variables in the variable `my` using time as the X variable, and automatic bandwidth selection. The second observation of the first variable is set to a missing value. The second spline smoothes the cross plot of the last three variables against the first, choosing the bandwidth as 12 equivalent parameters.

    #include <oxstd.oxh>
    #include <oxfloat.oxh>

    main()
    {   decl my, ms, gcv;

        my = loadmat("data/data.in7");
        my[1][0] = M_NAN;
        ms = spline(my, 0, 0);
        print( "%c", {"CONS", "smooth"}, my[:4][0] ~ ms[:4][0]);

        ms = spline(my[][1:], my[][0], 12, &gcv);
        print( "%r", {"GCV", "k_e"}, gcv);
    }

produces

             CONS       smooth
           890.45       890.01
             .NaN       888.19
           886.33       886.58
           884.88       885.38
           885.25       884.66

    GCV                 13.932       1.4645       24.309
    k_e                 12.000       11.999       11.999

Definition

## sprint

    #include <oxstd.oxh>
    main()
    {
        decl s = sprint("a", "_", "%0X", 10);
        print( s );
    }

produces: `a_A`

Definition

## sqr, sqrt

    #include <oxstd.oxh>
    main()
    {
        print( sqrt(<2,3>), <2,3> .^ 0.5 );
        print( sqr(<2,3>), <2,3> .^ 2 );

        println( sqr(2^14), isint(sqr(2^14)) ? " int" : " double");
        println( sqr(2^15), isint(sqr(2^15)) ? " int" : " double");
        println( pow(2,15), isint(pow(2,15)) ? " int" : " double");
    }

produces

           1.4142       1.7321
           1.4142       1.7321
           4.0000       9.0000
           4.0000       9.0000
    268435456 int
    1.07374e+009 double
    32768 double

Definition

## sscan

The following example (samples/inout/inout5.ox) reads one input line at a time (leading spaces in each line are skipped, because of the starting space in `" %z"`), and reads from that string using `sscan`. The `*` in `"%*d"` suppresses assignment, so the integer is skipped in the file.

    #include <oxstd.oxh>
    main()
    {
        decl file, s, c;
        decl svar, address;

        file = fopen("data/data.in7");
        if (!isfile(file))
        {
            print("failed to open file\n");
            exit(1);
        }
        do
        {   c = fscan(file, " %z", &s);
            if (c > 0 && s[0] == '>')
            {
                sscan(&s, ">%s", &svar, "%*d", "%*d", "%*d",
                    "%*d", "%*d", "%d", &address, " ");
                println("variable : ", svar, " address:", address);
                println("remainder: ", s);
            }
        } while (c > 0);

        fclose(file);
    }

If the .in7 file can be found, this program produces:

    variable : CONS address:32
    remainder: data 10-04-1992 13:20:38.33
    variable : INC address:1336
    remainder: data 10-04-1992 13:20:38.33
    variable : INFLAT address:2640
    remainder: data 10-04-1992 13:20:38.33
    variable : OUTPUT address:3944
    remainder: data 10-04-1992 13:20:38.33

Definition

## strfind, strfindr, strifind, strifindr

    #include <oxstd.oxh>
    main()
    {
        decl as1 = {"aa", "bb", "cc", "cc"};
        decl as2 = {"cc", "dd", "aa"};

        print("index = ", strfind(as1, "cc"), "\n",
              "index = ", strfindr(as1, "cc"), "\n",
              "index = ", strfind(as1, "ee"), "\n",
              "index = ", strfind(as1,  as2));
        println("first ox is at position ", strfind("ooxox", "ox"),
            " in \"ooxox\"");
        println("last  ox is at position ", strifindr("oOXoX", "ox"),
            " in \"oOXoX\" (no case)");
        println("x is at position ", strfind("ox", 'x'), " in \"ox\"");
        println("x is at position ", strfind("OX", 'x'), " in \"OX\"");
        println("x is at position ", strifind("OX", 'x'),
            " in \"OX\" (no case)");
        println("index of x,o in \"OX\" (no case):",
            strifind("OX", 'x'~'o'));
    }

produces (remember that the first entry has index 0):

    index = 2
    index = 3
    index = -1
    index =
           2.0000      -1.0000      0.00000
    first ox is at position 1 in "ooxox"
    last  ox is at position 3 in "oOXoX" (no case)
    x is at position 1 in "ox"
    x is at position -1 in "OX"
    x is at position 1 in "OX" (no case)
    index of x,o in "OX" (no case):
           1.0000      0.00000

Definition

## strlwr, strtrim, strupr

    #include <oxstd.oxh>
    main()
    {
        decl s = "A StrinG\n";
        print( strlwr(s), strupr(s), s);
        s = "  aa  bb \t\n";
        print( "{", strtrim(s), "}");
    }

produces

    a string
    A STRING
    A StrinG
    {aa bb}

Definition

## sumc, sumr

    #include <oxstd.oxh>
    main()
    {
        print( sumc(<0:3;1:4;2:5>) | sumsqrc(<0:3;1:4;2:5>));
        print( sumr(<0:3;1:4;2:5>) ~ sumsqrr(<0:3;1:4;2:5>));
    }

produces

           3.0000       6.0000       9.0000       12.000
           5.0000       14.000       29.000       50.000

           6.0000       14.000
           10.000       30.000
           14.000       54.000

Definition

## tailchi, tailf, tailn, tailt

    #include <oxstd.oxh>
    main()
    {
        print("%r", {"chi(2):"}, tailchi(<0,4.61,5.99>, 2));
        print("%r", {"normal:"}, tailn(<-1.96, 0, 1.96>) );
        print("%r", {"t(4):  "}, tailt(<-1.96, 0, 1.96>, 4) );
        print("%r", {"t(50): "}, tailt(<-1.96, 0, 1.96>, 50) );
    }

produces

    chi(2):             1.0000     0.099759     0.050037
    normal:            0.97500      0.50000     0.024998
    t(4):              0.93922      0.50000     0.060777
    t(50):             0.97221      0.50000     0.027790

Definition

## thinc, thinr

Note in the example that, strictly speaking, it is not necessary to truncate the random indices in `idx`, as this is done automatically when using a matrix to index another matrix.

    #include <oxstd.oxh>
    main()
    {
        decl m = rann(1000, 2), idx;
        print( thinr(m, 3) ~ m[<0,499,998>][] );
        print( thinc(m', 3)' ~ m[<0,499,998>][] );
                   /* get three random indices in idx */
        idx = trunc(ranu(1,3) * rows(m));
        print(idx, m[idx][] ~ m[sortr(idx)][] );
    }

produces

          0.22489       1.7400      0.22489       1.7400
         -0.21417      -1.0037     -0.21417      -1.0037
         0.084549      0.83591     0.084549      0.83591

          0.22489       1.7400      0.22489       1.7400
         -0.21417      -1.0037     -0.21417      -1.0037
         0.084549      0.83591     0.084549      0.83591

           408.00       852.00       877.00

           1.9639     0.073371       1.9639     0.073371
          0.25375      -1.2006      0.25375      -1.2006
          -1.1932     -0.52929      -1.1932     -0.52929

Definition

## timeofday

    #include <oxstd.oxh>
    main()
    {
        decl timeidx = range(0,4)' / 6 + range(0,4)' / 360;

        println("%cf", {"%5.0f","%5.0f","%5.0f","%5.0f","; %20C"},
            timeofday(timeidx) ~ timeidx + dayofcalendar(2005,1,1) );

        println("time today ", "%C", timeofday());
    }

produces

        0    0    0    0;           2005-01-01
        4    4    0    0;  2005-01-01T04:04:00
        8    8    0    0;  2005-01-01T08:08:00
       12   12    0    0;  2005-01-01T12:12:00
       16   16    0    0;  2005-01-01T16:16:00
    time today 22:11

Definition

## timer, timespan

    #include <oxstd.oxh>
    main()
    {
        decl i, time, m = rann(100,10), m2;

        time = timer();

        for (i = 0; i < 1000; ++i)
            m2 = m'm;

        print("time lapsed: ", timespan(time), "\n");
        print("or in seconds: ", (timer() - time) / 100, "\n");
        print("time lapsed: ", timespan(time, timer()), "\n");
    }

prints the time it took to do the for loop.

Definition

## timestr, timing, today

    #include <oxstd.oxh>
    main()
    {
        decl time1, time2;

        time1 = timing(<1990, 12, 1; 1991, 1, 1>);
        time2 = timing(<1990, 12, 1, 12, 0, 1>);

        println("time1[0]: ", timestr(time1[0]));
        println("time1[1]: ", timestr(time1[1]));
        println("time2:    ", timestr(time2));
        println("today:    ", timestr(today()));
        println("today:    ", "%6.0f", timing(today(), 1));
        println("today:    ", "%C", timing(today(), 2));
    }

which produces as output:

    time1[0]: 1990-12-01
    time1[1]: 1991-01-01
    time2:    1990-12-01 12:00:01
    today:    2012-06-26 14:27:44
    today:
      2012     6    26    14    27    44
    today:    2012-06-26T14:27:44

Definition

## trace

    #include <oxstd.oxh>
    main()
    {
        print( trace(<2,1;1,4>) );
    }

produces: `6`

Definition

## trunc, truncf

    #include <oxstd.oxh>
    main()
    {
        print( trunc(<-2.0-1e-15, -2.0+1e-15, 2.0-1e-15, 2.0+1e-15>));
        print(truncf(<-2.0-1e-15, -2.0+1e-15, 2.0-1e-15, 2.0+1e-15>));
    }

produces

          -2.0000      -1.0000       1.0000       2.0000
          -1.0000      -1.0000       2.0000       2.0000

Definition

## unit

    #include <oxstd.oxh>
    main()
    {
        print( unit(2) );
    }

produces

        1.0000      0.00000
       0.00000       1.0000

Definition

## va_arglist

    #include <oxstd.oxh>
    test(const a, ...)
    {
        decl i, args = va_arglist();

        println("number of extra arguments: ", sizeof(args));
        for (i = 0; i < sizeof(args); i++)
            println("vararg [", i, "] = ", args[i]);
    }
    main()
    {
        test("tinker", "tailor", "soldier");
    }

which prints

    number of extra arguments: 2
    vararg [0] = tailor
    vararg [1] = soldier

Definition

## varc, varr

    #include <oxstd.oxh>
    main()
    {
        decl m1 = rann(100,2), m2;
        print( variance(m1), varc(m1) | varr(m1') ' );
    }

%produces

           1.0356    -0.037133
        -0.037133      0.86569

           1.0356      0.86569
           1.0356      0.86569

Definition

## variance

    #include <oxstd.oxh>
    main()
    {
        decl m1 = rann(100,2), m2 = m1 - meanc(m1);
        print( variance(m1), m2'm2/rows(m2) );
    }

%produces

           1.0356    -0.037133
        -0.037133      0.86569

           1.0356    -0.037133
        -0.037133      0.86569

Definition

## vec

    #include <oxstd.oxh>
    main()
    {
        print( vec(<0,1;2,3>) );
    }

produces

       0.00000
        2.0000
        1.0000
        3.0000

Definition

## vech

    #include <oxstd.oxh>
    main()
    {
        decl m = <0,1;2,3>;
        print( vech(m), unvech(vech(m)) );
    }

produces

          0.00000
           2.0000
           3.0000

          0.00000       2.0000
           2.0000       3.0000

Definition

## vecindex

    #include <oxstd.oxh>
    main()
    {
        decl x = <0,1,2;0,2,0>;
        print(vec(x), vecindex(x)', vecindex(x, 0)' );
    }

produces

          0.00000
          0.00000
           1.0000
           2.0000
           2.0000
          0.00000

           2.0000       3.0000       4.0000

          0.00000       1.0000       5.0000

Definition

## vecr

    #include <oxstd.oxh>
    main()
    {
        decl x = <0,1;2,3>;
        print( vecr(x) ~ x[] );
    }

produces

          0.00000      0.00000
           1.0000       1.0000
           2.0000       2.0000
           3.0000       3.0000

Definition

## vecrindex

    #include <oxstd.oxh>
    main()
    {
        decl x = <0,1,2;0,2,0>;
        print(vecr(x),  "1 argument:",  vecrindex(x)',
                        "2 arguments:", vecrindex(x, 0)',
                        "3 arguments:", vecrindex(x, <0,2>, 1)');
        print("non-zeros:", vecr(x)[vecrindex(x)]',
              "zeros:", vecr(x)[vecrindex(x, 0)]' );
    }

%produces

          0.00000
           1.0000
           2.0000
          0.00000
           2.0000
          0.00000
    1 argument:
           1.0000       2.0000       4.0000
    2 arguments:
          0.00000       3.0000       5.0000
    3 arguments:
          0.00000       2.0000
    non-zeros:
           1.0000       2.0000       2.0000
    zeros:
          0.00000      0.00000      0.00000

Definition

## zeros

    #include <oxstd.oxh>
    main()
    {
        print( zeros(2, 2) );
    }

produces

       0.00000      0.00000
       0.00000      0.00000

Definition

Ox version 4.00. © JA Doornik This file last changed
.