File: cs_frand_mex.c

package info (click to toggle)
suitesparse 1%3A5.12.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 176,720 kB
  • sloc: ansic: 1,193,914; cpp: 31,704; makefile: 6,638; fortran: 1,927; java: 1,826; csh: 765; ruby: 725; sh: 529; python: 333; perl: 225; sed: 164; awk: 35
file content (59 lines) | stat: -rw-r--r-- 1,592 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "cs_mex.h"
/* A = cs_frand (n,nel,s) creates an n-by-n sparse matrix consisting of nel
 * finite elements, each of which are of size s-by-s with random symmetric
 * nonzero pattern, plus the identity matrix.
 * See also MATLAB/Demo/private/frand.m */

cs_dl *cs_dl_frand (CS_INT n, CS_INT nel, CS_INT s)
{
    CS_INT ss = s*s, nz = nel*ss, e, i, j, *P ;
    cs *A, *T = cs_dl_spalloc (n, n, nz, 1, 1) ;
    if (!T) return (NULL) ;
    P = cs_dl_malloc (s, sizeof (CS_INT)) ;
    if (!P) return (cs_dl_spfree (T)) ;
    for (e = 0 ; e < nel ; e++)
    {
        for (i = 0 ; i < s ; i++) P [i] = rand () % n ;
        for (j = 0 ; j < s ; j++)
        {
            for (i = 0 ; i < s ; i++)
            {
                cs_dl_entry (T, P [i], P [j], rand () / (double) RAND_MAX) ;
            }
        }
    }
    for (i = 0 ; i < n ; i++) cs_dl_entry (T, i, i, 1) ;
    A = cs_dl_compress (T) ;
    cs_dl_spfree (T) ;
    return (cs_dl_dupl (A) ? A : cs_dl_spfree (A)) ;
}

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{
    CS_INT n, nel, s ;
    cs *A, *AT ;
    if (nargout > 1 || nargin != 3)
    {
        mexErrMsgTxt ("Usage: C = cs_frand(n,nel,s)") ;
    }
    n = mxGetScalar (pargin [0]) ;
    nel = mxGetScalar (pargin [1]) ;
    s = mxGetScalar (pargin [2]) ;

    n = CS_MAX (1,n) ;
    nel = CS_MAX (1,nel) ;
    s = CS_MAX (1,s) ;

    AT = cs_dl_frand (n, nel, s) ;
    A = cs_dl_transpose (AT, 1) ;
    cs_dl_spfree (AT) ;
    cs_dl_dropzeros (A) ;

    pargout [0] = cs_dl_mex_put_sparse (&A) ;
}