Esempio n. 1
0
int main()
{

std::cout << std::boolalpha;

// Not antisymetric
std::cout << mycomp(2, -3) << '\n';
std::cout << mycomp(-3, 2) << '\n';

// Not transitive
std::cout << mycomp(-2, -2) << '\n';
std::cout << mycomp(-3, 2) << '\n';
std::cout << mycomp(2, 2) << '\n';

}
void mexFunction(
        int nlhs,       mxArray *plhs[],
        int nrhs, const mxArray *prhs[]
        )

{
    mwSize n, k, col, nskip, lastcol, *ss_ndxs;
    double *map, *scl, lastnorm;
    bool first;
    
    n       = mxGetN(  prhs[0] );
    ss_ir   = mxGetIr( prhs[0] );
    ss_jc   = mxGetJc( prhs[0] );
    ss_pr   = mxGetPr( prhs[0] );
    ss_mode = nrhs < 2 ? FULL_COMPRESS : (int)mxGetScalar( prhs[1] );
    nskip   = nrhs < 3 ? 0 : (int)mxGetScalar(prhs[2]);
    ss_ndxs = mxCalloc( n, sizeof(mwSize) );
    ss_temp = mxCalloc( n, sizeof(mwSize) );
    
    /*
     * Sort the column indices
     */

    for ( col = 0 ; col != n ; ++col )
        ss_ndxs[col] = col;
    if ( nskip < n )
        merge_sort( ss_ndxs + nskip, ss_ndxs + n );

    /*
     * Determine which rows are unique, and which are scales of another row
     */

    plhs[0] = mxCreateDoubleMatrix( (mwSize)1, n, mxREAL );
    plhs[1] = mxCreateDoubleMatrix( (mwSize)1, n, mxREAL );
    if ( plhs[0] == 0 || plhs[1] == 0 )
        mexErrMsgTxt( "Unable to allocate output arguments" );
    map = mxGetPr( plhs[0] );
    scl = mxGetPr( plhs[1] );
    first = true;
    lastnorm = 0.0;
    for ( k = 0 ; k < n ; ++k ) {
        col = ss_ndxs[k];
        if ( col < nskip ) {
            map[col] = col + 1;
            scl[col] = 1.0;
        } else if ( ss_jc[col] == ss_jc[col+1] ) {
            map[col] = col + 1;
            scl[col] = 0;
        } else if ( first || mycomp( lastcol, col ) != 0 ) {
            lastcol  = col;
            lastnorm = ss_pr[ss_jc[col]];
            map[col] = lastcol + 1;
            scl[col] = 1.0;
            first = false;
        } else {
            map[col] = lastcol + 1;
            scl[col] = ss_pr[ss_jc[col]] / lastnorm;
        }
    }
}
void merge( mwSize *nb, mwSize *nm, mwSize *ne )
{
    mwSize *i = nb, *j = nm, *t = ss_temp;
    for ( ;; )
        if ( mycomp( *i, *j ) <= 0 ) {
            *t++ = *i++;
            if ( i == nm ) {
                while ( j != ne )
                    *t++ = *j++;
                break;
            }
        } else {
            *t++ = *j++;
            if ( j == ne ) {
                while ( i != nm )
                    *t++ = *i++;
                break;
            }
        }
    for ( i = nb, t = ss_temp ; i != ne ; *i++ = *t++ );
}